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 mlockall() function set errno = EPERM if the calling process 11 * does not have the appropriate privilege to perform the requested operation 12 * 13 * It is a may assertion. 14 */ 15 16 #define _XOPEN_SOURCE 600 17 #include <sys/mman.h> 18 #include <stdio.h> 19 #include <unistd.h> 20 #include <errno.h> 21 #include <sys/types.h> 22 #include <pwd.h> 23 #include <string.h> 24 #include <sys/resource.h> 25 #include "posixtest.h" 26 27 /** Set the euid of this process to a non-root uid */ set_nonroot()28int set_nonroot() 29 { 30 struct passwd *pw; 31 struct rlimit rlim; 32 int ret = 0; 33 34 setpwent(); 35 /* search for the first user which is non root */ 36 while ((pw = getpwent()) != NULL) 37 if (strcmp(pw->pw_name, "root")) 38 break; 39 endpwent(); 40 if (pw == NULL) { 41 printf("There is no other user than current and root.\n"); 42 return 1; 43 } 44 45 rlim.rlim_cur = 0; 46 rlim.rlim_max = 0; 47 if ((ret = setrlimit(RLIMIT_MEMLOCK, &rlim)) != 0) 48 printf("Failed at setrlimit() return %d \n", ret); 49 50 if (seteuid(pw->pw_uid) != 0) { 51 if (errno == EPERM) { 52 printf 53 ("You don't have permission to change your UID.\n"); 54 return 1; 55 } 56 perror("An error occurs when calling seteuid()"); 57 return 1; 58 } 59 60 printf("Testing with user '%s' (uid: %d)\n", 61 pw->pw_name, (int)geteuid()); 62 return 0; 63 } 64 main(void)65int main(void) 66 { 67 int result; 68 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 result = mlockall(MCL_CURRENT); 78 79 if (result == -1 && errno == EPERM) { 80 printf("Test PASSED\n"); 81 return PTS_PASS; 82 } else if (result == 0) { 83 printf("You have the right to call mlockall\n"); 84 return PTS_UNRESOLVED; 85 } else { 86 perror("Unexpected error"); 87 return PTS_UNRESOLVED; 88 } 89 } 90