diff options
Diffstat (limited to 'arch/um/os-Linux/mem.c')
| -rw-r--r-- | arch/um/os-Linux/mem.c | 118 |
1 files changed, 117 insertions, 1 deletions
diff --git a/arch/um/os-Linux/mem.c b/arch/um/os-Linux/mem.c index 71bb90a7606d..c6432e729241 100644 --- a/arch/um/os-Linux/mem.c +++ b/arch/um/os-Linux/mem.c | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | #include <fcntl.h> | 8 | #include <fcntl.h> |
| 9 | #include <sys/types.h> | 9 | #include <sys/types.h> |
| 10 | #include <sys/mman.h> | 10 | #include <sys/mman.h> |
| 11 | #include <sys/statfs.h> | ||
| 11 | #include "kern_util.h" | 12 | #include "kern_util.h" |
| 12 | #include "user.h" | 13 | #include "user.h" |
| 13 | #include "user_util.h" | 14 | #include "user_util.h" |
| @@ -19,6 +20,7 @@ | |||
| 19 | 20 | ||
| 20 | #include <sys/param.h> | 21 | #include <sys/param.h> |
| 21 | 22 | ||
| 23 | static char *default_tmpdir = "/tmp"; | ||
| 22 | static char *tempdir = NULL; | 24 | static char *tempdir = NULL; |
| 23 | 25 | ||
| 24 | static void __init find_tempdir(void) | 26 | static void __init find_tempdir(void) |
| @@ -34,7 +36,7 @@ static void __init find_tempdir(void) | |||
| 34 | break; | 36 | break; |
| 35 | } | 37 | } |
| 36 | if((dir == NULL) || (*dir == '\0')) | 38 | if((dir == NULL) || (*dir == '\0')) |
| 37 | dir = "/tmp"; | 39 | dir = default_tmpdir; |
| 38 | 40 | ||
| 39 | tempdir = malloc(strlen(dir) + 2); | 41 | tempdir = malloc(strlen(dir) + 2); |
| 40 | if(tempdir == NULL){ | 42 | if(tempdir == NULL){ |
| @@ -46,6 +48,96 @@ static void __init find_tempdir(void) | |||
| 46 | strcat(tempdir, "/"); | 48 | strcat(tempdir, "/"); |
| 47 | } | 49 | } |
| 48 | 50 | ||
| 51 | /* This will return 1, with the first character in buf being the | ||
| 52 | * character following the next instance of c in the file. This will | ||
| 53 | * read the file as needed. If there's an error, -errno is returned; | ||
| 54 | * if the end of the file is reached, 0 is returned. | ||
| 55 | */ | ||
| 56 | static int next(int fd, char *buf, int size, char c) | ||
| 57 | { | ||
| 58 | int n; | ||
| 59 | char *ptr; | ||
| 60 | |||
| 61 | while((ptr = strchr(buf, c)) == NULL){ | ||
| 62 | n = read(fd, buf, size - 1); | ||
| 63 | if(n == 0) | ||
| 64 | return 0; | ||
| 65 | else if(n < 0) | ||
| 66 | return -errno; | ||
| 67 | |||
| 68 | buf[n] = '\0'; | ||
| 69 | } | ||
| 70 | |||
| 71 | ptr++; | ||
| 72 | memmove(buf, ptr, strlen(ptr) + 1); | ||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | static int checked_tmpdir = 0; | ||
| 77 | |||
| 78 | /* Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner | ||
| 79 | * way to do this than to parse /proc/mounts. statfs will return the | ||
| 80 | * same filesystem magic number and fs id for both /dev and /dev/shm | ||
| 81 | * when they are both tmpfs, so you can't tell if they are different | ||
| 82 | * filesystems. Also, there seems to be no other way of finding the | ||
| 83 | * mount point of a filesystem from within it. | ||
| 84 | * | ||
| 85 | * If a /dev/shm tmpfs entry is found, then we switch to using it. | ||
| 86 | * Otherwise, we stay with the default /tmp. | ||
| 87 | */ | ||
| 88 | static void which_tmpdir(void) | ||
| 89 | { | ||
| 90 | int fd, found; | ||
| 91 | char buf[128] = { '\0' }; | ||
| 92 | |||
| 93 | if(checked_tmpdir) | ||
| 94 | return; | ||
| 95 | |||
| 96 | checked_tmpdir = 1; | ||
| 97 | |||
| 98 | printf("Checking for tmpfs mount on /dev/shm..."); | ||
| 99 | |||
| 100 | fd = open("/proc/mounts", O_RDONLY); | ||
| 101 | if(fd < 0){ | ||
| 102 | printf("failed to open /proc/mounts, errno = %d\n", errno); | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | while(1){ | ||
| 107 | found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), ' '); | ||
| 108 | if(found != 1) | ||
| 109 | break; | ||
| 110 | |||
| 111 | if(!strncmp(buf, "/dev/shm", strlen("/dev/shm"))) | ||
| 112 | goto found; | ||
| 113 | |||
| 114 | found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), '\n'); | ||
| 115 | if(found != 1) | ||
| 116 | break; | ||
| 117 | } | ||
| 118 | |||
| 119 | err: | ||
| 120 | if(found == 0) | ||
| 121 | printf("nothing mounted on /dev/shm\n"); | ||
| 122 | else if(found < 0) | ||
| 123 | printf("read returned errno %d\n", -found); | ||
| 124 | |||
| 125 | return; | ||
| 126 | |||
| 127 | found: | ||
| 128 | found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), ' '); | ||
| 129 | if(found != 1) | ||
| 130 | goto err; | ||
| 131 | |||
| 132 | if(strncmp(buf, "tmpfs", strlen("tmpfs"))){ | ||
| 133 | printf("not tmpfs\n"); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | printf("OK\n"); | ||
| 138 | default_tmpdir = "/dev/shm"; | ||
| 139 | } | ||
| 140 | |||
| 49 | /* | 141 | /* |
| 50 | * This proc still used in tt-mode | 142 | * This proc still used in tt-mode |
| 51 | * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger). | 143 | * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger). |
| @@ -56,6 +148,7 @@ int make_tempfile(const char *template, char **out_tempname, int do_unlink) | |||
| 56 | char *tempname; | 148 | char *tempname; |
| 57 | int fd; | 149 | int fd; |
| 58 | 150 | ||
| 151 | which_tmpdir(); | ||
| 59 | tempname = malloc(MAXPATHLEN); | 152 | tempname = malloc(MAXPATHLEN); |
| 60 | 153 | ||
| 61 | find_tempdir(); | 154 | find_tempdir(); |
| @@ -137,3 +230,26 @@ int create_mem_file(unsigned long long len) | |||
| 137 | } | 230 | } |
| 138 | return(fd); | 231 | return(fd); |
| 139 | } | 232 | } |
| 233 | |||
| 234 | |||
| 235 | void check_tmpexec(void) | ||
| 236 | { | ||
| 237 | void *addr; | ||
| 238 | int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE); | ||
| 239 | |||
| 240 | addr = mmap(NULL, UM_KERN_PAGE_SIZE, | ||
| 241 | PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); | ||
| 242 | printf("Checking PROT_EXEC mmap in %s...",tempdir); | ||
| 243 | fflush(stdout); | ||
| 244 | if(addr == MAP_FAILED){ | ||
| 245 | err = errno; | ||
| 246 | perror("failed"); | ||
| 247 | if(err == EPERM) | ||
| 248 | printf("%s must be not mounted noexec\n",tempdir); | ||
| 249 | exit(1); | ||
| 250 | } | ||
| 251 | printf("OK\n"); | ||
| 252 | munmap(addr, UM_KERN_PAGE_SIZE); | ||
| 253 | |||
| 254 | close(fd); | ||
| 255 | } | ||
