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