3 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd All Rights Reserved
5 * Contact: Bumjin Im <bj.im@samsung.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License
27 #include <sys/types.h>
29 #include <sys/smack.h>
31 #include <security-server-cookie.h>
32 #include <security-server-comm.h>
33 #include <smack-check.h>
35 /* Delete useless cookie item *
36 * then connect prev and next */
37 int free_cookie_item(cookie_list *cookie)
39 if(cookie->path != NULL)
41 if(cookie->permissions != NULL)
42 free(cookie->permissions);
43 if(cookie->smack_label != NULL)
44 free(cookie->smack_label);
45 if(cookie->prev != NULL)
46 cookie->prev->next = cookie->next;
47 if(cookie->next != NULL)
48 cookie->next->prev = cookie->prev;
54 /* Cut the link of the current cookie item and connect previous link and next line *
55 * That is remove a cookie item *
56 * Returns next cookie item if exist, NULL for no more cookie item */
57 cookie_list *delete_cookie_item(cookie_list *cookie)
59 cookie_list *retval = NULL;
62 SEC_SVR_DBG("%s", "Cannot delete null cookie");
66 /* Reconnect cookie item */
67 if(cookie->next != NULL)
69 cookie->prev->next = cookie->next;
70 cookie->next->prev = cookie->prev;
71 retval = cookie->next;
75 cookie->prev->next = NULL;
78 free_cookie_item(cookie);
82 cookie_list * garbage_collection(cookie_list *cookie)
85 cookie_list *retval = NULL;
91 /* Skip default cookie */
95 /* Try to find the PID directory from proc fs */
96 snprintf(path, sizeof(path), "/proc/%d", cookie->pid);
98 ret = stat(path, &statbuf);
101 /* If it's not exist, delete the cookie */
104 SEC_SVR_DBG("Garbage found. PID:%d, deleting...", cookie->pid);
105 cookie = delete_cookie_item(cookie);
110 /* Some error occurred */
111 SEC_SVR_DBG("Error occurred on stat: errno = %d", errno);
117 /* This is not a garbage. returning */
124 /* Search existing cookie from the cookie list for the client process *
125 * At the same time, it collects garbage cookie which PID is no longer exist and delete them */
126 cookie_list *search_existing_cookie(int pid, const cookie_list *c_list)
128 cookie_list *current =(cookie_list *)c_list, *cookie = NULL;
129 char *exe = NULL, *debug_cmdline = NULL;
131 /* Search from the list */
132 while(current != NULL)
134 /* print_cookie(current);*/
135 current = garbage_collection(current);
139 /* PID must be same */
140 if(current->pid == pid)
142 /* Found cookie for the pid. Check the cookie is reused by dirrent executable */
143 /* Check the path of the process */
144 exe = read_exe_path_from_proc(pid);
147 SEC_SVR_DBG("%s", "cannot read cmdline");
150 /* Check the path is different. */
151 if(strcmp(exe, current->path) != 0)
153 SEC_SVR_DBG("pid [%d] has been reused by %s. deleting the old cookie.", pid, exe);
154 debug_cmdline = malloc(current->path_len + 1);
155 if(debug_cmdline == NULL)
157 SEC_SVR_DBG("%s", "out of memory error");
161 strncpy(debug_cmdline, current->path, current->path_len);
162 debug_cmdline[current->path_len] = 0;
163 SEC_SVR_DBG("[%s] --> [%s]", exe, debug_cmdline);
164 if(debug_cmdline != NULL)
167 debug_cmdline = NULL;
169 /* Okay. delete current cookie */
170 current = delete_cookie_item(current);
180 SEC_SVR_DBG("%s", "cookie found");
190 current = current->next;
195 /* Search existing cookie from the cookie list for matching pid *
196 * Default cookie (meaning PID 0) is not allowed in here */
197 cookie_list *search_cookie_from_pid(cookie_list *c_list, int pid)
199 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
201 /* Search from the list */
202 while(current != NULL)
204 /* print_cookie(current);*/
205 /* PID must be same */
206 current = garbage_collection(current);
210 if(current->pid == pid)
212 SEC_SVR_DBG("%s", "cookie has been found");
216 current = current->next;
222 /* Search existing cookie from the cookie list for matching cookie and privilege */
223 /* If privilege is 0, just search cookie exists or not */
224 cookie_list *search_cookie(const cookie_list *c_list, const unsigned char *cookie, int * privileges, int privilegesSize)
226 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
229 /* Search from the list */
230 while(current != NULL)
232 /* print_cookie(current);*/
233 /* PID must be same */
234 current = garbage_collection(current);
238 //searching for cookie
239 if(memcmp(current->cookie, cookie, SECURITY_SERVER_COOKIE_LEN) == 0)
241 SEC_SVR_DBG("%s", "Cookie has been found");
243 //check if this cookie belongs to root process (root process created it)
244 if(current->is_roots_process == 1)
246 SEC_SVR_DBG("%s", "Root process cookie, special privileges");
247 //we can skip privilege checking
252 if((privileges == NULL) || (privilegesSize == 0))
254 SEC_SVR_DBG("%s", "No privileges to search in cookie!");
256 else if(current->permissions == NULL)
258 SEC_SVR_DBG("%s", "Cookie has no privileges inside!");
262 SEC_SVR_DBG("%s", "Searching for privileges");
263 SEC_SVR_DBG("%s %d", "Privileges in cookie:", current->permission_len);
264 SEC_SVR_DBG("%s %d", "Privileges to search:", privilegesSize);
266 for(j = 0; j < privilegesSize; j++)
268 for(i = 0; i < current->permission_len; i++)
270 if(privileges[j] == current->permissions[i])
272 SEC_SVR_DBG("Found privilege %d", privileges[j]);
280 current = current->next;
287 cookie_list *search_cookie_new(const cookie_list *c_list,
288 const unsigned char *cookie,
290 const char *access_rights)
292 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
295 /* Search from the list */
296 while(current != NULL)
298 /* print_cookie(current);*/
299 /* PID must be same */
300 current = garbage_collection(current);
304 if(memcmp(current->cookie, cookie, SECURITY_SERVER_COOKIE_LEN) == 0)
306 SEC_SVR_DBG("%s", "cookie has been found");
309 ret = smack_have_access(current->smack_label, object, access_rights);
310 SEC_SVR_DBG("SMACK have access returned %d", ret);
311 SEC_SVR_DBG("SS_SMACK: subject=%s, object=%s, access=%s, result=%d", current->smack_label, object, access_rights, ret);
323 current = current->next;
330 /* Generage a random stream value of size to cookie *
331 * by reading /dev/uranddom file */
332 int generate_random_cookie(unsigned char *cookie, int size)
336 if (cookie == NULL) {
337 SEC_SVR_DBG("%s", "Null pointer passed to function");
338 return SECURITY_SERVER_ERROR_UNKNOWN;
340 fd = open("/dev/urandom", O_RDONLY);
343 SEC_SVR_DBG("%s", "Cannot open /dev/urandom");
344 return SECURITY_SERVER_ERROR_FILE_OPERATION;
346 ret = TEMP_FAILURE_RETRY(read(fd, cookie, size));
349 SEC_SVR_DBG("Cannot read /dev/urandom: %d", ret);
350 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
353 ret = SECURITY_SERVER_SUCCESS;
360 /* Create a cookie item from PID */
361 cookie_list *create_cookie_item(int pid, int sockfd, cookie_list *c_list)
364 cookie_list *added = NULL, *current = NULL;
365 char path[24], *exe = NULL;
366 char *buf = NULL, inputed, *tempptr = NULL;
367 char delim[] = ": ", *token = NULL;
368 int *permissions = NULL, perm_num = 1, cnt, i, *tempperm = NULL;
369 char *smack_label = NULL;
372 current = search_existing_cookie(pid, c_list);
375 /* There is a cookie for this process already */
377 SEC_SVR_DBG("%s", "Existing cookie found");
381 /* Read command line of the PID from proc fs */
382 exe = (char *)read_exe_path_from_proc(pid);
385 SEC_SVR_DBG("Error on reading /proc/%d/exe", pid);
390 * modified by security part
391 * - get gid from /etc/group
393 /* Read group info of the PID from proc fs - /proc/[PID]/status */
394 snprintf(path, sizeof(path), "/proc/%d/status", pid);
395 fp = fopen(path, "r");
397 /* Find the line which starts with 'Groups:' */
402 buf = (char*)malloc(sizeof(char) * 128);
405 SEC_SVR_DBG("%s", "Error on malloc()");
408 memset(buf, 0x00, 128);
411 /* get one line from /proc/[PID]/status */
415 inputed = (char)tempint;
418 else if(inputed == '\n')
423 else if((i == cnt) && (inputed != '\n'))
425 tempptr = (char*)realloc(buf, sizeof(char) * (i + 128));
428 SEC_SVR_DBG("%s", "Error on realloc()");
441 if(strncmp(buf, "Groups:", 7) == 0)
443 /* get gid from the line and insert to 'permissions' array */
444 token = strtok(buf, delim); // first string is "Groups"
445 while((token = strtok(NULL, delim)))
447 tempperm = realloc(permissions, sizeof(int) * perm_num);
450 SEC_SVR_DBG("%s", "Error on realloc()");
453 permissions = tempperm;
455 permissions[perm_num - 1] = strtoul(token, 0, 10);
458 SEC_SVR_DBG("cannot change string to integer [%s]", token);
459 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
466 /* goto out of while loop */
477 /* Each group ID is stored in each line of the file */
478 // while(fgets(permline, sizeof(permline), fp) != NULL)
480 // permissions = realloc(permissions, sizeof(int) * perm_num);
481 // if(permissions == NULL)
483 // SEC_SVR_DBG("%s", "Error on realloc()");
486 // permissions[perm_num -1] = strtoul(permline, 0, 10);
494 /* Go to last cookie from the list */
496 while(current->next != NULL)
498 current = current->next;
501 /* Create a new one and assign values */
502 added = malloc(sizeof(cookie_list));
506 ret = generate_random_cookie(added->cookie, SECURITY_SERVER_COOKIE_LEN);
507 if(ret != SECURITY_SERVER_SUCCESS)
509 SEC_SVR_DBG("Error on making random cookie: %d", ret);
515 /* Check SMACK label */
518 ret = smack_new_label_from_socket(sockfd, &smack_label);
521 SEC_SVR_DBG("Error checking peer label: %d", ret);
528 added->path_len = strlen(exe);
529 added->path = calloc(1, strlen(exe));
530 memcpy(added->path, exe, strlen(exe));
532 added->permission_len = perm_num;
534 added->permissions = permissions;
535 added->smack_label = smack_label;
536 added->prev = current;
537 current->next = added;
548 if(added == NULL && permissions != NULL)
554 /* Check stored default cookie, if it's not exist make a new one and store it */
555 int check_stored_cookie(unsigned char *cookie, int size)
559 /* First, check the default cookie is stored */
560 fd = open(SECURITY_SERVER_DEFAULT_COOKIE_PATH, O_RDONLY);
565 SEC_SVR_DBG("Cannot open default cookie. errno=%d", errno);
566 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
567 unlink(SECURITY_SERVER_DEFAULT_COOKIE_PATH);
570 ret = generate_random_cookie(cookie, size);
572 /* Save cookie to disk */
573 fd = open(SECURITY_SERVER_DEFAULT_COOKIE_PATH, O_WRONLY | O_CREAT, 0600);
576 SEC_SVR_DBG("Cannot open default cookie errno=%d", errno);
577 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
580 ret = TEMP_FAILURE_RETRY(write(fd, cookie, size));
583 SEC_SVR_DBG("%s", "Cannot save default cookie");
584 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
589 return SECURITY_SERVER_SUCCESS;
592 ret = TEMP_FAILURE_RETRY(read(fd, cookie, size));
595 SEC_SVR_DBG("Cannot read default cookie errno=%d", errno);
596 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
599 ret = SECURITY_SERVER_SUCCESS;
606 /* Create a cookie item from PID */
608 /* Create a default cookie when security server is executed *
609 * Default cookie is for root processes that needs cookie */
610 cookie_list *create_default_cookie(void)
612 cookie_list *first = NULL;
615 first = malloc(sizeof(cookie_list));
617 ret = check_stored_cookie(first->cookie, SECURITY_SERVER_COOKIE_LEN);
618 if(ret != SECURITY_SERVER_SUCCESS)
620 SEC_SVR_DBG("Error on making random cookie: %d", ret);
626 first->permission_len = 0;
629 first->permissions = NULL;
630 first->smack_label = NULL;