umh.c 15 KB

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