xref: /aosp_15_r20/external/ltp/testcases/open_posix_testsuite/conformance/interfaces/shm_unlink/8-1.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License version 2.
4  *
5  *  This program is distributed in the hope that it will be useful,
6  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
7  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8  *  GNU General Public License for more details.
9  *
10  * Test that the named shared memory object is not changed by this function
11  * call when the process does not have permission to unlink the name.
12  *
13  * Steps:
14  *  1. Create a shared memory object.
15  *  2. Set his effective user id to an other user id which is not root.
16  *  3. Try to unlink the name.
17  *     If it fail: set the effective user id to real user id and unlink.
18  * In most case this test will be unresolved if not run by root.
19  */
20 
21 /* getpwent() is part of XSI option */
22 
23 #include <stdio.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <pwd.h>
30 #include <string.h>
31 #include "posixtest.h"
32 
33 #define SHM_NAME "posixtest_9-1"
34 #define BUF_SIZE 8
35 
main(void)36 int main(void)
37 {
38 	int fd, result;
39 	struct passwd *pw;
40 	struct stat stat_before, stat_after;
41 
42 	fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
43 	if (fd == -1) {
44 		perror("An error occurs when calling shm_open()");
45 		return PTS_UNRESOLVED;
46 	}
47 
48 	if (ftruncate(fd, BUF_SIZE) != 0) {
49 		perror("An error occurs when calling ftruncate()");
50 		shm_unlink(SHM_NAME);
51 		return PTS_UNRESOLVED;
52 	}
53 
54 	if (fstat(fd, &stat_before) != 0) {
55 		perror("An error occurs when calling fstat()");
56 		shm_unlink(SHM_NAME);
57 		return PTS_UNRESOLVED;
58 	}
59 
60 	/* search for the first user which is non root and which is not the
61 	   current user */
62 	while ((pw = getpwent()) != NULL)
63 		if (strcmp(pw->pw_name, "root") && pw->pw_uid != getuid())
64 			break;
65 
66 	if (pw == NULL) {
67 		printf("There is no other user than current and root.\n");
68 		return PTS_UNRESOLVED;
69 	}
70 
71 	if (seteuid(pw->pw_uid) != 0) {
72 		if (errno == EPERM) {
73 			printf
74 			    ("You don't have permission to change your UID.\nTry to rerun this test as root.\n");
75 			return PTS_UNRESOLVED;
76 		}
77 		perror("An error occurs when calling seteuid()");
78 		return PTS_UNRESOLVED;
79 	}
80 
81 	printf("Testing with user '%s' (uid: %i)\n", pw->pw_name, pw->pw_uid);
82 
83 	result = shm_unlink(SHM_NAME);
84 	if (result == 0) {
85 		printf("shm_unlink() success.\n");
86 		return PTS_UNRESOLVED;
87 	}
88 
89 	if (seteuid(getuid()))
90 		perror("seteuid");
91 
92 	if (fstat(fd, &stat_after) != 0) {
93 		perror("An error occurs when calling fstat()");
94 		shm_unlink(SHM_NAME);
95 		return PTS_UNRESOLVED;
96 	}
97 
98 	if (stat_after.st_uid != stat_before.st_uid ||
99 	    stat_after.st_gid != stat_before.st_gid ||
100 	    stat_after.st_size != stat_before.st_size ||
101 	    stat_after.st_mode != stat_before.st_mode) {
102 		printf("The shared memory object has changed.\n");
103 		return PTS_FAIL;
104 	}
105 
106 	printf("Test PASSED\n");
107 	return PTS_PASS;
108 
109 }
110