umh.c 15 KB

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