kmod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. kmod_loop_msg++;
  106. }
  107. atomic_dec(&kmod_concurrent);
  108. return -ENOMEM;
  109. }
  110. trace_module_request(module_name, wait, _RET_IP_);
  111. ret = call_usermodehelper_fns(modprobe_path, argv, envp,
  112. wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC,
  113. NULL, NULL, NULL);
  114. atomic_dec(&kmod_concurrent);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL(__request_module);
  118. #endif /* CONFIG_MODULES */
  119. /*
  120. * This is the task which runs the usermode application
  121. */
  122. static int ____call_usermodehelper(void *data)
  123. {
  124. struct subprocess_info *sub_info = data;
  125. struct cred *new;
  126. int retval;
  127. spin_lock_irq(&current->sighand->siglock);
  128. flush_signal_handlers(current, 1);
  129. spin_unlock_irq(&current->sighand->siglock);
  130. /* We can run anywhere, unlike our parent keventd(). */
  131. set_cpus_allowed_ptr(current, cpu_all_mask);
  132. /*
  133. * Our parent is keventd, which runs with elevated scheduling priority.
  134. * Avoid propagating that into the userspace child.
  135. */
  136. set_user_nice(current, 0);
  137. retval = -ENOMEM;
  138. new = prepare_kernel_cred(current);
  139. if (!new)
  140. goto fail;
  141. spin_lock(&umh_sysctl_lock);
  142. new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
  143. new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
  144. new->cap_inheritable);
  145. spin_unlock(&umh_sysctl_lock);
  146. if (sub_info->init) {
  147. retval = sub_info->init(sub_info, new);
  148. if (retval) {
  149. abort_creds(new);
  150. goto fail;
  151. }
  152. }
  153. commit_creds(new);
  154. retval = kernel_execve(sub_info->path,
  155. (const char *const *)sub_info->argv,
  156. (const char *const *)sub_info->envp);
  157. /* Exec failed? */
  158. fail:
  159. sub_info->retval = retval;
  160. do_exit(0);
  161. }
  162. void call_usermodehelper_freeinfo(struct subprocess_info *info)
  163. {
  164. if (info->cleanup)
  165. (*info->cleanup)(info);
  166. kfree(info);
  167. }
  168. EXPORT_SYMBOL(call_usermodehelper_freeinfo);
  169. /* Keventd can't block, but this (a child) can. */
  170. static int wait_for_helper(void *data)
  171. {
  172. struct subprocess_info *sub_info = data;
  173. pid_t pid;
  174. /* If SIGCLD is ignored sys_wait4 won't populate the status. */
  175. spin_lock_irq(&current->sighand->siglock);
  176. current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;
  177. spin_unlock_irq(&current->sighand->siglock);
  178. pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
  179. if (pid < 0) {
  180. sub_info->retval = pid;
  181. } else {
  182. int ret = -ECHILD;
  183. /*
  184. * Normally it is bogus to call wait4() from in-kernel because
  185. * wait4() wants to write the exit code to a userspace address.
  186. * But wait_for_helper() always runs as keventd, and put_user()
  187. * to a kernel address works OK for kernel threads, due to their
  188. * having an mm_segment_t which spans the entire address space.
  189. *
  190. * Thus the __user pointer cast is valid here.
  191. */
  192. sys_wait4(pid, (int __user *)&ret, 0, NULL);
  193. /*
  194. * If ret is 0, either ____call_usermodehelper failed and the
  195. * real error code is already in sub_info->retval or
  196. * sub_info->retval is 0 anyway, so don't mess with it then.
  197. */
  198. if (ret)
  199. sub_info->retval = ret;
  200. }
  201. complete(sub_info->complete);
  202. return 0;
  203. }
  204. /* This is run by khelper thread */
  205. static void __call_usermodehelper(struct work_struct *work)
  206. {
  207. struct subprocess_info *sub_info =
  208. container_of(work, struct subprocess_info, work);
  209. enum umh_wait wait = sub_info->wait;
  210. pid_t pid;
  211. /* CLONE_VFORK: wait until the usermode helper has execve'd
  212. * successfully We need the data structures to stay around
  213. * until that is done. */
  214. if (wait == UMH_WAIT_PROC)
  215. pid = kernel_thread(wait_for_helper, sub_info,
  216. CLONE_FS | CLONE_FILES | SIGCHLD);
  217. else
  218. pid = kernel_thread(____call_usermodehelper, sub_info,
  219. CLONE_VFORK | SIGCHLD);
  220. switch (wait) {
  221. case UMH_NO_WAIT:
  222. call_usermodehelper_freeinfo(sub_info);
  223. break;
  224. case UMH_WAIT_PROC:
  225. if (pid > 0)
  226. break;
  227. /* FALLTHROUGH */
  228. case UMH_WAIT_EXEC:
  229. if (pid < 0)
  230. sub_info->retval = pid;
  231. complete(sub_info->complete);
  232. }
  233. }
  234. /*
  235. * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
  236. * (used for preventing user land processes from being created after the user
  237. * land has been frozen during a system-wide hibernation or suspend operation).
  238. */
  239. static int usermodehelper_disabled;
  240. /* Number of helpers running */
  241. static atomic_t running_helpers = ATOMIC_INIT(0);
  242. /*
  243. * Wait queue head used by usermodehelper_pm_callback() to wait for all running
  244. * helpers to finish.
  245. */
  246. static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
  247. /*
  248. * Time to wait for running_helpers to become zero before the setting of
  249. * usermodehelper_disabled in usermodehelper_pm_callback() fails
  250. */
  251. #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
  252. /**
  253. * usermodehelper_disable - prevent new helpers from being started
  254. */
  255. int usermodehelper_disable(void)
  256. {
  257. long retval;
  258. usermodehelper_disabled = 1;
  259. smp_mb();
  260. /*
  261. * From now on call_usermodehelper_exec() won't start any new
  262. * helpers, so it is sufficient if running_helpers turns out to
  263. * be zero at one point (it may be increased later, but that
  264. * doesn't matter).
  265. */
  266. retval = wait_event_timeout(running_helpers_waitq,
  267. atomic_read(&running_helpers) == 0,
  268. RUNNING_HELPERS_TIMEOUT);
  269. if (retval)
  270. return 0;
  271. usermodehelper_disabled = 0;
  272. return -EAGAIN;
  273. }
  274. /**
  275. * usermodehelper_enable - allow new helpers to be started again
  276. */
  277. void usermodehelper_enable(void)
  278. {
  279. usermodehelper_disabled = 0;
  280. }
  281. /**
  282. * usermodehelper_is_disabled - check if new helpers are allowed to be started
  283. */
  284. bool usermodehelper_is_disabled(void)
  285. {
  286. return usermodehelper_disabled;
  287. }
  288. EXPORT_SYMBOL_GPL(usermodehelper_is_disabled);
  289. static void helper_lock(void)
  290. {
  291. atomic_inc(&running_helpers);
  292. smp_mb__after_atomic_inc();
  293. }
  294. static void helper_unlock(void)
  295. {
  296. if (atomic_dec_and_test(&running_helpers))
  297. wake_up(&running_helpers_waitq);
  298. }
  299. /**
  300. * call_usermodehelper_setup - prepare to call a usermode helper
  301. * @path: path to usermode executable
  302. * @argv: arg vector for process
  303. * @envp: environment for process
  304. * @gfp_mask: gfp mask for memory allocation
  305. *
  306. * Returns either %NULL on allocation failure, or a subprocess_info
  307. * structure. This should be passed to call_usermodehelper_exec to
  308. * exec the process and free the structure.
  309. */
  310. struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
  311. char **envp, gfp_t gfp_mask)
  312. {
  313. struct subprocess_info *sub_info;
  314. sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
  315. if (!sub_info)
  316. goto out;
  317. INIT_WORK(&sub_info->work, __call_usermodehelper);
  318. sub_info->path = path;
  319. sub_info->argv = argv;
  320. sub_info->envp = envp;
  321. out:
  322. return sub_info;
  323. }
  324. EXPORT_SYMBOL(call_usermodehelper_setup);
  325. /**
  326. * call_usermodehelper_setfns - set a cleanup/init function
  327. * @info: a subprocess_info returned by call_usermodehelper_setup
  328. * @cleanup: a cleanup function
  329. * @init: an init function
  330. * @data: arbitrary context sensitive data
  331. *
  332. * The init function is used to customize the helper process prior to
  333. * exec. A non-zero return code causes the process to error out, exit,
  334. * and return the failure to the calling process
  335. *
  336. * The cleanup function is just before ethe subprocess_info is about to
  337. * be freed. This can be used for freeing the argv and envp. The
  338. * Function must be runnable in either a process context or the
  339. * context in which call_usermodehelper_exec is called.
  340. */
  341. void call_usermodehelper_setfns(struct subprocess_info *info,
  342. int (*init)(struct subprocess_info *info, struct cred *new),
  343. void (*cleanup)(struct subprocess_info *info),
  344. void *data)
  345. {
  346. info->cleanup = cleanup;
  347. info->init = init;
  348. info->data = data;
  349. }
  350. EXPORT_SYMBOL(call_usermodehelper_setfns);
  351. /**
  352. * call_usermodehelper_exec - start a usermode application
  353. * @sub_info: information about the subprocessa
  354. * @wait: wait for the application to finish and return status.
  355. * when -1 don't wait at all, but you get no useful error back when
  356. * the program couldn't be exec'ed. This makes it safe to call
  357. * from interrupt context.
  358. *
  359. * Runs a user-space application. The application is started
  360. * asynchronously if wait is not set, and runs as a child of keventd.
  361. * (ie. it runs with full root capabilities).
  362. */
  363. int call_usermodehelper_exec(struct subprocess_info *sub_info,
  364. enum umh_wait wait)
  365. {
  366. DECLARE_COMPLETION_ONSTACK(done);
  367. int retval = 0;
  368. helper_lock();
  369. if (sub_info->path[0] == '\0')
  370. goto out;
  371. if (!khelper_wq || usermodehelper_disabled) {
  372. retval = -EBUSY;
  373. goto out;
  374. }
  375. sub_info->complete = &done;
  376. sub_info->wait = wait;
  377. queue_work(khelper_wq, &sub_info->work);
  378. if (wait == UMH_NO_WAIT) /* task has freed sub_info */
  379. goto unlock;
  380. wait_for_completion(&done);
  381. retval = sub_info->retval;
  382. out:
  383. call_usermodehelper_freeinfo(sub_info);
  384. unlock:
  385. helper_unlock();
  386. return retval;
  387. }
  388. EXPORT_SYMBOL(call_usermodehelper_exec);
  389. static int proc_cap_handler(struct ctl_table *table, int write,
  390. void __user *buffer, size_t *lenp, loff_t *ppos)
  391. {
  392. struct ctl_table t;
  393. unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
  394. kernel_cap_t new_cap;
  395. int err, i;
  396. if (write && (!capable(CAP_SETPCAP) ||
  397. !capable(CAP_SYS_MODULE)))
  398. return -EPERM;
  399. /*
  400. * convert from the global kernel_cap_t to the ulong array to print to
  401. * userspace if this is a read.
  402. */
  403. spin_lock(&umh_sysctl_lock);
  404. for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
  405. if (table->data == CAP_BSET)
  406. cap_array[i] = usermodehelper_bset.cap[i];
  407. else if (table->data == CAP_PI)
  408. cap_array[i] = usermodehelper_inheritable.cap[i];
  409. else
  410. BUG();
  411. }
  412. spin_unlock(&umh_sysctl_lock);
  413. t = *table;
  414. t.data = &cap_array;
  415. /*
  416. * actually read or write and array of ulongs from userspace. Remember
  417. * these are least significant 32 bits first
  418. */
  419. err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
  420. if (err < 0)
  421. return err;
  422. /*
  423. * convert from the sysctl array of ulongs to the kernel_cap_t
  424. * internal representation
  425. */
  426. for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
  427. new_cap.cap[i] = cap_array[i];
  428. /*
  429. * Drop everything not in the new_cap (but don't add things)
  430. */
  431. spin_lock(&umh_sysctl_lock);
  432. if (write) {
  433. if (table->data == CAP_BSET)
  434. usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
  435. if (table->data == CAP_PI)
  436. usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
  437. }
  438. spin_unlock(&umh_sysctl_lock);
  439. return 0;
  440. }
  441. struct ctl_table usermodehelper_table[] = {
  442. {
  443. .procname = "bset",
  444. .data = CAP_BSET,
  445. .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
  446. .mode = 0600,
  447. .proc_handler = proc_cap_handler,
  448. },
  449. {
  450. .procname = "inheritable",
  451. .data = CAP_PI,
  452. .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
  453. .mode = 0600,
  454. .proc_handler = proc_cap_handler,
  455. },
  456. { }
  457. };
  458. void __init usermodehelper_init(void)
  459. {
  460. khelper_wq = create_singlethread_workqueue("khelper");
  461. BUG_ON(!khelper_wq);
  462. }