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 *cmdline = 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 cmdline = (char*)read_cmdline_from_proc(pid);
147 SEC_SVR_DBG("%s", "cannot read cmdline");
150 /* Check the path is different */
151 if(strncmp(cmdline, current->path, current->path_len) != 0
152 || (int)strlen(cmdline) != current->path_len)
154 SEC_SVR_DBG("pid [%d] has been reused by %s. deleting the old cookie.", pid, cmdline);
155 debug_cmdline = malloc(current->path_len + 1);
156 if(debug_cmdline == NULL)
158 SEC_SVR_DBG("%s", "out of memory error");
162 strncpy(debug_cmdline, current->path, current->path_len);
163 debug_cmdline[current->path_len] = 0;
164 SEC_SVR_DBG("[%s] --> [%s]", cmdline, debug_cmdline);
165 if(debug_cmdline != NULL)
168 debug_cmdline = NULL;
170 /* Okay. delete current cookie */
171 current = delete_cookie_item(current);
181 SEC_SVR_DBG("%s", "cookie found");
191 current = current->next;
196 /* Search existing cookie from the cookie list for matching pid *
197 * Default cookie (meaning PID 0) is not allowed in here */
198 cookie_list *search_cookie_from_pid(cookie_list *c_list, int pid)
200 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
202 /* Search from the list */
203 while(current != NULL)
205 /* print_cookie(current);*/
206 /* PID must be same */
207 current = garbage_collection(current);
211 if(current->pid == pid)
213 SEC_SVR_DBG("%s", "cookie has been found");
217 current = current->next;
223 /* Search existing cookie from the cookie list for matching cookie and privilege */
224 /* If privilege is 0, just search cookie exists or not */
225 cookie_list *search_cookie(const cookie_list *c_list, const unsigned char *cookie, int * privileges, int privilegesSize)
227 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
230 /* Search from the list */
231 while(current != NULL)
233 /* print_cookie(current);*/
234 /* PID must be same */
235 current = garbage_collection(current);
239 //searching for cookie
240 if(memcmp(current->cookie, cookie, SECURITY_SERVER_COOKIE_LEN) == 0)
242 SEC_SVR_DBG("%s", "Cookie has been found");
244 //check if this cookie belongs to root process (root process created it)
245 if(current->is_roots_process == 1)
247 SEC_SVR_DBG("%s", "Root process cookie, special privileges");
248 //we can skip privilege checking
253 if((privileges == NULL) || (privilegesSize == 0))
255 SEC_SVR_DBG("%s", "No privileges to search in cookie!");
257 else if(current->permissions == NULL)
259 SEC_SVR_DBG("%s", "Cookie has no privileges inside!");
263 SEC_SVR_DBG("%s", "Searching for privileges");
264 SEC_SVR_DBG("%s %d", "Privileges in cookie:", current->permission_len);
265 SEC_SVR_DBG("%s %d", "Privileges to search:", privilegesSize);
267 for(j = 0; j < privilegesSize; j++)
269 for(i = 0; i < current->permission_len; i++)
271 if(privileges[j] == current->permissions[i])
273 SEC_SVR_DBG("Found privilege %d", privileges[j]);
281 current = current->next;
288 cookie_list *search_cookie_new(const cookie_list *c_list,
289 const unsigned char *cookie,
291 const char *access_rights)
293 cookie_list *current = (cookie_list *)c_list, *retval = NULL;
297 /* Search from the list */
298 while(current != NULL)
300 /* print_cookie(current);*/
301 /* PID must be same */
302 current = garbage_collection(current);
306 if(memcmp(current->cookie, cookie, SECURITY_SERVER_COOKIE_LEN) == 0)
308 SEC_SVR_DBG("%s", "cookie has been found");
310 ret = smack_have_access(current->smack_label, object, access_rights);
311 SEC_SVR_DBG("smack_have_access, subject >%s< object >%s< access >%s< ===> %d",
312 current->smack_label, object, access_rights, ret);
319 current = current->next;
326 /* Generage a random stream value of size to cookie *
327 * by reading /dev/uranddom file */
328 int generate_random_cookie(unsigned char *cookie, int size)
332 if (cookie == NULL) {
333 SEC_SVR_DBG("%s", "Null pointer passed to function");
334 return SECURITY_SERVER_ERROR_UNKNOWN;
336 fd = open("/dev/urandom", O_RDONLY);
339 SEC_SVR_DBG("%s", "Cannot open /dev/urandom");
340 return SECURITY_SERVER_ERROR_FILE_OPERATION;
342 ret = read(fd, cookie, size);
345 SEC_SVR_DBG("Cannot read /dev/urandom: %d", ret);
346 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
349 ret = SECURITY_SERVER_SUCCESS;
356 /* Create a cookie item from PID */
357 cookie_list *create_cookie_item(int pid, int sockfd, cookie_list *c_list)
360 cookie_list *added = NULL, *current = NULL;
361 char path[24], *cmdline = NULL;
362 char *buf = NULL, inputed, *tempptr = NULL;
363 char delim[] = ": ", *token = NULL;
364 int *permissions = NULL, perm_num = 1, cnt, i, *tempperm = NULL;
365 char *smack_label = NULL;
368 current = search_existing_cookie(pid, c_list);
371 /* There is a cookie for this process already */
373 SEC_SVR_DBG("%s", "Existing cookie found");
377 /* Read command line of the PID from proc fs */
378 cmdline = (char *)read_cmdline_from_proc(pid);
381 SEC_SVR_DBG("Error on reading /proc/%d/cmdline", pid);
386 * modified by security part
387 * - get gid from /etc/group
389 /* Read group info of the PID from proc fs - /proc/[PID]/status */
390 snprintf(path, sizeof(path), "/proc/%d/status", pid);
391 fp = fopen(path, "r");
393 /* Find the line which starts with 'Groups:' */
398 buf = (char*)malloc(sizeof(char) * 128);
401 SEC_SVR_DBG("%s", "Error on malloc()");
404 memset(buf, 0x00, 128);
407 /* get one line from /proc/[PID]/status */
411 inputed = (char)tempint;
414 else if(inputed == '\n')
419 else if((i == cnt) && (inputed != '\n'))
421 tempptr = (char*)realloc(buf, sizeof(char) * (i + 128));
424 SEC_SVR_DBG("%s", "Error on realloc()");
437 if(strncmp(buf, "Groups:", 7) == 0)
439 /* get gid from the line and insert to 'permissions' array */
440 token = strtok(buf, delim); // first string is "Groups"
441 while((token = strtok(NULL, delim)))
443 tempperm = realloc(permissions, sizeof(int) * perm_num);
446 SEC_SVR_DBG("%s", "Error on realloc()");
449 permissions = tempperm;
451 permissions[perm_num - 1] = strtoul(token, 0, 10);
454 SEC_SVR_DBG("cannot change string to integer [%s]", token);
455 ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
462 /* goto out of while loop */
473 /* Each group ID is stored in each line of the file */
474 // while(fgets(permline, sizeof(permline), fp) != NULL)
476 // permissions = realloc(permissions, sizeof(int) * perm_num);
477 // if(permissions == NULL)
479 // SEC_SVR_DBG("%s", "Error on realloc()");
482 // permissions[perm_num -1] = strtoul(permline, 0, 10);
490 /* Go to last cookie from the list */
492 while(current->next != NULL)
494 current = current->next;
497 /* Create a new one and assign values */
498 added = malloc(sizeof(cookie_list));
502 ret = generate_random_cookie(added->cookie, SECURITY_SERVER_COOKIE_LEN);
503 if(ret != SECURITY_SERVER_SUCCESS)
505 SEC_SVR_DBG("Error on making random cookie: %d", ret);
511 /* Check SMACK label */
512 ret = smack_new_label_from_socket(sockfd, &smack_label);
515 SEC_SVR_DBG("Error checking peer label: %d", ret);
521 added->path_len = strlen(cmdline);
522 added->path = calloc(1, strlen(cmdline));
523 memcpy(added->path, cmdline, strlen(cmdline));
525 added->permission_len = perm_num;
527 added->permissions = permissions;
528 added->smack_label = smack_label;
529 added->prev = current;
530 current->next = added;
541 if(added == NULL && permissions != NULL)
547 /* Check stored default cookie, if it's not exist make a new one and store it */
548 int check_stored_cookie(unsigned char *cookie, int size)
552 /* First, check the default cookie is stored */
553 fd = open(SECURITY_SERVER_DEFAULT_COOKIE_PATH, O_RDONLY);
558 SEC_SVR_DBG("Cannot open default cookie. errno=%d", errno);
559 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
560 unlink(SECURITY_SERVER_DEFAULT_COOKIE_PATH);
563 ret = generate_random_cookie(cookie, size);
565 /* Save cookie to disk */
566 fd = open(SECURITY_SERVER_DEFAULT_COOKIE_PATH, O_WRONLY | O_CREAT, 0600);
569 SEC_SVR_DBG("Cannot open default cookie errno=%d", errno);
570 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
573 ret = write(fd, cookie, size);
576 SEC_SVR_DBG("%s", "Cannot save default cookie");
577 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
582 return SECURITY_SERVER_SUCCESS;
585 ret = read (fd, cookie, size);
588 SEC_SVR_DBG("Cannot read default cookie errno=%d", errno);
589 ret = SECURITY_SERVER_ERROR_FILE_OPERATION;
592 ret = SECURITY_SERVER_SUCCESS;
599 /* Create a cookie item from PID */
601 /* Create a default cookie when security server is executed *
602 * Default cookie is for root processes that needs cookie */
603 cookie_list *create_default_cookie(void)
605 cookie_list *first = NULL;
608 first = malloc(sizeof(cookie_list));
610 ret = check_stored_cookie(first->cookie, SECURITY_SERVER_COOKIE_LEN);
611 if(ret != SECURITY_SERVER_SUCCESS)
613 SEC_SVR_DBG("Error on making random cookie: %d", ret);
619 first->permission_len = 0;
622 first->permissions = NULL;
623 first->smack_label = NULL;