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/mnt_namespace.h>
28#include <linux/completion.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
41extern int max_threads;
42
43static struct workqueue_struct *khelper_wq;
44
45#ifdef CONFIG_MODULES
46
47
48
49
50char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67int __request_module(bool wait, const char *fmt, ...)
68{
69 va_list args;
70 char module_name[MODULE_NAME_LEN];
71 unsigned int max_modprobes;
72 int ret;
73 char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
74 static char *envp[] = { "HOME=/",
75 "TERM=linux",
76 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
77 NULL };
78 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
79#define MAX_KMOD_CONCURRENT 50
80 static int kmod_loop_msg;
81
82 va_start(args, fmt);
83 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
84 va_end(args);
85 if (ret >= MODULE_NAME_LEN)
86 return -ENAMETOOLONG;
87
88
89
90
91
92
93
94
95
96
97
98
99
100 max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
101 atomic_inc(&kmod_concurrent);
102 if (atomic_read(&kmod_concurrent) > max_modprobes) {
103
104 if (kmod_loop_msg++ < 5)
105 printk(KERN_ERR
106 "request_module: runaway loop modprobe %s\n",
107 module_name);
108 atomic_dec(&kmod_concurrent);
109 return -ENOMEM;
110 }
111
112 ret = call_usermodehelper(modprobe_path, argv, envp,
113 wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
114 atomic_dec(&kmod_concurrent);
115 return ret;
116}
117EXPORT_SYMBOL(__request_module);
118#endif
119
120struct subprocess_info {
121 struct work_struct work;
122 struct completion *complete;
123 struct cred *cred;
124 char *path;
125 char **argv;
126 char **envp;
127 enum umh_wait wait;
128 int retval;
129 struct file *stdin;
130 void (*cleanup)(char **argv, char **envp);
131};
132
133
134
135
136static int ____call_usermodehelper(void *data)
137{
138 struct subprocess_info *sub_info = data;
139 int retval;
140
141 BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
142
143
144 spin_lock_irq(¤t->sighand->siglock);
145 flush_signal_handlers(current, 1);
146 sigemptyset(¤t->blocked);
147 recalc_sigpending();
148 spin_unlock_irq(¤t->sighand->siglock);
149
150
151 commit_creds(sub_info->cred);
152 sub_info->cred = NULL;
153
154
155 if (sub_info->stdin) {
156 struct files_struct *f = current->files;
157 struct fdtable *fdt;
158
159 sys_close(0);
160 fd_install(0, sub_info->stdin);
161 spin_lock(&f->file_lock);
162 fdt = files_fdtable(f);
163 FD_SET(0, fdt->open_fds);
164 FD_CLR(0, fdt->close_on_exec);
165 spin_unlock(&f->file_lock);
166
167
168 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
169 }
170
171
172 set_cpus_allowed_ptr(current, cpu_all_mask);
173
174
175
176
177
178 set_user_nice(current, 0);
179
180 retval = kernel_execve(sub_info->path, sub_info->argv, sub_info->envp);
181
182
183 sub_info->retval = retval;
184 do_exit(0);
185}
186
187void call_usermodehelper_freeinfo(struct subprocess_info *info)
188{
189 if (info->cleanup)
190 (*info->cleanup)(info->argv, info->envp);
191 if (info->cred)
192 put_cred(info->cred);
193 kfree(info);
194}
195EXPORT_SYMBOL(call_usermodehelper_freeinfo);
196
197
198static int wait_for_helper(void *data)
199{
200 struct subprocess_info *sub_info = data;
201 pid_t pid;
202
203
204
205 allow_signal(SIGCHLD);
206
207 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
208 if (pid < 0) {
209 sub_info->retval = pid;
210 } else {
211 int ret;
212
213
214
215
216
217
218
219
220
221
222 sys_wait4(pid, (int __user *)&ret, 0, NULL);
223
224
225
226
227
228
229 if (ret)
230 sub_info->retval = ret;
231 }
232
233 if (sub_info->wait == UMH_NO_WAIT)
234 call_usermodehelper_freeinfo(sub_info);
235 else
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 pid_t pid;
246 enum umh_wait wait = sub_info->wait;
247
248 BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
249
250
251
252
253 if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT)
254 pid = kernel_thread(wait_for_helper, sub_info,
255 CLONE_FS | CLONE_FILES | SIGCHLD);
256 else
257 pid = kernel_thread(____call_usermodehelper, sub_info,
258 CLONE_VFORK | SIGCHLD);
259
260 switch (wait) {
261 case UMH_NO_WAIT:
262 break;
263
264 case UMH_WAIT_PROC:
265 if (pid > 0)
266 break;
267 sub_info->retval = pid;
268
269
270 case UMH_WAIT_EXEC:
271 complete(sub_info->complete);
272 }
273}
274
275#ifdef CONFIG_PM_SLEEP
276
277
278
279
280
281static int usermodehelper_disabled;
282
283
284static atomic_t running_helpers = ATOMIC_INIT(0);
285
286
287
288
289
290static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
291
292
293
294
295
296#define RUNNING_HELPERS_TIMEOUT (5 * HZ)
297
298
299
300
301int usermodehelper_disable(void)
302{
303 long retval;
304
305 usermodehelper_disabled = 1;
306 smp_mb();
307
308
309
310
311
312
313 retval = wait_event_timeout(running_helpers_waitq,
314 atomic_read(&running_helpers) == 0,
315 RUNNING_HELPERS_TIMEOUT);
316 if (retval)
317 return 0;
318
319 usermodehelper_disabled = 0;
320 return -EAGAIN;
321}
322
323
324
325
326void usermodehelper_enable(void)
327{
328 usermodehelper_disabled = 0;
329}
330
331static void helper_lock(void)
332{
333 atomic_inc(&running_helpers);
334 smp_mb__after_atomic_inc();
335}
336
337static void helper_unlock(void)
338{
339 if (atomic_dec_and_test(&running_helpers))
340 wake_up(&running_helpers_waitq);
341}
342#else
343#define usermodehelper_disabled 0
344
345static inline void helper_lock(void) {}
346static inline void helper_unlock(void) {}
347#endif
348
349
350
351
352
353
354
355
356
357
358
359
360struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
361 char **envp, gfp_t gfp_mask)
362{
363 struct subprocess_info *sub_info;
364 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
365 if (!sub_info)
366 goto out;
367
368 INIT_WORK(&sub_info->work, __call_usermodehelper);
369 sub_info->path = path;
370 sub_info->argv = argv;
371 sub_info->envp = envp;
372 sub_info->cred = prepare_usermodehelper_creds();
373 if (!sub_info->cred) {
374 kfree(sub_info);
375 return NULL;
376 }
377
378 out:
379 return sub_info;
380}
381EXPORT_SYMBOL(call_usermodehelper_setup);
382
383
384
385
386
387
388void call_usermodehelper_setkeys(struct subprocess_info *info,
389 struct key *session_keyring)
390{
391#ifdef CONFIG_KEYS
392 struct thread_group_cred *tgcred = info->cred->tgcred;
393 key_put(tgcred->session_keyring);
394 tgcred->session_keyring = key_get(session_keyring);
395#else
396 BUG();
397#endif
398}
399EXPORT_SYMBOL(call_usermodehelper_setkeys);
400
401
402
403
404
405
406
407
408
409
410
411void call_usermodehelper_setcleanup(struct subprocess_info *info,
412 void (*cleanup)(char **argv, char **envp))
413{
414 info->cleanup = cleanup;
415}
416EXPORT_SYMBOL(call_usermodehelper_setcleanup);
417
418
419
420
421
422
423
424
425
426int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info,
427 struct file **filp)
428{
429 struct file *f;
430
431 f = create_write_pipe(0);
432 if (IS_ERR(f))
433 return PTR_ERR(f);
434 *filp = f;
435
436 f = create_read_pipe(f, 0);
437 if (IS_ERR(f)) {
438 free_write_pipe(*filp);
439 return PTR_ERR(f);
440 }
441 sub_info->stdin = f;
442
443 return 0;
444}
445EXPORT_SYMBOL(call_usermodehelper_stdinpipe);
446
447
448
449
450
451
452
453
454
455
456
457
458
459int call_usermodehelper_exec(struct subprocess_info *sub_info,
460 enum umh_wait wait)
461{
462 DECLARE_COMPLETION_ONSTACK(done);
463 int retval = 0;
464
465 BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
466
467 helper_lock();
468 if (sub_info->path[0] == '\0')
469 goto out;
470
471 if (!khelper_wq || usermodehelper_disabled) {
472 retval = -EBUSY;
473 goto out;
474 }
475
476 sub_info->complete = &done;
477 sub_info->wait = wait;
478
479 queue_work(khelper_wq, &sub_info->work);
480 if (wait == UMH_NO_WAIT)
481 goto unlock;
482 wait_for_completion(&done);
483 retval = sub_info->retval;
484
485out:
486 call_usermodehelper_freeinfo(sub_info);
487unlock:
488 helper_unlock();
489 return retval;
490}
491EXPORT_SYMBOL(call_usermodehelper_exec);
492
493
494
495
496
497
498
499
500
501
502
503
504int call_usermodehelper_pipe(char *path, char **argv, char **envp,
505 struct file **filp)
506{
507 struct subprocess_info *sub_info;
508 int ret;
509
510 sub_info = call_usermodehelper_setup(path, argv, envp, GFP_KERNEL);
511 if (sub_info == NULL)
512 return -ENOMEM;
513
514 ret = call_usermodehelper_stdinpipe(sub_info, filp);
515 if (ret < 0)
516 goto out;
517
518 return call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
519
520 out:
521 call_usermodehelper_freeinfo(sub_info);
522 return ret;
523}
524EXPORT_SYMBOL(call_usermodehelper_pipe);
525
526void __init usermodehelper_init(void)
527{
528 khelper_wq = create_singlethread_workqueue("khelper");
529 BUG_ON(!khelper_wq);
530}
531