umh.c 15 KB

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