src/server/security-server-cookie.c
/* Create a cookie item from PID */
Create a cookie item from PID
Create a cookie item from PID
cookie_list *create_cookie_item(int pid, int sockfd, cookie_list *c_list)
{
	int ret, tempint;
	cookie_list *added = NULL, *current = NULL;
	char path[24], *cmdline = NULL;
	char *buf = NULL, inputed, *tempptr = NULL;
	char delim[] = ": ", *token = NULL;
	int *permissions = NULL, perm_num = 1, cnt, i, *tempperm = NULL;
        char *smack_label = NULL;
	FILE *fp = NULL;
	current = search_existing_cookie(pid, c_list);
	if(current != NULL)
	{
		added = current;
		SEC_SVR_DBG("%s", "Existing cookie found");
		goto error;
	}
	cmdline = (char *)read_cmdline_from_proc(pid);
	if(cmdline == NULL)
	{
		SEC_SVR_DBG("Error on reading /proc/%d/cmdline", pid);
		goto error;
	}
	snprintf(path, sizeof(path), "/proc/%d/status", pid);
	fp = fopen(path, "r");
	i = 0;
	while(1)
	{
		buf = (char*)malloc(sizeof(char) * 128);
		if(buf == NULL)
		{
			SEC_SVR_DBG("%s", "Error on malloc()");
			goto error;
		}
		memset(buf, 0x00, 128);
		cnt = 128;
		while(1)
		{
			tempint = fgetc(fp);
			inputed = (char)tempint;
			if(tempint == EOF)
				goto out_of_while;
			else if(inputed == '\n')
			{
				buf[i] = '\0';
				break;
			}
			else if((i == cnt) && (inputed != '\n'))
			{
				tempptr = (char*)realloc(buf, sizeof(char) * (i + 128));
				if(tempptr == NULL)
				{
					SEC_SVR_DBG("%s", "Error on realloc()");
					goto error;
				}
				buf = tempptr;
				buf[i++] = inputed;
				cnt = i + 128;
			}
			else
				buf[i++] = inputed;
		}
		i = 0;
		if(strncmp(buf, "Groups:", 7) == 0)
		{
			token = strtok(buf, delim); 
			while((token = strtok(NULL, delim)))
			{
				tempperm = realloc(permissions, sizeof(int) * perm_num);
				if(tempperm == NULL)
				{
					SEC_SVR_DBG("%s", "Error on realloc()");
					goto error;
				}
				permissions = tempperm;
				errno = 0;
				permissions[perm_num - 1] = strtoul(token, 0, 10);
				if (errno != 0)
				{
					SEC_SVR_DBG("cannot change string to integer [%s]", token);
					ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
					goto error;
				}
				perm_num++;
			}
			perm_num--;
			break;
		}
		if(buf != NULL)
		{
			free(buf);
			buf = NULL;
		}
	}
out_of_while:
	current = c_list;
	while(current->next != NULL)
	{
		current = current->next;
	}
	added = malloc(sizeof(cookie_list));
	if(added == NULL)
		goto error;
	ret = generate_random_cookie(added->cookie, SECURITY_SERVER_COOKIE_LEN);
	if(ret != SECURITY_SERVER_SUCCESS)
	{
		SEC_SVR_DBG("Error on making random cookie: %d", ret);
		free(added);
		added = NULL;
		goto error;
	}
        ret = smack_new_label_from_socket(sockfd, &smack_label);
        if (ret != 0)
	{
		SEC_SVR_DBG("Error checking peer label: %d", ret);
		free(added);
		added = NULL;
		goto error;
	}
	added->path_len = strlen(cmdline);
	added->path = calloc(1, strlen(cmdline));
	memcpy(added->path, cmdline, strlen(cmdline));
	added->permission_len = perm_num;
	added->pid = pid;
	added->permissions = permissions;
	added->smack_label = smack_label;
	added->prev = current;
	current->next = added;
	added->next = NULL;
error:
	if(cmdline != NULL)
		free(cmdline);
	if(fp != NULL)
		fclose(fp);
	if(buf != NULL)
		free(buf);
	if(added == NULL && permissions != NULL)
		free(permissions);
	return added;
}
cookie_list * create_cookie_item ( int pid , int sockfd , cookie_list * c_list ) { int ret , tempint ; cookie_list * added = NULL , * current = NULL ; char path [ 24 ] , * cmdline = NULL ; char * buf = NULL , inputed , * tempptr = NULL ; char delim [ ] = " " , * token = NULL ; int * permissions = NULL , perm_num = 1 , cnt , i , * tempperm = NULL ; char * smack_label = NULL ; FILE * fp = NULL ; current = search_existing_cookie ( pid , c_list ) ; if ( current != NULL ) { added = current ; SEC_SVR_DBG ( " " , " " ) ; goto error ; } cmdline = ( char * ) read_cmdline_from_proc ( pid ) ; if ( cmdline == NULL ) { SEC_SVR_DBG ( " " , pid ) ; goto error ; } snprintf ( path , sizeof ( path ) , " " , pid ) ; fp = fopen ( path , " " ) ; i = 0 ; while ( 1 ) { buf = ( char * ) malloc ( sizeof ( char ) * 128 ) ; if ( buf == NULL ) { SEC_SVR_DBG ( " " , " " ) ; goto error ; } memset ( buf , 0x00 , 128 ) ; cnt = 128 ; while ( 1 ) { tempint = fgetc ( fp ) ; inputed = ( char ) tempint ; if ( tempint == EOF ) goto out_of_while ; else if ( inputed == ' \n ' ) { buf [ i ] = ' \0 ' ; break ; } else if ( ( i == cnt ) && ( inputed != ' \n ' ) ) { tempptr = ( char * ) realloc ( buf , sizeof ( char ) * ( i + 128 ) ) ; if ( tempptr == NULL ) { SEC_SVR_DBG ( " " , " " ) ; goto error ; } buf = tempptr ; buf [ i ++ ] = inputed ; cnt = i + 128 ; } else buf [ i ++ ] = inputed ; } i = 0 ; if ( strncmp ( buf , " " , 7 ) == 0 ) { token = strtok ( buf , delim ) ; while ( ( token = strtok ( NULL , delim ) ) ) { tempperm = realloc ( permissions , sizeof ( int ) * perm_num ) ; if ( tempperm == NULL ) { SEC_SVR_DBG ( " " , " " ) ; goto error ; } permissions = tempperm ; errno = 0 ; permissions [ perm_num - 1 ] = strtoul ( token , 0 , 10 ) ; if ( errno != 0 ) { SEC_SVR_DBG ( " " , token ) ; ret = SECURITY_SERVER_ERROR_SERVER_ERROR ; goto error ; } perm_num ++ ; } perm_num -- ; break ; } if ( buf != NULL ) { free ( buf ) ; buf = NULL ; } } out_of_while : current = c_list ; while ( current -> next != NULL ) { current = current -> next ; } added = malloc ( sizeof ( cookie_list ) ) ; if ( added == NULL ) goto error ; ret = generate_random_cookie ( added -> cookie , SECURITY_SERVER_COOKIE_LEN ) ; if ( ret != SECURITY_SERVER_SUCCESS ) { SEC_SVR_DBG ( " " , ret ) ; free ( added ) ; added = NULL ; goto error ; } ret = smack_new_label_from_socket ( sockfd , & smack_label ) ; if ( ret != 0 ) { SEC_SVR_DBG ( " " , ret ) ; free ( added ) ; added = NULL ; goto error ; } added -> path_len = strlen ( cmdline ) ; added -> path = calloc ( 1 , strlen ( cmdline ) ) ; memcpy ( added -> path , cmdline , strlen ( cmdline ) ) ; added -> permission_len = perm_num ; added -> pid = pid ; added -> permissions = permissions ; added -> smack_label = smack_label ; added -> prev = current ; current -> next = added ; added -> next = NULL ; error : if ( cmdline != NULL ) free ( cmdline ) ; if ( fp != NULL ) fclose ( fp ) ; if ( buf != NULL ) free ( buf ) ; if ( added == NULL && permissions != NULL ) free ( permissions ) ; return added ; }
Create a cookie item from PID
Create a cookie item from PID
/* There is a cookie for this process already */ /* Read command line of the PID from proc fs */ /*
	 * modified by security part
	 *  - get gid from /etc/group
	 */ /* Read group info of the PID from proc fs - /proc/[PID]/status */ /* Find the line which starts with 'Groups:' */ /* get one line from /proc/[PID]/status */ /* find 'Groups:' */ /* get gid from the line and insert to 'permissions' array */ // first string is "Groups" /* goto out of while loop */ /* Each group ID is stored in each line of the file */ //	while(fgets(permline, sizeof(permline), fp) != NULL) //	{ //		permissions = realloc(permissions, sizeof(int) * perm_num); //		if(permissions == NULL) //		{ //			SEC_SVR_DBG("%s", "Error on realloc()"); //			goto error; //		} //		permissions[perm_num -1] = strtoul(permline, 0, 10); //		perm_num++; //	} //	perm_num--; /*
	 * modifying end
	 */ /* Go to last cookie from the list */ /* Create a new one and assign values */ /* Check SMACK label */