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"
33 /* Delete useless cookie item *
34 * then connect prev and next */
35 int free_cookie_item(cookie_list *cookie)
37 if(cookie->path != NULL)
39 if(cookie->permissions != NULL)
40 free(cookie->permissions);
41 if(cookie->smack_label != NULL)
42 free(cookie->smack_label);
43 if(cookie->prev != NULL)
44 cookie->prev->next = cookie->next;
45 if(cookie->next != NULL)
46 cookie->next->prev = cookie->prev;
52 /* Cut the link of the current cookie item and connect previous link and next line *
53 * That is remove a cookie item *
54 * Returns next cookie item if exist, NULL for no more cookie item */
55 cookie_list *delete_cookie_item(cookie_list *cookie)
57 cookie_list *retval = NULL;
60 SEC_SVR_DBG("%s", "Cannot delete null cookie");
64 /* Reconnect cookie item */
65 if(cookie->next != NULL)
67 cookie->prev->next = cookie->next;
68 cookie->next->prev = cookie->prev;
69 retval = cookie->next;
73 cookie->prev->next = NULL;
76 free_cookie_item(cookie);
80 cookie_list * garbage_collection(cookie_list *cookie)
83 cookie_list *retval = NULL;
89 /* Skip default cookie */
93 /* Try to find the PID directory from proc fs */
94 snprintf(path, sizeof(path), "/proc/%d", cookie->pid);
96 ret = stat(path, &statbuf);
99 /* If it's not exist, delete the cookie */
102 SEC_SVR_DBG("Garbage found. PID:%d, deleting...", cookie->pid);
103 cookie = delete_cookie_item(cookie);
108 /* Some error occurred */
109 SEC_SVR_DBG("Error occurred on stat: errno = %d", errno);
115 /* This is not a garbage. returning */
122 /* Search existing cookie from the cookie list for the client process *
123 * At the same time, it collects garbage cookie which PID is no longer exist and delete them */
124 cookie_list *search_existing_cookie(int pid, const cookie_list *c_list)
126 cookie_list *current =(cookie_list *)c_list, *cookie = NULL;
127 char *cmdline = NULL, *debug_cmdline = NULL;
129 /* Search from the list */
130 while(current != NULL)
132 /* print_cookie(current);*/
133 current = garbage_collection(current);
137 /* PID must be same */
138 if(current->pid == pid)
140 /* Found cookie for the pid. Check the cookie is reused by dirrent executable */
141 /* Check the path of the process */
142 cmdline = (char*)read_cmdline_from_proc(pid);
145 SEC_SVR_DBG("%s", "cannot read cmdline");
148 /* Check the path is different */
149 if(strncmp(cmdline, current->path, current->path_len) != 0 || strlen(cmdline) != current->path_len)
151 SEC_SVR_DBG("pid [%d] has been reused by %s. deleting the old cookie.", pid, cmdline);
152 debug_cmdline = malloc(current->path_len + 1);
153 if(debug_cmdline == NULL)
155 SEC_SVR_DBG("%s", "out of memory error");
159 strncpy(debug_cmdline, current->path, current->path_len);
160 debug_cmdline[current->path_len] = 0;
161 SEC_SVR_DBG("[%s] --> [%s]", cmdline, debug_cmdline);
162 if(debug_cmdline != NULL)
165 debug_cmdline = NULL;
167 /* Okay. delete current cookie */
168 current = delete_cookie_item(current);
178 SEC_SVR_DBG("%s", "cookie found");
188 current = current->next;
193 /* Search existing cookie from the cookie list for matching pid *
194 * Default cookie (meaning PID 0) is not allowed in here */
195 cookie_list *search_cookie_from_pid(cookie_list *c_list, int pid)
197 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
199 /* Search from the list */
200 while(current != NULL)
202 /* print_cookie(current);*/
203 /* PID must be same */
204 current = garbage_collection(current);
208 if(current->pid == pid)
210 SEC_SVR_DBG("%s", "cookie has been found");
214 current = current->next;
220 /* Search existing cookie from the cookie list for matching cookie and privilege */
221 /* If privilege is 0, just search cookie exists or not */
222 cookie_list *search_cookie(const cookie_list *c_list, const unsigned char *cookie, int * privileges, int privilegesSize)
224 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
227 /* Search from the list */
228 while(current != NULL)
230 /* print_cookie(current);*/
231 /* PID must be same */
232 current = garbage_collection(current);
236 //searching for cookie
237 if(memcmp(current->cookie, cookie, SECURITY_SERVER_COOKIE_LEN) == 0)
239 SEC_SVR_DBG("%s", "Cookie has been found");
241 //check if this cookie belongs to root process (root process created it)
242 if(current->is_roots_process == 1)
244 SEC_SVR_DBG("%s", "Root process cookie, special privileges");
245 //we can skip privilege checking
250 if((privileges == NULL) || (privilegesSize == 0))
252 SEC_SVR_DBG("%s", "No privileges to search in cookie!");
254 else if(current->permissions == NULL)
256 SEC_SVR_DBG("%s", "Cookie has no privileges inside!");
260 SEC_SVR_DBG("%s", "Searching for privileges");
261 SEC_SVR_DBG("%s %d", "Privileges in cookie:", current->permission_len);
262 SEC_SVR_DBG("%s %d", "Privileges to search:", privilegesSize);
264 for(j = 0; j < privilegesSize; j++)
266 for(i = 0; i < current->permission_len; i++)
268 if(privileges[j] == current->permissions[i])
270 SEC_SVR_DBG("Found privilege %d", privileges[j]);
278 current = current->next;
285 cookie_list *search_cookie_new(const cookie_list *c_list,
286 const unsigned char *cookie,
288 const char *access_rights)
290 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
294 /* Search from the list */
295 while(current != NULL)
297 /* print_cookie(current);*/
298 /* PID must be same */
299 current = garbage_collection(current);
303 if(memcmp(current->cookie, cookie, SECURITY_SERVER_COOKIE_LEN) == 0)
305 SEC_SVR_DBG("%s", "cookie has been found");
307 ret = smack_have_access(current->smack_label, object, access_rights);
308 SEC_SVR_DBG("smack_have_access, subject >%s< object >%s< access >%s< ===> %d",
309 current->smack_label, object, access_rights, ret);
316 current = current->next;
323 /* Generage a random stream value of size to cookie *
324 * by reading /dev/uranddom file */
325 int generate_random_cookie(unsigned char *cookie, int size)
329 if (cookie == NULL) {
330 SEC_SVR_DBG("%s", "Null pointer passed to function");
331 return SECURITY_SERVER_ERROR_UNKNOWN;
333 fd = open("/dev/urandom", O_RDONLY);
336 SEC_SVR_DBG("%s", "Cannot open /dev/urandom");
337 return SECURITY_SERVER_ERROR_FILE_OPERATION;
339 ret = read(fd, cookie, size);
342 SEC_SVR_DBG("Cannot read /dev/urandom: %d", ret);
343 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
346 ret = SECURITY_SERVER_SUCCESS;
353 /* Create a cookie item from PID */
354 cookie_list *create_cookie_item(int pid, int sockfd, cookie_list *c_list)
357 cookie_list *added = NULL, *current = NULL;
358 char path[24], *cmdline = NULL;
359 char *buf = NULL, inputed, *tempptr = NULL;
360 char delim[] = ": ", *token = NULL;
361 int *permissions = NULL, perm_num = 1, cnt, i, *tempperm = NULL;
362 char *smack_label = NULL;
365 current = search_existing_cookie(pid, c_list);
368 /* There is a cookie for this process already */
370 SEC_SVR_DBG("%s", "Existing cookie found");
374 /* Read command line of the PID from proc fs */
375 cmdline = (char *)read_cmdline_from_proc(pid);
378 SEC_SVR_DBG("Error on reading /proc/%d/cmdline", pid);
383 * modified by security part
384 * - get gid from /etc/group
386 /* Read group info of the PID from proc fs - /proc/[PID]/status */
387 snprintf(path, sizeof(path), "/proc/%d/status", pid);
388 fp = fopen(path, "r");
390 /* Find the line which starts with 'Groups:' */
395 buf = (char*)malloc(sizeof(char) * 128);
398 SEC_SVR_DBG("%s", "Error on malloc()");
401 memset(buf, 0x00, 128);
404 /* get one line from /proc/[PID]/status */
408 inputed = (char)tempint;
411 else if(inputed == '\n')
416 else if((i == cnt) && (inputed != '\n'))
418 tempptr = (char*)realloc(buf, sizeof(char) * (i + 128));
421 SEC_SVR_DBG("%s", "Error on realloc()");
434 if(strncmp(buf, "Groups:", 7) == 0)
436 /* get gid from the line and insert to 'permissions' array */
437 token = strtok(buf, delim); // first string is "Groups"
438 while((token = strtok(NULL, delim)))
440 tempperm = realloc(permissions, sizeof(int) * perm_num);
443 SEC_SVR_DBG("%s", "Error on realloc()");
446 permissions = tempperm;
448 permissions[perm_num - 1] = strtoul(token, 0, 10);
451 SEC_SVR_DBG("cannot change string to integer [%s]", token);
452 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
459 /* goto out of while loop */
470 /* Each group ID is stored in each line of the file */
471 // while(fgets(permline, sizeof(permline), fp) != NULL)
473 // permissions = realloc(permissions, sizeof(int) * perm_num);
474 // if(permissions == NULL)
476 // SEC_SVR_DBG("%s", "Error on realloc()");
479 // permissions[perm_num -1] = strtoul(permline, 0, 10);
487 /* Go to last cookie from the list */
489 while(current->next != NULL)
491 current = current->next;
494 /* Create a new one and assign values */
495 added = malloc(sizeof(cookie_list));
499 ret = generate_random_cookie(added->cookie, SECURITY_SERVER_COOKIE_LEN);
500 if(ret != SECURITY_SERVER_SUCCESS)
502 SEC_SVR_DBG("Error on making random cookie: %d", ret);
508 /* Check SMACK label */
509 ret = smack_new_label_from_socket(sockfd, &smack_label);
512 SEC_SVR_DBG("Error checking peer label: %d", ret);
518 added->path_len = strlen(cmdline);
519 added->path = calloc(1, strlen(cmdline));
520 memcpy(added->path, cmdline, strlen(cmdline));
522 added->permission_len = perm_num;
524 added->permissions = permissions;
525 added->smack_label = smack_label;
526 added->prev = current;
527 current->next = added;
538 if(added == NULL && permissions != NULL)
544 /* Check stored default cookie, if it's not exist make a new one and store it */
545 int check_stored_cookie(unsigned char *cookie, int size)
549 /* First, check the default cookie is stored */
550 fd = open(SECURITY_SERVER_DEFAULT_COOKIE_PATH, O_RDONLY);
555 SEC_SVR_DBG("Cannot open default cookie. errno=%d", errno);
556 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
557 unlink(SECURITY_SERVER_DEFAULT_COOKIE_PATH);
560 ret = generate_random_cookie(cookie, size);
562 /* Save cookie to disk */
563 fd = open(SECURITY_SERVER_DEFAULT_COOKIE_PATH, O_WRONLY | O_CREAT, 0600);
566 SEC_SVR_DBG("Cannot open default cookie errno=%d", errno);
567 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
570 ret = write(fd, cookie, size);
573 SEC_SVR_DBG("%s", "Cannot save default cookie");
574 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
579 return SECURITY_SERVER_SUCCESS;
582 ret = read (fd, cookie, size);
585 SEC_SVR_DBG("Cannot read default cookie errno=%d", errno);
586 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
589 ret = SECURITY_SERVER_SUCCESS;
596 /* Create a cookie item from PID */
598 /* Create a default cookie when security server is executed *
599 * Default cookie is for root processes that needs cookie */
600 cookie_list *create_default_cookie(void)
602 cookie_list *first = NULL;
605 first = malloc(sizeof(cookie_list));
607 ret = check_stored_cookie(first->cookie, SECURITY_SERVER_COOKIE_LEN);
608 if(ret != SECURITY_SERVER_SUCCESS)
610 SEC_SVR_DBG("Error on making random cookie: %d", ret);
616 first->permission_len = 0;
619 first->permissions = NULL;
620 first->smack_label = NULL;