1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de) 4 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 #include <errno.h> 11 #include <signal.h> 12 #include <string.h> 13 #include <limits.h> 14 #include <sys/resource.h> 15 #include <sys/personality.h> 16 #include <as-layout.h> 17 #include <init.h> 18 #include <kern_util.h> 19 #include <os.h> 20 #include <um_malloc.h> 21 #include "internal.h" 22 23 #define PGD_BOUND (4 * 1024 * 1024) 24 #define STACKSIZE (8 * 1024 * 1024) 25 #define THREAD_NAME_LEN (256) 26 27 long elf_aux_hwcap; 28 29 static void set_stklim(void) 30 { 31 struct rlimit lim; 32 33 if (getrlimit(RLIMIT_STACK, &lim) < 0) { 34 perror("getrlimit"); 35 exit(1); 36 } 37 if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) { 38 lim.rlim_cur = STACKSIZE; 39 if (setrlimit(RLIMIT_STACK, &lim) < 0) { 40 perror("setrlimit"); 41 exit(1); 42 } 43 } 44 } 45 46 static void last_ditch_exit(int sig) 47 { 48 uml_cleanup(); 49 exit(1); 50 } 51 52 static void install_fatal_handler(int sig) 53 { 54 struct sigaction action; 55 56 /* All signals are enabled in this handler ... */ 57 sigemptyset(&action.sa_mask); 58 59 /* 60 * ... including the signal being handled, plus we want the 61 * handler reset to the default behavior, so that if an exit 62 * handler is hanging for some reason, the UML will just die 63 * after this signal is sent a second time. 64 */ 65 action.sa_flags = SA_RESETHAND | SA_NODEFER; 66 action.sa_restorer = NULL; 67 action.sa_handler = last_ditch_exit; 68 if (sigaction(sig, &action, NULL) < 0) { 69 os_warn("failed to install handler for signal %d " 70 "- errno = %d\n", sig, errno); 71 exit(1); 72 } 73 } 74 75 #define UML_LIB_PATH ":" OS_LIB_PATH "/uml" 76 77 static void setup_env_path(void) 78 { 79 char *new_path = NULL; 80 char *old_path = NULL; 81 int path_len = 0; 82 83 old_path = getenv("PATH"); 84 /* 85 * if no PATH variable is set or it has an empty value 86 * just use the default + /usr/lib/uml 87 */ 88 if (!old_path || (path_len = strlen(old_path)) == 0) { 89 if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH)) 90 perror("couldn't putenv"); 91 return; 92 } 93 94 /* append /usr/lib/uml to the existing path */ 95 path_len += strlen("PATH=" UML_LIB_PATH) + 1; 96 new_path = malloc(path_len); 97 if (!new_path) { 98 perror("couldn't malloc to set a new PATH"); 99 return; 100 } 101 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path); 102 if (putenv(new_path)) { 103 perror("couldn't putenv to set a new PATH"); 104 free(new_path); 105 } 106 } 107 108 int __init main(int argc, char **argv, char **envp) 109 { 110 char **new_argv; 111 int ret, i, err; 112 113 /* Disable randomization and re-exec if it was changed successfully */ 114 ret = personality(PER_LINUX | ADDR_NO_RANDOMIZE); 115 if (ret >= 0 && (ret & (PER_LINUX | ADDR_NO_RANDOMIZE)) != 116 (PER_LINUX | ADDR_NO_RANDOMIZE)) { 117 char buf[PATH_MAX] = {}; 118 ssize_t ret; 119 120 ret = readlink("/proc/self/exe", buf, sizeof(buf)); 121 if (ret < 0 || ret >= sizeof(buf)) { 122 perror("readlink failure"); 123 exit(1); 124 } 125 execve(buf, argv, envp); 126 } 127 128 set_stklim(); 129 130 setup_env_path(); 131 132 setsid(); 133 134 new_argv = malloc((argc + 1) * sizeof(char *)); 135 if (new_argv == NULL) { 136 perror("Mallocing argv"); 137 exit(1); 138 } 139 for (i = 0; i < argc; i++) { 140 new_argv[i] = strdup(argv[i]); 141 if (new_argv[i] == NULL) { 142 perror("Mallocing an arg"); 143 exit(1); 144 } 145 } 146 new_argv[argc] = NULL; 147 148 /* 149 * Allow these signals to bring down a UML if all other 150 * methods of control fail. 151 */ 152 install_fatal_handler(SIGINT); 153 install_fatal_handler(SIGTERM); 154 155 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA 156 scan_elf_aux(envp); 157 #endif 158 159 change_sig(SIGPIPE, 0); 160 ret = linux_main(argc, argv, envp); 161 162 /* 163 * Disable SIGPROF - I have no idea why libc doesn't do this or turn 164 * off the profiling time, but UML dies with a SIGPROF just before 165 * exiting when profiling is active. 166 */ 167 change_sig(SIGPROF, 0); 168 169 /* 170 * This signal stuff used to be in the reboot case. However, 171 * sometimes a timer signal can come in when we're halting (reproducably 172 * when writing out gcov information, presumably because that takes 173 * some time) and cause a segfault. 174 */ 175 176 /* stop timers and set timer signal to be ignored */ 177 os_timer_disable(); 178 179 /* disable SIGIO for the fds and set SIGIO to be ignored */ 180 err = deactivate_all_fds(); 181 if (err) 182 os_warn("deactivate_all_fds failed, errno = %d\n", -err); 183 184 /* 185 * Let any pending signals fire now. This ensures 186 * that they won't be delivered after the exec, when 187 * they are definitely not expected. 188 */ 189 unblock_signals(); 190 191 os_info("\n"); 192 /* Reboot */ 193 if (ret) { 194 execvp(new_argv[0], new_argv); 195 perror("Failed to exec kernel"); 196 ret = 1; 197 } 198 return uml_exitcode; 199 } 200 201 extern void *__real_malloc(int); 202 extern void __real_free(void *); 203 204 /* workaround for -Wmissing-prototypes warnings */ 205 void *__wrap_malloc(int size); 206 void *__wrap_calloc(int n, int size); 207 void __wrap_free(void *ptr); 208 209 void *__wrap_malloc(int size) 210 { 211 void *ret; 212 213 if (!kmalloc_ok) 214 return __real_malloc(size); 215 else if (size <= UM_KERN_PAGE_SIZE) 216 /* finding contiguous pages can be hard*/ 217 ret = uml_kmalloc(size, UM_GFP_KERNEL); 218 else ret = vmalloc(size); 219 220 /* 221 * glibc people insist that if malloc fails, errno should be 222 * set by malloc as well. So we do. 223 */ 224 if (ret == NULL) 225 errno = ENOMEM; 226 227 return ret; 228 } 229 230 void *__wrap_calloc(int n, int size) 231 { 232 void *ptr = __wrap_malloc(n * size); 233 234 if (ptr == NULL) 235 return NULL; 236 memset(ptr, 0, n * size); 237 return ptr; 238 } 239 240 void __wrap_free(void *ptr) 241 { 242 unsigned long addr = (unsigned long) ptr; 243 244 /* 245 * We need to know how the allocation happened, so it can be correctly 246 * freed. This is done by seeing what region of memory the pointer is 247 * in - 248 * physical memory - kmalloc/kfree 249 * kernel virtual memory - vmalloc/vfree 250 * anywhere else - malloc/free 251 * If kmalloc is not yet possible, then either high_physmem and/or 252 * end_vm are still 0 (as at startup), in which case we call free, or 253 * we have set them, but anyway addr has not been allocated from those 254 * areas. So, in both cases __real_free is called. 255 * 256 * CAN_KMALLOC is checked because it would be bad to free a buffer 257 * with kmalloc/vmalloc after they have been turned off during 258 * shutdown. 259 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so 260 * there is a possibility for memory leaks. 261 */ 262 263 if ((addr >= uml_physmem) && (addr < high_physmem)) { 264 if (kmalloc_ok) 265 kfree(ptr); 266 } 267 else if ((addr >= start_vm) && (addr < end_vm)) { 268 if (kmalloc_ok) 269 vfree(ptr); 270 } 271 else __real_free(ptr); 272 } 273