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