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

TOMOYO Linux Cross Reference
Linux/fs/binfmt_misc.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ 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.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/binfmt_misc.c (Version linux-6.12-rc7) and /fs/binfmt_misc.c (Version linux-2.6.0)


  1 // SPDX-License-Identifier: GPL-2.0-only       << 
  2 /*                                                  1 /*
  3  * binfmt_misc.c                               !!   2  *  binfmt_misc.c
  4  *                                                  3  *
  5  * Copyright (C) 1997 Richard Günther         !!   4  *  Copyright (C) 1997 Richard Günther
  6  *                                                  5  *
  7  * binfmt_misc detects binaries via a magic or !!   6  *  binfmt_misc detects binaries via a magic or filename extension and invokes
  8  * a specified wrapper. See Documentation/admi !!   7  *  a specified wrapper. This should obsolete binfmt_java, binfmt_em86 and
                                                   >>   8  *  binfmt_mz.
                                                   >>   9  *
                                                   >>  10  *  1997-04-25 first version
                                                   >>  11  *  [...]
                                                   >>  12  *  1997-05-19 cleanup
                                                   >>  13  *  1997-06-26 hpa: pass the real filename rather than argv[0]
                                                   >>  14  *  1997-06-30 minor cleanup
                                                   >>  15  *  1997-08-09 removed extension stripping, locking cleanup
                                                   >>  16  *  2001-02-28 AV: rewritten into something that resembles C. Original didn't.
  9  */                                                17  */
 10                                                    18 
 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt    << 
 12                                                << 
 13 #include <linux/kernel.h>                      << 
 14 #include <linux/module.h>                          19 #include <linux/module.h>
 15 #include <linux/init.h>                            20 #include <linux/init.h>
 16 #include <linux/sched/mm.h>                    !!  21 
 17 #include <linux/magic.h>                       << 
 18 #include <linux/binfmts.h>                         22 #include <linux/binfmts.h>
 19 #include <linux/slab.h>                            23 #include <linux/slab.h>
 20 #include <linux/ctype.h>                           24 #include <linux/ctype.h>
 21 #include <linux/string_helpers.h>              << 
 22 #include <linux/file.h>                            25 #include <linux/file.h>
 23 #include <linux/pagemap.h>                         26 #include <linux/pagemap.h>
 24 #include <linux/namei.h>                           27 #include <linux/namei.h>
 25 #include <linux/mount.h>                           28 #include <linux/mount.h>
 26 #include <linux/fs_context.h>                  !!  29 
 27 #include <linux/syscalls.h>                    !!  30 #include <asm/uaccess.h>
 28 #include <linux/fs.h>                          << 
 29 #include <linux/uaccess.h>                     << 
 30                                                << 
 31 #include "internal.h"                          << 
 32                                                << 
 33 #ifdef DEBUG                                   << 
 34 # define USE_DEBUG 1                           << 
 35 #else                                          << 
 36 # define USE_DEBUG 0                           << 
 37 #endif                                         << 
 38                                                    31 
 39 enum {                                             32 enum {
 40         VERBOSE_STATUS = 1 /* make it zero to      33         VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
 41 };                                                 34 };
 42                                                    35 
                                                   >>  36 static LIST_HEAD(entries);
                                                   >>  37 static int enabled = 1;
                                                   >>  38 
 43 enum {Enabled, Magic};                             39 enum {Enabled, Magic};
 44 #define MISC_FMT_PRESERVE_ARGV0 (1UL << 31)    !!  40 #define MISC_FMT_PRESERVE_ARGV0 (1<<31)
 45 #define MISC_FMT_OPEN_BINARY (1UL << 30)       << 
 46 #define MISC_FMT_CREDENTIALS (1UL << 29)       << 
 47 #define MISC_FMT_OPEN_FILE (1UL << 28)         << 
 48                                                    41 
 49 typedef struct {                                   42 typedef struct {
 50         struct list_head list;                     43         struct list_head list;
 51         unsigned long flags;            /* typ     44         unsigned long flags;            /* type, status, etc. */
 52         int offset;                     /* off     45         int offset;                     /* offset of magic */
 53         int size;                       /* siz     46         int size;                       /* size of magic/mask */
 54         char *magic;                    /* mag     47         char *magic;                    /* magic or filename extension */
 55         char *mask;                     /* mas     48         char *mask;                     /* mask, NULL for exact match */
 56         const char *interpreter;        /* fil !!  49         char *interpreter;              /* filename of interpreter */
 57         char *name;                                50         char *name;
 58         struct dentry *dentry;                     51         struct dentry *dentry;
 59         struct file *interp_file;              << 
 60         refcount_t users;               /* syn << 
 61 } Node;                                            52 } Node;
 62                                                    53 
 63 static struct file_system_type bm_fs_type;     !!  54 static rwlock_t entries_lock __attribute__((unused)) = RW_LOCK_UNLOCKED;
 64                                                !!  55 static struct vfsmount *bm_mnt;
 65 /*                                             !!  56 static int entry_count;
 66  * Max length of the register string.  Determi !!  57 
 67  *  - 7 delimiters                             !!  58 /* 
 68  *  - name:   ~50 bytes                        !!  59  * Check if we support the binfmt
 69  *  - type:   1 byte                           !!  60  * if we do, return the node, else NULL
 70  *  - offset: 3 bytes (has to be smaller than  !!  61  * locking is done in load_misc_binary
 71  *  - magic:  128 bytes (512 in escaped form)  << 
 72  *  - mask:   128 bytes (512 in escaped form)  << 
 73  *  - interp: ~50 bytes                        << 
 74  *  - flags:  5 bytes                          << 
 75  * Round that up a bit, and then back off to h << 
 76  * (like struct Node).                         << 
 77  */                                            << 
 78 #define MAX_REGISTER_LENGTH 1920               << 
 79                                                << 
 80 /**                                            << 
 81  * search_binfmt_handler - search for a binary << 
 82  * @misc: handle to binfmt_misc instance       << 
 83  * @bprm: binary for which we are looking for  << 
 84  *                                             << 
 85  * Search for a binary type handler for @bprm  << 
 86  * type handlers.                              << 
 87  *                                             << 
 88  * Return: binary type list entry on success,  << 
 89  */                                                62  */
 90 static Node *search_binfmt_handler(struct binf !!  63 static Node *check_file(struct linux_binprm *bprm)
 91                                    struct linu << 
 92 {                                                  64 {
 93         char *p = strrchr(bprm->interp, '.');      65         char *p = strrchr(bprm->interp, '.');
 94         Node *e;                               !!  66         struct list_head *l;
 95                                                    67 
 96         /* Walk all the registered handlers. * !!  68         list_for_each(l, &entries) {
 97         list_for_each_entry(e, &misc->entries, !!  69                 Node *e = list_entry(l, Node, list);
 98                 char *s;                           70                 char *s;
 99                 int j;                             71                 int j;
100                                                    72 
101                 /* Make sure this one is curre << 
102                 if (!test_bit(Enabled, &e->fla     73                 if (!test_bit(Enabled, &e->flags))
103                         continue;                  74                         continue;
104                                                    75 
105                 /* Do matching based on extens << 
106                 if (!test_bit(Magic, &e->flags     76                 if (!test_bit(Magic, &e->flags)) {
107                         if (p && !strcmp(e->ma     77                         if (p && !strcmp(e->magic, p + 1))
108                                 return e;          78                                 return e;
109                         continue;                  79                         continue;
110                 }                                  80                 }
111                                                    81 
112                 /* Do matching based on magic  << 
113                 s = bprm->buf + e->offset;         82                 s = bprm->buf + e->offset;
114                 if (e->mask) {                     83                 if (e->mask) {
115                         for (j = 0; j < e->siz     84                         for (j = 0; j < e->size; j++)
116                                 if ((*s++ ^ e-     85                                 if ((*s++ ^ e->magic[j]) & e->mask[j])
117                                         break;     86                                         break;
118                 } else {                           87                 } else {
119                         for (j = 0; j < e->siz     88                         for (j = 0; j < e->size; j++)
120                                 if ((*s++ ^ e-     89                                 if ((*s++ ^ e->magic[j]))
121                                         break;     90                                         break;
122                 }                                  91                 }
123                 if (j == e->size)                  92                 if (j == e->size)
124                         return e;                  93                         return e;
125         }                                          94         }
126                                                << 
127         return NULL;                               95         return NULL;
128 }                                                  96 }
129                                                    97 
130 /**                                            << 
131  * get_binfmt_handler - try to find a binary t << 
132  * @misc: handle to binfmt_misc instance       << 
133  * @bprm: binary for which we are looking for  << 
134  *                                             << 
135  * Try to find a binfmt handler for the binary << 
136  * reference to protect against removal via bm << 
137  *                                             << 
138  * Return: binary type list entry on success,  << 
139  */                                            << 
140 static Node *get_binfmt_handler(struct binfmt_ << 
141                                 struct linux_b << 
142 {                                              << 
143         Node *e;                               << 
144                                                << 
145         read_lock(&misc->entries_lock);        << 
146         e = search_binfmt_handler(misc, bprm); << 
147         if (e)                                 << 
148                 refcount_inc(&e->users);       << 
149         read_unlock(&misc->entries_lock);      << 
150         return e;                              << 
151 }                                              << 
152                                                << 
153 /**                                            << 
154  * put_binfmt_handler - put binary handler nod << 
155  * @e: node to put                             << 
156  *                                             << 
157  * Free node syncing with load_misc_binary() a << 
158  * load_misc_binary() in case it is using the  << 
159  * requested to remove.                        << 
160  */                                            << 
161 static void put_binfmt_handler(Node *e)        << 
162 {                                              << 
163         if (refcount_dec_and_test(&e->users))  << 
164                 if (e->flags & MISC_FMT_OPEN_F << 
165                         filp_close(e->interp_f << 
166                 kfree(e);                      << 
167         }                                      << 
168 }                                              << 
169                                                << 
170 /**                                            << 
171  * load_binfmt_misc - load the binfmt_misc of  << 
172  *                                             << 
173  * To be called in load_misc_binary() to load  << 
174  * If a user namespace doesn't have its own bi << 
175  * of its ancestor's binfmt_misc handlers. Thi << 
176  * pre-namespaced binfmt_misc where all regist << 
177  * available to all user and user namespaces o << 
178  *                                             << 
179  * Return: the binfmt_misc instance of the cal << 
180  */                                            << 
181 static struct binfmt_misc *load_binfmt_misc(vo << 
182 {                                              << 
183         const struct user_namespace *user_ns;  << 
184         struct binfmt_misc *misc;              << 
185                                                << 
186         user_ns = current_user_ns();           << 
187         while (user_ns) {                      << 
188                 /* Pairs with smp_store_releas << 
189                 misc = smp_load_acquire(&user_ << 
190                 if (misc)                      << 
191                         return misc;           << 
192                                                << 
193                 user_ns = user_ns->parent;     << 
194         }                                      << 
195                                                << 
196         return &init_binfmt_misc;              << 
197 }                                              << 
198                                                << 
199 /*                                                 98 /*
200  * the loader itself                               99  * the loader itself
201  */                                               100  */
202 static int load_misc_binary(struct linux_binpr !! 101 static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
203 {                                                 102 {
204         Node *fmt;                                103         Node *fmt;
205         struct file *interp_file = NULL;       !! 104         struct file * file;
206         int retval = -ENOEXEC;                 !! 105         char iname[BINPRM_BUF_SIZE];
207         struct binfmt_misc *misc;              !! 106         char *iname_addr = iname;
208                                                !! 107         int retval;
209         misc = load_binfmt_misc();             !! 108 
210         if (!misc->enabled)                    !! 109         retval = -ENOEXEC;
211                 return retval;                 !! 110         if (!enabled)
212                                                !! 111                 goto _ret;
213         fmt = get_binfmt_handler(misc, bprm);  !! 112 
                                                   >> 113         /* to keep locking time low, we copy the interpreter string */
                                                   >> 114         read_lock(&entries_lock);
                                                   >> 115         fmt = check_file(bprm);
                                                   >> 116         if (fmt)
                                                   >> 117                 strlcpy(iname, fmt->interpreter, BINPRM_BUF_SIZE);
                                                   >> 118         read_unlock(&entries_lock);
214         if (!fmt)                                 119         if (!fmt)
215                 return retval;                 !! 120                 goto _ret;
216                                                   121 
217         /* Need to be able to load the file af !! 122         allow_write_access(bprm->file);
218         retval = -ENOENT;                      !! 123         fput(bprm->file);
219         if (bprm->interp_flags & BINPRM_FLAGS_ !! 124         bprm->file = NULL;
220                 goto ret;                      !! 125 
221                                                !! 126         /* Build args for interpreter */
222         if (fmt->flags & MISC_FMT_PRESERVE_ARG !! 127         if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
223                 bprm->interp_flags |= BINPRM_F !! 128                 remove_arg_zero(bprm);
224         } else {                               << 
225                 retval = remove_arg_zero(bprm) << 
226                 if (retval)                    << 
227                         goto ret;              << 
228         }                                         129         }
229                                                !! 130         retval = copy_strings_kernel(1, &bprm->interp, bprm);
230         if (fmt->flags & MISC_FMT_OPEN_BINARY) !! 131         if (retval < 0) goto _ret; 
231                 bprm->have_execfd = 1;         << 
232                                                << 
233         /* make argv[1] be the path to the bin << 
234         retval = copy_string_kernel(bprm->inte << 
235         if (retval < 0)                        << 
236                 goto ret;                      << 
237         bprm->argc++;                             132         bprm->argc++;
238                                                !! 133         retval = copy_strings_kernel(1, &iname_addr, bprm);
239         /* add the interp as argv[0] */        !! 134         if (retval < 0) goto _ret; 
240         retval = copy_string_kernel(fmt->inter << 
241         if (retval < 0)                        << 
242                 goto ret;                      << 
243         bprm->argc++;                             135         bprm->argc++;
                                                   >> 136         bprm->interp = iname;   /* for binfmt_script */
244                                                   137 
245         /* Update interp in case binfmt_script !! 138         file = open_exec(iname);
246         retval = bprm_change_interp(fmt->inter !! 139         retval = PTR_ERR(file);
247         if (retval < 0)                        !! 140         if (IS_ERR(file))
248                 goto ret;                      !! 141                 goto _ret;
249                                                !! 142         bprm->file = file;
250         if (fmt->flags & MISC_FMT_OPEN_FILE)   !! 143 
251                 interp_file = file_clone_open( !! 144         retval = prepare_binprm(bprm);
252         else                                   !! 145         if (retval >= 0)
253                 interp_file = open_exec(fmt->i !! 146                 retval = search_binary_handler(bprm, regs);
254         retval = PTR_ERR(interp_file);         !! 147 _ret:
255         if (IS_ERR(interp_file))               << 
256                 goto ret;                      << 
257                                                << 
258         bprm->interpreter = interp_file;       << 
259         if (fmt->flags & MISC_FMT_CREDENTIALS) << 
260                 bprm->execfd_creds = 1;        << 
261                                                << 
262         retval = 0;                            << 
263 ret:                                           << 
264                                                << 
265         /*                                     << 
266          * If we actually put the node here al << 
267          * load_misc_binary() will have finish << 
268          * that for the refcount to be zero so << 
269          * removed the binary type handler fro << 
270          * free it.                            << 
271          */                                    << 
272         put_binfmt_handler(fmt);               << 
273                                                << 
274         return retval;                            148         return retval;
275 }                                                 149 }
276                                                   150 
277 /* Command parsers */                             151 /* Command parsers */
278                                                   152 
279 /*                                                153 /*
280  * parses and copies one argument enclosed in     154  * parses and copies one argument enclosed in del from *sp to *dp,
281  * recognising the \x special.                    155  * recognising the \x special.
282  * returns pointer to the copied argument or N    156  * returns pointer to the copied argument or NULL in case of an
283  * error (and sets err) or null argument lengt    157  * error (and sets err) or null argument length.
284  */                                               158  */
285 static char *scanarg(char *s, char del)           159 static char *scanarg(char *s, char del)
286 {                                                 160 {
287         char c;                                   161         char c;
288                                                   162 
289         while ((c = *s++) != del) {               163         while ((c = *s++) != del) {
290                 if (c == '\\' && *s == 'x') {     164                 if (c == '\\' && *s == 'x') {
291                         s++;                      165                         s++;
292                         if (!isxdigit(*s++))      166                         if (!isxdigit(*s++))
293                                 return NULL;      167                                 return NULL;
294                         if (!isxdigit(*s++))      168                         if (!isxdigit(*s++))
295                                 return NULL;      169                                 return NULL;
296                 }                                 170                 }
297         }                                         171         }
298         s[-1] ='\0';                           << 
299         return s;                                 172         return s;
300 }                                                 173 }
301                                                   174 
302 static char *check_special_flags(char *sfs, No !! 175 static int unquote(char *from)
303 {                                                 176 {
304         char *p = sfs;                         !! 177         char c = 0, *s = from, *p = from;
305         int cont = 1;                          << 
306                                                   178 
307         /* special flags */                    !! 179         while ((c = *s++) != '\0') {
308         while (cont) {                         !! 180                 if (c == '\\' && *s == 'x') {
309                 switch (*p) {                  !! 181                         s++;
310                 case 'P':                      !! 182                         c = toupper(*s++);
311                         pr_debug("register: fl !! 183                         *p = (c - (isdigit(c) ? '' : 'A' - 10)) << 4;
312                         p++;                   !! 184                         c = toupper(*s++);
313                         e->flags |= MISC_FMT_P !! 185                         *p++ |= c - (isdigit(c) ? '' : 'A' - 10);
314                         break;                 !! 186                         continue;
315                 case 'O':                      << 
316                         pr_debug("register: fl << 
317                         p++;                   << 
318                         e->flags |= MISC_FMT_O << 
319                         break;                 << 
320                 case 'C':                      << 
321                         pr_debug("register: fl << 
322                         p++;                   << 
323                         /* this flags also imp << 
324                            open-binary flag */ << 
325                         e->flags |= (MISC_FMT_ << 
326                                         MISC_F << 
327                         break;                 << 
328                 case 'F':                      << 
329                         pr_debug("register: fl << 
330                         p++;                   << 
331                         e->flags |= MISC_FMT_O << 
332                         break;                 << 
333                 default:                       << 
334                         cont = 0;              << 
335                 }                                 187                 }
                                                   >> 188                 *p++ = c;
336         }                                         189         }
337                                                !! 190         return p - from;
338         return p;                              << 
339 }                                                 191 }
340                                                   192 
341 /*                                                193 /*
342  * This registers a new binary format, it reco    194  * This registers a new binary format, it recognises the syntax
343  * ':name:type:offset:magic:mask:interpreter:f !! 195  * ':name:type:offset:magic:mask:interpreter:'
344  * where the ':' is the IFS, that can be chose    196  * where the ':' is the IFS, that can be chosen with the first char
345  */                                               197  */
346 static Node *create_entry(const char __user *b !! 198 static Node *create_entry(const char *buffer, size_t count)
347 {                                                 199 {
348         Node *e;                                  200         Node *e;
349         int memsize, err;                         201         int memsize, err;
350         char *buf, *p;                            202         char *buf, *p;
351         char del;                                 203         char del;
352                                                   204 
353         pr_debug("register: received %zu bytes << 
354                                                << 
355         /* some sanity checks */                  205         /* some sanity checks */
356         err = -EINVAL;                            206         err = -EINVAL;
357         if ((count < 11) || (count > MAX_REGIS !! 207         if ((count < 11) || (count > 256))
358                 goto out;                         208                 goto out;
359                                                   209 
360         err = -ENOMEM;                            210         err = -ENOMEM;
361         memsize = sizeof(Node) + count + 8;       211         memsize = sizeof(Node) + count + 8;
362         e = kmalloc(memsize, GFP_KERNEL_ACCOUN !! 212         e = (Node *) kmalloc(memsize, GFP_USER);
363         if (!e)                                   213         if (!e)
364                 goto out;                         214                 goto out;
365                                                   215 
366         p = buf = (char *)e + sizeof(Node);       216         p = buf = (char *)e + sizeof(Node);
367                                                   217 
368         memset(e, 0, sizeof(Node));               218         memset(e, 0, sizeof(Node));
369         if (copy_from_user(buf, buffer, count)    219         if (copy_from_user(buf, buffer, count))
370                 goto efault;                   !! 220                 goto Efault;
371                                                   221 
372         del = *p++;     /* delimeter */           222         del = *p++;     /* delimeter */
373                                                   223 
374         pr_debug("register: delim: %#x {%c}\n" !! 224         memset(buf+count, del, 8);
375                                                   225 
376         /* Pad the buffer with the delim to si << 
377         memset(buf + count, del, 8);           << 
378                                                << 
379         /* Parse the 'name' field. */          << 
380         e->name = p;                              226         e->name = p;
381         p = strchr(p, del);                       227         p = strchr(p, del);
382         if (!p)                                   228         if (!p)
383                 goto einval;                   !! 229                 goto Einval;
384         *p++ = '\0';                              230         *p++ = '\0';
385         if (!e->name[0] ||                        231         if (!e->name[0] ||
386             !strcmp(e->name, ".") ||              232             !strcmp(e->name, ".") ||
387             !strcmp(e->name, "..") ||             233             !strcmp(e->name, "..") ||
388             strchr(e->name, '/'))                 234             strchr(e->name, '/'))
389                 goto einval;                   !! 235                 goto Einval;
390                                                << 
391         pr_debug("register: name: {%s}\n", e-> << 
392                                                << 
393         /* Parse the 'type' field. */          << 
394         switch (*p++) {                           236         switch (*p++) {
395         case 'E':                              !! 237                 case 'E': e->flags = 1<<Enabled; break;
396                 pr_debug("register: type: E (e !! 238                 case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
397                 e->flags = 1 << Enabled;       !! 239                 default: goto Einval;
398                 break;                         << 
399         case 'M':                              << 
400                 pr_debug("register: type: M (m << 
401                 e->flags = (1 << Enabled) | (1 << 
402                 break;                         << 
403         default:                               << 
404                 goto einval;                   << 
405         }                                         240         }
406         if (*p++ != del)                          241         if (*p++ != del)
407                 goto einval;                   !! 242                 goto Einval;
408                                                << 
409         if (test_bit(Magic, &e->flags)) {         243         if (test_bit(Magic, &e->flags)) {
410                 /* Handle the 'M' (magic) form !! 244                 char *s = strchr(p, del);
411                 char *s;                       << 
412                                                << 
413                 /* Parse the 'offset' field. * << 
414                 s = strchr(p, del);            << 
415                 if (!s)                           245                 if (!s)
416                         goto einval;           !! 246                         goto Einval;
417                 *s = '\0';                     !! 247                 *s++ = '\0';
418                 if (p != s) {                  !! 248                 e->offset = simple_strtoul(p, &p, 10);
419                         int r = kstrtoint(p, 1 << 
420                         if (r != 0 || e->offse << 
421                                 goto einval;   << 
422                 }                              << 
423                 p = s;                         << 
424                 if (*p++)                         249                 if (*p++)
425                         goto einval;           !! 250                         goto Einval;
426                 pr_debug("register: offset: %# << 
427                                                << 
428                 /* Parse the 'magic' field. */ << 
429                 e->magic = p;                     251                 e->magic = p;
430                 p = scanarg(p, del);              252                 p = scanarg(p, del);
431                 if (!p)                           253                 if (!p)
432                         goto einval;           !! 254                         goto Einval;
                                                   >> 255                 p[-1] = '\0';
433                 if (!e->magic[0])                 256                 if (!e->magic[0])
434                         goto einval;           !! 257                         goto Einval;
435                 if (USE_DEBUG)                 << 
436                         print_hex_dump_bytes(  << 
437                                 KBUILD_MODNAME << 
438                                 DUMP_PREFIX_NO << 
439                                                << 
440                 /* Parse the 'mask' field. */  << 
441                 e->mask = p;                      258                 e->mask = p;
442                 p = scanarg(p, del);              259                 p = scanarg(p, del);
443                 if (!p)                           260                 if (!p)
444                         goto einval;           !! 261                         goto Einval;
445                 if (!e->mask[0]) {             !! 262                 p[-1] = '\0';
                                                   >> 263                 if (!e->mask[0])
446                         e->mask = NULL;           264                         e->mask = NULL;
447                         pr_debug("register:  m !! 265                 e->size = unquote(e->magic);
448                 } else if (USE_DEBUG)          !! 266                 if (e->mask && unquote(e->mask) != e->size)
449                         print_hex_dump_bytes(  !! 267                         goto Einval;
450                                 KBUILD_MODNAME !! 268                 if (e->size + e->offset > BINPRM_BUF_SIZE)
451                                 DUMP_PREFIX_NO !! 269                         goto Einval;
452                                                << 
453                 /*                             << 
454                  * Decode the magic & mask fie << 
455                  * Note: while we might have a << 
456                  * above, the unescape helpers << 
457                  * it encounters.              << 
458                  */                            << 
459                 e->size = string_unescape_inpl << 
460                 if (e->mask &&                 << 
461                     string_unescape_inplace(e- << 
462                         goto einval;           << 
463                 if (e->size > BINPRM_BUF_SIZE  << 
464                     BINPRM_BUF_SIZE - e->size  << 
465                         goto einval;           << 
466                 pr_debug("register: magic/mask << 
467                 if (USE_DEBUG) {               << 
468                         print_hex_dump_bytes(  << 
469                                 KBUILD_MODNAME << 
470                                 DUMP_PREFIX_NO << 
471                                                << 
472                         if (e->mask) {         << 
473                                 int i;         << 
474                                 char *masked = << 
475                                                << 
476                                 print_hex_dump << 
477                                         KBUILD << 
478                                         DUMP_P << 
479                                                << 
480                                 if (masked) {  << 
481                                         for (i << 
482                                                << 
483                                         print_ << 
484                                                << 
485                                                << 
486                                                << 
487                                         kfree( << 
488                                 }              << 
489                         }                      << 
490                 }                              << 
491         } else {                                  270         } else {
492                 /* Handle the 'E' (extension)  << 
493                                                << 
494                 /* Skip the 'offset' field. */ << 
495                 p = strchr(p, del);               271                 p = strchr(p, del);
496                 if (!p)                           272                 if (!p)
497                         goto einval;           !! 273                         goto Einval;
498                 *p++ = '\0';                      274                 *p++ = '\0';
499                                                << 
500                 /* Parse the 'magic' field. */ << 
501                 e->magic = p;                     275                 e->magic = p;
502                 p = strchr(p, del);               276                 p = strchr(p, del);
503                 if (!p)                           277                 if (!p)
504                         goto einval;           !! 278                         goto Einval;
505                 *p++ = '\0';                      279                 *p++ = '\0';
506                 if (!e->magic[0] || strchr(e->    280                 if (!e->magic[0] || strchr(e->magic, '/'))
507                         goto einval;           !! 281                         goto Einval;
508                 pr_debug("register: extension: << 
509                                                << 
510                 /* Skip the 'mask' field. */   << 
511                 p = strchr(p, del);               282                 p = strchr(p, del);
512                 if (!p)                           283                 if (!p)
513                         goto einval;           !! 284                         goto Einval;
514                 *p++ = '\0';                      285                 *p++ = '\0';
515         }                                         286         }
516                                                << 
517         /* Parse the 'interpreter' field. */   << 
518         e->interpreter = p;                       287         e->interpreter = p;
519         p = strchr(p, del);                       288         p = strchr(p, del);
520         if (!p)                                   289         if (!p)
521                 goto einval;                   !! 290                 goto Einval;
522         *p++ = '\0';                              291         *p++ = '\0';
523         if (!e->interpreter[0])                   292         if (!e->interpreter[0])
524                 goto einval;                   !! 293                 goto Einval;
525         pr_debug("register: interpreter: {%s}\ !! 294 
                                                   >> 295         if (*p == 'P') {
                                                   >> 296                 p++;
                                                   >> 297                 e->flags |= MISC_FMT_PRESERVE_ARGV0;
                                                   >> 298         }
526                                                   299 
527         /* Parse the 'flags' field. */         << 
528         p = check_special_flags(p, e);         << 
529         if (*p == '\n')                           300         if (*p == '\n')
530                 p++;                              301                 p++;
531         if (p != buf + count)                     302         if (p != buf + count)
532                 goto einval;                   !! 303                 goto Einval;
533                                                << 
534         return e;                                 304         return e;
535                                                   305 
536 out:                                              306 out:
537         return ERR_PTR(err);                      307         return ERR_PTR(err);
538                                                   308 
539 efault:                                        !! 309 Efault:
540         kfree(e);                                 310         kfree(e);
541         return ERR_PTR(-EFAULT);                  311         return ERR_PTR(-EFAULT);
542 einval:                                        !! 312 Einval:
543         kfree(e);                                 313         kfree(e);
544         return ERR_PTR(-EINVAL);                  314         return ERR_PTR(-EINVAL);
545 }                                                 315 }
546                                                   316 
547 /*                                                317 /*
548  * Set status of entry/binfmt_misc:               318  * Set status of entry/binfmt_misc:
549  * '1' enables, '' disables and '-1' clears en    319  * '1' enables, '' disables and '-1' clears entry/binfmt_misc
550  */                                               320  */
551 static int parse_command(const char __user *bu !! 321 static int parse_command(const char *buffer, size_t count)
552 {                                                 322 {
553         char s[4];                                323         char s[4];
554                                                   324 
                                                   >> 325         if (!count)
                                                   >> 326                 return 0;
555         if (count > 3)                            327         if (count > 3)
556                 return -EINVAL;                   328                 return -EINVAL;
557         if (copy_from_user(s, buffer, count))     329         if (copy_from_user(s, buffer, count))
558                 return -EFAULT;                   330                 return -EFAULT;
559         if (!count)                            !! 331         if (s[count-1] == '\n')
560                 return 0;                      << 
561         if (s[count - 1] == '\n')              << 
562                 count--;                          332                 count--;
563         if (count == 1 && s[0] == '')             333         if (count == 1 && s[0] == '')
564                 return 1;                         334                 return 1;
565         if (count == 1 && s[0] == '1')            335         if (count == 1 && s[0] == '1')
566                 return 2;                         336                 return 2;
567         if (count == 2 && s[0] == '-' && s[1]     337         if (count == 2 && s[0] == '-' && s[1] == '1')
568                 return 3;                         338                 return 3;
569         return -EINVAL;                           339         return -EINVAL;
570 }                                                 340 }
571                                                   341 
572 /* generic stuff */                               342 /* generic stuff */
573                                                   343 
574 static void entry_status(Node *e, char *page)     344 static void entry_status(Node *e, char *page)
575 {                                                 345 {
576         char *dp = page;                       !! 346         char *dp;
577         const char *status = "disabled";       !! 347         char *status = "disabled";
578                                                   348 
579         if (test_bit(Enabled, &e->flags))         349         if (test_bit(Enabled, &e->flags))
580                 status = "enabled";               350                 status = "enabled";
581                                                   351 
582         if (!VERBOSE_STATUS) {                    352         if (!VERBOSE_STATUS) {
583                 sprintf(page, "%s\n", status);    353                 sprintf(page, "%s\n", status);
584                 return;                           354                 return;
585         }                                         355         }
586                                                   356 
587         dp += sprintf(dp, "%s\ninterpreter %s\ !! 357         sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
588                                                !! 358         dp = page + strlen(page);
589         /* print the special flags */          << 
590         dp += sprintf(dp, "flags: ");          << 
591         if (e->flags & MISC_FMT_PRESERVE_ARGV0 << 
592                 *dp++ = 'P';                   << 
593         if (e->flags & MISC_FMT_OPEN_BINARY)   << 
594                 *dp++ = 'O';                   << 
595         if (e->flags & MISC_FMT_CREDENTIALS)   << 
596                 *dp++ = 'C';                   << 
597         if (e->flags & MISC_FMT_OPEN_FILE)     << 
598                 *dp++ = 'F';                   << 
599         *dp++ = '\n';                          << 
600                                                << 
601         if (!test_bit(Magic, &e->flags)) {        359         if (!test_bit(Magic, &e->flags)) {
602                 sprintf(dp, "extension .%s\n",    360                 sprintf(dp, "extension .%s\n", e->magic);
603         } else {                                  361         } else {
604                 dp += sprintf(dp, "offset %i\n !! 362                 int i;
605                 dp = bin2hex(dp, e->magic, e-> !! 363 
                                                   >> 364                 sprintf(dp, "offset %i\nmagic ", e->offset);
                                                   >> 365                 dp = page + strlen(page);
                                                   >> 366                 for (i = 0; i < e->size; i++) {
                                                   >> 367                         sprintf(dp, "%02x", 0xff & (int) (e->magic[i]));
                                                   >> 368                         dp += 2;
                                                   >> 369                 }
606                 if (e->mask) {                    370                 if (e->mask) {
607                         dp += sprintf(dp, "\nm !! 371                         sprintf(dp, "\nmask ");
608                         dp = bin2hex(dp, e->ma !! 372                         dp += 6;
                                                   >> 373                         for (i = 0; i < e->size; i++) {
                                                   >> 374                                 sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
                                                   >> 375                                 dp += 2;
                                                   >> 376                         }
609                 }                                 377                 }
610                 *dp++ = '\n';                     378                 *dp++ = '\n';
611                 *dp = '\0';                       379                 *dp = '\0';
612         }                                         380         }
613 }                                                 381 }
614                                                   382 
615 static struct inode *bm_get_inode(struct super    383 static struct inode *bm_get_inode(struct super_block *sb, int mode)
616 {                                                 384 {
617         struct inode *inode = new_inode(sb);   !! 385         struct inode * inode = new_inode(sb);
618                                                   386 
619         if (inode) {                              387         if (inode) {
620                 inode->i_ino = get_next_ino(); << 
621                 inode->i_mode = mode;             388                 inode->i_mode = mode;
622                 simple_inode_init_ts(inode);   !! 389                 inode->i_uid = 0;
                                                   >> 390                 inode->i_gid = 0;
                                                   >> 391                 inode->i_blksize = PAGE_CACHE_SIZE;
                                                   >> 392                 inode->i_blocks = 0;
                                                   >> 393                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
623         }                                         394         }
624         return inode;                             395         return inode;
625 }                                                 396 }
626                                                   397 
627 /**                                            !! 398 static void bm_clear_inode(struct inode *inode)
628  * i_binfmt_misc - retrieve struct binfmt_misc << 
629  * @inode: inode of the relevant binfmt_misc i << 
630  *                                             << 
631  * This helper retrieves struct binfmt_misc fr << 
632  * be done without any memory barriers because << 
633  * user_ns->binfmt_misc is fully initialized.  << 
634  * binfmt_misc mount was first created.        << 
635  *                                             << 
636  * Return: struct binfmt_misc of the relevant  << 
637  */                                            << 
638 static struct binfmt_misc *i_binfmt_misc(struc << 
639 {                                              << 
640         return inode->i_sb->s_user_ns->binfmt_ << 
641 }                                              << 
642                                                << 
643 /**                                            << 
644  * bm_evict_inode - cleanup data associated wi << 
645  * @inode: inode to which the data is attached << 
646  *                                             << 
647  * Cleanup the binary type handler data associ << 
648  * entry is removed or the filesystem is unmou << 
649  * shutdown.                                   << 
650  *                                             << 
651  * If the ->evict call was not caused by a sup << 
652  * to remove the entry or all entries via bm_{ << 
653  * will have already been removed from the lis << 
654  * to make that explicit.                      << 
655 */                                             << 
656 static void bm_evict_inode(struct inode *inode << 
657 {                                                 399 {
658         Node *e = inode->i_private;            !! 400         kfree(inode->u.generic_ip);
659                                                << 
660         clear_inode(inode);                    << 
661                                                << 
662         if (e) {                               << 
663                 struct binfmt_misc *misc;      << 
664                                                << 
665                 misc = i_binfmt_misc(inode);   << 
666                 write_lock(&misc->entries_lock << 
667                 if (!list_empty(&e->list))     << 
668                         list_del_init(&e->list << 
669                 write_unlock(&misc->entries_lo << 
670                 put_binfmt_handler(e);         << 
671         }                                      << 
672 }                                                 401 }
673                                                   402 
674 /**                                            !! 403 static void kill_node(Node *e)
675  * unlink_binfmt_dentry - remove the dentry fo << 
676  * @dentry: dentry associated with the binary  << 
677  *                                             << 
678  * Do the actual filesystem work to remove a d << 
679  * type handler. Since binfmt_misc only allows << 
680  * directly under the root dentry of the files << 
681  * indeed passed a dentry directly beneath the << 
682  * associated with the root dentry is locked,  << 
683  * are asked to remove.                        << 
684  */                                            << 
685 static void unlink_binfmt_dentry(struct dentry << 
686 {                                                 404 {
687         struct dentry *parent = dentry->d_pare !! 405         struct dentry *dentry;
688         struct inode *inode, *parent_inode;    << 
689                                                << 
690         /* All entries are immediate descendan << 
691         if (WARN_ON_ONCE(dentry->d_sb->s_root  << 
692                 return;                        << 
693                                                << 
694         /* We only expect to be called on regu << 
695         inode = d_inode(dentry);               << 
696         if (WARN_ON_ONCE(!S_ISREG(inode->i_mod << 
697                 return;                        << 
698                                                << 
699         /* The parent inode must be locked. */ << 
700         parent_inode = d_inode(parent);        << 
701         if (WARN_ON_ONCE(!inode_is_locked(pare << 
702                 return;                        << 
703                                                   406 
704         if (simple_positive(dentry)) {         !! 407         write_lock(&entries_lock);
705                 dget(dentry);                  !! 408         dentry = e->dentry;
706                 simple_unlink(parent_inode, de !! 409         if (dentry) {
707                 d_delete(dentry);              !! 410                 list_del_init(&e->list);
                                                   >> 411                 e->dentry = NULL;
                                                   >> 412         }
                                                   >> 413         write_unlock(&entries_lock);
                                                   >> 414 
                                                   >> 415         if (dentry) {
                                                   >> 416                 dentry->d_inode->i_nlink--;
                                                   >> 417                 d_drop(dentry);
708                 dput(dentry);                     418                 dput(dentry);
                                                   >> 419                 simple_release_fs(&bm_mnt, &entry_count);
709         }                                         420         }
710 }                                                 421 }
711                                                   422 
712 /**                                            << 
713  * remove_binfmt_handler - remove a binary typ << 
714  * @misc: handle to binfmt_misc instance       << 
715  * @e: binary type handler to remove           << 
716  *                                             << 
717  * Remove a binary type handler from the list  << 
718  * remove its associated dentry. This is calle << 
719  * binfmt_{entry,status}_write(). In the futur << 
720  * adding a proper ->unlink() method to binfmt << 
721  * to use writes to files in order to delete b << 
722  * worked for so long that it's not a pressing << 
723  */                                            << 
724 static void remove_binfmt_handler(struct binfm << 
725 {                                              << 
726         write_lock(&misc->entries_lock);       << 
727         list_del_init(&e->list);               << 
728         write_unlock(&misc->entries_lock);     << 
729         unlink_binfmt_dentry(e->dentry);       << 
730 }                                              << 
731                                                << 
732 /* /<entry> */                                    423 /* /<entry> */
733                                                   424 
734 static ssize_t                                    425 static ssize_t
735 bm_entry_read(struct file *file, char __user * !! 426 bm_entry_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
736 {                                                 427 {
737         Node *e = file_inode(file)->i_private; !! 428         Node *e = file->f_dentry->d_inode->u.generic_ip;
                                                   >> 429         loff_t pos = *ppos;
738         ssize_t res;                              430         ssize_t res;
739         char *page;                               431         char *page;
                                                   >> 432         int len;
740                                                   433 
741         page = (char *) __get_free_page(GFP_KE !! 434         if (!(page = (char*) __get_free_page(GFP_KERNEL)))
742         if (!page)                             << 
743                 return -ENOMEM;                   435                 return -ENOMEM;
744                                                   436 
745         entry_status(e, page);                    437         entry_status(e, page);
                                                   >> 438         len = strlen(page);
746                                                   439 
747         res = simple_read_from_buffer(buf, nby !! 440         res = -EINVAL;
748                                                !! 441         if (pos < 0)
                                                   >> 442                 goto out;
                                                   >> 443         res = 0;
                                                   >> 444         if (pos >= len)
                                                   >> 445                 goto out;
                                                   >> 446         if (len < pos + nbytes)
                                                   >> 447                 nbytes = len - pos;
                                                   >> 448         res = -EFAULT;
                                                   >> 449         if (copy_to_user(buf, page + pos, nbytes))
                                                   >> 450                 goto out;
                                                   >> 451         *ppos = pos + nbytes;
                                                   >> 452         res = nbytes;
                                                   >> 453 out:
749         free_page((unsigned long) page);          454         free_page((unsigned long) page);
750         return res;                               455         return res;
751 }                                                 456 }
752                                                   457 
753 static ssize_t bm_entry_write(struct file *fil !! 458 static ssize_t bm_entry_write(struct file *file, const char *buffer,
754                                 size_t count,     459                                 size_t count, loff_t *ppos)
755 {                                                 460 {
756         struct inode *inode = file_inode(file) !! 461         struct dentry *root;
757         Node *e = inode->i_private;            !! 462         Node *e = file->f_dentry->d_inode->u.generic_ip;
758         int res = parse_command(buffer, count)    463         int res = parse_command(buffer, count);
759                                                   464 
760         switch (res) {                            465         switch (res) {
761         case 1:                                !! 466                 case 1: clear_bit(Enabled, &e->flags);
762                 /* Disable this handler. */    !! 467                         break;
763                 clear_bit(Enabled, &e->flags); !! 468                 case 2: set_bit(Enabled, &e->flags);
764                 break;                         !! 469                         break;
765         case 2:                                !! 470                 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
766                 /* Enable this handler. */     !! 471                         down(&root->d_inode->i_sem);
767                 set_bit(Enabled, &e->flags);   << 
768                 break;                         << 
769         case 3:                                << 
770                 /* Delete this handler. */     << 
771                 inode = d_inode(inode->i_sb->s << 
772                 inode_lock(inode);             << 
773                                                << 
774                 /*                             << 
775                  * In order to add new element << 
776                  * via bm_{entry,register,stat << 
777                  * root inode must be held.    << 
778                  * The lock is exclusive ensur << 
779                  * modified. Only load_misc_bi << 
780                  * read-only. So we only need  << 
781                  * actually remove the entry f << 
782                  */                            << 
783                 if (!list_empty(&e->list))     << 
784                         remove_binfmt_handler( << 
785                                                << 
786                 inode_unlock(inode);           << 
787                 break;                         << 
788         default:                               << 
789                 return res;                    << 
790         }                                      << 
791                                                   472 
                                                   >> 473                         kill_node(e);
                                                   >> 474 
                                                   >> 475                         up(&root->d_inode->i_sem);
                                                   >> 476                         dput(root);
                                                   >> 477                         break;
                                                   >> 478                 default: return res;
                                                   >> 479         }
792         return count;                             480         return count;
793 }                                                 481 }
794                                                   482 
795 static const struct file_operations bm_entry_o !! 483 static struct file_operations bm_entry_operations = {
796         .read           = bm_entry_read,          484         .read           = bm_entry_read,
797         .write          = bm_entry_write,         485         .write          = bm_entry_write,
798         .llseek         = default_llseek,      << 
799 };                                                486 };
800                                                   487 
801 /* /register */                                   488 /* /register */
802                                                   489 
803 static ssize_t bm_register_write(struct file * !! 490 static ssize_t bm_register_write(struct file *file, const char *buffer,
804                                size_t count, l    491                                size_t count, loff_t *ppos)
805 {                                                 492 {
806         Node *e;                                  493         Node *e;
807         struct inode *inode;                      494         struct inode *inode;
808         struct super_block *sb = file_inode(fi !! 495         struct dentry *root, *dentry;
809         struct dentry *root = sb->s_root, *den !! 496         struct super_block *sb = file->f_vfsmnt->mnt_sb;
810         struct binfmt_misc *misc;              << 
811         int err = 0;                              497         int err = 0;
812         struct file *f = NULL;                 << 
813                                                   498 
814         e = create_entry(buffer, count);          499         e = create_entry(buffer, count);
815                                                   500 
816         if (IS_ERR(e))                            501         if (IS_ERR(e))
817                 return PTR_ERR(e);                502                 return PTR_ERR(e);
818                                                   503 
819         if (e->flags & MISC_FMT_OPEN_FILE) {   !! 504         root = dget(sb->s_root);
820                 const struct cred *old_cred;   !! 505         down(&root->d_inode->i_sem);
821                                                << 
822                 /*                             << 
823                  * Now that we support unprivi << 
824                  * sure we use the credentials << 
825                  * opened with to also open th << 
826                  * didn't matter much as only  << 
827                  * the register file.          << 
828                  */                            << 
829                 old_cred = override_creds(file << 
830                 f = open_exec(e->interpreter); << 
831                 revert_creds(old_cred);        << 
832                 if (IS_ERR(f)) {               << 
833                         pr_notice("register: f << 
834                                  e->interprete << 
835                         kfree(e);              << 
836                         return PTR_ERR(f);     << 
837                 }                              << 
838                 e->interp_file = f;            << 
839         }                                      << 
840                                                << 
841         inode_lock(d_inode(root));             << 
842         dentry = lookup_one_len(e->name, root,    506         dentry = lookup_one_len(e->name, root, strlen(e->name));
843         err = PTR_ERR(dentry);                    507         err = PTR_ERR(dentry);
844         if (IS_ERR(dentry))                       508         if (IS_ERR(dentry))
845                 goto out;                         509                 goto out;
846                                                   510 
847         err = -EEXIST;                            511         err = -EEXIST;
848         if (d_really_is_positive(dentry))      !! 512         if (dentry->d_inode)
849                 goto out2;                        513                 goto out2;
850                                                   514 
851         inode = bm_get_inode(sb, S_IFREG | 064    515         inode = bm_get_inode(sb, S_IFREG | 0644);
852                                                   516 
853         err = -ENOMEM;                            517         err = -ENOMEM;
854         if (!inode)                               518         if (!inode)
855                 goto out2;                        519                 goto out2;
856                                                   520 
857         refcount_set(&e->users, 1);            !! 521         err = simple_pin_fs("binfmt_misc", &bm_mnt, &entry_count);
                                                   >> 522         if (err) {
                                                   >> 523                 iput(inode);
                                                   >> 524                 inode = NULL;
                                                   >> 525                 goto out2;
                                                   >> 526         }
                                                   >> 527 
858         e->dentry = dget(dentry);                 528         e->dentry = dget(dentry);
859         inode->i_private = e;                  !! 529         inode->u.generic_ip = e;
860         inode->i_fop = &bm_entry_operations;      530         inode->i_fop = &bm_entry_operations;
861                                                   531 
862         d_instantiate(dentry, inode);             532         d_instantiate(dentry, inode);
863         misc = i_binfmt_misc(inode);           !! 533         write_lock(&entries_lock);
864         write_lock(&misc->entries_lock);       !! 534         list_add(&e->list, &entries);
865         list_add(&e->list, &misc->entries);    !! 535         write_unlock(&entries_lock);
866         write_unlock(&misc->entries_lock);     << 
867                                                   536 
868         err = 0;                                  537         err = 0;
869 out2:                                             538 out2:
870         dput(dentry);                             539         dput(dentry);
871 out:                                              540 out:
872         inode_unlock(d_inode(root));           !! 541         up(&root->d_inode->i_sem);
                                                   >> 542         dput(root);
873                                                   543 
874         if (err) {                                544         if (err) {
875                 if (f)                         << 
876                         filp_close(f, NULL);   << 
877                 kfree(e);                         545                 kfree(e);
878                 return err;                    !! 546                 return -EINVAL;
879         }                                         547         }
880         return count;                             548         return count;
881 }                                                 549 }
882                                                   550 
883 static const struct file_operations bm_registe !! 551 static struct file_operations bm_register_operations = {
884         .write          = bm_register_write,      552         .write          = bm_register_write,
885         .llseek         = noop_llseek,         << 
886 };                                                553 };
887                                                   554 
888 /* /status */                                     555 /* /status */
889                                                   556 
890 static ssize_t                                    557 static ssize_t
891 bm_status_read(struct file *file, char __user  !! 558 bm_status_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
892 {                                                 559 {
893         struct binfmt_misc *misc;              !! 560         char *s = enabled ? "enabled" : "disabled";
894         char *s;                               !! 561         int len = strlen(s);
                                                   >> 562         loff_t pos = *ppos;
895                                                   563 
896         misc = i_binfmt_misc(file_inode(file)) !! 564         if (pos < 0)
897         s = misc->enabled ? "enabled\n" : "dis !! 565                 return -EINVAL;
898         return simple_read_from_buffer(buf, nb !! 566         if (pos >= len)
                                                   >> 567                 return 0;
                                                   >> 568         if (len < pos + nbytes)
                                                   >> 569                 nbytes = len - pos;
                                                   >> 570         if (copy_to_user(buf, s + pos, nbytes))
                                                   >> 571                 return -EFAULT;
                                                   >> 572         *ppos = pos + nbytes;
                                                   >> 573         return nbytes;
899 }                                                 574 }
900                                                   575 
901 static ssize_t bm_status_write(struct file *fi !! 576 static ssize_t bm_status_write(struct file * file, const char * buffer,
902                 size_t count, loff_t *ppos)       577                 size_t count, loff_t *ppos)
903 {                                                 578 {
904         struct binfmt_misc *misc;              << 
905         int res = parse_command(buffer, count)    579         int res = parse_command(buffer, count);
906         Node *e, *next;                        !! 580         struct dentry *root;
907         struct inode *inode;                   << 
908                                                   581 
909         misc = i_binfmt_misc(file_inode(file)) << 
910         switch (res) {                            582         switch (res) {
911         case 1:                                !! 583                 case 1: enabled = 0; break;
912                 /* Disable all handlers. */    !! 584                 case 2: enabled = 1; break;
913                 misc->enabled = false;         !! 585                 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
914                 break;                         !! 586                         down(&root->d_inode->i_sem);
915         case 2:                                !! 587 
916                 /* Enable all handlers. */     !! 588                         while (!list_empty(&entries))
917                 misc->enabled = true;          !! 589                                 kill_node(list_entry(entries.next, Node, list));
918                 break;                         !! 590 
919         case 3:                                !! 591                         up(&root->d_inode->i_sem);
920                 /* Delete all handlers. */     !! 592                         dput(root);
921                 inode = d_inode(file_inode(fil !! 593                 default: return res;
922                 inode_lock(inode);             << 
923                                                << 
924                 /*                             << 
925                  * In order to add new element << 
926                  * via bm_{entry,register,stat << 
927                  * root inode must be held.    << 
928                  * The lock is exclusive ensur << 
929                  * modified. Only load_misc_bi << 
930                  * read-only. So we only need  << 
931                  * actually remove the entry f << 
932                  */                            << 
933                 list_for_each_entry_safe(e, ne << 
934                         remove_binfmt_handler( << 
935                                                << 
936                 inode_unlock(inode);           << 
937                 break;                         << 
938         default:                               << 
939                 return res;                    << 
940         }                                         594         }
941                                                << 
942         return count;                             595         return count;
943 }                                                 596 }
944                                                   597 
945 static const struct file_operations bm_status_ !! 598 static struct file_operations bm_status_operations = {
946         .read           = bm_status_read,         599         .read           = bm_status_read,
947         .write          = bm_status_write,        600         .write          = bm_status_write,
948         .llseek         = default_llseek,      << 
949 };                                                601 };
950                                                   602 
951 /* Superblock handling */                         603 /* Superblock handling */
952                                                   604 
953 static void bm_put_super(struct super_block *s !! 605 static struct super_operations s_ops = {
954 {                                              << 
955         struct user_namespace *user_ns = sb->s << 
956                                                << 
957         sb->s_fs_info = NULL;                  << 
958         put_user_ns(user_ns);                  << 
959 }                                              << 
960                                                << 
961 static const struct super_operations s_ops = { << 
962         .statfs         = simple_statfs,          606         .statfs         = simple_statfs,
963         .evict_inode    = bm_evict_inode,      !! 607         .clear_inode    = bm_clear_inode,
964         .put_super      = bm_put_super,        << 
965 };                                                608 };
966                                                   609 
967 static int bm_fill_super(struct super_block *s !! 610 static int bm_fill_super(struct super_block * sb, void * data, int silent)
968 {                                                 611 {
969         int err;                               !! 612         static struct tree_descr bm_files[] = {
970         struct user_namespace *user_ns = sb->s !! 613                 [1] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
971         struct binfmt_misc *misc;              !! 614                 [2] = {"register", &bm_register_operations, S_IWUSR},
972         static const struct tree_descr bm_file << 
973                 [2] = {"status", &bm_status_op << 
974                 [3] = {"register", &bm_registe << 
975                 /* last one */ {""}               615                 /* last one */ {""}
976         };                                        616         };
977                                                !! 617         int err = simple_fill_super(sb, 0x42494e4d, bm_files);
978         if (WARN_ON(user_ns != current_user_ns << 
979                 return -EINVAL;                << 
980                                                << 
981         /*                                     << 
982          * Lazily allocate a new binfmt_misc i << 
983          * do it here during the first mount o << 
984          * waste memory for every user namespa << 
985          * more common to not mount a separate << 
986          * to mount one.                       << 
987          *                                     << 
988          * While multiple superblocks can exis << 
989          * s_fs_info for binfmt_misc. Hence, t << 
990          * bm_fill_super() is called exactly o << 
991          * superblock for a userns is created. << 
992          * that when a binfmt_misc superblock  << 
993          * a userns there's no one racing us.  << 
994          * barriers when we dereference binfmt << 
995          */                                    << 
996         misc = user_ns->binfmt_misc;           << 
997         if (!misc) {                           << 
998                 /*                             << 
999                  * If it turns out that most u << 
1000                  * register their own binary  << 
1001                  * create their own separate  << 
1002                  * consider turning this into << 
1003                  */                           << 
1004                 misc = kzalloc(sizeof(struct  << 
1005                 if (!misc)                    << 
1006                         return -ENOMEM;       << 
1007                                               << 
1008                 INIT_LIST_HEAD(&misc->entries << 
1009                 rwlock_init(&misc->entries_lo << 
1010                                               << 
1011                 /* Pairs with smp_load_acquir << 
1012                 smp_store_release(&user_ns->b << 
1013         }                                     << 
1014                                               << 
1015         /*                                    << 
1016          * When the binfmt_misc superblock fo << 
1017          * ->enabled might have been set to f << 
1018          * ->enabled again in put_super() as  << 
1019          * binfmt_misc again. It also would b << 
1020          * ->put_super() is called we know th << 
1021          * bintfmt_misc mount is empty making << 
1022          * -ENOEXEC independent of whether -> << 
1023          * someone mounts binfmt_misc for the << 
1024          * reset ->enabled to true.           << 
1025          */                                   << 
1026         misc->enabled = true;                 << 
1027                                               << 
1028         err = simple_fill_super(sb, BINFMTFS_ << 
1029         if (!err)                                618         if (!err)
1030                 sb->s_op = &s_ops;               619                 sb->s_op = &s_ops;
1031         return err;                              620         return err;
1032 }                                                621 }
1033                                                  622 
1034 static void bm_free(struct fs_context *fc)    !! 623 static struct super_block *bm_get_sb(struct file_system_type *fs_type,
                                                   >> 624         int flags, const char *dev_name, void *data)
1035 {                                                625 {
1036         if (fc->s_fs_info)                    !! 626         return get_sb_single(fs_type, flags, data, bm_fill_super);
1037                 put_user_ns(fc->s_fs_info);   << 
1038 }                                             << 
1039                                               << 
1040 static int bm_get_tree(struct fs_context *fc) << 
1041 {                                             << 
1042         return get_tree_keyed(fc, bm_fill_sup << 
1043 }                                             << 
1044                                               << 
1045 static const struct fs_context_operations bm_ << 
1046         .free           = bm_free,            << 
1047         .get_tree       = bm_get_tree,        << 
1048 };                                            << 
1049                                               << 
1050 static int bm_init_fs_context(struct fs_conte << 
1051 {                                             << 
1052         fc->ops = &bm_context_ops;            << 
1053         return 0;                             << 
1054 }                                                627 }
1055                                                  628 
1056 static struct linux_binfmt misc_format = {       629 static struct linux_binfmt misc_format = {
1057         .module = THIS_MODULE,                   630         .module = THIS_MODULE,
1058         .load_binary = load_misc_binary,         631         .load_binary = load_misc_binary,
1059 };                                               632 };
1060                                                  633 
1061 static struct file_system_type bm_fs_type = {    634 static struct file_system_type bm_fs_type = {
1062         .owner          = THIS_MODULE,           635         .owner          = THIS_MODULE,
1063         .name           = "binfmt_misc",         636         .name           = "binfmt_misc",
1064         .init_fs_context = bm_init_fs_context !! 637         .get_sb         = bm_get_sb,
1065         .fs_flags       = FS_USERNS_MOUNT,    << 
1066         .kill_sb        = kill_litter_super,     638         .kill_sb        = kill_litter_super,
1067 };                                               639 };
1068 MODULE_ALIAS_FS("binfmt_misc");               << 
1069                                                  640 
1070 static int __init init_misc_binfmt(void)         641 static int __init init_misc_binfmt(void)
1071 {                                                642 {
1072         int err = register_filesystem(&bm_fs_    643         int err = register_filesystem(&bm_fs_type);
1073         if (!err)                             !! 644         if (!err) {
1074                 insert_binfmt(&misc_format);  !! 645                 err = register_binfmt(&misc_format);
                                                   >> 646                 if (err)
                                                   >> 647                         unregister_filesystem(&bm_fs_type);
                                                   >> 648         }
1075         return err;                              649         return err;
1076 }                                                650 }
1077                                                  651 
1078 static void __exit exit_misc_binfmt(void)        652 static void __exit exit_misc_binfmt(void)
1079 {                                                653 {
1080         unregister_binfmt(&misc_format);         654         unregister_binfmt(&misc_format);
1081         unregister_filesystem(&bm_fs_type);      655         unregister_filesystem(&bm_fs_type);
1082 }                                                656 }
1083                                                  657 
1084 core_initcall(init_misc_binfmt);              !! 658 module_init(init_misc_binfmt);
1085 module_exit(exit_misc_binfmt);                   659 module_exit(exit_misc_binfmt);
1086 MODULE_DESCRIPTION("Kernel support for miscel << 
1087 MODULE_LICENSE("GPL");                           660 MODULE_LICENSE("GPL");
1088                                                  661 

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