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
29#include <trace/events/module.h>
30
31#define CAP_BSET (void *)1
32#define CAP_PI (void *)2
33
34static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
35static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
36static DEFINE_SPINLOCK(umh_sysctl_lock);
37static DECLARE_RWSEM(umhelper_sem);
38
39static void call_usermodehelper_freeinfo(struct subprocess_info *info)
40{
41 if (info->cleanup)
42 (*info->cleanup)(info);
43 kfree(info);
44}
45
46static void umh_complete(struct subprocess_info *sub_info)
47{
48 struct completion *comp = xchg(&sub_info->complete, NULL);
49
50
51
52
53
54 if (comp)
55 complete(comp);
56 else
57 call_usermodehelper_freeinfo(sub_info);
58}
59
60
61
62
63static int call_usermodehelper_exec_async(void *data)
64{
65 struct subprocess_info *sub_info = data;
66 struct cred *new;
67 int retval;
68
69 spin_lock_irq(¤t->sighand->siglock);
70 flush_signal_handlers(current, 1);
71 spin_unlock_irq(¤t->sighand->siglock);
72
73
74
75
76
77 set_user_nice(current, 0);
78
79 retval = -ENOMEM;
80 new = prepare_kernel_cred(current);
81 if (!new)
82 goto out;
83
84 spin_lock(&umh_sysctl_lock);
85 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
86 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
87 new->cap_inheritable);
88 spin_unlock(&umh_sysctl_lock);
89
90 if (sub_info->init) {
91 retval = sub_info->init(sub_info, new);
92 if (retval) {
93 abort_creds(new);
94 goto out;
95 }
96 }
97
98 commit_creds(new);
99
100 retval = do_execve(getname_kernel(sub_info->path),
101 (const char __user *const __user *)sub_info->argv,
102 (const char __user *const __user *)sub_info->envp);
103out:
104 sub_info->retval = retval;
105
106
107
108
109 if (!(sub_info->wait & UMH_WAIT_PROC))
110 umh_complete(sub_info);
111 if (!retval)
112 return 0;
113 do_exit(0);
114}
115
116
117static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
118{
119 pid_t pid;
120
121
122 kernel_sigaction(SIGCHLD, SIG_DFL);
123 pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
124 if (pid < 0) {
125 sub_info->retval = pid;
126 } else {
127 int ret = -ECHILD;
128
129
130
131
132
133
134
135
136
137
138 kernel_wait4(pid, (int __user *)&ret, 0, NULL);
139
140
141
142
143
144
145 if (ret)
146 sub_info->retval = ret;
147 }
148
149
150 kernel_sigaction(SIGCHLD, SIG_IGN);
151
152 umh_complete(sub_info);
153}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169static void call_usermodehelper_exec_work(struct work_struct *work)
170{
171 struct subprocess_info *sub_info =
172 container_of(work, struct subprocess_info, work);
173
174 if (sub_info->wait & UMH_WAIT_PROC) {
175 call_usermodehelper_exec_sync(sub_info);
176 } else {
177 pid_t pid;
178
179
180
181
182
183 pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
184 CLONE_PARENT | SIGCHLD);
185 if (pid < 0) {
186 sub_info->retval = pid;
187 umh_complete(sub_info);
188 }
189 }
190}
191
192
193
194
195
196
197
198static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
199
200
201static atomic_t running_helpers = ATOMIC_INIT(0);
202
203
204
205
206
207static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
208
209
210
211
212
213static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
214
215
216
217
218
219#define RUNNING_HELPERS_TIMEOUT (5 * HZ)
220
221int usermodehelper_read_trylock(void)
222{
223 DEFINE_WAIT(wait);
224 int ret = 0;
225
226 down_read(&umhelper_sem);
227 for (;;) {
228 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
229 TASK_INTERRUPTIBLE);
230 if (!usermodehelper_disabled)
231 break;
232
233 if (usermodehelper_disabled == UMH_DISABLED)
234 ret = -EAGAIN;
235
236 up_read(&umhelper_sem);
237
238 if (ret)
239 break;
240
241 schedule();
242 try_to_freeze();
243
244 down_read(&umhelper_sem);
245 }
246 finish_wait(&usermodehelper_disabled_waitq, &wait);
247 return ret;
248}
249EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
250
251long usermodehelper_read_lock_wait(long timeout)
252{
253 DEFINE_WAIT(wait);
254
255 if (timeout < 0)
256 return -EINVAL;
257
258 down_read(&umhelper_sem);
259 for (;;) {
260 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
261 TASK_UNINTERRUPTIBLE);
262 if (!usermodehelper_disabled)
263 break;
264
265 up_read(&umhelper_sem);
266
267 timeout = schedule_timeout(timeout);
268 if (!timeout)
269 break;
270
271 down_read(&umhelper_sem);
272 }
273 finish_wait(&usermodehelper_disabled_waitq, &wait);
274 return timeout;
275}
276EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
277
278void usermodehelper_read_unlock(void)
279{
280 up_read(&umhelper_sem);
281}
282EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
283
284
285
286
287
288
289
290
291void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
292{
293 down_write(&umhelper_sem);
294 usermodehelper_disabled = depth;
295 wake_up(&usermodehelper_disabled_waitq);
296 up_write(&umhelper_sem);
297}
298
299
300
301
302
303
304
305int __usermodehelper_disable(enum umh_disable_depth depth)
306{
307 long retval;
308
309 if (!depth)
310 return -EINVAL;
311
312 down_write(&umhelper_sem);
313 usermodehelper_disabled = depth;
314 up_write(&umhelper_sem);
315
316
317
318
319
320
321
322 retval = wait_event_timeout(running_helpers_waitq,
323 atomic_read(&running_helpers) == 0,
324 RUNNING_HELPERS_TIMEOUT);
325 if (retval)
326 return 0;
327
328 __usermodehelper_set_disable_depth(UMH_ENABLED);
329 return -EAGAIN;
330}
331
332static void helper_lock(void)
333{
334 atomic_inc(&running_helpers);
335 smp_mb__after_atomic();
336}
337
338static void helper_unlock(void)
339{
340 if (atomic_dec_and_test(&running_helpers))
341 wake_up(&running_helpers_waitq);
342}
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
368 char **envp, gfp_t gfp_mask,
369 int (*init)(struct subprocess_info *info, struct cred *new),
370 void (*cleanup)(struct subprocess_info *info),
371 void *data)
372{
373 struct subprocess_info *sub_info;
374 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
375 if (!sub_info)
376 goto out;
377
378 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
379
380#ifdef CONFIG_STATIC_USERMODEHELPER
381 sub_info->path = CONFIG_STATIC_USERMODEHELPER_PATH;
382#else
383 sub_info->path = path;
384#endif
385 sub_info->argv = argv;
386 sub_info->envp = envp;
387
388 sub_info->cleanup = cleanup;
389 sub_info->init = init;
390 sub_info->data = data;
391 out:
392 return sub_info;
393}
394EXPORT_SYMBOL(call_usermodehelper_setup);
395
396
397
398
399
400
401
402
403
404
405
406
407
408int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
409{
410 DECLARE_COMPLETION_ONSTACK(done);
411 int retval = 0;
412
413 if (!sub_info->path) {
414 call_usermodehelper_freeinfo(sub_info);
415 return -EINVAL;
416 }
417 helper_lock();
418 if (usermodehelper_disabled) {
419 retval = -EBUSY;
420 goto out;
421 }
422
423
424
425
426
427
428 if (strlen(sub_info->path) == 0)
429 goto out;
430
431
432
433
434
435
436 sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
437 sub_info->wait = wait;
438
439 queue_work(system_unbound_wq, &sub_info->work);
440 if (wait == UMH_NO_WAIT)
441 goto unlock;
442
443 if (wait & UMH_KILLABLE) {
444 retval = wait_for_completion_killable(&done);
445 if (!retval)
446 goto wait_done;
447
448
449 if (xchg(&sub_info->complete, NULL))
450 goto unlock;
451
452 }
453
454 wait_for_completion(&done);
455wait_done:
456 retval = sub_info->retval;
457out:
458 call_usermodehelper_freeinfo(sub_info);
459unlock:
460 helper_unlock();
461 return retval;
462}
463EXPORT_SYMBOL(call_usermodehelper_exec);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
479{
480 struct subprocess_info *info;
481 gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
482
483 info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
484 NULL, NULL, NULL);
485 if (info == NULL)
486 return -ENOMEM;
487
488 return call_usermodehelper_exec(info, wait);
489}
490EXPORT_SYMBOL(call_usermodehelper);
491
492static int proc_cap_handler(struct ctl_table *table, int write,
493 void __user *buffer, size_t *lenp, loff_t *ppos)
494{
495 struct ctl_table t;
496 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
497 kernel_cap_t new_cap;
498 int err, i;
499
500 if (write && (!capable(CAP_SETPCAP) ||
501 !capable(CAP_SYS_MODULE)))
502 return -EPERM;
503
504
505
506
507
508 spin_lock(&umh_sysctl_lock);
509 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
510 if (table->data == CAP_BSET)
511 cap_array[i] = usermodehelper_bset.cap[i];
512 else if (table->data == CAP_PI)
513 cap_array[i] = usermodehelper_inheritable.cap[i];
514 else
515 BUG();
516 }
517 spin_unlock(&umh_sysctl_lock);
518
519 t = *table;
520 t.data = &cap_array;
521
522
523
524
525
526 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
527 if (err < 0)
528 return err;
529
530
531
532
533
534 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
535 new_cap.cap[i] = cap_array[i];
536
537
538
539
540 if (write) {
541 spin_lock(&umh_sysctl_lock);
542 if (table->data == CAP_BSET)
543 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
544 if (table->data == CAP_PI)
545 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
546 spin_unlock(&umh_sysctl_lock);
547 }
548
549 return 0;
550}
551
552struct ctl_table usermodehelper_table[] = {
553 {
554 .procname = "bset",
555 .data = CAP_BSET,
556 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
557 .mode = 0600,
558 .proc_handler = proc_cap_handler,
559 },
560 {
561 .procname = "inheritable",
562 .data = CAP_PI,
563 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
564 .mode = 0600,
565 .proc_handler = proc_cap_handler,
566 },
567 { }
568};
569