~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/umh.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/umh.c (Version linux-6.11.5) and /kernel/umh.c (Version linux-4.20.17)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php