2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
5 * @file security_server_tests_server.cpp
6 * @author Bumjin Im (bj.im@samsung.com)
7 * @author Mariusz Domanski (m.domanski@samsung.com)
9 * @brief Test cases for security server
15 #include <sys/types.h>
16 #include <sys/param.h>
21 #include <sys/socket.h>
23 #include "security-server.h"
24 #include <dpl/test/test_runner.h>
28 #define SECURITY_SERVER_SOCK_PATH "/tmp/.security_server.sock"
33 unsigned char version;
35 unsigned short msg_len;
40 basic_header basic_hdr;
41 unsigned char return_code;
44 int server_sockfd, client_sockfd, ret, recved_gid, client_len, i;
45 unsigned char cookie[20], recved_cookie[20], recvbuf[33], wrong_cookie[20];
47 struct pollfd accept_poll[1], client_poll[1];
48 struct sockaddr_un clientaddr;
53 /* Create a Unix domain socket and bind */
54 int create_new_socket()
56 int localsockfd = 0, flags;
58 struct sockaddr_un serveraddr;
61 tmp = remove(SECURITY_SERVER_TEST_SOCK_PATH);
65 LOGE("%s\n", "Unable to remove /tmp/.security_server.sock");
69 /* Create Unix domain socket */
70 if((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
73 LOGE("%s\n", "Socket creation failed");
77 /* Make socket as non blocking */
78 if((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
79 fcntl(localsockfd, F_SETFL, flags | O_NONBLOCK) < 0)
83 LOGE("%s\n", "Cannot go to nonblocking mode");
87 bzero (&serveraddr, sizeof(serveraddr));
88 serveraddr.sun_family = AF_UNIX;
89 strncpy(serveraddr.sun_path, SECURITY_SERVER_TEST_SOCK_PATH,
90 strlen(SECURITY_SERVER_TEST_SOCK_PATH) + 1);
93 if((bind(localsockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
95 LOGE("%s\n", "Cannot bind");
101 /* Change permission to accept all processes that has different uID/gID */
102 sock_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
103 /* Flawfinder hits this chmod function as level 5 CRITICAL as race condition flaw *
104 * Flawfinder recommends to user fchmod insted of chmod
105 * But, fchmod doesn't work on socket file so there is no other choice at this point */
106 if(chmod(SECURITY_SERVER_TEST_SOCK_PATH, sock_mode) < 0) /* Flawfinder: ignore */
108 LOGE("%s\n", "chmod() error");
117 int check_socket_poll(int sockfd, int event, int timeout)
119 struct pollfd poll_fd[1];
122 poll_fd[0].fd = sockfd;
123 poll_fd[0].events = event;
124 retval = poll(poll_fd, 1, timeout);
127 LOGE("%s\n", "poll() error");
134 LOGE("%s", "poll() timeout");
140 int send_gid_request(int sock_fd, const char* object)
143 int retval, send_len = 0;
144 unsigned char *buf = NULL;
146 hdr.version = 0x01; /* SECURITY_SERVER_MSG_VERSION; */
147 hdr.msg_id = 0x07; /* SECURITY_SERVER_MSG_TYPE_GID_REQUEST; */
148 hdr.msg_len = strlen(object);
150 send_len = sizeof(hdr) + strlen(object);
152 buf = (unsigned char *) malloc(send_len);
155 LOGE("%s\n", "out of memory");
159 memcpy(buf, &hdr, sizeof(hdr));
160 memcpy(buf + sizeof(hdr), object, strlen(object));
163 retval = check_socket_poll(sock_fd, POLLOUT, 1000);
166 LOGE("%s\n", "poll() error");
173 LOGE("%s\n", "poll() timeout");
179 retval = write(sock_fd, buf, send_len);
180 if(retval < send_len)
183 LOGE("Error on write(): %d. errno=%d, sockfd=%d\n", retval, errno, sock_fd);
194 int connect_to_server(int *fd)
196 struct sockaddr_un clientaddr;
197 int client_len = 0, localsockfd, ret, flags;
200 /* Create a socket */
201 localsockfd = socket(AF_UNIX, SOCK_STREAM, 0);
204 LOGE("%s\n", "Error on socket()");
208 /* Make socket as non blocking */
209 if((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
210 fcntl(localsockfd, F_SETFL, flags | O_NONBLOCK) < 0)
213 LOGE("%s\n", "Cannot go to nonblocking mode");
217 bzero(&clientaddr, sizeof(clientaddr));
218 clientaddr.sun_family = AF_UNIX;
219 strncpy(clientaddr.sun_path, SECURITY_SERVER_SOCK_PATH, strlen(SECURITY_SERVER_SOCK_PATH));
220 clientaddr.sun_path[strlen(SECURITY_SERVER_SOCK_PATH)] = 0;
221 client_len = sizeof(clientaddr);
223 ret = connect(localsockfd, (struct sockaddr*)&clientaddr, client_len);
226 if(errno == EINPROGRESS)
228 LOGD("%s\n", "Connection is in progress");
229 check_socket_poll(localsockfd, POLLOUT, 1000);
232 LOGE("%s\n", "poll() error");
236 ret = connect(localsockfd, (struct sockaddr*)&clientaddr, client_len);
239 LOGE("%s\n", "connection failed");
246 LOGE("%s\n", "Connection failed");
257 int fake_get_gid(const char *object)
259 int sockfd = -1, retval, gid;
262 retval = connect_to_server(&sockfd);
265 /* Error on socket */
266 LOGE("Connection failed: %d\n", retval);
270 /* make request packet and send to server*/
271 retval = send_gid_request(sockfd, object);
274 /* Error on socket */
275 LOGE("Send request failed: %d\n", retval);
278 LOGD("%s", "Just closing the socket and exit\n");
287 RUNNER_TEST_GROUP_INIT(SECURITY_SERVER_TESTS_SERVER);
289 RUNNER_TEST(tc_getting_default_cookie)
291 printhex(cookie, 20);
292 RUNNER_ASSERT(security_server_request_cookie((char *)cookie, 20) == SECURITY_SERVER_API_SUCCESS);
295 RUNNER_TEST(tc_security_server_get_gid_normal_case_trying_to_get_gid_of_tel_gprs)
297 RUNNER_ASSERT(security_server_get_gid("tel_gprs") >= 0);
300 RUNNER_TEST(tc_security_server_get_gid_empty_object_name)
302 RUNNER_ASSERT(security_server_get_gid("") == SECURITY_SERVER_API_ERROR_INPUT_PARAM);
305 RUNNER_TEST(tc_security_server_get_gid_wrong_object_name_teltel)
307 RUNNER_ASSERT(security_server_get_gid("teltel") == SECURITY_SERVER_API_ERROR_NO_SUCH_OBJECT);
310 RUNNER_TEST(tc_security_server_get_object_name_normal_case_trying_6001)
312 ret = security_server_get_object_name(6001, obj_name, sizeof(obj_name));
313 LOGD("Result: %s\n", obj_name);
314 RUNNER_ASSERT(ret == SECURITY_SERVER_API_SUCCESS);
317 RUNNER_TEST(tc_security_server_get_object_name_too_small_buffer_size)
319 ret = security_server_get_object_name(6001, obj_name, 5);
320 RUNNER_ASSERT(ret == SECURITY_SERVER_API_ERROR_BUFFER_TOO_SMALL);
323 RUNNER_TEST(tc_security_server_get_object_name_invalid_gid)
325 ret = security_server_get_object_name(9876, obj_name, sizeof(obj_name));
326 RUNNER_ASSERT(ret == SECURITY_SERVER_API_ERROR_NO_SUCH_OBJECT);
329 RUNNER_TEST(tc_ask_for_priviege_with_default_cookie_normal_case_to_check_audio_privilege)
331 ret = security_server_get_gid("audio");
332 ret = security_server_check_privilege((char *) cookie, ret);
333 RUNNER_ASSERT(ret == SECURITY_SERVER_API_SUCCESS);
336 RUNNER_TEST(tc_ask_for_priviege_with_default_cookie_case_with_wrong_cookie)
338 ret = security_server_get_gid("audio");
341 wrong_cookie[i] = rand() % 255;
342 ret = security_server_check_privilege((const char *) wrong_cookie, ret);
343 RUNNER_ASSERT(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED);
346 RUNNER_TEST(tc_fake_security_server_get_gid)
348 /* Close socket just after sending request msg.
349 * This is done with fake security_server_get_gid()*/
350 ret = fake_get_gid("audio");
351 RUNNER_IGNORED_MSG("Watch whether security server has crashed or not.");
354 RUNNER_TEST(tc_get_pid_of_a_given_cookie_default_cookie_case)
356 RUNNER_ASSERT(security_server_get_cookie_pid((const char *) cookie) == 0);
359 RUNNER_TEST(tc_get_pid_of_non_existing_cookie)
361 RUNNER_ASSERT(security_server_get_cookie_pid((const char *) wrong_cookie)== SECURITY_SERVER_API_ERROR_NO_SUCH_COOKIE);
364 RUNNER_TEST(tc_get_pid_of_null_cookie)
366 RUNNER_ASSERT(security_server_get_cookie_pid(NULL) == SECURITY_SERVER_API_ERROR_INPUT_PARAM);
369 RUNNER_TEST(tc_communicating_with_client_and_test_cookie_and_privilege_control)
371 server_sockfd = create_new_socket();
372 RUNNER_ASSERT_MSG(server_sockfd >= 1,"Error on creating a new socket");
373 RUNNER_ASSERT_MSG(listen(server_sockfd, 5) >= 0, "listen() failed");
377 accept_poll[0].fd = server_sockfd;
378 accept_poll[0].events = POLLIN;
379 ret = poll(accept_poll, 1, 5000);
381 /* Call poll() to wait for socket connection */
382 ret = poll(accept_poll, 1, 5000);
383 RUNNER_ASSERT(ret > 0);
390 client_len = sizeof(clientaddr);
391 client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr,
392 (socklen_t *) &client_len);
393 RUNNER_ASSERT_MSG(client_sockfd >= 0, "Cannot accept client");
395 LOGD("New session accepted\n");
397 /* Wait until packet received */
398 client_poll[0].fd = client_sockfd;
399 client_poll[0].events = POLLIN;
402 ret = poll(client_poll, 1, 500);
403 RUNNER_ASSERT(ret > 0); /* poll() error */
406 close(client_sockfd);
408 LOGD("%s\n", "No request from client. closing socket");
412 ret = read(client_sockfd, recvbuf, 4);
413 if(recvbuf[0] == 0 && recvbuf[1] == 0 && recvbuf[2] == 0 && recvbuf[3] == 0)
415 ret = read(client_sockfd, recvbuf, 24);
418 close(client_sockfd);
419 LOGD("cannot read request:%d\n", ret);
420 close(client_sockfd);
424 memcpy(recved_cookie, recvbuf, 20);
425 memcpy(&recved_gid, recvbuf+20, sizeof(int));
427 LOGD("requested cookie: \n");
428 printhex(recved_cookie, 20);
429 LOGD("requested gid: %d\n", recved_gid);
431 ret = security_server_check_privilege((const char *) recved_cookie, recved_gid);
432 RUNNER_ASSERT(ret == SECURITY_SERVER_API_SUCCESS ||
433 ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED); /* Unexpected error occurred */
435 LOGD("Privilege for the request: %d\n", ret);
437 RUNNER_ASSERT(write(client_sockfd, &ret, sizeof(int)) >= sizeof(int)); /* Send error */
439 ret = security_server_get_cookie_pid((const char *) recved_cookie);
440 RUNNER_ASSERT(ret > 0); /* Unexpected error occurred */
443 LOGD("client is root process\n");
447 LOGD("Peer PID is %d\n", ret);
450 else if(recvbuf[0] == 255 && recvbuf[1] == 255 && recvbuf[2] == 255 && recvbuf[3] == 255)
452 char *myargv[5] = {NULL};
454 ret = read(client_sockfd, recvbuf, 28);
457 close(client_sockfd);
458 LOGD("cannot read request:%d\n", ret);
459 close(client_sockfd);
463 memcpy(recved_cookie, recvbuf, 20);
464 memcpy(&recved_gid, recvbuf + 20, sizeof(int));
465 memcpy(&cnt, recvbuf + 24, sizeof(int));
469 myargv[0] = (char *) malloc(strlen("/opt/home/root/security_server_tc_pid_reuser") + 1);
470 sprintf(myargv[0], "/opt/home/root/security_server_tc_pid_reuser");
471 myargv[1] = (char *) malloc(6);
472 sprintf(myargv[1], "%d", cnt);
473 myargv[2] = (char *) malloc(6);
474 sprintf(myargv[2], "%d", recved_gid);
475 myargv[3] = (char *) malloc(40);
477 for(i=0, cnt=0;i<20;i++)
479 if(recved_cookie[i] < 0x10)
480 sprintf(myargv[3] + cnt, "0%x", recved_cookie[i]);
482 sprintf(myargv[3] + cnt, "%x", recved_cookie[i]);
485 LOGD("argv[0]=%s, argv[1]=%s, argv[2]=%s, argv[3]=%s\n", myargv[0], myargv[1], myargv[2], myargv[3]);
486 ret = execve("/opt/home/root/security_server_tc_pid_reuser", myargv, NULL);
487 LOGD("execve failed. errno=%d\n", errno);
489 if(myargv[0] != NULL)
491 if(myargv[1] != NULL)
493 if(myargv[2] != NULL)
495 if(myargv[3] != NULL)
499 else if(recvbuf[0] == 17 )
503 ret = read(client_sockfd, recvbuf, 20);
506 close(client_sockfd);
507 LOGD("cannot read request:%d\n", ret);
508 close(client_sockfd);
511 memcpy(recved_cookie, recvbuf, 20);
512 ret = read(client_sockfd, &olen, 4);
515 close(client_sockfd);
516 LOGD("cannot read request:%d\n", ret);
517 close(client_sockfd);
520 ret = read(client_sockfd, &alen, 4);
523 close(client_sockfd);
524 LOGD("cannot read request:%d\n", ret);
525 close(client_sockfd);
528 ret = read(client_sockfd, olabel, olen);
531 close(client_sockfd);
532 LOGD("cannot read request:%d\n", ret);
533 close(client_sockfd);
537 ret = read(client_sockfd, arights, alen);
540 close(client_sockfd);
541 LOGD("cannot read request:%d\n", ret);
542 close(client_sockfd);
545 arights[alen] = '\0';
546 LOGD("Check by cookie requested.\n");
547 LOGD("requested cookie: \n");
548 printhex(recved_cookie, 20);
549 LOGD("olen: %d\n", olen);
550 LOGD("object label: >%s<\n", olabel);
551 LOGD("alen: %d\n", alen);
552 LOGD("access rights: >%s<\n", arights);
554 ret = security_server_check_privilege_by_cookie(
555 (const char *) recved_cookie, olabel, arights);
557 LOGD("return: %d\n", ret);
559 RUNNER_ASSERT(write(client_sockfd, &ret, sizeof(int)) >= sizeof(int));
561 else if (recvbuf[3] == 1)
563 ret = read(client_sockfd, &olen, 4);
566 close(client_sockfd);
567 LOGD("cannot read request:%d\n", ret);
568 close(client_sockfd);
571 ret = read(client_sockfd, &alen, 4);
574 close(client_sockfd);
575 LOGD("cannot read request:%d\n", ret);
576 close(client_sockfd);
579 ret = read(client_sockfd, olabel, olen);
582 close(client_sockfd);
583 LOGD("cannot read request:%d\n", ret);
584 close(client_sockfd);
588 ret = read(client_sockfd, arights, alen);
591 close(client_sockfd);
592 LOGD("cannot read request:%d\n", ret);
593 close(client_sockfd);
596 arights[alen] = '\0';
597 LOGD("Check by sockfd requested.\n");
598 LOGD("olen: %d\n", olen);
599 LOGD("object label: >%s<\n", olabel);
600 LOGD("alen: %d\n", alen);
601 LOGD("access rights: >%s<\n", arights);
603 ret = security_server_check_privilege_by_sockfd(
604 client_sockfd, olabel, arights);
606 RUNNER_ASSERT(write(client_sockfd, &ret, sizeof(int)) >= sizeof(int));
608 LOGE("malformed request. %d, %d, %d, %d\n", recvbuf[0], recvbuf[1], recvbuf[2], recvbuf[3]);
609 RUNNER_ASSERT_MSG(false, "malformed request");
614 LOGE("malformed request. %d, %d, %d, %d\n", recvbuf[0], recvbuf[1], recvbuf[2], recvbuf[3]);
615 RUNNER_ASSERT_MSG(false, "malfiormed request");
617 if(client_sockfd > 0)
619 close(client_sockfd);
624 int main(int argc, char *argv[])
631 printf("Error: %s must be executed by root\n", argv[0]);
636 DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
638 if(server_sockfd > 0)
639 close(server_sockfd);
640 if(client_sockfd > 0)
641 close(client_sockfd);