~ [ 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-6.0.19)


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

~ [ 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