• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *  Test that when the process does not have the appropriate privilege
9     to send the signal to the receiving process, then sigqueue()
10     returns -1 and errno is set to [EPERM]
11 
12     The real or effective user ID of the sending process shall match
13     the real or saved set-user-ID of the receiving process.
14  */
15 
16 
17 #include <signal.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <pwd.h>
24 #include <string.h>
25 #include "posixtest.h"
26 
27 /** Set the euid of this process to a non-root uid */
set_nonroot()28 static int set_nonroot()
29 {
30 	struct passwd *pw;
31 	setpwent();
32 	/* search for the first user which is non root */
33 	while ((pw = getpwent()) != NULL)
34 		if (strcmp(pw->pw_name, "root"))
35 			break;
36 	endpwent();
37 	if (pw == NULL) {
38 		printf("There is no other user than current and root.\n");
39 		return 1;
40 	}
41 	/* setuid will change uid, euid */
42 	if (setuid(pw->pw_uid) != 0) {
43 		if (errno == EPERM) {
44 			printf
45 			    ("You don't have permission to change your UID.\n");
46 			return 1;
47 		}
48 		perror("An error occurs when calling seteuid()");
49 		return 1;
50 	}
51 
52 	printf("Testing with user '%s' (euid: %d)(uid: %d)\n",
53 	       pw->pw_name, (int)geteuid(), (int)getuid());
54 	return 0;
55 }
56 
main(void)57 int main(void)
58 {
59 	int failure = 0;
60 	union sigval value;
61 	value.sival_int = 0;	/* 0 is just an arbitrary value */
62 
63 	/* We assume process Number 1 is created by root */
64 	/* and can only be accessed by root */
65 	/* This test should be run under standard user permissions */
66 	if (getuid() == 0) {
67 		if (set_nonroot() != 0) {
68 			printf("Cannot run this test as non-root user\n");
69 			return PTS_UNTESTED;
70 		}
71 	}
72 
73 	if (-1 == sigqueue(1, 0, value)) {
74 		if (EPERM == errno) {
75 			printf("EPERM error received\n");
76 		} else {
77 			printf
78 			    ("sigqueue() failed but errno not set correctly\n");
79 			failure = 1;
80 		}
81 	} else {
82 		printf("sigqueue() did not return -1\n");
83 		failure = 1;
84 	}
85 
86 	if (failure) {
87 		return PTS_FAIL;
88 	}
89 	printf("Test PASSED\n");
90 	return PTS_PASS;
91 }
92