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 shm_unlink() sets errno = EACCES if permission is denied to unlink 11 * the named shared memory object. 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 #define _XOPEN_SOURCE 600 23 24 #include <stdio.h> 25 #include <sys/mman.h> 26 #include <sys/stat.h> 27 #include <unistd.h> 28 #include <fcntl.h> 29 #include <errno.h> 30 #include <pwd.h> 31 #include <string.h> 32 #include "posixtest.h" 33 34 #define SHM_NAME "posixtest_9-1" 35 36 int main(void) 37 { 38 int fd, result; 39 struct passwd *pw; 40 41 fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 42 if (fd == -1) { 43 perror("An error occurs when calling shm_open()"); 44 return PTS_UNRESOLVED; 45 } 46 47 /* search for the first user which is non root and which is not the 48 current user */ 49 while ((pw = getpwent()) != NULL) 50 if (strcmp(pw->pw_name, "root") && pw->pw_uid != getuid()) 51 break; 52 53 if (pw == NULL) { 54 printf("There is no other user than current and root.\n"); 55 return PTS_UNRESOLVED; 56 } 57 58 if (seteuid(pw->pw_uid) != 0) { 59 if (errno == EPERM) { 60 printf 61 ("You don't have permission to change your UID.\nTry to rerun this test as root.\n"); 62 return PTS_UNRESOLVED; 63 } 64 perror("An error occurs when calling seteuid()"); 65 return PTS_UNRESOLVED; 66 } 67 68 printf("Testing with user '%s' (uid: %i)\n", pw->pw_name, pw->pw_uid); 69 70 result = shm_unlink(SHM_NAME); 71 if (result == -1 && errno == EACCES) { 72 printf("Test PASSED\n"); 73 seteuid(getuid()); 74 shm_unlink(SHM_NAME); 75 return PTS_PASS; 76 } else if (result == -1) { 77 perror("Unexpected error"); 78 seteuid(getuid()); 79 shm_unlink(SHM_NAME); 80 return PTS_FAIL; 81 } 82 83 printf("shm_unlink() success.\n"); 84 return PTS_UNRESOLVED; 85 86 } 87