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