umh.c 19 KB

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