kmod.c 19 KB

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