Home | History | Annotate | Download | only in sigqueue
      1 /*
      2  * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
      3  * Created by:  salwan.searty REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  This program tests the assertion that if the user id of the sending process
      9  doesn't match the user id of the receiving process (pid), then the sigqueue
     10  function will fail with errno set to EPERM, unless the sending process
     11  has appropriate privileges.
     12 
     13  Since process pid 1 (init) is not killable by even root, it is used as a the receiving
     14  process. Even if process id 1 is killable, this test is still safe because the
     15  value of the sig parameter is 0.
     16 
     17  */
     18 
     19 #define _XOPEN_SOURCE 600
     20 
     21 #include <signal.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <errno.h>
     25 #include <unistd.h>
     26 #include <sys/types.h>
     27 #include <pwd.h>
     28 #include <string.h>
     29 #include "posixtest.h"
     30 
     31 /** Set the euid of this process to a non-root uid */
     32 int set_nonroot()
     33 {
     34 	struct passwd *pw;
     35 	setpwent();
     36 	/* search for the first user which is non root */
     37 	while ((pw = getpwent()) != NULL)
     38 		if (strcmp(pw->pw_name, "root"))
     39 			break;
     40 	endpwent();
     41 	if (pw == NULL) {
     42 		printf("There is no other user than current and root.\n");
     43 		return 1;
     44 	}
     45 
     46 	if (setuid(pw->pw_uid) != 0) {
     47 		if (errno == EPERM) {
     48 			printf
     49 			    ("You don't have permission to change your UID.\n");
     50 			return 1;
     51 		}
     52 		perror("An error occurs when calling seteuid()");
     53 		return 1;
     54 	}
     55 
     56 	printf("Testing with user '%s' (uid: %d)(euid: %d)\n",
     57 	       pw->pw_name, (int)getuid(), (int)geteuid());
     58 	return 0;
     59 }
     60 
     61 int main(void)
     62 {
     63 
     64 	union sigval value;
     65 	value.sival_int = 0;	/* 0 is just an arbitrary value */
     66 
     67 	/* We assume process Number 1 is created by root */
     68 	/* and can only be accessed by root */
     69 	/* This test should be run under standard user permissions */
     70 	if (getuid() == 0) {
     71 		if (set_nonroot() != 0) {
     72 			printf("Cannot run this test as non-root user\n");
     73 			return PTS_UNTESTED;
     74 		}
     75 	}
     76 
     77 	if (sigqueue(1, 0, value) != -1) {
     78 		printf
     79 		    ("Test FAILED: sigqueue() succeeded even though this program's user id did not match the recieving process's user id\n");
     80 		return PTS_FAIL;
     81 	}
     82 
     83 	if (EPERM != errno) {
     84 		printf("Test FAILED: EPERM error not received\n");
     85 		return PTS_FAIL;
     86 	}
     87 
     88 	return PTS_PASS;
     89 }
     90