1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define __KERNEL_SYSCALLS__
20
21#include <linux/config.h>
22#include <linux/module.h>
23#include <linux/sched.h>
24#include <linux/unistd.h>
25#include <linux/kmod.h>
26#include <linux/smp_lock.h>
27#include <linux/slab.h>
28#include <linux/namespace.h>
29#include <linux/completion.h>
30#include <linux/file.h>
31#include <linux/tqueue.h>
32
33#include <asm/uaccess.h>
34
35extern int max_threads, system_running;
36
37static inline void
38use_init_fs_context(void)
39{
40 struct fs_struct *our_fs, *init_fs;
41 struct dentry *root, *pwd;
42 struct vfsmount *rootmnt, *pwdmnt;
43 struct namespace *our_ns, *init_ns;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 init_fs = init_task.fs;
63 init_ns = init_task.namespace;
64 get_namespace(init_ns);
65 our_ns = current->namespace;
66 current->namespace = init_ns;
67 put_namespace(our_ns);
68 read_lock(&init_fs->lock);
69 rootmnt = mntget(init_fs->rootmnt);
70 root = dget(init_fs->root);
71 pwdmnt = mntget(init_fs->pwdmnt);
72 pwd = dget(init_fs->pwd);
73 read_unlock(&init_fs->lock);
74
75
76 our_fs = current->fs;
77 our_fs->umask = init_fs->umask;
78 set_fs_root(our_fs, rootmnt, root);
79 set_fs_pwd(our_fs, pwdmnt, pwd);
80 write_lock(&our_fs->lock);
81 if (our_fs->altroot) {
82 struct vfsmount *mnt = our_fs->altrootmnt;
83 struct dentry *dentry = our_fs->altroot;
84 our_fs->altrootmnt = NULL;
85 our_fs->altroot = NULL;
86 write_unlock(&our_fs->lock);
87 dput(dentry);
88 mntput(mnt);
89 } else
90 write_unlock(&our_fs->lock);
91 dput(root);
92 mntput(rootmnt);
93 dput(pwd);
94 mntput(pwdmnt);
95}
96
97int exec_usermodehelper(char *program_path, char *argv[], char *envp[])
98{
99 int i;
100 struct task_struct *curtask = current;
101
102 curtask->session = 1;
103 curtask->pgrp = 1;
104
105 use_init_fs_context();
106
107
108
109
110
111
112
113 spin_lock_irq(&curtask->sigmask_lock);
114 sigemptyset(&curtask->blocked);
115 flush_signals(curtask);
116 flush_signal_handlers(curtask);
117 recalc_sigpending();
118 spin_unlock_irq(&curtask->sigmask_lock);
119
120 for (i = 0; i < curtask->files->max_fds; i++ ) {
121 if (curtask->files->fd[i]) close(i);
122 }
123
124
125 {
126 struct user_struct *user = curtask->user;
127 curtask->user = INIT_USER;
128 atomic_inc(&INIT_USER->__count);
129 atomic_inc(&INIT_USER->processes);
130 atomic_dec(&user->processes);
131 free_uid(user);
132 }
133
134
135 curtask->euid = curtask->fsuid = 0;
136 curtask->egid = curtask->fsgid = 0;
137 security_ops->task_kmod_set_label();
138
139
140 set_fs(KERNEL_DS);
141
142
143 if (execve(program_path, argv, envp) < 0)
144 return -errno;
145 return 0;
146}
147
148#ifdef CONFIG_KMOD
149
150
151
152
153char modprobe_path[256] = "/sbin/modprobe";
154
155static int exec_modprobe(void * module_name)
156{
157 static char * envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
158 char *argv[] = { modprobe_path, "-s", "-k", "--", (char*)module_name, NULL };
159 int ret;
160
161 if (!system_running)
162 return -EBUSY;
163
164 ret = exec_usermodehelper(modprobe_path, argv, envp);
165 if (ret) {
166 static unsigned long last;
167 unsigned long now = jiffies;
168 if (now - last > HZ) {
169 last = now;
170 printk(KERN_DEBUG
171 "kmod: failed to exec %s -s -k %s, errno = %d\n",
172 modprobe_path, (char*) module_name, errno);
173 }
174 }
175 return ret;
176}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191int request_module(const char * module_name)
192{
193 pid_t pid;
194 int waitpid_result;
195 sigset_t tmpsig;
196 int i, ret;
197 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
198#define MAX_KMOD_CONCURRENT 50
199 static int kmod_loop_msg;
200 unsigned long saved_policy = current->policy;
201
202 current->policy = SCHED_NORMAL;
203
204 if ( ! system_running ) {
205 printk(KERN_ERR "request_module[%s]: not ready\n", module_name);
206 ret = -EPERM;
207 goto out;
208 }
209
210
211
212
213
214
215
216
217
218
219 i = max_threads/2;
220 if (i > MAX_KMOD_CONCURRENT)
221 i = MAX_KMOD_CONCURRENT;
222 atomic_inc(&kmod_concurrent);
223 if (atomic_read(&kmod_concurrent) > i) {
224 if (kmod_loop_msg++ < 5)
225 printk(KERN_ERR
226 "kmod: runaway modprobe loop assumed and stopped\n");
227 atomic_dec(&kmod_concurrent);
228 ret = -ENOMEM;
229 goto out;
230 }
231
232 pid = kernel_thread(exec_modprobe, (void*) module_name, 0);
233 if (pid < 0) {
234 printk(KERN_ERR "request_module[%s]: fork failed, errno %d\n", module_name, -pid);
235 atomic_dec(&kmod_concurrent);
236 ret = pid;
237 goto out;
238 }
239
240
241 spin_lock_irq(¤t->sigmask_lock);
242 tmpsig = current->blocked;
243 siginitsetinv(¤t->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP));
244 recalc_sigpending();
245 spin_unlock_irq(¤t->sigmask_lock);
246
247 waitpid_result = waitpid(pid, NULL, __WCLONE);
248 atomic_dec(&kmod_concurrent);
249
250
251 spin_lock_irq(¤t->sigmask_lock);
252 current->blocked = tmpsig;
253 recalc_sigpending();
254 spin_unlock_irq(¤t->sigmask_lock);
255
256 if (waitpid_result != pid) {
257 printk(KERN_ERR "request_module[%s]: waitpid(%d,...) failed, errno %d\n",
258 module_name, pid, -waitpid_result);
259 }
260 ret = 0;
261out:
262 current->policy = saved_policy;
263 return ret;
264}
265#endif
266
267
268#ifdef CONFIG_HOTPLUG
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285char hotplug_path[256] = "/sbin/hotplug";
286
287EXPORT_SYMBOL(hotplug_path);
288
289#endif
290
291struct subprocess_info {
292 struct completion *complete;
293 char *path;
294 char **argv;
295 char **envp;
296 pid_t retval;
297};
298
299
300
301
302static int ____call_usermodehelper(void *data)
303{
304 struct subprocess_info *sub_info = data;
305 int retval;
306
307 retval = -EPERM;
308 if (current->fs->root)
309 retval = exec_usermodehelper(sub_info->path, sub_info->argv, sub_info->envp);
310
311
312 sub_info->retval = (pid_t)retval;
313 do_exit(0);
314}
315
316
317
318
319static void __call_usermodehelper(void *data)
320{
321 struct subprocess_info *sub_info = data;
322 pid_t pid;
323
324
325
326
327
328 pid = kernel_thread(____call_usermodehelper, sub_info, CLONE_VFORK | SIGCHLD);
329 if (pid < 0)
330 sub_info->retval = pid;
331 complete(sub_info->complete);
332}
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347int call_usermodehelper(char *path, char **argv, char **envp)
348{
349 DECLARE_COMPLETION(work);
350 struct subprocess_info sub_info = {
351 .complete = &work,
352 .path = path,
353 .argv = argv,
354 .envp = envp,
355 .retval = 0,
356 };
357 struct tq_struct tqs = {
358 .routine = __call_usermodehelper,
359 .data = &sub_info,
360 };
361
362 if (!system_running)
363 return -EBUSY;
364
365 if (path[0] == '\0')
366 goto out;
367
368 if (current_is_keventd()) {
369
370 __call_usermodehelper(&sub_info);
371 } else {
372 schedule_task(&tqs);
373 wait_for_completion(&work);
374 }
375out:
376 return sub_info.retval;
377}
378
379
380
381
382
383static DECLARE_MUTEX(dev_probe_sem);
384
385void dev_probe_lock(void)
386{
387 down(&dev_probe_sem);
388}
389
390void dev_probe_unlock(void)
391{
392 up(&dev_probe_sem);
393}
394
395EXPORT_SYMBOL(exec_usermodehelper);
396EXPORT_SYMBOL(call_usermodehelper);
397
398#ifdef CONFIG_KMOD
399EXPORT_SYMBOL(request_module);
400#endif
401
402