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