kmod.c 16 KB

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