kmod.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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/sched/task.h>
  19. #include <linux/binfmts.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/unistd.h>
  22. #include <linux/kmod.h>
  23. #include <linux/slab.h>
  24. #include <linux/completion.h>
  25. #include <linux/cred.h>
  26. #include <linux/file.h>
  27. #include <linux/fdtable.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/security.h>
  30. #include <linux/mount.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/resource.h>
  34. #include <linux/notifier.h>
  35. #include <linux/suspend.h>
  36. #include <linux/rwsem.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/async.h>
  39. #include <linux/uaccess.h>
  40. #include <trace/events/module.h>
  41. #define CAP_BSET (void *)1
  42. #define CAP_PI (void *)2
  43. static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
  44. static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
  45. static DEFINE_SPINLOCK(umh_sysctl_lock);
  46. static DECLARE_RWSEM(umhelper_sem);
  47. #ifdef CONFIG_MODULES
  48. /*
  49. * Assuming:
  50. *
  51. * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
  52. * (u64) THREAD_SIZE * 8UL);
  53. *
  54. * If you need less than 50 threads would mean we're dealing with systems
  55. * smaller than 3200 pages. This assuems you are capable of having ~13M memory,
  56. * and this would only be an be an upper limit, after which the OOM killer
  57. * would take effect. Systems like these are very unlikely if modules are
  58. * enabled.
  59. */
  60. #define MAX_KMOD_CONCURRENT 50
  61. static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT);
  62. static DECLARE_WAIT_QUEUE_HEAD(kmod_wq);
  63. /*
  64. modprobe_path is set via /proc/sys.
  65. */
  66. char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
  67. static void free_modprobe_argv(struct subprocess_info *info)
  68. {
  69. kfree(info->argv[3]); /* check call_modprobe() */
  70. kfree(info->argv);
  71. }
  72. static int call_modprobe(char *module_name, int wait)
  73. {
  74. struct subprocess_info *info;
  75. static char *envp[] = {
  76. "HOME=/",
  77. "TERM=linux",
  78. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  79. NULL
  80. };
  81. char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL);
  82. if (!argv)
  83. goto out;
  84. module_name = kstrdup(module_name, GFP_KERNEL);
  85. if (!module_name)
  86. goto free_argv;
  87. argv[0] = modprobe_path;
  88. argv[1] = "-q";
  89. argv[2] = "--";
  90. argv[3] = module_name; /* check free_modprobe_argv() */
  91. argv[4] = NULL;
  92. info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL,
  93. NULL, free_modprobe_argv, NULL);
  94. if (!info)
  95. goto free_module_name;
  96. return call_usermodehelper_exec(info, wait | UMH_KILLABLE);
  97. free_module_name:
  98. kfree(module_name);
  99. free_argv:
  100. kfree(argv);
  101. out:
  102. return -ENOMEM;
  103. }
  104. /**
  105. * __request_module - try to load a kernel module
  106. * @wait: wait (or not) for the operation to complete
  107. * @fmt: printf style format string for the name of the module
  108. * @...: arguments as specified in the format string
  109. *
  110. * Load a module using the user mode module loader. The function returns
  111. * zero on success or a negative errno code or positive exit code from
  112. * "modprobe" on failure. Note that a successful module load does not mean
  113. * the module did not then unload and exit on an error of its own. Callers
  114. * must check that the service they requested is now available not blindly
  115. * invoke it.
  116. *
  117. * If module auto-loading support is disabled then this function
  118. * becomes a no-operation.
  119. */
  120. int __request_module(bool wait, const char *fmt, ...)
  121. {
  122. va_list args;
  123. char module_name[MODULE_NAME_LEN];
  124. int ret;
  125. /*
  126. * We don't allow synchronous module loading from async. Module
  127. * init may invoke async_synchronize_full() which will end up
  128. * waiting for this task which already is waiting for the module
  129. * loading to complete, leading to a deadlock.
  130. */
  131. WARN_ON_ONCE(wait && current_is_async());
  132. if (!modprobe_path[0])
  133. return 0;
  134. va_start(args, fmt);
  135. ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
  136. va_end(args);
  137. if (ret >= MODULE_NAME_LEN)
  138. return -ENAMETOOLONG;
  139. ret = security_kernel_module_request(module_name);
  140. if (ret)
  141. return ret;
  142. if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) {
  143. pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...",
  144. atomic_read(&kmod_concurrent_max),
  145. MAX_KMOD_CONCURRENT, module_name);
  146. wait_event_interruptible(kmod_wq,
  147. atomic_dec_if_positive(&kmod_concurrent_max) >= 0);
  148. }
  149. trace_module_request(module_name, wait, _RET_IP_);
  150. ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
  151. atomic_inc(&kmod_concurrent_max);
  152. wake_up(&kmod_wq);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(__request_module);
  156. #endif /* CONFIG_MODULES */
  157. static void call_usermodehelper_freeinfo(struct subprocess_info *info)
  158. {
  159. if (info->cleanup)
  160. (*info->cleanup)(info);
  161. kfree(info);
  162. }
  163. static void umh_complete(struct subprocess_info *sub_info)
  164. {
  165. struct completion *comp = xchg(&sub_info->complete, NULL);
  166. /*
  167. * See call_usermodehelper_exec(). If xchg() returns NULL
  168. * we own sub_info, the UMH_KILLABLE caller has gone away
  169. * or the caller used UMH_NO_WAIT.
  170. */
  171. if (comp)
  172. complete(comp);
  173. else
  174. call_usermodehelper_freeinfo(sub_info);
  175. }
  176. /*
  177. * This is the task which runs the usermode application
  178. */
  179. static int call_usermodehelper_exec_async(void *data)
  180. {
  181. struct subprocess_info *sub_info = data;
  182. struct cred *new;
  183. int retval;
  184. spin_lock_irq(&current->sighand->siglock);
  185. flush_signal_handlers(current, 1);
  186. spin_unlock_irq(&current->sighand->siglock);
  187. /*
  188. * Our parent (unbound workqueue) runs with elevated scheduling
  189. * priority. Avoid propagating that into the userspace child.
  190. */
  191. set_user_nice(current, 0);
  192. retval = -ENOMEM;
  193. new = prepare_kernel_cred(current);
  194. if (!new)
  195. goto out;
  196. spin_lock(&umh_sysctl_lock);
  197. new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
  198. new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
  199. new->cap_inheritable);
  200. spin_unlock(&umh_sysctl_lock);
  201. if (sub_info->init) {
  202. retval = sub_info->init(sub_info, new);
  203. if (retval) {
  204. abort_creds(new);
  205. goto out;
  206. }
  207. }
  208. commit_creds(new);
  209. retval = do_execve(getname_kernel(sub_info->path),
  210. (const char __user *const __user *)sub_info->argv,
  211. (const char __user *const __user *)sub_info->envp);
  212. out:
  213. sub_info->retval = retval;
  214. /*
  215. * call_usermodehelper_exec_sync() will call umh_complete
  216. * if UHM_WAIT_PROC.
  217. */
  218. if (!(sub_info->wait & UMH_WAIT_PROC))
  219. umh_complete(sub_info);
  220. if (!retval)
  221. return 0;
  222. do_exit(0);
  223. }
  224. /* Handles UMH_WAIT_PROC. */
  225. static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
  226. {
  227. pid_t pid;
  228. /* If SIGCLD is ignored sys_wait4 won't populate the status. */
  229. kernel_sigaction(SIGCHLD, SIG_DFL);
  230. pid = kernel_thread(call_usermodehelper_exec_async, 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 call_usermodehelper_exec_sync() always runs as kernel
  239. * thread (workqueue) and put_user() to a kernel address works
  240. * OK for kernel threads, due to their having an mm_segment_t
  241. * which spans the entire address space.
  242. *
  243. * Thus the __user pointer cast is valid here.
  244. */
  245. sys_wait4(pid, (int __user *)&ret, 0, NULL);
  246. /*
  247. * If ret is 0, either call_usermodehelper_exec_async failed and
  248. * the real error code is already in sub_info->retval or
  249. * sub_info->retval is 0 anyway, so don't mess with it then.
  250. */
  251. if (ret)
  252. sub_info->retval = ret;
  253. }
  254. /* Restore default kernel sig handler */
  255. kernel_sigaction(SIGCHLD, SIG_IGN);
  256. umh_complete(sub_info);
  257. }
  258. /*
  259. * We need to create the usermodehelper kernel thread from a task that is affine
  260. * to an optimized set of CPUs (or nohz housekeeping ones) such that they
  261. * inherit a widest affinity irrespective of call_usermodehelper() callers with
  262. * possibly reduced affinity (eg: per-cpu workqueues). We don't want
  263. * usermodehelper targets to contend a busy CPU.
  264. *
  265. * Unbound workqueues provide such wide affinity and allow to block on
  266. * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
  267. *
  268. * Besides, workqueues provide the privilege level that caller might not have
  269. * to perform the usermodehelper request.
  270. *
  271. */
  272. static void call_usermodehelper_exec_work(struct work_struct *work)
  273. {
  274. struct subprocess_info *sub_info =
  275. container_of(work, struct subprocess_info, work);
  276. if (sub_info->wait & UMH_WAIT_PROC) {
  277. call_usermodehelper_exec_sync(sub_info);
  278. } else {
  279. pid_t pid;
  280. /*
  281. * Use CLONE_PARENT to reparent it to kthreadd; we do not
  282. * want to pollute current->children, and we need a parent
  283. * that always ignores SIGCHLD to ensure auto-reaping.
  284. */
  285. pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
  286. CLONE_PARENT | SIGCHLD);
  287. if (pid < 0) {
  288. sub_info->retval = pid;
  289. umh_complete(sub_info);
  290. }
  291. }
  292. }
  293. /*
  294. * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
  295. * (used for preventing user land processes from being created after the user
  296. * land has been frozen during a system-wide hibernation or suspend operation).
  297. * Should always be manipulated under umhelper_sem acquired for write.
  298. */
  299. static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
  300. /* Number of helpers running */
  301. static atomic_t running_helpers = ATOMIC_INIT(0);
  302. /*
  303. * Wait queue head used by usermodehelper_disable() to wait for all running
  304. * helpers to finish.
  305. */
  306. static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
  307. /*
  308. * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
  309. * to become 'false'.
  310. */
  311. static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
  312. /*
  313. * Time to wait for running_helpers to become zero before the setting of
  314. * usermodehelper_disabled in usermodehelper_disable() fails
  315. */
  316. #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
  317. int usermodehelper_read_trylock(void)
  318. {
  319. DEFINE_WAIT(wait);
  320. int ret = 0;
  321. down_read(&umhelper_sem);
  322. for (;;) {
  323. prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
  324. TASK_INTERRUPTIBLE);
  325. if (!usermodehelper_disabled)
  326. break;
  327. if (usermodehelper_disabled == UMH_DISABLED)
  328. ret = -EAGAIN;
  329. up_read(&umhelper_sem);
  330. if (ret)
  331. break;
  332. schedule();
  333. try_to_freeze();
  334. down_read(&umhelper_sem);
  335. }
  336. finish_wait(&usermodehelper_disabled_waitq, &wait);
  337. return ret;
  338. }
  339. EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
  340. long usermodehelper_read_lock_wait(long timeout)
  341. {
  342. DEFINE_WAIT(wait);
  343. if (timeout < 0)
  344. return -EINVAL;
  345. down_read(&umhelper_sem);
  346. for (;;) {
  347. prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
  348. TASK_UNINTERRUPTIBLE);
  349. if (!usermodehelper_disabled)
  350. break;
  351. up_read(&umhelper_sem);
  352. timeout = schedule_timeout(timeout);
  353. if (!timeout)
  354. break;
  355. down_read(&umhelper_sem);
  356. }
  357. finish_wait(&usermodehelper_disabled_waitq, &wait);
  358. return timeout;
  359. }
  360. EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
  361. void usermodehelper_read_unlock(void)
  362. {
  363. up_read(&umhelper_sem);
  364. }
  365. EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
  366. /**
  367. * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
  368. * @depth: New value to assign to usermodehelper_disabled.
  369. *
  370. * Change the value of usermodehelper_disabled (under umhelper_sem locked for
  371. * writing) and wakeup tasks waiting for it to change.
  372. */
  373. void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
  374. {
  375. down_write(&umhelper_sem);
  376. usermodehelper_disabled = depth;
  377. wake_up(&usermodehelper_disabled_waitq);
  378. up_write(&umhelper_sem);
  379. }
  380. /**
  381. * __usermodehelper_disable - Prevent new helpers from being started.
  382. * @depth: New value to assign to usermodehelper_disabled.
  383. *
  384. * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
  385. */
  386. int __usermodehelper_disable(enum umh_disable_depth depth)
  387. {
  388. long retval;
  389. if (!depth)
  390. return -EINVAL;
  391. down_write(&umhelper_sem);
  392. usermodehelper_disabled = depth;
  393. up_write(&umhelper_sem);
  394. /*
  395. * From now on call_usermodehelper_exec() won't start any new
  396. * helpers, so it is sufficient if running_helpers turns out to
  397. * be zero at one point (it may be increased later, but that
  398. * doesn't matter).
  399. */
  400. retval = wait_event_timeout(running_helpers_waitq,
  401. atomic_read(&running_helpers) == 0,
  402. RUNNING_HELPERS_TIMEOUT);
  403. if (retval)
  404. return 0;
  405. __usermodehelper_set_disable_depth(UMH_ENABLED);
  406. return -EAGAIN;
  407. }
  408. static void helper_lock(void)
  409. {
  410. atomic_inc(&running_helpers);
  411. smp_mb__after_atomic();
  412. }
  413. static void helper_unlock(void)
  414. {
  415. if (atomic_dec_and_test(&running_helpers))
  416. wake_up(&running_helpers_waitq);
  417. }
  418. /**
  419. * call_usermodehelper_setup - prepare to call a usermode helper
  420. * @path: path to usermode executable
  421. * @argv: arg vector for process
  422. * @envp: environment for process
  423. * @gfp_mask: gfp mask for memory allocation
  424. * @cleanup: a cleanup function
  425. * @init: an init function
  426. * @data: arbitrary context sensitive data
  427. *
  428. * Returns either %NULL on allocation failure, or a subprocess_info
  429. * structure. This should be passed to call_usermodehelper_exec to
  430. * exec the process and free the structure.
  431. *
  432. * The init function is used to customize the helper process prior to
  433. * exec. A non-zero return code causes the process to error out, exit,
  434. * and return the failure to the calling process
  435. *
  436. * The cleanup function is just before ethe subprocess_info is about to
  437. * be freed. This can be used for freeing the argv and envp. The
  438. * Function must be runnable in either a process context or the
  439. * context in which call_usermodehelper_exec is called.
  440. */
  441. struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
  442. char **envp, gfp_t gfp_mask,
  443. int (*init)(struct subprocess_info *info, struct cred *new),
  444. void (*cleanup)(struct subprocess_info *info),
  445. void *data)
  446. {
  447. struct subprocess_info *sub_info;
  448. sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
  449. if (!sub_info)
  450. goto out;
  451. INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
  452. #ifdef CONFIG_STATIC_USERMODEHELPER
  453. sub_info->path = CONFIG_STATIC_USERMODEHELPER_PATH;
  454. #else
  455. sub_info->path = path;
  456. #endif
  457. sub_info->argv = argv;
  458. sub_info->envp = envp;
  459. sub_info->cleanup = cleanup;
  460. sub_info->init = init;
  461. sub_info->data = data;
  462. out:
  463. return sub_info;
  464. }
  465. EXPORT_SYMBOL(call_usermodehelper_setup);
  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 UMH_NO_WAIT don't wait at all, but you get no useful error back
  471. * when 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 system workqueues.
  476. * (ie. it runs with full root capabilities and optimized affinity).
  477. */
  478. int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
  479. {
  480. DECLARE_COMPLETION_ONSTACK(done);
  481. int retval = 0;
  482. if (!sub_info->path) {
  483. call_usermodehelper_freeinfo(sub_info);
  484. return -EINVAL;
  485. }
  486. helper_lock();
  487. if (usermodehelper_disabled) {
  488. retval = -EBUSY;
  489. goto out;
  490. }
  491. /*
  492. * If there is no binary for us to call, then just return and get out of
  493. * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
  494. * disable all call_usermodehelper() calls.
  495. */
  496. if (strlen(sub_info->path) == 0)
  497. goto out;
  498. /*
  499. * Set the completion pointer only if there is a waiter.
  500. * This makes it possible to use umh_complete to free
  501. * the data structure in case of UMH_NO_WAIT.
  502. */
  503. sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
  504. sub_info->wait = wait;
  505. queue_work(system_unbound_wq, &sub_info->work);
  506. if (wait == UMH_NO_WAIT) /* task has freed sub_info */
  507. goto unlock;
  508. if (wait & UMH_KILLABLE) {
  509. retval = wait_for_completion_killable(&done);
  510. if (!retval)
  511. goto wait_done;
  512. /* umh_complete() will see NULL and free sub_info */
  513. if (xchg(&sub_info->complete, NULL))
  514. goto unlock;
  515. /* fallthrough, umh_complete() was already called */
  516. }
  517. wait_for_completion(&done);
  518. wait_done:
  519. retval = sub_info->retval;
  520. out:
  521. call_usermodehelper_freeinfo(sub_info);
  522. unlock:
  523. helper_unlock();
  524. return retval;
  525. }
  526. EXPORT_SYMBOL(call_usermodehelper_exec);
  527. /**
  528. * call_usermodehelper() - prepare and start a usermode application
  529. * @path: path to usermode executable
  530. * @argv: arg vector for process
  531. * @envp: environment for process
  532. * @wait: wait for the application to finish and return status.
  533. * when UMH_NO_WAIT don't wait at all, but you get no useful error back
  534. * when the program couldn't be exec'ed. This makes it safe to call
  535. * from interrupt context.
  536. *
  537. * This function is the equivalent to use call_usermodehelper_setup() and
  538. * call_usermodehelper_exec().
  539. */
  540. int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
  541. {
  542. struct subprocess_info *info;
  543. gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
  544. info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
  545. NULL, NULL, NULL);
  546. if (info == NULL)
  547. return -ENOMEM;
  548. return call_usermodehelper_exec(info, wait);
  549. }
  550. EXPORT_SYMBOL(call_usermodehelper);
  551. static int proc_cap_handler(struct ctl_table *table, int write,
  552. void __user *buffer, size_t *lenp, loff_t *ppos)
  553. {
  554. struct ctl_table t;
  555. unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
  556. kernel_cap_t new_cap;
  557. int err, i;
  558. if (write && (!capable(CAP_SETPCAP) ||
  559. !capable(CAP_SYS_MODULE)))
  560. return -EPERM;
  561. /*
  562. * convert from the global kernel_cap_t to the ulong array to print to
  563. * userspace if this is a read.
  564. */
  565. spin_lock(&umh_sysctl_lock);
  566. for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
  567. if (table->data == CAP_BSET)
  568. cap_array[i] = usermodehelper_bset.cap[i];
  569. else if (table->data == CAP_PI)
  570. cap_array[i] = usermodehelper_inheritable.cap[i];
  571. else
  572. BUG();
  573. }
  574. spin_unlock(&umh_sysctl_lock);
  575. t = *table;
  576. t.data = &cap_array;
  577. /*
  578. * actually read or write and array of ulongs from userspace. Remember
  579. * these are least significant 32 bits first
  580. */
  581. err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
  582. if (err < 0)
  583. return err;
  584. /*
  585. * convert from the sysctl array of ulongs to the kernel_cap_t
  586. * internal representation
  587. */
  588. for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
  589. new_cap.cap[i] = cap_array[i];
  590. /*
  591. * Drop everything not in the new_cap (but don't add things)
  592. */
  593. spin_lock(&umh_sysctl_lock);
  594. if (write) {
  595. if (table->data == CAP_BSET)
  596. usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
  597. if (table->data == CAP_PI)
  598. usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
  599. }
  600. spin_unlock(&umh_sysctl_lock);
  601. return 0;
  602. }
  603. struct ctl_table usermodehelper_table[] = {
  604. {
  605. .procname = "bset",
  606. .data = CAP_BSET,
  607. .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
  608. .mode = 0600,
  609. .proc_handler = proc_cap_handler,
  610. },
  611. {
  612. .procname = "inheritable",
  613. .data = CAP_PI,
  614. .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
  615. .mode = 0600,
  616. .proc_handler = proc_cap_handler,
  617. },
  618. { }
  619. };