4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Bumjin Im <bj.im@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License
27 #include <sys/types.h>
28 #include <sys/smack.h>
34 #include <sys/smack.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
42 #include <privilege-control.h>
43 #include <security-server-system-observer.h>
44 #include <security-server-rules-revoker.h>
46 #include "security-server-cookie.h"
47 #include "security-server-common.h"
48 #include "security-server-password.h"
49 #include "security-server-comm.h"
50 #include "smack-check.h"
52 //definitions of security-server API labels
53 #define API_PASSWD_SET "security-server::api-password-set"
54 #define API_PASSWD_CHECK "security-server::api-password-check"
55 #define API_DATA_SHARE "security-server::api-data-share"
56 #define API_MIDDLEWARE "security-server::api-middleware"
57 #define API_FREE_ACCESS "*"
60 #define API_RULE_REQUIRED "w"
62 /* Set cookie as a global variable */
64 pthread_mutex_t cookie_mutex;
65 int thread_status[SECURITY_SERVER_NUM_THREADS];
66 struct security_server_thread_param {
72 int process_app_get_access_request(int sockfd, size_t msg_len);
73 static int netlink_enabled = 1; /* prevent memory leaks when netlink is disabled */
76 /************************************************************************************************/
77 /* Just for test. This code must be removed on release */
78 #include "security-server-util.h"
79 /************************************************************************************************/
82 void printhex(unsigned char *data, int size)
85 for (i = 0; i < size; i++)
90 printf("%X ", data[i]);
91 if (((i + 1) % 16) == 0 && i != 0)
97 void print_cookie(cookie_list *list)
100 printf("%s", "cookie:\n");
101 printhex(list->cookie, SECURITY_SERVER_COOKIE_LEN);
102 printf("path_len: %d\n", list->path ? strlen(list->path) : 0);
103 printf("permission_len: %d\n", list->permission_len);
104 printf("PID: %d\n", list->pid);
105 printf("path: %s\n", list->path);
106 printf("%s", "permissions: ");
107 for (i = 0; i < list->permission_len; i++)
109 printf("%d ", list->permissions[i]);
112 printf("prev: %p\n", list->prev);
113 printf("next: %p\n", list->next);
117 char *read_exe_path_from_proc(pid_t pid)
124 // get link to executable
125 snprintf(link, sizeof(link), "/proc/%d/exe", pid);
132 SEC_SVR_ERR("Out of memory");
137 cnt = readlink(link, exe, size);
140 if (cnt < 0 || (size_t) cnt > size)
142 SEC_SVR_ERR("Can't locate process binary for pid[%d]", pid);
147 // read less than requested
148 if ((size_t) cnt < size)
151 // read exactly the number of bytes requested
153 if (size > (SIZE_MAX >> 1))
155 SEC_SVR_ERR("Exe path too long (more than %d characters)", size);
160 // readlink does not append null byte to buffer.
166 * Function that checks if API caller have access to specified label.
167 * In positive case (caller has access to the API) returns 1.
168 * In case of no access returns 0, and -1 in case of error.
170 int authorize_SS_API_caller_socket(int sockfd, char *required_API_label, char *required_rule)
175 //for getting socket options
179 SEC_SVR_DBG("Checking client SMACK access to SS API");
181 if (!smack_check()) {
182 SEC_SVR_ERR("No SMACK on device found, API PROTECTION DISABLED!!!");
187 retval = smack_new_label_from_socket(sockfd, &label);
189 SEC_SVR_ERR("%s", "Error in getting label from socket");
195 retval = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
197 SEC_SVR_ERR("Error in getsockopt() and getting binary path");
199 path = read_exe_path_from_proc(cr.pid);
201 retval = smack_have_access(label, required_API_label, required_rule);
203 //some log in SMACK format
205 SEC_SVR_DBG("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", cr.pid, label, required_API_label, required_rule, retval, path);
207 SEC_SVR_ERR("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", cr.pid, label, required_API_label, required_rule, retval, path);
218 /* Object name is actually name of a Group ID *
219 * This function opens /etc/group file and search group ID and
220 * returns the string */
221 int search_object_name(int gid, char *obj, int obj_size)
224 char *linebuf = NULL, *token = NULL, *token2, *tempstr = NULL;
225 int ret = 0, tmp_gid, bufsize;
226 fp = fopen("/etc/group", "r");
229 /* cannot open /etc/group */
230 SEC_SVR_ERR("%s", "Cannot open /etc/group");
231 return SECURITY_SERVER_ERROR_FILE_OPERATION;
234 linebuf = malloc(128);
238 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
239 SEC_SVR_ERR("%s", "cannot malloc()");
243 bzero(linebuf, bufsize);
244 ret = SECURITY_SERVER_ERROR_NO_SUCH_OBJECT;
245 while (fgets(linebuf, bufsize, fp) != NULL)
247 while (linebuf[bufsize - 2] != 0)
249 linebuf[bufsize - 1] = (char) fgetc(fp);
250 tempstr = realloc(linebuf, bufsize + 128);
253 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
257 bzero(linebuf + bufsize, 128);
258 fgets(linebuf + bufsize, 128, fp);
262 token = strtok(linebuf, ":"); /* group name */
265 SEC_SVR_ERR("/etc/group is not valid. cannot find gid: [%s]", linebuf);
266 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
269 token2 = strtok(NULL, ":"); /* group password */
272 SEC_SVR_ERR("/etc/group is not valid. cannot find gid: [%s]", linebuf);
273 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
276 token2 = strtok(NULL, ":"); /* gid */
279 SEC_SVR_ERR("/etc/group is not valid. cannot find gid: [%s]", linebuf);
280 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
285 tmp_gid = strtoul(token2, 0, 10);
288 SEC_SVR_ERR("cannot change string to integer [%s]", token2);
289 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
296 if ((int)strlen(token) > obj_size)
298 ret = SECURITY_SERVER_ERROR_BUFFER_TOO_SMALL;
299 SEC_SVR_ERR("buffer is too small. %d --> %d", obj_size, strlen(token));
302 strncpy(obj, token, strlen(token));
303 obj[strlen(token)] = 0;
304 ret = SECURITY_SERVER_SUCCESS;
307 bzero(linebuf, bufsize);
319 * Searches for group ID by given group name
322 int search_gid(const char *obj)
325 struct group *grpbuf = NULL;
328 char *bigger_buf = NULL;
329 long int max_buf_size = 0;
332 * The maximum needed size for buf can be found using sysconf(3) with the argument _SC_GETGR_R_SIZE_MAX
333 * If _SC_GETGR_R_SIZE_MAX is not returned we set max_buf_size to 1024 bytes. Enough to store few groups.
335 max_buf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
336 if (max_buf_size == -1)
339 buf = malloc((size_t)max_buf_size);
342 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
343 SEC_SVR_ERR("Out Of Memory");
348 * There can be some corner cases when for example user is assigned to a lot of groups.
349 * In that case if buffer is to small getgrnam_r will return ERANGE error.
350 * Solution could be calling getgrnam_r with bigger buffer until it's enough big.
352 while ((ret = getgrnam_r(obj, &grp, buf, (size_t)max_buf_size, &grpbuf)) == ERANGE) {
355 bigger_buf = realloc(buf, (size_t)max_buf_size);
356 if (bigger_buf == NULL) {
357 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
358 SEC_SVR_ERR("Out Of Memory");
367 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
368 SEC_SVR_ERR("getgrnam_r failed with error %s\n", strerror(errno));
370 } else if (grpbuf == NULL) {
371 ret = SECURITY_SERVER_ERROR_NO_SUCH_OBJECT;
372 SEC_SVR_ERR("Cannot find gid for group %s\n", obj);
376 ret = grpbuf->gr_gid;
383 /* Signal handler for processes */
384 static void security_server_sig_child(int signo, siginfo_t *info, void *data)
393 child_pgid = getpgid(info->si_pid);
394 SEC_SVR_DBG("Signal handler: dead_pid=%d, pgid=%d",info->si_pid,child_pgid);
396 while ((child_pid = waitpid(-1, &status, WNOHANG)) > 0) {
397 if (child_pid == child_pgid)
398 killpg(child_pgid,SIGKILL);
404 /* Execute a debugging tool by fork() and execve() */
405 int execute_debug_tool(int argc, char*const *argv, int server_sockfd, int client_sockfd)
408 SEC_SVR_DBG("%s", "Executing tool");
415 close(client_sockfd);
416 close(server_sockfd);
419 for (i = 0; i < _NSIG; i++)
422 ret = execv(argv[0], argv);
425 SEC_SVR_ERR("Error:Failed to execute [%d]", errno);
431 SEC_SVR_ERR("Error: Failed to fork [%d]", errno);
432 return SECURITY_SERVER_ERROR_SERVER_ERROR;
434 return SECURITY_SERVER_SUCCESS;
437 /* Authenticate the application is middleware daemon
438 * The middleware must run as root and the cmd line must be pre listed */
439 int authenticate_developer_shell(int sockfd)
441 int retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
443 unsigned int cl = sizeof(cr);
446 /* get PID of socket peer */
447 if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) != 0)
449 retval = SECURITY_SERVER_ERROR_SOCKET;
450 SEC_SVR_ERR("%s", "Error on getsockopt");
454 /* All middlewares will run as root */
455 if (cr.uid != SECURITY_SERVER_DEVELOPER_UID)
457 retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
458 SEC_SVR_ERR("Non root process has called API: %d", cr.uid);
462 /* Read executable path of the PID from proc fs */
463 exe = read_exe_path_from_proc(cr.pid);
466 /* It's weired. no file in proc file system, */
467 retval = SECURITY_SERVER_ERROR_FILE_OPERATION;
468 SEC_SVR_ERR("Error on opening /proc/%d/exe", cr.pid);
472 /* Search exe of the peer that is really debug tool */
473 if (strcmp(exe, SECURITY_SERVER_DEBUG_TOOL_PATH) != 0)
475 SEC_SVR_ERR("Error: Wrong exe path [%s]", exe);
476 retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
479 retval = SECURITY_SERVER_SUCCESS;
480 SEC_SVR_DBG("%s", "Client Authenticated");
489 int process_cookie_request(int sockfd)
491 int retval, client_pid, client_uid;
492 cookie_list *created_cookie = NULL;
493 unsigned char cookie[SECURITY_SERVER_COOKIE_LEN];
495 char *cookie_label = NULL;
497 /* Authenticate client */
498 retval = authenticate_client_application(sockfd, &client_pid, &client_uid);
499 if (retval != SECURITY_SERVER_SUCCESS)
501 SEC_SVR_ERR("%s", "Client Authentication Failed");
502 retval = send_generic_response(sockfd,
503 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
504 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
505 if (retval != SECURITY_SERVER_SUCCESS)
507 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
511 /* If client application is root process, just respond default cookie */
515 SEC_SVR_DBG("%s", "Requested application is a root process");
516 created_cookie = c_list;
519 SEC_SVR_DBG("%s", "Cannot read default cookie");
526 //TODO: Remove above code if there will be no crashes without it
527 //All process should be treaded the same
529 /* Create a new cookie. or find existing one */
530 pthread_mutex_lock(&cookie_mutex);
531 created_cookie = create_cookie_item(client_pid, sockfd, c_list);
532 if (created_cookie == NULL)
534 pthread_mutex_unlock(&cookie_mutex);
535 SEC_SVR_ERR("%s","Cannot create a cookie");
539 //let others know if this cookie belongs to root process
541 created_cookie->is_roots_process = 1;
543 created_cookie->is_roots_process = 0;
544 memcpy(cookie, created_cookie->cookie, SECURITY_SERVER_COOKIE_LEN);
545 cookie_pid = created_cookie->pid;
546 if (created_cookie->smack_label)
547 cookie_label = strdup(created_cookie->smack_label);
549 cookie_label = strdup("NULL");
550 pthread_mutex_unlock(&cookie_mutex);
553 /* send cookie as response */
554 retval = send_cookie(sockfd, cookie);
555 if (retval != SECURITY_SERVER_SUCCESS)
557 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
559 SEC_SVR_DBG("Server: Cookie created for client PID %d LABEL >%s<",
560 cookie_pid, cookie_label);
562 SEC_SVR_DBG("%s", "Server: Cookie has been sent to client");
569 int process_check_privilege_request(int sockfd)
571 /* Authenticate client */
572 int retval, client_pid, requested_privilege;
574 unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
575 cookie_list *search_result = NULL;
577 retval = authenticate_client_middleware(sockfd, &client_pid);
578 if (retval != SECURITY_SERVER_SUCCESS)
580 SEC_SVR_ERR("%s", "Client Authentication Failed");
581 retval = send_generic_response(sockfd,
582 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
583 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
584 if (retval != SECURITY_SERVER_SUCCESS)
586 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
591 retval = recv_check_privilege_request(sockfd,
592 requested_cookie, &requested_privilege);
593 if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
595 SEC_SVR_ERR("%s", "Receiving request failed");
596 retval = send_generic_response(sockfd,
597 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
598 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
599 if (retval != SECURITY_SERVER_SUCCESS)
601 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
606 if (requested_privilege < 1)
608 SEC_SVR_ERR("Requiring bad privilege [%d]", requested_privilege);
609 retval = send_generic_response(sockfd,
610 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
611 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
612 if (retval != SECURITY_SERVER_SUCCESS)
614 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
619 /* Search cookie list */
620 pthread_mutex_lock(&cookie_mutex);
621 privileges[0] = requested_privilege;
622 search_result = search_cookie(c_list, requested_cookie, privileges, 1);
623 pthread_mutex_unlock(&cookie_mutex);
624 if (search_result != NULL)
627 SEC_SVR_DBG("We found the cookie with %d privilege and pid:%d", requested_privilege, client_pid);
628 SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
629 retval = send_generic_response(sockfd,
630 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
631 SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED);
632 if (retval != SECURITY_SERVER_SUCCESS)
634 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
640 SEC_SVR_ERR("Could not find the cookie with %d privilege", requested_privilege);
641 retval = send_generic_response(sockfd,
642 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
643 SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
644 if (retval != SECURITY_SERVER_SUCCESS)
646 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
653 int process_check_privilege_new_request(int sockfd)
655 /* Authenticate client */
656 int retval, client_pid;
657 unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
658 cookie_list *search_result = NULL;
659 char object_label[MAX_OBJECT_LABEL_LEN + 1];
660 char access_rights[MAX_MODE_STR_LEN + 1];
662 retval = authenticate_client_middleware(sockfd, &client_pid);
663 if (retval != SECURITY_SERVER_SUCCESS)
665 SEC_SVR_ERR("%s", "Client Authentication Failed");
666 retval = send_generic_response(sockfd,
667 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
668 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
669 if (retval != SECURITY_SERVER_SUCCESS)
671 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
676 retval = recv_check_privilege_new_request(
677 sockfd, requested_cookie, object_label, access_rights);
678 if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
680 SEC_SVR_ERR("%s", "Receiving request failed");
681 retval = send_generic_response(sockfd,
682 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
683 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
684 if (retval != SECURITY_SERVER_SUCCESS)
686 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
691 /* Search cookie list */
692 pthread_mutex_lock(&cookie_mutex);
693 search_result = search_cookie_new(c_list, requested_cookie, object_label, access_rights);
694 pthread_mutex_unlock(&cookie_mutex);
696 if (search_result != NULL)
699 SEC_SVR_DBG("We found the cookie with %s rights and pid:%d", access_rights, client_pid);
700 SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
701 retval = send_generic_response(sockfd,
702 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
703 SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED);
704 if (retval != SECURITY_SERVER_SUCCESS)
706 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
712 SEC_SVR_ERR("Could not find the cookie with %s rights", access_rights);
713 retval = send_generic_response(sockfd,
714 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
715 SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
716 if (retval != SECURITY_SERVER_SUCCESS)
718 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
725 int process_object_name_request(int sockfd)
727 int retval, client_pid, requested_privilege;
728 char object_name[SECURITY_SERVER_MAX_OBJ_NAME];
730 /* Authenticate client */
731 retval = authenticate_client_middleware(sockfd, &client_pid);
732 if (retval != SECURITY_SERVER_SUCCESS)
734 SEC_SVR_ERR("%s", "Client Authentication Failed");
735 retval = send_generic_response(sockfd,
736 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
737 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
738 if (retval != SECURITY_SERVER_SUCCESS)
740 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
746 retval = TEMP_FAILURE_RETRY(read(sockfd, &requested_privilege, sizeof(requested_privilege)));
747 if (retval < (int)sizeof(requested_privilege))
749 SEC_SVR_ERR("%s", "Receiving request failed");
750 retval = send_generic_response(sockfd,
751 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
752 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
753 if (retval != SECURITY_SERVER_SUCCESS)
755 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
760 /* Search from /etc/group */
761 retval = search_object_name(requested_privilege,
763 SECURITY_SERVER_MAX_OBJ_NAME);
764 if (retval == SECURITY_SERVER_ERROR_NO_SUCH_OBJECT)
767 SEC_SVR_ERR("There is no such object for gid [%d]", requested_privilege);
768 retval = send_generic_response(sockfd,
769 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
770 SECURITY_SERVER_RETURN_CODE_NO_SUCH_OBJECT);
771 if (retval != SECURITY_SERVER_SUCCESS)
773 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
777 if (retval != SECURITY_SERVER_SUCCESS)
780 SEC_SVR_ERR("Error on searching object name [%d]", retval);
781 retval = send_generic_response(sockfd,
782 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
783 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
784 if (retval != SECURITY_SERVER_SUCCESS)
786 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
792 SECURE_LOGD("We found object: %s", object_name);
793 retval = send_object_name(sockfd, object_name);
794 if (retval != SECURITY_SERVER_SUCCESS)
796 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
803 int process_gid_request(int sockfd, int msg_len)
805 int retval, client_pid;
806 char object_name[SECURITY_SERVER_MAX_OBJ_NAME];
807 /* Authenticate client as middleware daemon */
808 retval = authenticate_client_middleware(sockfd, &client_pid);
809 if (retval != SECURITY_SERVER_SUCCESS)
811 SEC_SVR_ERR("%s", "Client authentication failed");
812 retval = send_generic_response(sockfd,
813 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
814 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
815 if (retval != SECURITY_SERVER_SUCCESS)
817 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
822 if (msg_len >= SECURITY_SERVER_MAX_OBJ_NAME)
824 /* Too big ojbect name */
825 SEC_SVR_ERR("%s", "Object name is too big");
826 retval = send_generic_response(sockfd,
827 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
828 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
829 if (retval != SECURITY_SERVER_SUCCESS)
831 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
836 /* Receive group name */
837 retval = TEMP_FAILURE_RETRY(read(sockfd, object_name, msg_len));
838 if (retval < msg_len)
840 SECURE_LOGE("%s", "Failed to read object name");
841 retval = send_generic_response(sockfd,
842 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
843 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
844 if (retval != SECURITY_SERVER_SUCCESS)
846 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
850 object_name[msg_len] = 0;
852 /* Search /etc/group for the given group name */
853 retval = search_gid(object_name);
854 if (retval == SECURITY_SERVER_ERROR_NO_SUCH_OBJECT)
857 SECURE_LOGE("The object [%s] is not exist", object_name);
858 retval = send_generic_response(sockfd,
859 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
860 SECURITY_SERVER_RETURN_CODE_NO_SUCH_OBJECT);
861 if (retval != SECURITY_SERVER_SUCCESS)
863 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
871 SEC_SVR_ERR("Cannot send the response. %d", retval);
872 retval = send_generic_response(sockfd,
873 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
874 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
875 if (retval != SECURITY_SERVER_SUCCESS)
877 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
883 retval = send_gid(sockfd, retval);
884 if (retval != SECURITY_SERVER_SUCCESS)
886 SEC_SVR_ERR("ERROR: Cannot gid response: %d", retval);
892 int process_pid_request(int sockfd)
894 int retval, client_pid;
895 unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
896 int *privileges = NULL;
897 cookie_list *search_result = NULL;
898 pid_t cookie_pid = 0;
900 /* Authenticate client */
901 retval = authenticate_client_middleware(sockfd, &client_pid);
902 if (retval != SECURITY_SERVER_SUCCESS)
904 SEC_SVR_ERR("%s", "Client Authentication Failed");
905 retval = send_generic_response(sockfd,
906 SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
907 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
908 if (retval != SECURITY_SERVER_SUCCESS)
910 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
915 retval = recv_pid_request(sockfd, requested_cookie);
916 if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
918 SEC_SVR_ERR("%s", "Receiving request failed");
919 retval = send_generic_response(sockfd,
920 SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
921 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
922 if (retval != SECURITY_SERVER_SUCCESS)
924 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
929 retval = get_client_gid_list(sockfd, &privileges);
932 SEC_SVR_ERR("ERROR: Cannot get GID list");
936 /* Search cookie list */
937 pthread_mutex_lock(&cookie_mutex);
938 search_result = search_cookie(c_list, requested_cookie, privileges, retval);
940 cookie_pid = search_result->pid;
941 pthread_mutex_unlock(&cookie_mutex);
945 if (search_result != NULL)
948 SEC_SVR_DBG("We found the cookie and pid:%d", cookie_pid);
949 SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
950 retval = send_pid(sockfd, cookie_pid);
952 if (retval != SECURITY_SERVER_SUCCESS)
954 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
960 SEC_SVR_ERR("%s", "Could not find the cookie");
961 retval = send_generic_response(sockfd,
962 SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
963 SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
964 if (retval != SECURITY_SERVER_SUCCESS)
966 SEC_SVR_ERR("ERROR: Cannot send pid response: %d", retval);
973 int process_smack_request(int sockfd)
975 int retval, client_pid;
976 int *privileges = NULL;
977 unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
978 cookie_list *search_result = NULL;
979 //handler for SMACK label
982 /* Authenticate client */
983 retval = authenticate_client_middleware(sockfd, &client_pid);
984 if (retval != SECURITY_SERVER_SUCCESS)
986 SEC_SVR_ERR("%s", "Client Authentication Failed");
987 retval = send_generic_response(sockfd,
988 SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
989 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
990 if (retval != SECURITY_SERVER_SUCCESS)
992 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
997 retval = recv_smack_request(sockfd, requested_cookie);
998 if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
1000 SEC_SVR_ERR("%s", "Receiving request failed");
1001 retval = send_generic_response(sockfd,
1002 SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
1003 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1004 if (retval != SECURITY_SERVER_SUCCESS)
1006 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1011 retval = get_client_gid_list(sockfd, &privileges);
1014 SEC_SVR_ERR("ERROR: Cannot get GID list");
1018 /* Search cookie list */
1019 pthread_mutex_lock(&cookie_mutex);
1020 search_result = search_cookie(c_list, requested_cookie, privileges, retval);
1021 if (search_result) {
1022 if (search_result->smack_label)
1023 label = strdup(search_result->smack_label);
1025 SEC_SVR_DBG("%s", "No SMACK support on device - returning empty label");
1029 pthread_mutex_unlock(&cookie_mutex);
1033 if (search_result != NULL)
1036 SEC_SVR_DBG("We found the cookie and pid:%d", search_result->pid);
1037 SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
1038 SEC_SVR_DBG("Read label is: %s\n", label);
1040 retval = send_smack(sockfd, label);
1042 if (retval != SECURITY_SERVER_SUCCESS)
1044 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1049 /* It's not exist */
1050 SEC_SVR_ERR("%s", "Could not find the cookie");
1051 retval = send_generic_response(sockfd,
1052 SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
1053 SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
1054 if (retval != SECURITY_SERVER_SUCCESS)
1056 SEC_SVR_ERR("ERROR: Cannot send SMACK label response: %d", retval);
1064 int process_pid_privilege_check(int sockfd, int datasize)
1066 //In this function we parsing received PID privilege check request
1070 char *object = NULL;
1071 char *access_rights = NULL;
1072 unsigned char return_code;
1074 char subject[SMACK_LABEL_LEN + 1];
1077 //authenticate client
1078 retval = authenticate_client_middleware(sockfd, &client_pid);
1080 if (retval != SECURITY_SERVER_SUCCESS) {
1081 SEC_SVR_ERR("%s", "Client Authentication Failed");
1082 retval = send_generic_response(sockfd,
1083 SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_RESPONSE,
1084 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1086 if (retval != SECURITY_SERVER_SUCCESS)
1087 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1093 retval = recv_pid_privilege_request(sockfd, datasize, &pid, &object, &access_rights);
1095 if (retval == SECURITY_SERVER_ERROR_RECV_FAILED) {
1096 SEC_SVR_ERR("%s", "Receiving request failed");
1097 retval = send_generic_response(sockfd,
1098 SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_RESPONSE,
1099 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1101 if (retval != SECURITY_SERVER_SUCCESS)
1102 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1107 if (smack_check()) {
1108 retval = smack_pid_have_access(pid, object, access_rights);
1109 SEC_SVR_DBG("smack_pid_have_access returned %d", retval);
1111 if (get_smack_label_from_process(pid, subject) != PC_OPERATION_SUCCESS) {
1112 // subject label is set to empty string
1113 SEC_SVR_ERR("get_smack_label_from_process failed. Subject label has not been read.");
1115 SEC_SVR_DBG("Subject label of client PID %d is: %s", pid, subject);
1118 SEC_SVR_DBG("SMACK is not available. Subject label has not been read.");
1122 path = read_exe_path_from_proc(pid);
1125 SECURE_LOGD("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", pid, subject, object, access_rights, retval, path);
1127 SECURE_LOGE("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", pid, subject, object, access_rights, retval, path);
1132 if (retval == 1) //there is permission
1133 return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
1134 else //there is no permission
1135 return_code = SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED;
1138 retval = send_generic_response(sockfd,
1139 SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_RESPONSE,
1142 if (retval != SECURITY_SERVER_SUCCESS)
1143 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1149 if (access_rights != NULL)
1150 free(access_rights);
1155 int process_tool_request(int client_sockfd, int server_sockfd)
1157 int retval, argcnum = 0;
1158 char **recved_argv = NULL;
1160 /* Authenticate client */
1161 retval = authenticate_developer_shell(client_sockfd);
1162 if (retval != SECURITY_SERVER_SUCCESS)
1164 SEC_SVR_ERR("%s", "Client Authentication Failed");
1165 retval = send_generic_response(client_sockfd,
1166 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1167 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1168 if (retval != SECURITY_SERVER_SUCCESS)
1170 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1175 /* Receive Total number of argv */
1176 retval = TEMP_FAILURE_RETRY(read(client_sockfd, &argcnum, sizeof(int)));
1177 if ((retval < (int)sizeof(int)) || argcnum > (UINT_MAX / sizeof(char*)) - 2 || argcnum < 0)
1179 SEC_SVR_ERR("Error: argc recieve failed: %d", retval);
1180 retval = send_generic_response(client_sockfd,
1181 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1182 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1183 if (retval != SECURITY_SERVER_SUCCESS)
1185 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1190 recved_argv = (char**)malloc(sizeof(char*) * argcnum);
1191 if (recved_argv == NULL)
1193 SEC_SVR_ERR("Error: malloc() failed: %d", retval);
1194 retval = send_generic_response(client_sockfd,
1195 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1196 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
1197 if (retval != SECURITY_SERVER_SUCCESS)
1199 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1203 memset(recved_argv, 0, sizeof(char*) * argcnum);
1205 retval = recv_launch_tool_request(client_sockfd, argcnum - 1, recved_argv);
1206 if (retval == SECURITY_SERVER_ERROR_RECV_FAILED || retval == SECURITY_SERVER_ERROR_OUT_OF_MEMORY)
1208 SEC_SVR_ERR("%s", "Receiving request failed");
1209 retval = send_generic_response(client_sockfd,
1210 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1211 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1212 if (retval != SECURITY_SERVER_SUCCESS)
1214 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1220 SEC_SVR_ERR("Error: Too small number of argv [%d]", argcnum);
1221 retval = send_generic_response(client_sockfd,
1222 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1223 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1224 if (retval != SECURITY_SERVER_SUCCESS)
1226 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1230 /* Execute the command */
1231 retval = execute_debug_tool(argcnum, recved_argv, server_sockfd, client_sockfd);
1232 if (retval != SECURITY_SERVER_SUCCESS)
1234 SEC_SVR_ERR("Error: Cannot execute debug tool [%d]", retval);
1235 retval = send_generic_response(client_sockfd,
1236 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1237 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
1238 if (retval != SECURITY_SERVER_SUCCESS)
1240 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1245 SEC_SVR_DBG("%s", "Tool has been executed");
1246 retval = send_generic_response(client_sockfd,
1247 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1248 SECURITY_SERVER_RETURN_CODE_SUCCESS);
1249 if (retval != SECURITY_SERVER_SUCCESS)
1251 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1255 free_argv(recved_argv, argcnum);
1260 /* Send exe path response to client
1262 * Get exe path response packet format
1264 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1265 * |---------------------------------------------------------------|
1266 * | version=0x01 |MessageID=0x24 |Message Length = 4+path length |
1267 * |---------------------------------------------------------------|
1268 * | return code | Path length | Path |
1269 * |---------------------------------------------------------------|
1272 int send_exe_path_response(int sockfd, const char *path)
1274 response_header hdr;
1275 unsigned char *msg = NULL;
1276 unsigned char *ptr = NULL;
1278 size_t path_len = 0;
1279 unsigned short msg_len = 0;
1282 SEC_SVR_ERR("Path is NULL");
1283 return SECURITY_SERVER_ERROR_INPUT_PARAM;
1286 path_len = strlen(path);
1287 msg_len = sizeof(hdr) + sizeof(size_t) + path_len;
1288 msg = (unsigned char*)malloc(msg_len * sizeof(unsigned char));
1290 SEC_SVR_ERR("malloc failed");
1291 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1294 /* Assemble header */
1295 hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
1296 hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_EXE_PATH_RESPONSE;
1297 hdr.basic_hdr.msg_len = sizeof(size_t) + path_len;
1298 hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
1300 /* Prepare packet */
1302 memcpy(ptr, &hdr, sizeof(hdr));
1304 memcpy(ptr, &path_len, sizeof(size_t));
1305 ptr += sizeof(size_t);
1306 memcpy(ptr, path, path_len);
1309 ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1310 if (ret == SECURITY_SERVER_ERROR_POLL)
1312 SEC_SVR_ERR("%s", "poll() error");
1313 ret = SECURITY_SERVER_ERROR_SEND_FAILED;
1316 if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
1318 SEC_SVR_ERR("%s", "poll() timeout");
1319 ret = SECURITY_SERVER_ERROR_SEND_FAILED;
1324 ret = TEMP_FAILURE_RETRY(write(sockfd, msg, msg_len));
1327 SEC_SVR_ERR("Error on write(): %d", ret);
1328 ret = SECURITY_SERVER_ERROR_SEND_FAILED;
1331 ret = SECURITY_SERVER_SUCCESS;
1339 int process_exe_path_request(int sockfd)
1346 retval = TEMP_FAILURE_RETRY(read(sockfd, &pid, sizeof(pid_t)));
1347 if (retval < (ssize_t) sizeof(pid_t))
1349 SEC_SVR_ERR("Server Error: recieve failed: %d", retval);
1350 retval = send_generic_response(
1352 SECURITY_SERVER_MSG_TYPE_EXE_PATH_RESPONSE,
1353 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1355 if (retval != SECURITY_SERVER_SUCCESS)
1356 SEC_SVR_ERR("Server ERROR: Cannot send generic response: %d", retval);
1360 SEC_SVR_DBG("Server: Get exe path request for pid %d", pid);
1362 // get executable path
1363 exe = read_exe_path_from_proc(pid);
1366 SEC_SVR_ERR("Server: Failed to read executable path for pid %d", pid);
1367 retval = send_generic_response(
1369 SECURITY_SERVER_MSG_TYPE_EXE_PATH_RESPONSE,
1370 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
1372 if (retval != SECURITY_SERVER_SUCCESS)
1373 SEC_SVR_ERR("Server ERROR: Cannot send generic response: %d", retval);
1378 retval = send_exe_path_response(sockfd, exe);
1379 if (retval != SECURITY_SERVER_SUCCESS)
1380 SEC_SVR_ERR("ERROR: Cannot send exe path response: %d", retval);
1387 int client_has_access(int sockfd, const char *object)
1394 struct ucred socopt;
1395 unsigned int socoptSize = sizeof(socopt);
1399 retval = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &socopt, &socoptSize);
1401 SEC_SVR_DBG("%s", "Error on getsockopt");
1404 //now we have PID in sockopt.pid
1406 if (smack_new_label_from_socket(sockfd, &label)) {
1407 SEC_SVR_ERR("%s", "Error on smack_new_label_from_socket");
1411 if (0 >= (ret = smack_pid_have_access(socopt.pid, object, "rw"))) {
1416 if (SECURITY_SERVER_SUCCESS == authenticate_client_application(sockfd, &pid, &uid))
1417 SEC_SVR_DBG("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=rw, result=%d",
1418 pid, label, object, ret);
1424 void *security_server_thread(void *param)
1426 int client_sockfd = -1, client_uid, client_pid;
1427 int server_sockfd, retval;
1428 basic_header basic_hdr;
1429 struct security_server_thread_param *my_param;
1431 my_param = (struct security_server_thread_param*) param;
1432 client_sockfd = my_param->client_sockfd;
1433 server_sockfd = my_param->server_sockfd;
1435 /* Receive request header */
1436 retval = recv_hdr(client_sockfd, &basic_hdr);
1437 if (retval == SECURITY_SERVER_ERROR_TIMEOUT || retval == SECURITY_SERVER_ERROR_RECV_FAILED
1438 || retval == SECURITY_SERVER_ERROR_SOCKET)
1440 SEC_SVR_ERR("Receiving header error [%d]",retval);
1441 close(client_sockfd);
1446 if (retval != SECURITY_SERVER_SUCCESS)
1449 SEC_SVR_ERR("Receiving header error [%d]",retval);
1450 retval = send_generic_response(client_sockfd,
1451 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1452 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1453 if (retval != SECURITY_SERVER_SUCCESS)
1455 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1458 safe_server_sock_close(client_sockfd);
1463 //TODO: Below authorize_SS_API_caller_socket() is used for authorize API caller by SMACK,
1464 // at the moment return value is not checked and each access is allowed.
1465 // If we realy want to restrict access it must be changed in future.
1467 /* Act different for request message ID */
1468 switch (basic_hdr.msg_id)
1470 case SECURITY_SERVER_MSG_TYPE_COOKIE_REQUEST:
1471 SEC_SVR_DBG("%s", "Cookie request received");
1472 authorize_SS_API_caller_socket(client_sockfd, API_FREE_ACCESS, API_RULE_REQUIRED);
1473 process_cookie_request(client_sockfd);
1476 case SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_REQUEST:
1477 SEC_SVR_DBG("%s", "Privilege check received");
1478 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1479 process_check_privilege_request(client_sockfd);
1482 case SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_REQUEST:
1483 SEC_SVR_DBG("%s", "Privilege check (new mode) received");
1484 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1485 process_check_privilege_new_request(client_sockfd);
1488 case SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_REQUEST:
1489 SEC_SVR_DBG("%s", "Get object name request received");
1490 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1491 process_object_name_request(client_sockfd);
1494 case SECURITY_SERVER_MSG_TYPE_GID_REQUEST:
1495 SEC_SVR_DBG("%s", "Get GID received");
1496 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1497 process_gid_request(client_sockfd, (int)basic_hdr.msg_len);
1500 case SECURITY_SERVER_MSG_TYPE_PID_REQUEST:
1501 SEC_SVR_DBG("%s", "pid request received");
1502 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1503 process_pid_request(client_sockfd);
1506 case SECURITY_SERVER_MSG_TYPE_SMACK_REQUEST:
1507 SEC_SVR_DBG("%s", "SMACK label request received");
1508 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1509 process_smack_request(client_sockfd);
1512 case SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_REQUEST:
1513 SEC_SVR_DBG("%s", "PID privilege check request received");
1514 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1515 //pass data size to function
1516 process_pid_privilege_check(client_sockfd, basic_hdr.msg_len);
1519 case SECURITY_SERVER_MSG_TYPE_TOOL_REQUEST:
1520 SEC_SVR_DBG("%s", "launch tool request received");
1521 authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1522 process_tool_request(client_sockfd, server_sockfd);
1525 case SECURITY_SERVER_MSG_TYPE_VALID_PWD_REQUEST:
1526 SECURE_LOGD("%s", "Server: validate password request received");
1527 authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_CHECK, API_RULE_REQUIRED);
1528 process_valid_pwd_request(client_sockfd);
1531 case SECURITY_SERVER_MSG_TYPE_SET_PWD_REQUEST:
1532 SECURE_LOGD("%s", "Server: set password request received");
1533 authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1534 process_set_pwd_request(client_sockfd);
1537 case SECURITY_SERVER_MSG_TYPE_RESET_PWD_REQUEST:
1538 SECURE_LOGD("%s", "Server: reset password request received");
1539 authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1540 process_reset_pwd_request(client_sockfd);
1543 case SECURITY_SERVER_MSG_TYPE_CHK_PWD_REQUEST:
1544 SECURE_LOGD("%s", "Server: check password request received");
1545 authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_CHECK, API_RULE_REQUIRED);
1546 process_chk_pwd_request(client_sockfd);
1549 case SECURITY_SERVER_MSG_TYPE_SET_PWD_HISTORY_REQUEST:
1550 SECURE_LOGD("%s", "Server: set password histroy request received");
1551 authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1552 process_set_pwd_history_request(client_sockfd);
1555 case SECURITY_SERVER_MSG_TYPE_SET_PWD_MAX_CHALLENGE_REQUEST:
1556 SECURE_LOGD("%s", "Server: set password max challenge request received");
1557 authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1558 process_set_pwd_max_challenge_request(client_sockfd);
1561 case SECURITY_SERVER_MSG_TYPE_SET_PWD_VALIDITY_REQUEST:
1562 SECURE_LOGD("%s", "Server: set password validity request received");
1563 authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1564 process_set_pwd_validity_request(client_sockfd);
1567 case SECURITY_SERVER_MSG_TYPE_APP_GIVE_ACCESS_REQUEST:
1568 SEC_SVR_DBG("%s", "Server: app give access request received");
1569 authorize_SS_API_caller_socket(client_sockfd, API_DATA_SHARE, API_RULE_REQUIRED);
1570 if (client_has_access(client_sockfd, API_DATA_SHARE)) {
1571 SEC_SVR_DBG("%s", "Server: app give access request received");
1572 if (basic_hdr.msg_len >= 0 && (size_t)basic_hdr.msg_len >= sizeof(basic_hdr)) {
1573 process_app_get_access_request(client_sockfd,
1574 basic_hdr.msg_len - sizeof(basic_hdr));
1576 SEC_SVR_ERR("ERROR: Invalid message length: %d", basic_hdr.msg_len);
1579 SEC_SVR_DBG("%s", "Server: app give access request received (API DENIED - request will not proceed)");
1580 send_generic_response(client_sockfd,
1581 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1582 SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
1585 /************************************************************************************************/
1586 /* Just for test. This code must be removed on release */
1587 case SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_REQUEST:
1588 SEC_SVR_DBG("%s", "all cookie info request received -- NEED TO BE DELETED ON RELEASE");
1589 retval = authenticate_client_application(client_sockfd, &client_pid, &client_uid);
1590 if (retval != SECURITY_SERVER_SUCCESS)
1592 SEC_SVR_ERR("%s", "Client Authentication Failed");
1593 retval = send_generic_response(client_sockfd,
1594 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1595 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1596 if (retval != SECURITY_SERVER_SUCCESS)
1598 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1602 retval = util_process_all_cookie(client_sockfd, c_list);
1603 if (retval != SECURITY_SERVER_SUCCESS)
1605 SEC_SVR_ERR("ERROR: Cannot send all cookie info: %d", retval);
1609 case SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_PID_REQUEST:
1610 SEC_SVR_DBG("%s", "cookie info from pid request received -- NEED TO BE DELETED ON RELEASE");
1611 if (retval != SECURITY_SERVER_SUCCESS)
1613 SEC_SVR_ERR("%s", "Client Authentication Failed");
1614 retval = send_generic_response(client_sockfd,
1615 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1616 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1617 if (retval != SECURITY_SERVER_SUCCESS)
1619 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1623 util_process_cookie_from_pid(client_sockfd, c_list);
1626 case SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_COOKIE_REQUEST:
1627 SEC_SVR_DBG("%s", "cookie info from cookie request received -- NEED TO BE DELETED ON RELEASE");
1628 if (retval != SECURITY_SERVER_SUCCESS)
1630 SEC_SVR_ERR("%s", "Client Authentication Failed");
1631 retval = send_generic_response(client_sockfd,
1632 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1633 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1634 if (retval != SECURITY_SERVER_SUCCESS)
1636 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1640 util_process_cookie_from_cookie(client_sockfd, c_list);
1642 /************************************************************************************************/
1646 SEC_SVR_ERR("Unknown msg ID :%d", basic_hdr.msg_id);
1647 /* Unknown message ID */
1648 retval = send_generic_response(client_sockfd,
1649 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1650 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1651 if (retval != SECURITY_SERVER_SUCCESS)
1653 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1658 if (client_sockfd > 0)
1660 safe_server_sock_close(client_sockfd);
1665 if (client_sockfd > 0)
1666 close(client_sockfd);
1667 thread_status[my_param->thread_status] = 0;
1668 pthread_detach(pthread_self());
1672 void *security_server_main_thread(void *data)
1674 int server_sockfd = 0, retval, client_sockfd = -1, rc;
1675 struct sigaction act, dummy;
1676 pthread_t threads[SECURITY_SERVER_NUM_THREADS];
1677 struct security_server_thread_param param[SECURITY_SERVER_NUM_THREADS];
1681 SECURE_LOGD("%s", "Starting Security Server main thread");
1683 /* security server must be executed by root */
1686 fprintf(stderr, "%s\n", "You are not root. exiting...");
1690 for (retval = 0; retval < SECURITY_SERVER_NUM_THREADS; retval++)
1691 thread_status[retval] = 0;
1694 /* Create and bind a Unix domain socket */
1695 retval = create_new_socket(&server_sockfd);
1696 if (retval != SECURITY_SERVER_SUCCESS)
1698 SEC_SVR_ERR("%s", "cannot create socket. exiting...");
1702 if (listen(server_sockfd, 5) < 0)
1704 SEC_SVR_ERR("%s", "listen() failed. exiting...");
1708 /* Create a default cookie --> Cookie for root process */
1709 c_list = create_default_cookie();
1712 SEC_SVR_ERR("%s", "cannot make a default cookie. exiting...");
1716 /* Init signal handler */
1717 act.sa_handler = NULL;
1718 act.sa_sigaction = security_server_sig_child;
1719 sigemptyset(&act.sa_mask);
1720 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
1722 if (sigaction(SIGCHLD, &act, &dummy) < 0)
1724 SEC_SVR_ERR("%s", "cannot change session");
1727 pthread_mutex_init(&cookie_mutex, NULL);
1731 /* Accept a new client */
1732 if (client_sockfd < 0)
1733 client_sockfd = accept_client(server_sockfd);
1735 if (client_sockfd == SECURITY_SERVER_ERROR_TIMEOUT)
1737 if (client_sockfd < 0)
1739 SEC_SVR_DBG("Server: new connection has been accepted: %d", client_sockfd);
1743 if (thread_status[retval] == 0)
1745 thread_status[retval] = 1;
1746 param[retval].client_sockfd = client_sockfd;
1747 param[retval].server_sockfd = server_sockfd;
1748 param[retval].thread_status = retval;
1749 SEC_SVR_DBG("Server: Creating a new thread: %d", retval);
1750 rc = pthread_create(&threads[retval], NULL, security_server_thread, (void*)¶m[retval]);
1753 SEC_SVR_ERR("Error: Server: Cannot create thread:%d", rc);
1759 if (retval >= SECURITY_SERVER_NUM_THREADS)
1765 if (server_sockfd > 0)
1766 close(server_sockfd);
1768 pthread_detach(pthread_self());
1772 ssize_t read_wrapper(int sockfd, void *buffer, size_t len)
1774 unsigned char *buff = (unsigned char*)buffer;
1776 while (done < (int)len) {
1777 struct pollfd fds = { sockfd, POLLIN, 0};
1778 if (0 >= poll(&fds, 1, 1000))
1780 ssize_t ret = read(sockfd, buff + done, len - done);
1787 if (-1 == ret && EAGAIN != errno && EINTR != errno)
1793 int process_app_get_access_request(int sockfd, size_t msg_len)
1795 char *message_buffer = NULL;
1796 char *client_label = NULL;
1797 char *provider_label = NULL;
1798 struct smack_accesses *smack = NULL;
1799 int ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
1800 int send_message_id = SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE;
1801 int send_error_id = SECURITY_SERVER_RETURN_CODE_SERVER_ERROR;
1803 static const char*const revoke = "-----";
1804 const char *permissions = "rwxat";
1806 message_buffer = malloc(msg_len + 1);
1807 if (!message_buffer)
1808 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1809 message_buffer[msg_len] = 0;
1811 ssize_t retval = read_wrapper(sockfd, message_buffer, msg_len);
1813 if (retval < (ssize_t)msg_len) {
1814 SEC_SVR_ERR("%s", "Error in read. Message too short");
1815 send_error_id = SECURITY_SERVER_RETURN_CODE_BAD_REQUEST;
1816 ret = SECURITY_SERVER_ERROR_BAD_REQUEST;
1820 // Currently we don't use client_pid
1821 memcpy(&client_pid, message_buffer, sizeof(int));
1822 client_label = message_buffer + sizeof(int);
1824 if (smack_check()) {
1825 if (0 != smack_new_label_from_socket(sockfd, &provider_label)) {
1826 SEC_SVR_ERR("%s", "Error in smack_new_label_from_socket");
1830 if (!util_smack_label_is_valid(client_label)) {
1831 send_error_id = SECURITY_SERVER_RETURN_CODE_BAD_REQUEST;
1835 if (smack_accesses_new(&smack))
1838 if (smack_accesses_add_modify(smack, client_label,
1839 provider_label, permissions, revoke))
1842 if (smack_accesses_apply(smack)) {
1843 send_message_id = SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED;
1848 ret = SECURITY_SERVER_SUCCESS;
1849 send_message_id = SECURITY_SERVER_MSG_TYPE_APP_GIVE_ACCESS_RESPONSE;
1850 send_error_id = SECURITY_SERVER_RETURN_CODE_SUCCESS;
1854 retval = send_generic_response(sockfd, send_message_id, send_error_id);
1855 if (retval != SECURITY_SERVER_SUCCESS)
1856 SEC_SVR_ERR("Server ERROR: Cannot send response: %d", retval);
1858 free(message_buffer);
1859 free(provider_label);
1860 smack_accesses_free(smack);
1864 void *system_observer_main_thread(void *data)
1866 system_observer_main(data);
1867 SEC_SVR_ERR("%s", "System observer: exit. No garbage collector support.");
1868 netlink_enabled = 0;
1869 pthread_detach(pthread_self());
1873 int main(int argc, char *argv[])
1876 pthread_t main_thread;
1881 // create observer thread only if smack is enabled
1882 if (smack_check()) {
1883 pthread_t system_observer;
1884 system_observer_config so_config;
1885 so_config.event_callback = rules_revoker_callback;
1887 res = pthread_create(&system_observer, NULL, system_observer_main_thread, (void*)&so_config);
1893 SEC_SVR_DBG("SMACK is not available. Observer thread disabled.");
1896 res = pthread_create(&main_thread, NULL, security_server_main_thread, NULL);
1904 SECURE_LOGE("Error: Server: Cannot create main security server thread: %d", res);