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