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