blob: 9c41f05f969bf80ae7f597d97040c6977fbabc0f [file] [log] [blame]
Luis R. Rodriguez23558692017-09-08 16:17:00 -07001/*
2 * umh - the kernel usermode helper
3 */
4#include <linux/module.h>
5#include <linux/sched.h>
6#include <linux/sched/task.h>
7#include <linux/binfmts.h>
8#include <linux/syscalls.h>
9#include <linux/unistd.h>
10#include <linux/kmod.h>
11#include <linux/slab.h>
12#include <linux/completion.h>
13#include <linux/cred.h>
14#include <linux/file.h>
15#include <linux/fdtable.h>
Linus Torvalds681ba232020-10-05 10:56:22 -070016#include <linux/fs_struct.h>
Luis R. Rodriguez23558692017-09-08 16:17:00 -070017#include <linux/workqueue.h>
18#include <linux/security.h>
19#include <linux/mount.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/resource.h>
23#include <linux/notifier.h>
24#include <linux/suspend.h>
25#include <linux/rwsem.h>
26#include <linux/ptrace.h>
27#include <linux/async.h>
28#include <linux/uaccess.h>
29
30#include <trace/events/module.h>
31
32#define CAP_BSET (void *)1
33#define CAP_PI (void *)2
34
35static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
36static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
37static DEFINE_SPINLOCK(umh_sysctl_lock);
38static DECLARE_RWSEM(umhelper_sem);
39
40static void call_usermodehelper_freeinfo(struct subprocess_info *info)
41{
42 if (info->cleanup)
43 (*info->cleanup)(info);
44 kfree(info);
45}
46
47static void umh_complete(struct subprocess_info *sub_info)
48{
49 struct completion *comp = xchg(&sub_info->complete, NULL);
50 /*
51 * See call_usermodehelper_exec(). If xchg() returns NULL
52 * we own sub_info, the UMH_KILLABLE caller has gone away
53 * or the caller used UMH_NO_WAIT.
54 */
55 if (comp)
56 complete(comp);
57 else
58 call_usermodehelper_freeinfo(sub_info);
59}
60
61/*
62 * This is the task which runs the usermode application
63 */
64static int call_usermodehelper_exec_async(void *data)
65{
66 struct subprocess_info *sub_info = data;
67 struct cred *new;
68 int retval;
69
70 spin_lock_irq(&current->sighand->siglock);
71 flush_signal_handlers(current, 1);
72 spin_unlock_irq(&current->sighand->siglock);
73
74 /*
Linus Torvalds681ba232020-10-05 10:56:22 -070075 * Initial kernel threads share ther FS with init, in order to
76 * get the init root directory. But we've now created a new
77 * thread that is going to execve a user process and has its own
78 * 'struct fs_struct'. Reset umask to the default.
79 */
80 current->fs->umask = 0022;
81
82 /*
Luis R. Rodriguez23558692017-09-08 16:17:00 -070083 * Our parent (unbound workqueue) runs with elevated scheduling
84 * priority. Avoid propagating that into the userspace child.
85 */
86 set_user_nice(current, 0);
87
88 retval = -ENOMEM;
89 new = prepare_kernel_cred(current);
90 if (!new)
91 goto out;
92
93 spin_lock(&umh_sysctl_lock);
94 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
95 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
96 new->cap_inheritable);
97 spin_unlock(&umh_sysctl_lock);
98
99 if (sub_info->init) {
100 retval = sub_info->init(sub_info, new);
101 if (retval) {
102 abort_creds(new);
103 goto out;
104 }
105 }
106
107 commit_creds(new);
108
109 retval = do_execve(getname_kernel(sub_info->path),
110 (const char __user *const __user *)sub_info->argv,
111 (const char __user *const __user *)sub_info->envp);
112out:
113 sub_info->retval = retval;
114 /*
115 * call_usermodehelper_exec_sync() will call umh_complete
116 * if UHM_WAIT_PROC.
117 */
118 if (!(sub_info->wait & UMH_WAIT_PROC))
119 umh_complete(sub_info);
120 if (!retval)
121 return 0;
122 do_exit(0);
123}
124
125/* Handles UMH_WAIT_PROC. */
126static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
127{
128 pid_t pid;
129
130 /* If SIGCLD is ignored sys_wait4 won't populate the status. */
131 kernel_sigaction(SIGCHLD, SIG_DFL);
132 pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
133 if (pid < 0) {
134 sub_info->retval = pid;
135 } else {
136 int ret = -ECHILD;
137 /*
138 * Normally it is bogus to call wait4() from in-kernel because
139 * wait4() wants to write the exit code to a userspace address.
140 * But call_usermodehelper_exec_sync() always runs as kernel
141 * thread (workqueue) and put_user() to a kernel address works
142 * OK for kernel threads, due to their having an mm_segment_t
143 * which spans the entire address space.
144 *
145 * Thus the __user pointer cast is valid here.
146 */
147 sys_wait4(pid, (int __user *)&ret, 0, NULL);
148
149 /*
150 * If ret is 0, either call_usermodehelper_exec_async failed and
151 * the real error code is already in sub_info->retval or
152 * sub_info->retval is 0 anyway, so don't mess with it then.
153 */
154 if (ret)
155 sub_info->retval = ret;
156 }
157
158 /* Restore default kernel sig handler */
159 kernel_sigaction(SIGCHLD, SIG_IGN);
160
161 umh_complete(sub_info);
162}
163
164/*
165 * We need to create the usermodehelper kernel thread from a task that is affine
166 * to an optimized set of CPUs (or nohz housekeeping ones) such that they
167 * inherit a widest affinity irrespective of call_usermodehelper() callers with
168 * possibly reduced affinity (eg: per-cpu workqueues). We don't want
169 * usermodehelper targets to contend a busy CPU.
170 *
171 * Unbound workqueues provide such wide affinity and allow to block on
172 * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
173 *
174 * Besides, workqueues provide the privilege level that caller might not have
175 * to perform the usermodehelper request.
176 *
177 */
178static void call_usermodehelper_exec_work(struct work_struct *work)
179{
180 struct subprocess_info *sub_info =
181 container_of(work, struct subprocess_info, work);
182
183 if (sub_info->wait & UMH_WAIT_PROC) {
184 call_usermodehelper_exec_sync(sub_info);
185 } else {
186 pid_t pid;
187 /*
188 * Use CLONE_PARENT to reparent it to kthreadd; we do not
189 * want to pollute current->children, and we need a parent
190 * that always ignores SIGCHLD to ensure auto-reaping.
191 */
192 pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
193 CLONE_PARENT | SIGCHLD);
194 if (pid < 0) {
195 sub_info->retval = pid;
196 umh_complete(sub_info);
197 }
198 }
199}
200
201/*
202 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
203 * (used for preventing user land processes from being created after the user
204 * land has been frozen during a system-wide hibernation or suspend operation).
205 * Should always be manipulated under umhelper_sem acquired for write.
206 */
207static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
208
209/* Number of helpers running */
210static atomic_t running_helpers = ATOMIC_INIT(0);
211
212/*
213 * Wait queue head used by usermodehelper_disable() to wait for all running
214 * helpers to finish.
215 */
216static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
217
218/*
219 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
220 * to become 'false'.
221 */
222static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
223
224/*
225 * Time to wait for running_helpers to become zero before the setting of
226 * usermodehelper_disabled in usermodehelper_disable() fails
227 */
228#define RUNNING_HELPERS_TIMEOUT (5 * HZ)
229
230int usermodehelper_read_trylock(void)
231{
232 DEFINE_WAIT(wait);
233 int ret = 0;
234
235 down_read(&umhelper_sem);
236 for (;;) {
237 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
238 TASK_INTERRUPTIBLE);
239 if (!usermodehelper_disabled)
240 break;
241
242 if (usermodehelper_disabled == UMH_DISABLED)
243 ret = -EAGAIN;
244
245 up_read(&umhelper_sem);
246
247 if (ret)
248 break;
249
250 schedule();
251 try_to_freeze();
252
253 down_read(&umhelper_sem);
254 }
255 finish_wait(&usermodehelper_disabled_waitq, &wait);
256 return ret;
257}
258EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
259
260long usermodehelper_read_lock_wait(long timeout)
261{
262 DEFINE_WAIT(wait);
263
264 if (timeout < 0)
265 return -EINVAL;
266
267 down_read(&umhelper_sem);
268 for (;;) {
269 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
270 TASK_UNINTERRUPTIBLE);
271 if (!usermodehelper_disabled)
272 break;
273
274 up_read(&umhelper_sem);
275
276 timeout = schedule_timeout(timeout);
277 if (!timeout)
278 break;
279
280 down_read(&umhelper_sem);
281 }
282 finish_wait(&usermodehelper_disabled_waitq, &wait);
283 return timeout;
284}
285EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
286
287void usermodehelper_read_unlock(void)
288{
289 up_read(&umhelper_sem);
290}
291EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
292
293/**
294 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
295 * @depth: New value to assign to usermodehelper_disabled.
296 *
297 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
298 * writing) and wakeup tasks waiting for it to change.
299 */
300void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
301{
302 down_write(&umhelper_sem);
303 usermodehelper_disabled = depth;
304 wake_up(&usermodehelper_disabled_waitq);
305 up_write(&umhelper_sem);
306}
307
308/**
309 * __usermodehelper_disable - Prevent new helpers from being started.
310 * @depth: New value to assign to usermodehelper_disabled.
311 *
312 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
313 */
314int __usermodehelper_disable(enum umh_disable_depth depth)
315{
316 long retval;
317
318 if (!depth)
319 return -EINVAL;
320
321 down_write(&umhelper_sem);
322 usermodehelper_disabled = depth;
323 up_write(&umhelper_sem);
324
325 /*
326 * From now on call_usermodehelper_exec() won't start any new
327 * helpers, so it is sufficient if running_helpers turns out to
328 * be zero at one point (it may be increased later, but that
329 * doesn't matter).
330 */
331 retval = wait_event_timeout(running_helpers_waitq,
332 atomic_read(&running_helpers) == 0,
333 RUNNING_HELPERS_TIMEOUT);
334 if (retval)
335 return 0;
336
337 __usermodehelper_set_disable_depth(UMH_ENABLED);
338 return -EAGAIN;
339}
340
341static void helper_lock(void)
342{
343 atomic_inc(&running_helpers);
344 smp_mb__after_atomic();
345}
346
347static void helper_unlock(void)
348{
349 if (atomic_dec_and_test(&running_helpers))
350 wake_up(&running_helpers_waitq);
351}
352
353/**
354 * call_usermodehelper_setup - prepare to call a usermode helper
355 * @path: path to usermode executable
356 * @argv: arg vector for process
357 * @envp: environment for process
358 * @gfp_mask: gfp mask for memory allocation
359 * @cleanup: a cleanup function
360 * @init: an init function
361 * @data: arbitrary context sensitive data
362 *
363 * Returns either %NULL on allocation failure, or a subprocess_info
364 * structure. This should be passed to call_usermodehelper_exec to
365 * exec the process and free the structure.
366 *
367 * The init function is used to customize the helper process prior to
368 * exec. A non-zero return code causes the process to error out, exit,
369 * and return the failure to the calling process
370 *
371 * The cleanup function is just before ethe subprocess_info is about to
372 * be freed. This can be used for freeing the argv and envp. The
373 * Function must be runnable in either a process context or the
374 * context in which call_usermodehelper_exec is called.
375 */
376struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
377 char **envp, gfp_t gfp_mask,
378 int (*init)(struct subprocess_info *info, struct cred *new),
379 void (*cleanup)(struct subprocess_info *info),
380 void *data)
381{
382 struct subprocess_info *sub_info;
383 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
384 if (!sub_info)
385 goto out;
386
387 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
388
389#ifdef CONFIG_STATIC_USERMODEHELPER
390 sub_info->path = CONFIG_STATIC_USERMODEHELPER_PATH;
391#else
392 sub_info->path = path;
393#endif
394 sub_info->argv = argv;
395 sub_info->envp = envp;
396
397 sub_info->cleanup = cleanup;
398 sub_info->init = init;
399 sub_info->data = data;
400 out:
401 return sub_info;
402}
403EXPORT_SYMBOL(call_usermodehelper_setup);
404
405/**
406 * call_usermodehelper_exec - start a usermode application
407 * @sub_info: information about the subprocessa
408 * @wait: wait for the application to finish and return status.
409 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
410 * when the program couldn't be exec'ed. This makes it safe to call
411 * from interrupt context.
412 *
413 * Runs a user-space application. The application is started
414 * asynchronously if wait is not set, and runs as a child of system workqueues.
415 * (ie. it runs with full root capabilities and optimized affinity).
Luis Chamberlainff6c4722020-04-16 16:28:59 +0000416 *
417 * Note: successful return value does not guarantee the helper was called at
418 * all. You can't rely on sub_info->{init,cleanup} being called even for
419 * UMH_WAIT_* wait modes as STATIC_USERMODEHELPER_PATH="" turns all helpers
420 * into a successful no-op.
Luis R. Rodriguez23558692017-09-08 16:17:00 -0700421 */
422int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
423{
424 DECLARE_COMPLETION_ONSTACK(done);
425 int retval = 0;
426
427 if (!sub_info->path) {
428 call_usermodehelper_freeinfo(sub_info);
429 return -EINVAL;
430 }
431 helper_lock();
432 if (usermodehelper_disabled) {
433 retval = -EBUSY;
434 goto out;
435 }
436
437 /*
438 * If there is no binary for us to call, then just return and get out of
439 * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
440 * disable all call_usermodehelper() calls.
441 */
442 if (strlen(sub_info->path) == 0)
443 goto out;
444
445 /*
446 * Set the completion pointer only if there is a waiter.
447 * This makes it possible to use umh_complete to free
448 * the data structure in case of UMH_NO_WAIT.
449 */
450 sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
451 sub_info->wait = wait;
452
453 queue_work(system_unbound_wq, &sub_info->work);
454 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
455 goto unlock;
456
457 if (wait & UMH_KILLABLE) {
458 retval = wait_for_completion_killable(&done);
459 if (!retval)
460 goto wait_done;
461
462 /* umh_complete() will see NULL and free sub_info */
463 if (xchg(&sub_info->complete, NULL))
464 goto unlock;
465 /* fallthrough, umh_complete() was already called */
466 }
467
468 wait_for_completion(&done);
469wait_done:
470 retval = sub_info->retval;
471out:
472 call_usermodehelper_freeinfo(sub_info);
473unlock:
474 helper_unlock();
475 return retval;
476}
477EXPORT_SYMBOL(call_usermodehelper_exec);
478
479/**
480 * call_usermodehelper() - prepare and start a usermode application
481 * @path: path to usermode executable
482 * @argv: arg vector for process
483 * @envp: environment for process
484 * @wait: wait for the application to finish and return status.
485 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
486 * when the program couldn't be exec'ed. This makes it safe to call
487 * from interrupt context.
488 *
489 * This function is the equivalent to use call_usermodehelper_setup() and
490 * call_usermodehelper_exec().
491 */
492int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
493{
494 struct subprocess_info *info;
495 gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
496
497 info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
498 NULL, NULL, NULL);
499 if (info == NULL)
500 return -ENOMEM;
501
502 return call_usermodehelper_exec(info, wait);
503}
504EXPORT_SYMBOL(call_usermodehelper);
505
506static int proc_cap_handler(struct ctl_table *table, int write,
507 void __user *buffer, size_t *lenp, loff_t *ppos)
508{
509 struct ctl_table t;
510 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
511 kernel_cap_t new_cap;
512 int err, i;
513
514 if (write && (!capable(CAP_SETPCAP) ||
515 !capable(CAP_SYS_MODULE)))
516 return -EPERM;
517
518 /*
519 * convert from the global kernel_cap_t to the ulong array to print to
520 * userspace if this is a read.
521 */
522 spin_lock(&umh_sysctl_lock);
523 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
524 if (table->data == CAP_BSET)
525 cap_array[i] = usermodehelper_bset.cap[i];
526 else if (table->data == CAP_PI)
527 cap_array[i] = usermodehelper_inheritable.cap[i];
528 else
529 BUG();
530 }
531 spin_unlock(&umh_sysctl_lock);
532
533 t = *table;
534 t.data = &cap_array;
535
536 /*
537 * actually read or write and array of ulongs from userspace. Remember
538 * these are least significant 32 bits first
539 */
540 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
541 if (err < 0)
542 return err;
543
544 /*
545 * convert from the sysctl array of ulongs to the kernel_cap_t
546 * internal representation
547 */
548 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
549 new_cap.cap[i] = cap_array[i];
550
551 /*
552 * Drop everything not in the new_cap (but don't add things)
553 */
554 spin_lock(&umh_sysctl_lock);
555 if (write) {
556 if (table->data == CAP_BSET)
557 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
558 if (table->data == CAP_PI)
559 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
560 }
561 spin_unlock(&umh_sysctl_lock);
562
563 return 0;
564}
565
566struct ctl_table usermodehelper_table[] = {
567 {
568 .procname = "bset",
569 .data = CAP_BSET,
570 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
571 .mode = 0600,
572 .proc_handler = proc_cap_handler,
573 },
574 {
575 .procname = "inheritable",
576 .data = CAP_PI,
577 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
578 .mode = 0600,
579 .proc_handler = proc_cap_handler,
580 },
581 { }
582};