kmod.c 19 KB

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