umh.c 16 KB

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