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. See Documentation/admin-guide/binfmt-misc.rst for more details. 9 */ 8 */ 10 9 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 11 13 #include <linux/kernel.h> 12 #include <linux/kernel.h> 14 #include <linux/module.h> 13 #include <linux/module.h> 15 #include <linux/init.h> 14 #include <linux/init.h> 16 #include <linux/sched/mm.h> 15 #include <linux/sched/mm.h> 17 #include <linux/magic.h> 16 #include <linux/magic.h> 18 #include <linux/binfmts.h> 17 #include <linux/binfmts.h> 19 #include <linux/slab.h> 18 #include <linux/slab.h> 20 #include <linux/ctype.h> 19 #include <linux/ctype.h> 21 #include <linux/string_helpers.h> 20 #include <linux/string_helpers.h> 22 #include <linux/file.h> 21 #include <linux/file.h> 23 #include <linux/pagemap.h> 22 #include <linux/pagemap.h> 24 #include <linux/namei.h> 23 #include <linux/namei.h> 25 #include <linux/mount.h> 24 #include <linux/mount.h> 26 #include <linux/fs_context.h> << 27 #include <linux/syscalls.h> 25 #include <linux/syscalls.h> 28 #include <linux/fs.h> 26 #include <linux/fs.h> 29 #include <linux/uaccess.h> 27 #include <linux/uaccess.h> 30 28 31 #include "internal.h" 29 #include "internal.h" 32 30 33 #ifdef DEBUG 31 #ifdef DEBUG 34 # define USE_DEBUG 1 32 # define USE_DEBUG 1 35 #else 33 #else 36 # define USE_DEBUG 0 34 # define USE_DEBUG 0 37 #endif 35 #endif 38 36 39 enum { 37 enum { 40 VERBOSE_STATUS = 1 /* make it zero to 38 VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */ 41 }; 39 }; 42 40 >> 41 static LIST_HEAD(entries); >> 42 static int enabled = 1; >> 43 43 enum {Enabled, Magic}; 44 enum {Enabled, Magic}; 44 #define MISC_FMT_PRESERVE_ARGV0 (1UL << 31) !! 45 #define MISC_FMT_PRESERVE_ARGV0 (1 << 31) 45 #define MISC_FMT_OPEN_BINARY (1UL << 30) !! 46 #define MISC_FMT_OPEN_BINARY (1 << 30) 46 #define MISC_FMT_CREDENTIALS (1UL << 29) !! 47 #define MISC_FMT_CREDENTIALS (1 << 29) 47 #define MISC_FMT_OPEN_FILE (1UL << 28) !! 48 #define MISC_FMT_OPEN_FILE (1 << 28) 48 49 49 typedef struct { 50 typedef struct { 50 struct list_head list; 51 struct list_head list; 51 unsigned long flags; /* typ 52 unsigned long flags; /* type, status, etc. */ 52 int offset; /* off 53 int offset; /* offset of magic */ 53 int size; /* siz 54 int size; /* size of magic/mask */ 54 char *magic; /* mag 55 char *magic; /* magic or filename extension */ 55 char *mask; /* mas 56 char *mask; /* mask, NULL for exact match */ 56 const char *interpreter; /* fil 57 const char *interpreter; /* filename of interpreter */ 57 char *name; 58 char *name; 58 struct dentry *dentry; 59 struct dentry *dentry; 59 struct file *interp_file; 60 struct file *interp_file; 60 refcount_t users; /* syn << 61 } Node; 61 } Node; 62 62 >> 63 static DEFINE_RWLOCK(entries_lock); 63 static struct file_system_type bm_fs_type; 64 static struct file_system_type bm_fs_type; >> 65 static struct vfsmount *bm_mnt; >> 66 static int entry_count; 64 67 65 /* 68 /* 66 * Max length of the register string. Determi 69 * Max length of the register string. Determined by: 67 * - 7 delimiters 70 * - 7 delimiters 68 * - name: ~50 bytes 71 * - name: ~50 bytes 69 * - type: 1 byte 72 * - type: 1 byte 70 * - offset: 3 bytes (has to be smaller than 73 * - offset: 3 bytes (has to be smaller than BINPRM_BUF_SIZE) 71 * - magic: 128 bytes (512 in escaped form) 74 * - magic: 128 bytes (512 in escaped form) 72 * - mask: 128 bytes (512 in escaped form) 75 * - mask: 128 bytes (512 in escaped form) 73 * - interp: ~50 bytes 76 * - interp: ~50 bytes 74 * - flags: 5 bytes 77 * - flags: 5 bytes 75 * Round that up a bit, and then back off to h 78 * Round that up a bit, and then back off to hold the internal data 76 * (like struct Node). 79 * (like struct Node). 77 */ 80 */ 78 #define MAX_REGISTER_LENGTH 1920 81 #define MAX_REGISTER_LENGTH 1920 79 82 80 /** !! 83 /* 81 * search_binfmt_handler - search for a binary !! 84 * Check if we support the binfmt 82 * @misc: handle to binfmt_misc instance !! 85 * if we do, return the node, else NULL 83 * @bprm: binary for which we are looking for !! 86 * locking is done in load_misc_binary 84 * << 85 * Search for a binary type handler for @bprm << 86 * type handlers. << 87 * << 88 * Return: binary type list entry on success, << 89 */ 87 */ 90 static Node *search_binfmt_handler(struct binf !! 88 static Node *check_file(struct linux_binprm *bprm) 91 struct linu << 92 { 89 { 93 char *p = strrchr(bprm->interp, '.'); 90 char *p = strrchr(bprm->interp, '.'); 94 Node *e; !! 91 struct list_head *l; 95 92 96 /* Walk all the registered handlers. * 93 /* Walk all the registered handlers. */ 97 list_for_each_entry(e, &misc->entries, !! 94 list_for_each(l, &entries) { >> 95 Node *e = list_entry(l, Node, list); 98 char *s; 96 char *s; 99 int j; 97 int j; 100 98 101 /* Make sure this one is curre 99 /* Make sure this one is currently enabled. */ 102 if (!test_bit(Enabled, &e->fla 100 if (!test_bit(Enabled, &e->flags)) 103 continue; 101 continue; 104 102 105 /* Do matching based on extens 103 /* Do matching based on extension if applicable. */ 106 if (!test_bit(Magic, &e->flags 104 if (!test_bit(Magic, &e->flags)) { 107 if (p && !strcmp(e->ma 105 if (p && !strcmp(e->magic, p + 1)) 108 return e; 106 return e; 109 continue; 107 continue; 110 } 108 } 111 109 112 /* Do matching based on magic 110 /* Do matching based on magic & mask. */ 113 s = bprm->buf + e->offset; 111 s = bprm->buf + e->offset; 114 if (e->mask) { 112 if (e->mask) { 115 for (j = 0; j < e->siz 113 for (j = 0; j < e->size; j++) 116 if ((*s++ ^ e- 114 if ((*s++ ^ e->magic[j]) & e->mask[j]) 117 break; 115 break; 118 } else { 116 } else { 119 for (j = 0; j < e->siz 117 for (j = 0; j < e->size; j++) 120 if ((*s++ ^ e- 118 if ((*s++ ^ e->magic[j])) 121 break; 119 break; 122 } 120 } 123 if (j == e->size) 121 if (j == e->size) 124 return e; 122 return e; 125 } 123 } 126 << 127 return NULL; 124 return NULL; 128 } 125 } 129 126 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 /* 127 /* 200 * the loader itself 128 * the loader itself 201 */ 129 */ 202 static int load_misc_binary(struct linux_binpr 130 static int load_misc_binary(struct linux_binprm *bprm) 203 { 131 { 204 Node *fmt; 132 Node *fmt; 205 struct file *interp_file = NULL; 133 struct file *interp_file = NULL; 206 int retval = -ENOEXEC; !! 134 int retval; 207 struct binfmt_misc *misc; !! 135 int fd_binary = -1; 208 136 209 misc = load_binfmt_misc(); !! 137 retval = -ENOEXEC; 210 if (!misc->enabled) !! 138 if (!enabled) 211 return retval; 139 return retval; 212 140 213 fmt = get_binfmt_handler(misc, bprm); !! 141 /* to keep locking time low, we copy the interpreter string */ >> 142 read_lock(&entries_lock); >> 143 fmt = check_file(bprm); >> 144 if (fmt) >> 145 dget(fmt->dentry); >> 146 read_unlock(&entries_lock); 214 if (!fmt) 147 if (!fmt) 215 return retval; 148 return retval; 216 149 217 /* Need to be able to load the file af 150 /* Need to be able to load the file after exec */ 218 retval = -ENOENT; 151 retval = -ENOENT; 219 if (bprm->interp_flags & BINPRM_FLAGS_ 152 if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE) 220 goto ret; 153 goto ret; 221 154 222 if (fmt->flags & MISC_FMT_PRESERVE_ARG !! 155 if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) { 223 bprm->interp_flags |= BINPRM_F << 224 } else { << 225 retval = remove_arg_zero(bprm) 156 retval = remove_arg_zero(bprm); 226 if (retval) 157 if (retval) 227 goto ret; 158 goto ret; 228 } 159 } 229 160 230 if (fmt->flags & MISC_FMT_OPEN_BINARY) !! 161 if (fmt->flags & MISC_FMT_OPEN_BINARY) { 231 bprm->have_execfd = 1; << 232 162 >> 163 /* if the binary should be opened on behalf of the >> 164 * interpreter than keep it open and assign descriptor >> 165 * to it >> 166 */ >> 167 fd_binary = get_unused_fd_flags(0); >> 168 if (fd_binary < 0) { >> 169 retval = fd_binary; >> 170 goto ret; >> 171 } >> 172 fd_install(fd_binary, bprm->file); >> 173 >> 174 /* if the binary is not readable than enforce mm->dumpable=0 >> 175 regardless of the interpreter's permissions */ >> 176 would_dump(bprm, bprm->file); >> 177 >> 178 allow_write_access(bprm->file); >> 179 bprm->file = NULL; >> 180 >> 181 /* mark the bprm that fd should be passed to interp */ >> 182 bprm->interp_flags |= BINPRM_FLAGS_EXECFD; >> 183 bprm->interp_data = fd_binary; >> 184 >> 185 } else { >> 186 allow_write_access(bprm->file); >> 187 fput(bprm->file); >> 188 bprm->file = NULL; >> 189 } 233 /* make argv[1] be the path to the bin 190 /* make argv[1] be the path to the binary */ 234 retval = copy_string_kernel(bprm->inte !! 191 retval = copy_strings_kernel(1, &bprm->interp, bprm); 235 if (retval < 0) 192 if (retval < 0) 236 goto ret; !! 193 goto error; 237 bprm->argc++; 194 bprm->argc++; 238 195 239 /* add the interp as argv[0] */ 196 /* add the interp as argv[0] */ 240 retval = copy_string_kernel(fmt->inter !! 197 retval = copy_strings_kernel(1, &fmt->interpreter, bprm); 241 if (retval < 0) 198 if (retval < 0) 242 goto ret; !! 199 goto error; 243 bprm->argc++; 200 bprm->argc++; 244 201 245 /* Update interp in case binfmt_script 202 /* Update interp in case binfmt_script needs it. */ 246 retval = bprm_change_interp(fmt->inter 203 retval = bprm_change_interp(fmt->interpreter, bprm); 247 if (retval < 0) 204 if (retval < 0) 248 goto ret; !! 205 goto error; 249 206 250 if (fmt->flags & MISC_FMT_OPEN_FILE) !! 207 if (fmt->flags & MISC_FMT_OPEN_FILE) { 251 interp_file = file_clone_open( 208 interp_file = file_clone_open(fmt->interp_file); 252 else !! 209 if (!IS_ERR(interp_file)) >> 210 deny_write_access(interp_file); >> 211 } else { 253 interp_file = open_exec(fmt->i 212 interp_file = open_exec(fmt->interpreter); >> 213 } 254 retval = PTR_ERR(interp_file); 214 retval = PTR_ERR(interp_file); 255 if (IS_ERR(interp_file)) 215 if (IS_ERR(interp_file)) 256 goto ret; !! 216 goto error; 257 217 258 bprm->interpreter = interp_file; !! 218 bprm->file = interp_file; 259 if (fmt->flags & MISC_FMT_CREDENTIALS) !! 219 if (fmt->flags & MISC_FMT_CREDENTIALS) { 260 bprm->execfd_creds = 1; !! 220 loff_t pos = 0; 261 221 262 retval = 0; !! 222 /* 263 ret: !! 223 * No need to call prepare_binprm(), it's already been >> 224 * done. bprm->buf is stale, update from interp_file. >> 225 */ >> 226 memset(bprm->buf, 0, BINPRM_BUF_SIZE); >> 227 retval = kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, >> 228 &pos); >> 229 } else >> 230 retval = prepare_binprm(bprm); 264 231 265 /* !! 232 if (retval < 0) 266 * If we actually put the node here al !! 233 goto error; 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 234 >> 235 retval = search_binary_handler(bprm); >> 236 if (retval < 0) >> 237 goto error; >> 238 >> 239 ret: >> 240 dput(fmt->dentry); 274 return retval; 241 return retval; >> 242 error: >> 243 if (fd_binary > 0) >> 244 ksys_close(fd_binary); >> 245 bprm->interp_flags = 0; >> 246 bprm->interp_data = 0; >> 247 goto ret; 275 } 248 } 276 249 277 /* Command parsers */ 250 /* Command parsers */ 278 251 279 /* 252 /* 280 * parses and copies one argument enclosed in 253 * parses and copies one argument enclosed in del from *sp to *dp, 281 * recognising the \x special. 254 * recognising the \x special. 282 * returns pointer to the copied argument or N 255 * returns pointer to the copied argument or NULL in case of an 283 * error (and sets err) or null argument lengt 256 * error (and sets err) or null argument length. 284 */ 257 */ 285 static char *scanarg(char *s, char del) 258 static char *scanarg(char *s, char del) 286 { 259 { 287 char c; 260 char c; 288 261 289 while ((c = *s++) != del) { 262 while ((c = *s++) != del) { 290 if (c == '\\' && *s == 'x') { 263 if (c == '\\' && *s == 'x') { 291 s++; 264 s++; 292 if (!isxdigit(*s++)) 265 if (!isxdigit(*s++)) 293 return NULL; 266 return NULL; 294 if (!isxdigit(*s++)) 267 if (!isxdigit(*s++)) 295 return NULL; 268 return NULL; 296 } 269 } 297 } 270 } 298 s[-1] ='\0'; 271 s[-1] ='\0'; 299 return s; 272 return s; 300 } 273 } 301 274 302 static char *check_special_flags(char *sfs, No 275 static char *check_special_flags(char *sfs, Node *e) 303 { 276 { 304 char *p = sfs; 277 char *p = sfs; 305 int cont = 1; 278 int cont = 1; 306 279 307 /* special flags */ 280 /* special flags */ 308 while (cont) { 281 while (cont) { 309 switch (*p) { 282 switch (*p) { 310 case 'P': 283 case 'P': 311 pr_debug("register: fl 284 pr_debug("register: flag: P (preserve argv0)\n"); 312 p++; 285 p++; 313 e->flags |= MISC_FMT_P 286 e->flags |= MISC_FMT_PRESERVE_ARGV0; 314 break; 287 break; 315 case 'O': 288 case 'O': 316 pr_debug("register: fl 289 pr_debug("register: flag: O (open binary)\n"); 317 p++; 290 p++; 318 e->flags |= MISC_FMT_O 291 e->flags |= MISC_FMT_OPEN_BINARY; 319 break; 292 break; 320 case 'C': 293 case 'C': 321 pr_debug("register: fl 294 pr_debug("register: flag: C (preserve creds)\n"); 322 p++; 295 p++; 323 /* this flags also imp 296 /* this flags also implies the 324 open-binary flag */ 297 open-binary flag */ 325 e->flags |= (MISC_FMT_ 298 e->flags |= (MISC_FMT_CREDENTIALS | 326 MISC_F 299 MISC_FMT_OPEN_BINARY); 327 break; 300 break; 328 case 'F': 301 case 'F': 329 pr_debug("register: fl 302 pr_debug("register: flag: F: open interpreter file now\n"); 330 p++; 303 p++; 331 e->flags |= MISC_FMT_O 304 e->flags |= MISC_FMT_OPEN_FILE; 332 break; 305 break; 333 default: 306 default: 334 cont = 0; 307 cont = 0; 335 } 308 } 336 } 309 } 337 310 338 return p; 311 return p; 339 } 312 } 340 313 341 /* 314 /* 342 * This registers a new binary format, it reco 315 * This registers a new binary format, it recognises the syntax 343 * ':name:type:offset:magic:mask:interpreter:f 316 * ':name:type:offset:magic:mask:interpreter:flags' 344 * where the ':' is the IFS, that can be chose 317 * where the ':' is the IFS, that can be chosen with the first char 345 */ 318 */ 346 static Node *create_entry(const char __user *b 319 static Node *create_entry(const char __user *buffer, size_t count) 347 { 320 { 348 Node *e; 321 Node *e; 349 int memsize, err; 322 int memsize, err; 350 char *buf, *p; 323 char *buf, *p; 351 char del; 324 char del; 352 325 353 pr_debug("register: received %zu bytes 326 pr_debug("register: received %zu bytes\n", count); 354 327 355 /* some sanity checks */ 328 /* some sanity checks */ 356 err = -EINVAL; 329 err = -EINVAL; 357 if ((count < 11) || (count > MAX_REGIS 330 if ((count < 11) || (count > MAX_REGISTER_LENGTH)) 358 goto out; 331 goto out; 359 332 360 err = -ENOMEM; 333 err = -ENOMEM; 361 memsize = sizeof(Node) + count + 8; 334 memsize = sizeof(Node) + count + 8; 362 e = kmalloc(memsize, GFP_KERNEL_ACCOUN !! 335 e = kmalloc(memsize, GFP_KERNEL); 363 if (!e) 336 if (!e) 364 goto out; 337 goto out; 365 338 366 p = buf = (char *)e + sizeof(Node); 339 p = buf = (char *)e + sizeof(Node); 367 340 368 memset(e, 0, sizeof(Node)); 341 memset(e, 0, sizeof(Node)); 369 if (copy_from_user(buf, buffer, count) 342 if (copy_from_user(buf, buffer, count)) 370 goto efault; 343 goto efault; 371 344 372 del = *p++; /* delimeter */ 345 del = *p++; /* delimeter */ 373 346 374 pr_debug("register: delim: %#x {%c}\n" 347 pr_debug("register: delim: %#x {%c}\n", del, del); 375 348 376 /* Pad the buffer with the delim to si 349 /* Pad the buffer with the delim to simplify parsing below. */ 377 memset(buf + count, del, 8); 350 memset(buf + count, del, 8); 378 351 379 /* Parse the 'name' field. */ 352 /* Parse the 'name' field. */ 380 e->name = p; 353 e->name = p; 381 p = strchr(p, del); 354 p = strchr(p, del); 382 if (!p) 355 if (!p) 383 goto einval; 356 goto einval; 384 *p++ = '\0'; 357 *p++ = '\0'; 385 if (!e->name[0] || 358 if (!e->name[0] || 386 !strcmp(e->name, ".") || 359 !strcmp(e->name, ".") || 387 !strcmp(e->name, "..") || 360 !strcmp(e->name, "..") || 388 strchr(e->name, '/')) 361 strchr(e->name, '/')) 389 goto einval; 362 goto einval; 390 363 391 pr_debug("register: name: {%s}\n", e-> 364 pr_debug("register: name: {%s}\n", e->name); 392 365 393 /* Parse the 'type' field. */ 366 /* Parse the 'type' field. */ 394 switch (*p++) { 367 switch (*p++) { 395 case 'E': 368 case 'E': 396 pr_debug("register: type: E (e 369 pr_debug("register: type: E (extension)\n"); 397 e->flags = 1 << Enabled; 370 e->flags = 1 << Enabled; 398 break; 371 break; 399 case 'M': 372 case 'M': 400 pr_debug("register: type: M (m 373 pr_debug("register: type: M (magic)\n"); 401 e->flags = (1 << Enabled) | (1 374 e->flags = (1 << Enabled) | (1 << Magic); 402 break; 375 break; 403 default: 376 default: 404 goto einval; 377 goto einval; 405 } 378 } 406 if (*p++ != del) 379 if (*p++ != del) 407 goto einval; 380 goto einval; 408 381 409 if (test_bit(Magic, &e->flags)) { 382 if (test_bit(Magic, &e->flags)) { 410 /* Handle the 'M' (magic) form 383 /* Handle the 'M' (magic) format. */ 411 char *s; 384 char *s; 412 385 413 /* Parse the 'offset' field. * 386 /* Parse the 'offset' field. */ 414 s = strchr(p, del); 387 s = strchr(p, del); 415 if (!s) 388 if (!s) 416 goto einval; 389 goto einval; 417 *s = '\0'; 390 *s = '\0'; 418 if (p != s) { 391 if (p != s) { 419 int r = kstrtoint(p, 1 392 int r = kstrtoint(p, 10, &e->offset); 420 if (r != 0 || e->offse 393 if (r != 0 || e->offset < 0) 421 goto einval; 394 goto einval; 422 } 395 } 423 p = s; 396 p = s; 424 if (*p++) 397 if (*p++) 425 goto einval; 398 goto einval; 426 pr_debug("register: offset: %# 399 pr_debug("register: offset: %#x\n", e->offset); 427 400 428 /* Parse the 'magic' field. */ 401 /* Parse the 'magic' field. */ 429 e->magic = p; 402 e->magic = p; 430 p = scanarg(p, del); 403 p = scanarg(p, del); 431 if (!p) 404 if (!p) 432 goto einval; 405 goto einval; 433 if (!e->magic[0]) 406 if (!e->magic[0]) 434 goto einval; 407 goto einval; 435 if (USE_DEBUG) 408 if (USE_DEBUG) 436 print_hex_dump_bytes( 409 print_hex_dump_bytes( 437 KBUILD_MODNAME 410 KBUILD_MODNAME ": register: magic[raw]: ", 438 DUMP_PREFIX_NO 411 DUMP_PREFIX_NONE, e->magic, p - e->magic); 439 412 440 /* Parse the 'mask' field. */ 413 /* Parse the 'mask' field. */ 441 e->mask = p; 414 e->mask = p; 442 p = scanarg(p, del); 415 p = scanarg(p, del); 443 if (!p) 416 if (!p) 444 goto einval; 417 goto einval; 445 if (!e->mask[0]) { 418 if (!e->mask[0]) { 446 e->mask = NULL; 419 e->mask = NULL; 447 pr_debug("register: m 420 pr_debug("register: mask[raw]: none\n"); 448 } else if (USE_DEBUG) 421 } else if (USE_DEBUG) 449 print_hex_dump_bytes( 422 print_hex_dump_bytes( 450 KBUILD_MODNAME 423 KBUILD_MODNAME ": register: mask[raw]: ", 451 DUMP_PREFIX_NO 424 DUMP_PREFIX_NONE, e->mask, p - e->mask); 452 425 453 /* 426 /* 454 * Decode the magic & mask fie 427 * Decode the magic & mask fields. 455 * Note: while we might have a 428 * Note: while we might have accepted embedded NUL bytes from 456 * above, the unescape helpers 429 * above, the unescape helpers here will stop at the first one 457 * it encounters. 430 * it encounters. 458 */ 431 */ 459 e->size = string_unescape_inpl 432 e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX); 460 if (e->mask && 433 if (e->mask && 461 string_unescape_inplace(e- 434 string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size) 462 goto einval; 435 goto einval; 463 if (e->size > BINPRM_BUF_SIZE 436 if (e->size > BINPRM_BUF_SIZE || 464 BINPRM_BUF_SIZE - e->size 437 BINPRM_BUF_SIZE - e->size < e->offset) 465 goto einval; 438 goto einval; 466 pr_debug("register: magic/mask 439 pr_debug("register: magic/mask length: %i\n", e->size); 467 if (USE_DEBUG) { 440 if (USE_DEBUG) { 468 print_hex_dump_bytes( 441 print_hex_dump_bytes( 469 KBUILD_MODNAME 442 KBUILD_MODNAME ": register: magic[decoded]: ", 470 DUMP_PREFIX_NO 443 DUMP_PREFIX_NONE, e->magic, e->size); 471 444 472 if (e->mask) { 445 if (e->mask) { 473 int i; 446 int i; 474 char *masked = !! 447 char *masked = kmalloc(e->size, GFP_KERNEL); 475 448 476 print_hex_dump 449 print_hex_dump_bytes( 477 KBUILD 450 KBUILD_MODNAME ": register: mask[decoded]: ", 478 DUMP_P 451 DUMP_PREFIX_NONE, e->mask, e->size); 479 452 480 if (masked) { 453 if (masked) { 481 for (i 454 for (i = 0; i < e->size; ++i) 482 455 masked[i] = e->magic[i] & e->mask[i]; 483 print_ 456 print_hex_dump_bytes( 484 457 KBUILD_MODNAME ": register: magic[masked]: ", 485 458 DUMP_PREFIX_NONE, masked, e->size); 486 459 487 kfree( 460 kfree(masked); 488 } 461 } 489 } 462 } 490 } 463 } 491 } else { 464 } else { 492 /* Handle the 'E' (extension) 465 /* Handle the 'E' (extension) format. */ 493 466 494 /* Skip the 'offset' field. */ 467 /* Skip the 'offset' field. */ 495 p = strchr(p, del); 468 p = strchr(p, del); 496 if (!p) 469 if (!p) 497 goto einval; 470 goto einval; 498 *p++ = '\0'; 471 *p++ = '\0'; 499 472 500 /* Parse the 'magic' field. */ 473 /* Parse the 'magic' field. */ 501 e->magic = p; 474 e->magic = p; 502 p = strchr(p, del); 475 p = strchr(p, del); 503 if (!p) 476 if (!p) 504 goto einval; 477 goto einval; 505 *p++ = '\0'; 478 *p++ = '\0'; 506 if (!e->magic[0] || strchr(e-> 479 if (!e->magic[0] || strchr(e->magic, '/')) 507 goto einval; 480 goto einval; 508 pr_debug("register: extension: 481 pr_debug("register: extension: {%s}\n", e->magic); 509 482 510 /* Skip the 'mask' field. */ 483 /* Skip the 'mask' field. */ 511 p = strchr(p, del); 484 p = strchr(p, del); 512 if (!p) 485 if (!p) 513 goto einval; 486 goto einval; 514 *p++ = '\0'; 487 *p++ = '\0'; 515 } 488 } 516 489 517 /* Parse the 'interpreter' field. */ 490 /* Parse the 'interpreter' field. */ 518 e->interpreter = p; 491 e->interpreter = p; 519 p = strchr(p, del); 492 p = strchr(p, del); 520 if (!p) 493 if (!p) 521 goto einval; 494 goto einval; 522 *p++ = '\0'; 495 *p++ = '\0'; 523 if (!e->interpreter[0]) 496 if (!e->interpreter[0]) 524 goto einval; 497 goto einval; 525 pr_debug("register: interpreter: {%s}\ 498 pr_debug("register: interpreter: {%s}\n", e->interpreter); 526 499 527 /* Parse the 'flags' field. */ 500 /* Parse the 'flags' field. */ 528 p = check_special_flags(p, e); 501 p = check_special_flags(p, e); 529 if (*p == '\n') 502 if (*p == '\n') 530 p++; 503 p++; 531 if (p != buf + count) 504 if (p != buf + count) 532 goto einval; 505 goto einval; 533 506 534 return e; 507 return e; 535 508 536 out: 509 out: 537 return ERR_PTR(err); 510 return ERR_PTR(err); 538 511 539 efault: 512 efault: 540 kfree(e); 513 kfree(e); 541 return ERR_PTR(-EFAULT); 514 return ERR_PTR(-EFAULT); 542 einval: 515 einval: 543 kfree(e); 516 kfree(e); 544 return ERR_PTR(-EINVAL); 517 return ERR_PTR(-EINVAL); 545 } 518 } 546 519 547 /* 520 /* 548 * Set status of entry/binfmt_misc: 521 * Set status of entry/binfmt_misc: 549 * '1' enables, '' disables and '-1' clears en 522 * '1' enables, '' disables and '-1' clears entry/binfmt_misc 550 */ 523 */ 551 static int parse_command(const char __user *bu 524 static int parse_command(const char __user *buffer, size_t count) 552 { 525 { 553 char s[4]; 526 char s[4]; 554 527 555 if (count > 3) 528 if (count > 3) 556 return -EINVAL; 529 return -EINVAL; 557 if (copy_from_user(s, buffer, count)) 530 if (copy_from_user(s, buffer, count)) 558 return -EFAULT; 531 return -EFAULT; 559 if (!count) 532 if (!count) 560 return 0; 533 return 0; 561 if (s[count - 1] == '\n') 534 if (s[count - 1] == '\n') 562 count--; 535 count--; 563 if (count == 1 && s[0] == '') 536 if (count == 1 && s[0] == '') 564 return 1; 537 return 1; 565 if (count == 1 && s[0] == '1') 538 if (count == 1 && s[0] == '1') 566 return 2; 539 return 2; 567 if (count == 2 && s[0] == '-' && s[1] 540 if (count == 2 && s[0] == '-' && s[1] == '1') 568 return 3; 541 return 3; 569 return -EINVAL; 542 return -EINVAL; 570 } 543 } 571 544 572 /* generic stuff */ 545 /* generic stuff */ 573 546 574 static void entry_status(Node *e, char *page) 547 static void entry_status(Node *e, char *page) 575 { 548 { 576 char *dp = page; 549 char *dp = page; 577 const char *status = "disabled"; 550 const char *status = "disabled"; 578 551 579 if (test_bit(Enabled, &e->flags)) 552 if (test_bit(Enabled, &e->flags)) 580 status = "enabled"; 553 status = "enabled"; 581 554 582 if (!VERBOSE_STATUS) { 555 if (!VERBOSE_STATUS) { 583 sprintf(page, "%s\n", status); 556 sprintf(page, "%s\n", status); 584 return; 557 return; 585 } 558 } 586 559 587 dp += sprintf(dp, "%s\ninterpreter %s\ 560 dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter); 588 561 589 /* print the special flags */ 562 /* print the special flags */ 590 dp += sprintf(dp, "flags: "); 563 dp += sprintf(dp, "flags: "); 591 if (e->flags & MISC_FMT_PRESERVE_ARGV0 564 if (e->flags & MISC_FMT_PRESERVE_ARGV0) 592 *dp++ = 'P'; 565 *dp++ = 'P'; 593 if (e->flags & MISC_FMT_OPEN_BINARY) 566 if (e->flags & MISC_FMT_OPEN_BINARY) 594 *dp++ = 'O'; 567 *dp++ = 'O'; 595 if (e->flags & MISC_FMT_CREDENTIALS) 568 if (e->flags & MISC_FMT_CREDENTIALS) 596 *dp++ = 'C'; 569 *dp++ = 'C'; 597 if (e->flags & MISC_FMT_OPEN_FILE) 570 if (e->flags & MISC_FMT_OPEN_FILE) 598 *dp++ = 'F'; 571 *dp++ = 'F'; 599 *dp++ = '\n'; 572 *dp++ = '\n'; 600 573 601 if (!test_bit(Magic, &e->flags)) { 574 if (!test_bit(Magic, &e->flags)) { 602 sprintf(dp, "extension .%s\n", 575 sprintf(dp, "extension .%s\n", e->magic); 603 } else { 576 } else { 604 dp += sprintf(dp, "offset %i\n 577 dp += sprintf(dp, "offset %i\nmagic ", e->offset); 605 dp = bin2hex(dp, e->magic, e-> 578 dp = bin2hex(dp, e->magic, e->size); 606 if (e->mask) { 579 if (e->mask) { 607 dp += sprintf(dp, "\nm 580 dp += sprintf(dp, "\nmask "); 608 dp = bin2hex(dp, e->ma 581 dp = bin2hex(dp, e->mask, e->size); 609 } 582 } 610 *dp++ = '\n'; 583 *dp++ = '\n'; 611 *dp = '\0'; 584 *dp = '\0'; 612 } 585 } 613 } 586 } 614 587 615 static struct inode *bm_get_inode(struct super 588 static struct inode *bm_get_inode(struct super_block *sb, int mode) 616 { 589 { 617 struct inode *inode = new_inode(sb); 590 struct inode *inode = new_inode(sb); 618 591 619 if (inode) { 592 if (inode) { 620 inode->i_ino = get_next_ino(); 593 inode->i_ino = get_next_ino(); 621 inode->i_mode = mode; 594 inode->i_mode = mode; 622 simple_inode_init_ts(inode); !! 595 inode->i_atime = inode->i_mtime = inode->i_ctime = >> 596 current_time(inode); 623 } 597 } 624 return inode; 598 return inode; 625 } 599 } 626 600 627 /** << 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 601 static void bm_evict_inode(struct inode *inode) 657 { 602 { 658 Node *e = inode->i_private; 603 Node *e = inode->i_private; 659 604 660 clear_inode(inode); !! 605 if (e && e->flags & MISC_FMT_OPEN_FILE) 661 !! 606 filp_close(e->interp_file, NULL); 662 if (e) { << 663 struct binfmt_misc *misc; << 664 607 665 misc = i_binfmt_misc(inode); !! 608 clear_inode(inode); 666 write_lock(&misc->entries_lock !! 609 kfree(e); 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 } 610 } 673 611 674 /** !! 612 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 { 613 { 687 struct dentry *parent = dentry->d_pare !! 614 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 << 704 if (simple_positive(dentry)) { << 705 dget(dentry); << 706 simple_unlink(parent_inode, de << 707 d_delete(dentry); << 708 dput(dentry); << 709 } << 710 } << 711 615 712 /** !! 616 write_lock(&entries_lock); 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); 617 list_del_init(&e->list); 728 write_unlock(&misc->entries_lock); !! 618 write_unlock(&entries_lock); 729 unlink_binfmt_dentry(e->dentry); !! 619 >> 620 dentry = e->dentry; >> 621 drop_nlink(d_inode(dentry)); >> 622 d_drop(dentry); >> 623 dput(dentry); >> 624 simple_release_fs(&bm_mnt, &entry_count); 730 } 625 } 731 626 732 /* /<entry> */ 627 /* /<entry> */ 733 628 734 static ssize_t 629 static ssize_t 735 bm_entry_read(struct file *file, char __user * 630 bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 736 { 631 { 737 Node *e = file_inode(file)->i_private; 632 Node *e = file_inode(file)->i_private; 738 ssize_t res; 633 ssize_t res; 739 char *page; 634 char *page; 740 635 741 page = (char *) __get_free_page(GFP_KE 636 page = (char *) __get_free_page(GFP_KERNEL); 742 if (!page) 637 if (!page) 743 return -ENOMEM; 638 return -ENOMEM; 744 639 745 entry_status(e, page); 640 entry_status(e, page); 746 641 747 res = simple_read_from_buffer(buf, nby 642 res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page)); 748 643 749 free_page((unsigned long) page); 644 free_page((unsigned long) page); 750 return res; 645 return res; 751 } 646 } 752 647 753 static ssize_t bm_entry_write(struct file *fil 648 static ssize_t bm_entry_write(struct file *file, const char __user *buffer, 754 size_t count, 649 size_t count, loff_t *ppos) 755 { 650 { 756 struct inode *inode = file_inode(file) !! 651 struct dentry *root; 757 Node *e = inode->i_private; !! 652 Node *e = file_inode(file)->i_private; 758 int res = parse_command(buffer, count) 653 int res = parse_command(buffer, count); 759 654 760 switch (res) { 655 switch (res) { 761 case 1: 656 case 1: 762 /* Disable this handler. */ 657 /* Disable this handler. */ 763 clear_bit(Enabled, &e->flags); 658 clear_bit(Enabled, &e->flags); 764 break; 659 break; 765 case 2: 660 case 2: 766 /* Enable this handler. */ 661 /* Enable this handler. */ 767 set_bit(Enabled, &e->flags); 662 set_bit(Enabled, &e->flags); 768 break; 663 break; 769 case 3: 664 case 3: 770 /* Delete this handler. */ 665 /* Delete this handler. */ 771 inode = d_inode(inode->i_sb->s !! 666 root = file_inode(file)->i_sb->s_root; 772 inode_lock(inode); !! 667 inode_lock(d_inode(root)); 773 668 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)) 669 if (!list_empty(&e->list)) 784 remove_binfmt_handler( !! 670 kill_node(e); 785 671 786 inode_unlock(inode); !! 672 inode_unlock(d_inode(root)); 787 break; 673 break; 788 default: 674 default: 789 return res; 675 return res; 790 } 676 } 791 677 792 return count; 678 return count; 793 } 679 } 794 680 795 static const struct file_operations bm_entry_o 681 static const struct file_operations bm_entry_operations = { 796 .read = bm_entry_read, 682 .read = bm_entry_read, 797 .write = bm_entry_write, 683 .write = bm_entry_write, 798 .llseek = default_llseek, 684 .llseek = default_llseek, 799 }; 685 }; 800 686 801 /* /register */ 687 /* /register */ 802 688 803 static ssize_t bm_register_write(struct file * 689 static ssize_t bm_register_write(struct file *file, const char __user *buffer, 804 size_t count, l 690 size_t count, loff_t *ppos) 805 { 691 { 806 Node *e; 692 Node *e; 807 struct inode *inode; 693 struct inode *inode; 808 struct super_block *sb = file_inode(fi 694 struct super_block *sb = file_inode(file)->i_sb; 809 struct dentry *root = sb->s_root, *den 695 struct dentry *root = sb->s_root, *dentry; 810 struct binfmt_misc *misc; << 811 int err = 0; 696 int err = 0; 812 struct file *f = NULL; << 813 697 814 e = create_entry(buffer, count); 698 e = create_entry(buffer, count); 815 699 816 if (IS_ERR(e)) 700 if (IS_ERR(e)) 817 return PTR_ERR(e); 701 return PTR_ERR(e); 818 702 819 if (e->flags & MISC_FMT_OPEN_FILE) { << 820 const struct cred *old_cred; << 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)); 703 inode_lock(d_inode(root)); 842 dentry = lookup_one_len(e->name, root, 704 dentry = lookup_one_len(e->name, root, strlen(e->name)); 843 err = PTR_ERR(dentry); 705 err = PTR_ERR(dentry); 844 if (IS_ERR(dentry)) 706 if (IS_ERR(dentry)) 845 goto out; 707 goto out; 846 708 847 err = -EEXIST; 709 err = -EEXIST; 848 if (d_really_is_positive(dentry)) 710 if (d_really_is_positive(dentry)) 849 goto out2; 711 goto out2; 850 712 851 inode = bm_get_inode(sb, S_IFREG | 064 713 inode = bm_get_inode(sb, S_IFREG | 0644); 852 714 853 err = -ENOMEM; 715 err = -ENOMEM; 854 if (!inode) 716 if (!inode) 855 goto out2; 717 goto out2; 856 718 857 refcount_set(&e->users, 1); !! 719 err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count); >> 720 if (err) { >> 721 iput(inode); >> 722 inode = NULL; >> 723 goto out2; >> 724 } >> 725 >> 726 if (e->flags & MISC_FMT_OPEN_FILE) { >> 727 struct file *f; >> 728 >> 729 f = open_exec(e->interpreter); >> 730 if (IS_ERR(f)) { >> 731 err = PTR_ERR(f); >> 732 pr_notice("register: failed to install interpreter file %s\n", e->interpreter); >> 733 simple_release_fs(&bm_mnt, &entry_count); >> 734 iput(inode); >> 735 inode = NULL; >> 736 goto out2; >> 737 } >> 738 e->interp_file = f; >> 739 } >> 740 858 e->dentry = dget(dentry); 741 e->dentry = dget(dentry); 859 inode->i_private = e; 742 inode->i_private = e; 860 inode->i_fop = &bm_entry_operations; 743 inode->i_fop = &bm_entry_operations; 861 744 862 d_instantiate(dentry, inode); 745 d_instantiate(dentry, inode); 863 misc = i_binfmt_misc(inode); !! 746 write_lock(&entries_lock); 864 write_lock(&misc->entries_lock); !! 747 list_add(&e->list, &entries); 865 list_add(&e->list, &misc->entries); !! 748 write_unlock(&entries_lock); 866 write_unlock(&misc->entries_lock); << 867 749 868 err = 0; 750 err = 0; 869 out2: 751 out2: 870 dput(dentry); 752 dput(dentry); 871 out: 753 out: 872 inode_unlock(d_inode(root)); 754 inode_unlock(d_inode(root)); 873 755 874 if (err) { 756 if (err) { 875 if (f) << 876 filp_close(f, NULL); << 877 kfree(e); 757 kfree(e); 878 return err; 758 return err; 879 } 759 } 880 return count; 760 return count; 881 } 761 } 882 762 883 static const struct file_operations bm_registe 763 static const struct file_operations bm_register_operations = { 884 .write = bm_register_write, 764 .write = bm_register_write, 885 .llseek = noop_llseek, 765 .llseek = noop_llseek, 886 }; 766 }; 887 767 888 /* /status */ 768 /* /status */ 889 769 890 static ssize_t 770 static ssize_t 891 bm_status_read(struct file *file, char __user 771 bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 892 { 772 { 893 struct binfmt_misc *misc; !! 773 char *s = enabled ? "enabled\n" : "disabled\n"; 894 char *s; << 895 774 896 misc = i_binfmt_misc(file_inode(file)) << 897 s = misc->enabled ? "enabled\n" : "dis << 898 return simple_read_from_buffer(buf, nb 775 return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s)); 899 } 776 } 900 777 901 static ssize_t bm_status_write(struct file *fi 778 static ssize_t bm_status_write(struct file *file, const char __user *buffer, 902 size_t count, loff_t *ppos) 779 size_t count, loff_t *ppos) 903 { 780 { 904 struct binfmt_misc *misc; << 905 int res = parse_command(buffer, count) 781 int res = parse_command(buffer, count); 906 Node *e, *next; !! 782 struct dentry *root; 907 struct inode *inode; << 908 783 909 misc = i_binfmt_misc(file_inode(file)) << 910 switch (res) { 784 switch (res) { 911 case 1: 785 case 1: 912 /* Disable all handlers. */ 786 /* Disable all handlers. */ 913 misc->enabled = false; !! 787 enabled = 0; 914 break; 788 break; 915 case 2: 789 case 2: 916 /* Enable all handlers. */ 790 /* Enable all handlers. */ 917 misc->enabled = true; !! 791 enabled = 1; 918 break; 792 break; 919 case 3: 793 case 3: 920 /* Delete all handlers. */ 794 /* Delete all handlers. */ 921 inode = d_inode(file_inode(fil !! 795 root = file_inode(file)->i_sb->s_root; 922 inode_lock(inode); !! 796 inode_lock(d_inode(root)); 923 797 924 /* !! 798 while (!list_empty(&entries)) 925 * In order to add new element !! 799 kill_node(list_first_entry(&entries, Node, list)); 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 800 936 inode_unlock(inode); !! 801 inode_unlock(d_inode(root)); 937 break; 802 break; 938 default: 803 default: 939 return res; 804 return res; 940 } 805 } 941 806 942 return count; 807 return count; 943 } 808 } 944 809 945 static const struct file_operations bm_status_ 810 static const struct file_operations bm_status_operations = { 946 .read = bm_status_read, 811 .read = bm_status_read, 947 .write = bm_status_write, 812 .write = bm_status_write, 948 .llseek = default_llseek, 813 .llseek = default_llseek, 949 }; 814 }; 950 815 951 /* Superblock handling */ 816 /* Superblock handling */ 952 817 953 static void bm_put_super(struct super_block *s << 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 = { 818 static const struct super_operations s_ops = { 962 .statfs = simple_statfs, 819 .statfs = simple_statfs, 963 .evict_inode = bm_evict_inode, 820 .evict_inode = bm_evict_inode, 964 .put_super = bm_put_super, << 965 }; 821 }; 966 822 967 static int bm_fill_super(struct super_block *s !! 823 static int bm_fill_super(struct super_block *sb, void *data, int silent) 968 { 824 { 969 int err; 825 int err; 970 struct user_namespace *user_ns = sb->s << 971 struct binfmt_misc *misc; << 972 static const struct tree_descr bm_file 826 static const struct tree_descr bm_files[] = { 973 [2] = {"status", &bm_status_op 827 [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO}, 974 [3] = {"register", &bm_registe 828 [3] = {"register", &bm_register_operations, S_IWUSR}, 975 /* last one */ {""} 829 /* last one */ {""} 976 }; 830 }; 977 831 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_ 832 err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files); 1029 if (!err) 833 if (!err) 1030 sb->s_op = &s_ops; 834 sb->s_op = &s_ops; 1031 return err; 835 return err; 1032 } 836 } 1033 837 1034 static void bm_free(struct fs_context *fc) !! 838 static struct dentry *bm_mount(struct file_system_type *fs_type, 1035 { !! 839 int flags, const char *dev_name, void *data) 1036 if (fc->s_fs_info) << 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 { 840 { 1052 fc->ops = &bm_context_ops; !! 841 return mount_single(fs_type, flags, data, bm_fill_super); 1053 return 0; << 1054 } 842 } 1055 843 1056 static struct linux_binfmt misc_format = { 844 static struct linux_binfmt misc_format = { 1057 .module = THIS_MODULE, 845 .module = THIS_MODULE, 1058 .load_binary = load_misc_binary, 846 .load_binary = load_misc_binary, 1059 }; 847 }; 1060 848 1061 static struct file_system_type bm_fs_type = { 849 static struct file_system_type bm_fs_type = { 1062 .owner = THIS_MODULE, 850 .owner = THIS_MODULE, 1063 .name = "binfmt_misc", 851 .name = "binfmt_misc", 1064 .init_fs_context = bm_init_fs_context !! 852 .mount = bm_mount, 1065 .fs_flags = FS_USERNS_MOUNT, << 1066 .kill_sb = kill_litter_super, 853 .kill_sb = kill_litter_super, 1067 }; 854 }; 1068 MODULE_ALIAS_FS("binfmt_misc"); 855 MODULE_ALIAS_FS("binfmt_misc"); 1069 856 1070 static int __init init_misc_binfmt(void) 857 static int __init init_misc_binfmt(void) 1071 { 858 { 1072 int err = register_filesystem(&bm_fs_ 859 int err = register_filesystem(&bm_fs_type); 1073 if (!err) 860 if (!err) 1074 insert_binfmt(&misc_format); 861 insert_binfmt(&misc_format); 1075 return err; 862 return err; 1076 } 863 } 1077 864 1078 static void __exit exit_misc_binfmt(void) 865 static void __exit exit_misc_binfmt(void) 1079 { 866 { 1080 unregister_binfmt(&misc_format); 867 unregister_binfmt(&misc_format); 1081 unregister_filesystem(&bm_fs_type); 868 unregister_filesystem(&bm_fs_type); 1082 } 869 } 1083 870 1084 core_initcall(init_misc_binfmt); 871 core_initcall(init_misc_binfmt); 1085 module_exit(exit_misc_binfmt); 872 module_exit(exit_misc_binfmt); 1086 MODULE_DESCRIPTION("Kernel support for miscel << 1087 MODULE_LICENSE("GPL"); 873 MODULE_LICENSE("GPL"); 1088 874
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.