kmod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. kmod, the new module loader (replaces kerneld)
  3. Kirk Petersen
  4. Reorganized not to be a daemon by Adam Richter, with guidance
  5. from Greg Zornetzer.
  6. Modified to avoid chroot and file sharing problems.
  7. Mikael Pettersson
  8. Limit the concurrent number of kmod modprobes to catch loops from
  9. "modprobe needs a service that is in a module".
  10. Keith Owens <kaos@ocs.com.au> December 1999
  11. Unblock all signals when we exec a usermode process.
  12. Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
  13. call_usermodehelper wait flag, and remove exec_usermodehelper.
  14. Rusty Russell <rusty@rustcorp.com.au> Jan 2003
  15. */
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/unistd.h>
  20. #include <linux/kmod.h>
  21. #include <linux/slab.h>
  22. #include <linux/completion.h>
  23. #include <linux/cred.h>
  24. #include <linux/file.h>
  25. #include <linux/fdtable.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/security.h>
  28. #include <linux/mount.h>
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/resource.h>
  32. #include <linux/notifier.h>
  33. #include <linux/suspend.h>
  34. #include <asm/uaccess.h>
  35. #include <trace/events/module.h>
  36. extern int max_threads;
  37. static struct workqueue_struct *khelper_wq;
  38. #define CAP_BSET (void *)1
  39. #define CAP_PI (void *)2
  40. static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
  41. static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
  42. static DEFINE_SPINLOCK(umh_sysctl_lock);
  43. #ifdef CONFIG_MODULES
  44. /*
  45. modprobe_path is set via /proc/sys.
  46. */
  47. char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
  48. /**
  49. * __request_module - try to load a kernel module
  50. * @wait: wait (or not) for the operation to complete
  51. * @fmt: printf style format string for the name of the module
  52. * @...: arguments as specified in the format string
  53. *
  54. * Load a module using the user mode module loader. The function returns
  55. * zero on success or a negative errno code on failure. Note that a
  56. * successful module load does not mean the module did not then unload
  57. * and exit on an error of its own. Callers must check that the service
  58. * they requested is now available not blindly invoke it.
  59. *
  60. * If module auto-loading support is disabled then this function
  61. * becomes a no-operation.
  62. */
  63. int __request_module(bool wait, const char *fmt, ...)
  64. {
  65. va_list args;
  66. char module_name[MODULE_NAME_LEN];
  67. unsigned int max_modprobes;
  68. int ret;
  69. char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
  70. static char *envp[] = { "HOME=/",
  71. "TERM=linux",
  72. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  73. NULL };
  74. static atomic_t kmod_concurrent = ATOMIC_INIT(0);
  75. #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
  76. static int kmod_loop_msg;
  77. va_start(args, fmt);
  78. ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
  79. va_end(args);
  80. if (ret >= MODULE_NAME_LEN)
  81. return -ENAMETOOLONG;
  82. ret = security_kernel_module_request(module_name);
  83. if (ret)
  84. return ret;
  85. /* If modprobe needs a service that is in a module, we get a recursive
  86. * loop. Limit the number of running kmod threads to max_threads/2 or
  87. * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
  88. * would be to run the parents of this process, counting how many times
  89. * kmod was invoked. That would mean accessing the internals of the
  90. * process tables to get the command line, proc_pid_cmdline is static
  91. * and it is not worth changing the proc code just to handle this case.
  92. * KAO.
  93. *
  94. * "trace the ppid" is simple, but will fail if someone's
  95. * parent exits. I think this is as good as it gets. --RR
  96. */
  97. max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
  98. atomic_inc(&kmod_concurrent);
  99. if (atomic_read(&kmod_concurrent) > max_modprobes) {
  100. /* We may be blaming an innocent here, but unlikely */
  101. if (kmod_loop_msg++ < 5)
  102. printk(KERN_ERR
  103. "request_module: runaway loop modprobe %s\n",
  104. module_name);
  105. atomic_dec(&kmod_concurrent);
  106. return -ENOMEM;
  107. }
  108. trace_module_request(module_name, wait, _RET_IP_);
  109. ret = call_usermodehelper_fns(modprobe_path, argv, envp,
  110. wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC,
  111. NULL, NULL, NULL);
  112. atomic_dec(&kmod_concurrent);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL(__request_module);
  116. #endif /* CONFIG_MODULES */
  117. /*
  118. * This is the task which runs the usermode application
  119. */
  120. static int ____call_usermodehelper(void *data)
  121. {
  122. struct subprocess_info *sub_info = data;
  123. struct cred *new;
  124. int retval;
  125. spin_lock_irq(&current->sighand->siglock);
  126. flush_signal_handlers(current, 1);
  127. spin_unlock_irq(&current->sighand->siglock);
  128. /* We can run anywhere, unlike our parent keventd(). */
  129. set_cpus_allowed_ptr(current, cpu_all_mask);
  130. /*
  131. * Our parent is keventd, which runs with elevated scheduling priority.
  132. * Avoid propagating that into the userspace child.
  133. */
  134. set_user_nice(current, 0);
  135. retval = -ENOMEM;
  136. new = prepare_kernel_cred(current);
  137. if (!new)
  138. goto fail;
  139. spin_lock(&umh_sysctl_lock);
  140. new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
  141. new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
  142. new->cap_inheritable);
  143. spin_unlock(&umh_sysctl_lock);
  144. if (sub_info->init) {
  145. retval = sub_info->init(sub_info, new);
  146. if (retval) {
  147. abort_creds(new);
  148. goto fail;
  149. }
  150. }
  151. commit_creds(new);
  152. retval = kernel_execve(sub_info->path,
  153. (const char *const *)sub_info->argv,
  154. (const char *const *)sub_info->envp);
  155. /* Exec failed? */
  156. fail:
  157. sub_info->retval = retval;
  158. do_exit(0);
  159. }
  160. void call_usermodehelper_freeinfo(struct subprocess_info *info)
  161. {
  162. if (info->cleanup)
  163. (*info->cleanup)(info);
  164. kfree(info);
  165. }
  166. EXPORT_SYMBOL(call_usermodehelper_freeinfo);
  167. /* Keventd can't block, but this (a child) can. */
  168. static int wait_for_helper(void *data)
  169. {
  170. struct subprocess_info *sub_info = data;
  171. pid_t pid;
  172. /* If SIGCLD is ignored sys_wait4 won't populate the status. */
  173. spin_lock_irq(&current->sighand->siglock);
  174. current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;
  175. spin_unlock_irq(&current->sighand->siglock);
  176. pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
  177. if (pid < 0) {
  178. sub_info->retval = pid;
  179. } else {
  180. int ret = -ECHILD;
  181. /*
  182. * Normally it is bogus to call wait4() from in-kernel because
  183. * wait4() wants to write the exit code to a userspace address.
  184. * But wait_for_helper() always runs as keventd, and put_user()
  185. * to a kernel address works OK for kernel threads, due to their
  186. * having an mm_segment_t which spans the entire address space.
  187. *
  188. * Thus the __user pointer cast is valid here.
  189. */
  190. sys_wait4(pid, (int __user *)&ret, 0, NULL);
  191. /*
  192. * If ret is 0, either ____call_usermodehelper failed and the
  193. * real error code is already in sub_info->retval or
  194. * sub_info->retval is 0 anyway, so don't mess with it then.
  195. */
  196. if (ret)
  197. sub_info->retval = ret;
  198. }
  199. complete(sub_info->complete);
  200. return 0;
  201. }
  202. /* This is run by khelper thread */
  203. static void __call_usermodehelper(struct work_struct *work)
  204. {
  205. struct subprocess_info *sub_info =
  206. container_of(work, struct subprocess_info, work);
  207. enum umh_wait wait = sub_info->wait;
  208. pid_t pid;
  209. /* CLONE_VFORK: wait until the usermode helper has execve'd
  210. * successfully We need the data structures to stay around
  211. * until that is done. */
  212. if (wait == UMH_WAIT_PROC)
  213. pid = kernel_thread(wait_for_helper, sub_info,
  214. CLONE_FS | CLONE_FILES | SIGCHLD);
  215. else
  216. pid = kernel_thread(____call_usermodehelper, sub_info,
  217. CLONE_VFORK | SIGCHLD);
  218. switch (wait) {
  219. case UMH_NO_WAIT:
  220. call_usermodehelper_freeinfo(sub_info);
  221. break;
  222. case UMH_WAIT_PROC:
  223. if (pid > 0)
  224. break;
  225. /* FALLTHROUGH */
  226. case UMH_WAIT_EXEC:
  227. if (pid < 0)
  228. sub_info->retval = pid;
  229. complete(sub_info->complete);
  230. }
  231. }
  232. /*
  233. * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
  234. * (used for preventing user land processes from being created after the user
  235. * land has been frozen during a system-wide hibernation or suspend operation).
  236. */
  237. static int usermodehelper_disabled = 1;
  238. /* Number of helpers running */
  239. static atomic_t running_helpers = ATOMIC_INIT(0);
  240. /*
  241. * Wait queue head used by usermodehelper_pm_callback() to wait for all running
  242. * helpers to finish.
  243. */
  244. static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
  245. /*
  246. * Time to wait for running_helpers to become zero before the setting of
  247. * usermodehelper_disabled in usermodehelper_pm_callback() fails
  248. */
  249. #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
  250. /**
  251. * usermodehelper_disable - prevent new helpers from being started
  252. */
  253. int usermodehelper_disable(void)
  254. {
  255. long retval;
  256. usermodehelper_disabled = 1;
  257. smp_mb();
  258. /*
  259. * From now on call_usermodehelper_exec() won't start any new
  260. * helpers, so it is sufficient if running_helpers turns out to
  261. * be zero at one point (it may be increased later, but that
  262. * doesn't matter).
  263. */
  264. retval = wait_event_timeout(running_helpers_waitq,
  265. atomic_read(&running_helpers) == 0,
  266. RUNNING_HELPERS_TIMEOUT);
  267. if (retval)
  268. return 0;
  269. usermodehelper_disabled = 0;
  270. return -EAGAIN;
  271. }
  272. /**
  273. * usermodehelper_enable - allow new helpers to be started again
  274. */
  275. void usermodehelper_enable(void)
  276. {
  277. usermodehelper_disabled = 0;
  278. }
  279. /**
  280. * usermodehelper_is_disabled - check if new helpers are allowed to be started
  281. */
  282. bool usermodehelper_is_disabled(void)
  283. {
  284. return usermodehelper_disabled;
  285. }
  286. EXPORT_SYMBOL_GPL(usermodehelper_is_disabled);
  287. static void helper_lock(void)
  288. {
  289. atomic_inc(&running_helpers);
  290. smp_mb__after_atomic_inc();
  291. }
  292. static void helper_unlock(void)
  293. {
  294. if (atomic_dec_and_test(&running_helpers))
  295. wake_up(&running_helpers_waitq);
  296. }
  297. /**
  298. * call_usermodehelper_setup - prepare to call a usermode helper
  299. * @path: path to usermode executable
  300. * @argv: arg vector for process
  301. * @envp: environment for process
  302. * @gfp_mask: gfp mask for memory allocation
  303. *
  304. * Returns either %NULL on allocation failure, or a subprocess_info
  305. * structure. This should be passed to call_usermodehelper_exec to
  306. * exec the process and free the structure.
  307. */
  308. struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
  309. char **envp, gfp_t gfp_mask)
  310. {
  311. struct subprocess_info *sub_info;
  312. sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
  313. if (!sub_info)
  314. goto out;
  315. INIT_WORK(&sub_info->work, __call_usermodehelper);
  316. sub_info->path = path;
  317. sub_info->argv = argv;
  318. sub_info->envp = envp;
  319. out:
  320. return sub_info;
  321. }
  322. EXPORT_SYMBOL(call_usermodehelper_setup);
  323. /**
  324. * call_usermodehelper_setfns - set a cleanup/init function
  325. * @info: a subprocess_info returned by call_usermodehelper_setup
  326. * @cleanup: a cleanup function
  327. * @init: an init function
  328. * @data: arbitrary context sensitive data
  329. *
  330. * The init function is used to customize the helper process prior to
  331. * exec. A non-zero return code causes the process to error out, exit,
  332. * and return the failure to the calling process
  333. *
  334. * The cleanup function is just before ethe subprocess_info is about to
  335. * be freed. This can be used for freeing the argv and envp. The
  336. * Function must be runnable in either a process context or the
  337. * context in which call_usermodehelper_exec is called.
  338. */
  339. void call_usermodehelper_setfns(struct subprocess_info *info,
  340. int (*init)(struct subprocess_info *info, struct cred *new),
  341. void (*cleanup)(struct subprocess_info *info),
  342. void *data)
  343. {
  344. info->cleanup = cleanup;
  345. info->init = init;
  346. info->data = data;
  347. }
  348. EXPORT_SYMBOL(call_usermodehelper_setfns);
  349. /**
  350. * call_usermodehelper_exec - start a usermode application
  351. * @sub_info: information about the subprocessa
  352. * @wait: wait for the application to finish and return status.
  353. * when -1 don't wait at all, but you get no useful error back when
  354. * the program couldn't be exec'ed. This makes it safe to call
  355. * from interrupt context.
  356. *
  357. * Runs a user-space application. The application is started
  358. * asynchronously if wait is not set, and runs as a child of keventd.
  359. * (ie. it runs with full root capabilities).
  360. */
  361. int call_usermodehelper_exec(struct subprocess_info *sub_info,
  362. enum umh_wait wait)
  363. {
  364. DECLARE_COMPLETION_ONSTACK(done);
  365. int retval = 0;
  366. helper_lock();
  367. if (sub_info->path[0] == '\0')
  368. goto out;
  369. if (!khelper_wq || usermodehelper_disabled) {
  370. retval = -EBUSY;
  371. goto out;
  372. }
  373. sub_info->complete = &done;
  374. sub_info->wait = wait;
  375. queue_work(khelper_wq, &sub_info->work);
  376. if (wait == UMH_NO_WAIT) /* task has freed sub_info */
  377. goto unlock;
  378. wait_for_completion(&done);
  379. retval = sub_info->retval;
  380. out:
  381. call_usermodehelper_freeinfo(sub_info);
  382. unlock:
  383. helper_unlock();
  384. return retval;
  385. }
  386. EXPORT_SYMBOL(call_usermodehelper_exec);
  387. static int proc_cap_handler(struct ctl_table *table, int write,
  388. void __user *buffer, size_t *lenp, loff_t *ppos)
  389. {
  390. struct ctl_table t;
  391. unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
  392. kernel_cap_t new_cap;
  393. int err, i;
  394. if (write && (!capable(CAP_SETPCAP) ||
  395. !capable(CAP_SYS_MODULE)))
  396. return -EPERM;
  397. /*
  398. * convert from the global kernel_cap_t to the ulong array to print to
  399. * userspace if this is a read.
  400. */
  401. spin_lock(&umh_sysctl_lock);
  402. for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
  403. if (table->data == CAP_BSET)
  404. cap_array[i] = usermodehelper_bset.cap[i];
  405. else if (table->data == CAP_PI)
  406. cap_array[i] = usermodehelper_inheritable.cap[i];
  407. else
  408. BUG();
  409. }
  410. spin_unlock(&umh_sysctl_lock);
  411. t = *table;
  412. t.data = &cap_array;
  413. /*
  414. * actually read or write and array of ulongs from userspace. Remember
  415. * these are least significant 32 bits first
  416. */
  417. err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
  418. if (err < 0)
  419. return err;
  420. /*
  421. * convert from the sysctl array of ulongs to the kernel_cap_t
  422. * internal representation
  423. */
  424. for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
  425. new_cap.cap[i] = cap_array[i];
  426. /*
  427. * Drop everything not in the new_cap (but don't add things)
  428. */
  429. spin_lock(&umh_sysctl_lock);
  430. if (write) {
  431. if (table->data == CAP_BSET)
  432. usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
  433. if (table->data == CAP_PI)
  434. usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
  435. }
  436. spin_unlock(&umh_sysctl_lock);
  437. return 0;
  438. }
  439. struct ctl_table usermodehelper_table[] = {
  440. {
  441. .procname = "bset",
  442. .data = CAP_BSET,
  443. .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
  444. .mode = 0600,
  445. .proc_handler = proc_cap_handler,
  446. },
  447. {
  448. .procname = "inheritable",
  449. .data = CAP_PI,
  450. .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
  451. .mode = 0600,
  452. .proc_handler = proc_cap_handler,
  453. },
  454. { }
  455. };
  456. void __init usermodehelper_init(void)
  457. {
  458. khelper_wq = create_singlethread_workqueue("khelper");
  459. BUG_ON(!khelper_wq);
  460. }