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 mlock() function sets errno = EPERM if the calling process 11 * does not have the appropriate privilege to perform the requested operation 12 * (Linux 2.6.9 and later) and its RLIMIT_MEMLOCK soft resource limit set to 0. 13 */ 14 15 #define _GNU_SOURCE 1 /* XXX: Read baloney below about CAP_* */ 16 #define _XOPEN_SOURCE 600 17 18 #include <sys/mman.h> 19 #include <stdio.h> 20 #include <unistd.h> 21 #include <stdlib.h> 22 #include <errno.h> 23 #include <sys/types.h> 24 #include <pwd.h> 25 #include <string.h> 26 #include <sys/resource.h> 27 #include "posixtest.h" 28 29 #define BUFSIZE 8 30 31 /** Set the euid of this process to a non-root uid */ 32 int set_nonroot() 33 { 34 struct passwd *pw; 35 struct rlimit rlim; 36 int ret = 0; 37 38 setpwent(); 39 /* search for the first user which is non root */ 40 while ((pw = getpwent()) != NULL) 41 if (strcmp(pw->pw_name, "root")) 42 break; 43 endpwent(); 44 if (pw == NULL) { 45 printf("There is no other user than current and root.\n"); 46 return 1; 47 } 48 49 /* 50 * mlock() 51 * EPERM: 52 * (Linux 2.6.9 and later) the caller was not privileged (CAP_IPC_LOCK) 53 * and its RLIMIT_MEMLOCK soft resource limit was 0. 54 */ 55 56 rlim.rlim_cur = 0; 57 rlim.rlim_max = 0; 58 if ((ret = setrlimit(RLIMIT_MEMLOCK, &rlim)) != 0) 59 printf("Failed at setrlimit() return %d \n", ret); 60 61 if (seteuid(pw->pw_uid) != 0) { 62 if (errno == EPERM) { 63 printf 64 ("You don't have permission to change your UID.\n"); 65 return 1; 66 } 67 perror("An error occurs when calling seteuid()"); 68 return 1; 69 } 70 71 printf("Testing with user '%s' (uid: %d)\n", 72 pw->pw_name, (int)geteuid()); 73 return 0; 74 } 75 76 int main(void) 77 { 78 int result; 79 void *ptr; 80 81 /* This test should be run under standard user permissions */ 82 if (getuid() == 0) { 83 if (set_nonroot() != 0) { 84 printf("Cannot run this test as non-root user\n"); 85 return PTS_UNTESTED; 86 } 87 } 88 89 ptr = malloc(BUFSIZE); 90 if (ptr == NULL) { 91 printf("Can not allocate memory.\n"); 92 return PTS_UNRESOLVED; 93 } 94 95 result = mlock(ptr, BUFSIZE); 96 97 if (result == -1 && errno == EPERM) { 98 printf("Test PASSED\n"); 99 return PTS_PASS; 100 } else if (result == 0) { 101 printf("You have the right to call mlock\n"); 102 return PTS_FAIL; 103 } else { 104 perror("Unexpected error"); 105 return PTS_UNRESOLVED; 106 } 107 108 } 109