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