testcases/security_server_tc_server.c
/* Create a Unix domain socket and bind */
Create a Unix domain socket and bind
Create a Unix domain socket and bind
int create_new_socket()
{
	int localsockfd = 0, flags;
	struct sockaddr_un serveraddr;
	mode_t sock_mode;
	remove(SECURITY_SERVER_TEST_SOCK_PATH);
	if((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
	{
		localsockfd = -1;
		printf("%s\n", "Socket creation failed");
		goto error;
	}
	if((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
			fcntl(localsockfd, F_SETFL, flags | O_NONBLOCK) < 0)
	{
		close(localsockfd);
		localsockfd = -1;
		printf("%s\n", "Cannot go to nonblocking mode");
		goto error;
	}
	bzero (&serveraddr, sizeof(serveraddr));
	serveraddr.sun_family = AF_UNIX;
	strncpy(serveraddr.sun_path, SECURITY_SERVER_TEST_SOCK_PATH,
			strlen(SECURITY_SERVER_TEST_SOCK_PATH) + 1);
	if((bind(localsockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
	{
		printf("%s\n", "Cannot bind");
		close(localsockfd);
		localsockfd = -1;
		goto error;
	}
	sock_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
	if(chmod(SECURITY_SERVER_TEST_SOCK_PATH, sock_mode) < 0)		
	{
		printf("%s\n", "chmod() error");
		close(localsockfd);
		localsockfd = -1;
		goto error;
	}
error:
	return localsockfd;
}
int create_new_socket ( ) { int localsockfd = 0 , flags ; struct sockaddr_un serveraddr ; mode_t sock_mode ; remove ( SECURITY_SERVER_TEST_SOCK_PATH ) ; if ( ( localsockfd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ) < 0 ) { localsockfd = -1 ; printf ( " \n " , " " ) ; goto error ; } if ( ( flags = fcntl ( localsockfd , F_GETFL , 0 ) ) < 0 || fcntl ( localsockfd , F_SETFL , flags | O_NONBLOCK ) < 0 ) { close ( localsockfd ) ; localsockfd = -1 ; printf ( " \n " , " " ) ; goto error ; } bzero ( & serveraddr , sizeof ( serveraddr ) ) ; serveraddr . sun_family = AF_UNIX ; strncpy ( serveraddr . sun_path , SECURITY_SERVER_TEST_SOCK_PATH , strlen ( SECURITY_SERVER_TEST_SOCK_PATH ) + 1 ) ; if ( ( bind ( localsockfd , ( struct sockaddr * ) & serveraddr , sizeof ( serveraddr ) ) ) < 0 ) { printf ( " \n " , " " ) ; close ( localsockfd ) ; localsockfd = -1 ; goto error ; } sock_mode = ( S_IRWXU | S_IRWXG | S_IRWXO ) ; if ( chmod ( SECURITY_SERVER_TEST_SOCK_PATH , sock_mode ) < 0 ) { printf ( " \n " , " " ) ; close ( localsockfd ) ; localsockfd = -1 ; goto error ; } error : return localsockfd ; }
Create a Unix domain socket and bind
Create a Unix domain socket and bind
/* Create Unix domain socket */ /* Make socket as non blocking */ /* Bind the socket */ /* Change permission to accept all processes that has different uID/gID */ /* Flawfinder hits this chmod function as level 5 CRITICAL as race condition flaw *
	 * Flawfinder recommends to user fchmod insted of chmod
	 * But, fchmod doesn't work on socket file so there is no other choice at this point */ /* Flawfinder: ignore */