kmod.c 15 KB

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