1
2
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
36static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
37static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
38static DEFINE_SPINLOCK(umh_sysctl_lock);
39static DECLARE_RWSEM(umhelper_sem);
40
41static void call_usermodehelper_freeinfo(struct subprocess_info *info)
42{
43 if (info->cleanup)
44 (*info->cleanup)(info);
45 kfree(info);
46}
47
48static void umh_complete(struct subprocess_info *sub_info)
49{
50 struct completion *comp = xchg(&sub_info->complete, NULL);
51
52
53
54
55
56 if (comp)
57 complete(comp);
58 else
59 call_usermodehelper_freeinfo(sub_info);
60}
61
62
63
64
65static 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(¤t->sighand->siglock);
72 flush_signal_handlers(current, 1);
73 spin_unlock_irq(¤t->sighand->siglock);
74
75
76
77
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);
110out:
111 sub_info->retval = retval;
112
113
114
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
124static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
125{
126 pid_t pid;
127
128
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
137
138
139
140
141
142
143
144
145 kernel_wait4(pid, (int __user *)&ret, 0, NULL);
146
147
148
149
150
151
152 if (ret)
153 sub_info->retval = ret;
154 }
155
156
157 kernel_sigaction(SIGCHLD, SIG_IGN);
158
159 umh_complete(sub_info);
160}
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176static 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
187
188
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
201
202
203
204
205static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
206
207
208static atomic_t running_helpers = ATOMIC_INIT(0);
209
210
211
212
213
214static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
215
216
217
218
219
220static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
221
222
223
224
225
226#define RUNNING_HELPERS_TIMEOUT (5 * HZ)
227
228int 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}
256EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
257
258long 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}
283EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
284
285void usermodehelper_read_unlock(void)
286{
287 up_read(&umhelper_sem);
288}
289EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
290
291
292
293
294
295
296
297
298void __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
308
309
310
311
312int __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
325
326
327
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
339static void helper_lock(void)
340{
341 atomic_inc(&running_helpers);
342 smp_mb__after_atomic();
343}
344
345static void helper_unlock(void)
346{
347 if (atomic_dec_and_test(&running_helpers))
348 wake_up(&running_helpers_waitq);
349}
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374struct 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}
401EXPORT_SYMBOL(call_usermodehelper_setup);
402
403struct 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
409 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_KERNEL);
410 if (!sub_info)
411 return NULL;
412
413 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
414 sub_info->path = "none";
415 sub_info->file = file;
416 sub_info->init = init;
417 sub_info->cleanup = cleanup;
418 sub_info->data = data;
419 return sub_info;
420}
421
422static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
423{
424 struct umh_info *umh_info = info->data;
425 struct file *from_umh[2];
426 struct file *to_umh[2];
427 int err;
428
429
430 err = create_pipe_files(to_umh, 0);
431 if (err)
432 return err;
433 err = replace_fd(0, to_umh[0], 0);
434 fput(to_umh[0]);
435 if (err < 0) {
436 fput(to_umh[1]);
437 return err;
438 }
439
440
441 err = create_pipe_files(from_umh, 0);
442 if (err) {
443 fput(to_umh[1]);
444 replace_fd(0, NULL, 0);
445 return err;
446 }
447 err = replace_fd(1, from_umh[1], 0);
448 fput(from_umh[1]);
449 if (err < 0) {
450 fput(to_umh[1]);
451 replace_fd(0, NULL, 0);
452 fput(from_umh[0]);
453 return err;
454 }
455
456 umh_info->pipe_to_umh = to_umh[1];
457 umh_info->pipe_from_umh = from_umh[0];
458 return 0;
459}
460
461static void umh_save_pid(struct subprocess_info *info)
462{
463 struct umh_info *umh_info = info->data;
464
465 umh_info->pid = info->pid;
466}
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481int fork_usermode_blob(void *data, size_t len, struct umh_info *info)
482{
483 struct subprocess_info *sub_info;
484 struct file *file;
485 ssize_t written;
486 loff_t pos = 0;
487 int err;
488
489 file = shmem_kernel_file_setup("", len, 0);
490 if (IS_ERR(file))
491 return PTR_ERR(file);
492
493 written = kernel_write(file, data, len, &pos);
494 if (written != len) {
495 err = written;
496 if (err >= 0)
497 err = -ENOMEM;
498 goto out;
499 }
500
501 err = -ENOMEM;
502 sub_info = call_usermodehelper_setup_file(file, umh_pipe_setup,
503 umh_save_pid, info);
504 if (!sub_info)
505 goto out;
506
507 err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
508out:
509 fput(file);
510 return err;
511}
512EXPORT_SYMBOL_GPL(fork_usermode_blob);
513
514
515
516
517
518
519
520
521
522
523
524
525
526int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
527{
528 DECLARE_COMPLETION_ONSTACK(done);
529 int retval = 0;
530
531 if (!sub_info->path) {
532 call_usermodehelper_freeinfo(sub_info);
533 return -EINVAL;
534 }
535 helper_lock();
536 if (usermodehelper_disabled) {
537 retval = -EBUSY;
538 goto out;
539 }
540
541
542
543
544
545
546 if (strlen(sub_info->path) == 0)
547 goto out;
548
549
550
551
552
553
554 sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
555 sub_info->wait = wait;
556
557 queue_work(system_unbound_wq, &sub_info->work);
558 if (wait == UMH_NO_WAIT)
559 goto unlock;
560
561 if (wait & UMH_KILLABLE) {
562 retval = wait_for_completion_killable(&done);
563 if (!retval)
564 goto wait_done;
565
566
567 if (xchg(&sub_info->complete, NULL))
568 goto unlock;
569
570 }
571
572 wait_for_completion(&done);
573wait_done:
574 retval = sub_info->retval;
575out:
576 call_usermodehelper_freeinfo(sub_info);
577unlock:
578 helper_unlock();
579 return retval;
580}
581EXPORT_SYMBOL(call_usermodehelper_exec);
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
597{
598 struct subprocess_info *info;
599 gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
600
601 info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
602 NULL, NULL, NULL);
603 if (info == NULL)
604 return -ENOMEM;
605
606 return call_usermodehelper_exec(info, wait);
607}
608EXPORT_SYMBOL(call_usermodehelper);
609
610static int proc_cap_handler(struct ctl_table *table, int write,
611 void __user *buffer, size_t *lenp, loff_t *ppos)
612{
613 struct ctl_table t;
614 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
615 kernel_cap_t new_cap;
616 int err, i;
617
618 if (write && (!capable(CAP_SETPCAP) ||
619 !capable(CAP_SYS_MODULE)))
620 return -EPERM;
621
622
623
624
625
626 spin_lock(&umh_sysctl_lock);
627 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
628 if (table->data == CAP_BSET)
629 cap_array[i] = usermodehelper_bset.cap[i];
630 else if (table->data == CAP_PI)
631 cap_array[i] = usermodehelper_inheritable.cap[i];
632 else
633 BUG();
634 }
635 spin_unlock(&umh_sysctl_lock);
636
637 t = *table;
638 t.data = &cap_array;
639
640
641
642
643
644 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
645 if (err < 0)
646 return err;
647
648
649
650
651
652 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
653 new_cap.cap[i] = cap_array[i];
654
655
656
657
658 if (write) {
659 spin_lock(&umh_sysctl_lock);
660 if (table->data == CAP_BSET)
661 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
662 if (table->data == CAP_PI)
663 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
664 spin_unlock(&umh_sysctl_lock);
665 }
666
667 return 0;
668}
669
670struct ctl_table usermodehelper_table[] = {
671 {
672 .procname = "bset",
673 .data = CAP_BSET,
674 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
675 .mode = 0600,
676 .proc_handler = proc_cap_handler,
677 },
678 {
679 .procname = "inheritable",
680 .data = CAP_PI,
681 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
682 .mode = 0600,
683 .proc_handler = proc_cap_handler,
684 },
685 { }
686};
687