1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/sched.h>
23#include <linux/syscalls.h>
24#include <linux/unistd.h>
25#include <linux/kmod.h>
26#include <linux/slab.h>
27#include <linux/completion.h>
28#include <linux/cred.h>
29#include <linux/file.h>
30#include <linux/fdtable.h>
31#include <linux/workqueue.h>
32#include <linux/security.h>
33#include <linux/mount.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/resource.h>
37#include <linux/notifier.h>
38#include <linux/suspend.h>
39#include <asm/uaccess.h>
40
41#include <trace/events/module.h>
42
43extern int max_threads;
44
45static struct workqueue_struct *khelper_wq;
46
47#define CAP_BSET (void *)1
48#define CAP_PI (void *)2
49
50static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
51static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
52static DEFINE_SPINLOCK(umh_sysctl_lock);
53
54#ifdef CONFIG_MODULES
55
56
57
58
59char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76int __request_module(bool wait, const char *fmt, ...)
77{
78 va_list args;
79 char module_name[MODULE_NAME_LEN];
80 unsigned int max_modprobes;
81 int ret;
82 char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
83 static char *envp[] = { "HOME=/",
84 "TERM=linux",
85 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
86 NULL };
87 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
88#define MAX_KMOD_CONCURRENT 50
89 static int kmod_loop_msg;
90
91 va_start(args, fmt);
92 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
93 va_end(args);
94 if (ret >= MODULE_NAME_LEN)
95 return -ENAMETOOLONG;
96
97 ret = security_kernel_module_request(module_name);
98 if (ret)
99 return ret;
100
101
102
103
104
105
106
107
108
109
110
111
112
113 max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
114 atomic_inc(&kmod_concurrent);
115 if (atomic_read(&kmod_concurrent) > max_modprobes) {
116
117 if (kmod_loop_msg < 5) {
118 printk(KERN_ERR
119 "request_module: runaway loop modprobe %s\n",
120 module_name);
121 kmod_loop_msg++;
122 }
123 atomic_dec(&kmod_concurrent);
124 return -ENOMEM;
125 }
126
127 trace_module_request(module_name, wait, _RET_IP_);
128
129 ret = call_usermodehelper_fns(modprobe_path, argv, envp,
130 wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC,
131 NULL, NULL, NULL);
132
133 atomic_dec(&kmod_concurrent);
134 return ret;
135}
136EXPORT_SYMBOL(__request_module);
137#endif
138
139
140
141
142static int ____call_usermodehelper(void *data)
143{
144 struct subprocess_info *sub_info = data;
145 struct cred *new;
146 int retval;
147
148 spin_lock_irq(¤t->sighand->siglock);
149 flush_signal_handlers(current, 1);
150 spin_unlock_irq(¤t->sighand->siglock);
151
152
153 set_cpus_allowed_ptr(current, cpu_all_mask);
154
155
156
157
158
159 set_user_nice(current, 0);
160
161 retval = -ENOMEM;
162 new = prepare_kernel_cred(current);
163 if (!new)
164 goto fail;
165
166 spin_lock(&umh_sysctl_lock);
167 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
168 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
169 new->cap_inheritable);
170 spin_unlock(&umh_sysctl_lock);
171
172 if (sub_info->init) {
173 retval = sub_info->init(sub_info, new);
174 if (retval) {
175 abort_creds(new);
176 goto fail;
177 }
178 }
179
180 commit_creds(new);
181
182 retval = kernel_execve(sub_info->path,
183 (const char *const *)sub_info->argv,
184 (const char *const *)sub_info->envp);
185
186
187fail:
188 sub_info->retval = retval;
189 do_exit(0);
190}
191
192void call_usermodehelper_freeinfo(struct subprocess_info *info)
193{
194 if (info->cleanup)
195 (*info->cleanup)(info);
196 kfree(info);
197}
198EXPORT_SYMBOL(call_usermodehelper_freeinfo);
199
200
201static int wait_for_helper(void *data)
202{
203 struct subprocess_info *sub_info = data;
204 pid_t pid;
205
206
207 spin_lock_irq(¤t->sighand->siglock);
208 current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;
209 spin_unlock_irq(¤t->sighand->siglock);
210
211 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
212 if (pid < 0) {
213 sub_info->retval = pid;
214 } else {
215 int ret = -ECHILD;
216
217
218
219
220
221
222
223
224
225 sys_wait4(pid, (int __user *)&ret, 0, NULL);
226
227
228
229
230
231
232 if (ret)
233 sub_info->retval = ret;
234 }
235
236 complete(sub_info->complete);
237 return 0;
238}
239
240
241static void __call_usermodehelper(struct work_struct *work)
242{
243 struct subprocess_info *sub_info =
244 container_of(work, struct subprocess_info, work);
245 enum umh_wait wait = sub_info->wait;
246 pid_t pid;
247
248
249
250
251 if (wait == UMH_WAIT_PROC)
252 pid = kernel_thread(wait_for_helper, sub_info,
253 CLONE_FS | CLONE_FILES | SIGCHLD);
254 else
255 pid = kernel_thread(____call_usermodehelper, sub_info,
256 CLONE_VFORK | SIGCHLD);
257
258 switch (wait) {
259 case UMH_NO_WAIT:
260 call_usermodehelper_freeinfo(sub_info);
261 break;
262
263 case UMH_WAIT_PROC:
264 if (pid > 0)
265 break;
266
267 case UMH_WAIT_EXEC:
268 if (pid < 0)
269 sub_info->retval = pid;
270 complete(sub_info->complete);
271 }
272}
273
274
275
276
277
278
279static int usermodehelper_disabled = 1;
280
281
282static atomic_t running_helpers = ATOMIC_INIT(0);
283
284
285
286
287
288static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
289
290
291
292
293
294#define RUNNING_HELPERS_TIMEOUT (5 * HZ)
295
296
297
298
299int usermodehelper_disable(void)
300{
301 long retval;
302
303 usermodehelper_disabled = 1;
304 smp_mb();
305
306
307
308
309
310
311 retval = wait_event_timeout(running_helpers_waitq,
312 atomic_read(&running_helpers) == 0,
313 RUNNING_HELPERS_TIMEOUT);
314 if (retval)
315 return 0;
316
317 usermodehelper_disabled = 0;
318 return -EAGAIN;
319}
320
321
322
323
324void usermodehelper_enable(void)
325{
326 usermodehelper_disabled = 0;
327}
328
329
330
331
332bool usermodehelper_is_disabled(void)
333{
334 return usermodehelper_disabled;
335}
336EXPORT_SYMBOL_GPL(usermodehelper_is_disabled);
337
338static void helper_lock(void)
339{
340 atomic_inc(&running_helpers);
341 smp_mb__after_atomic_inc();
342}
343
344static void helper_unlock(void)
345{
346 if (atomic_dec_and_test(&running_helpers))
347 wake_up(&running_helpers_waitq);
348}
349
350
351
352
353
354
355
356
357
358
359
360
361struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
362 char **envp, gfp_t gfp_mask)
363{
364 struct subprocess_info *sub_info;
365 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
366 if (!sub_info)
367 goto out;
368
369 INIT_WORK(&sub_info->work, __call_usermodehelper);
370 sub_info->path = path;
371 sub_info->argv = argv;
372 sub_info->envp = envp;
373 out:
374 return sub_info;
375}
376EXPORT_SYMBOL(call_usermodehelper_setup);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394void call_usermodehelper_setfns(struct subprocess_info *info,
395 int (*init)(struct subprocess_info *info, struct cred *new),
396 void (*cleanup)(struct subprocess_info *info),
397 void *data)
398{
399 info->cleanup = cleanup;
400 info->init = init;
401 info->data = data;
402}
403EXPORT_SYMBOL(call_usermodehelper_setfns);
404
405
406
407
408
409
410
411
412
413
414
415
416
417int call_usermodehelper_exec(struct subprocess_info *sub_info,
418 enum umh_wait wait)
419{
420 DECLARE_COMPLETION_ONSTACK(done);
421 int retval = 0;
422
423 helper_lock();
424 if (sub_info->path[0] == '\0')
425 goto out;
426
427 if (!khelper_wq || usermodehelper_disabled) {
428 retval = -EBUSY;
429 goto out;
430 }
431
432 sub_info->complete = &done;
433 sub_info->wait = wait;
434
435 queue_work(khelper_wq, &sub_info->work);
436 if (wait == UMH_NO_WAIT)
437 goto unlock;
438 wait_for_completion(&done);
439 retval = sub_info->retval;
440
441out:
442 call_usermodehelper_freeinfo(sub_info);
443unlock:
444 helper_unlock();
445 return retval;
446}
447EXPORT_SYMBOL(call_usermodehelper_exec);
448
449static int proc_cap_handler(struct ctl_table *table, int write,
450 void __user *buffer, size_t *lenp, loff_t *ppos)
451{
452 struct ctl_table t;
453 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
454 kernel_cap_t new_cap;
455 int err, i;
456
457 if (write && (!capable(CAP_SETPCAP) ||
458 !capable(CAP_SYS_MODULE)))
459 return -EPERM;
460
461
462
463
464
465 spin_lock(&umh_sysctl_lock);
466 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
467 if (table->data == CAP_BSET)
468 cap_array[i] = usermodehelper_bset.cap[i];
469 else if (table->data == CAP_PI)
470 cap_array[i] = usermodehelper_inheritable.cap[i];
471 else
472 BUG();
473 }
474 spin_unlock(&umh_sysctl_lock);
475
476 t = *table;
477 t.data = &cap_array;
478
479
480
481
482
483 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
484 if (err < 0)
485 return err;
486
487
488
489
490
491 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
492 new_cap.cap[i] = cap_array[i];
493
494
495
496
497 spin_lock(&umh_sysctl_lock);
498 if (write) {
499 if (table->data == CAP_BSET)
500 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
501 if (table->data == CAP_PI)
502 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
503 }
504 spin_unlock(&umh_sysctl_lock);
505
506 return 0;
507}
508
509struct ctl_table usermodehelper_table[] = {
510 {
511 .procname = "bset",
512 .data = CAP_BSET,
513 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
514 .mode = 0600,
515 .proc_handler = proc_cap_handler,
516 },
517 {
518 .procname = "inheritable",
519 .data = CAP_PI,
520 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
521 .mode = 0600,
522 .proc_handler = proc_cap_handler,
523 },
524 { }
525};
526
527void __init usermodehelper_init(void)
528{
529 khelper_wq = create_singlethread_workqueue("khelper");
530 BUG_ON(!khelper_wq);
531}
532