1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1997 Richard Günther
7 * binfmt_misc detects binaries via a magic or filename extension and invokes
8 * a specified wrapper. See Documentation/admin-guide/binfmt-misc.rst for more details.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/sched/mm.h>
17 #include <linux/magic.h>
18 #include <linux/binfmts.h>
19 #include <linux/slab.h>
20 #include <linux/ctype.h>
21 #include <linux/string_helpers.h>
22 #include <linux/file.h>
23 #include <linux/pagemap.h>
24 #include <linux/namei.h>
25 #include <linux/mount.h>
26 #include <linux/fs_context.h>
27 #include <linux/syscalls.h>
29 #include <linux/uaccess.h>
40 VERBOSE_STATUS
= 1 /* make it zero to save 400 bytes kernel memory */
43 enum {Enabled
, Magic
};
44 #define MISC_FMT_PRESERVE_ARGV0 (1UL << 31)
45 #define MISC_FMT_OPEN_BINARY (1UL << 30)
46 #define MISC_FMT_CREDENTIALS (1UL << 29)
47 #define MISC_FMT_OPEN_FILE (1UL << 28)
50 struct list_head list
;
51 unsigned long flags
; /* type, status, etc. */
52 int offset
; /* offset of magic */
53 int size
; /* size of magic/mask */
54 char *magic
; /* magic or filename extension */
55 char *mask
; /* mask, NULL for exact match */
56 const char *interpreter
; /* filename of interpreter */
58 struct dentry
*dentry
;
59 struct file
*interp_file
;
60 refcount_t users
; /* sync removal with load_misc_binary() */
63 static struct file_system_type bm_fs_type
;
66 * Max length of the register string. Determined by:
70 * - offset: 3 bytes (has to be smaller than BINPRM_BUF_SIZE)
71 * - magic: 128 bytes (512 in escaped form)
72 * - mask: 128 bytes (512 in escaped form)
75 * Round that up a bit, and then back off to hold the internal data
78 #define MAX_REGISTER_LENGTH 1920
81 * search_binfmt_handler - search for a binary handler for @bprm
82 * @misc: handle to binfmt_misc instance
83 * @bprm: binary for which we are looking for a handler
85 * Search for a binary type handler for @bprm in the list of registered binary
88 * Return: binary type list entry on success, NULL on failure
90 static Node
*search_binfmt_handler(struct binfmt_misc
*misc
,
91 struct linux_binprm
*bprm
)
93 char *p
= strrchr(bprm
->interp
, '.');
96 /* Walk all the registered handlers. */
97 list_for_each_entry(e
, &misc
->entries
, list
) {
101 /* Make sure this one is currently enabled. */
102 if (!test_bit(Enabled
, &e
->flags
))
105 /* Do matching based on extension if applicable. */
106 if (!test_bit(Magic
, &e
->flags
)) {
107 if (p
&& !strcmp(e
->magic
, p
+ 1))
112 /* Do matching based on magic & mask. */
113 s
= bprm
->buf
+ e
->offset
;
115 for (j
= 0; j
< e
->size
; j
++)
116 if ((*s
++ ^ e
->magic
[j
]) & e
->mask
[j
])
119 for (j
= 0; j
< e
->size
; j
++)
120 if ((*s
++ ^ e
->magic
[j
]))
131 * get_binfmt_handler - try to find a binary type handler
132 * @misc: handle to binfmt_misc instance
133 * @bprm: binary for which we are looking for a handler
135 * Try to find a binfmt handler for the binary type. If one is found take a
136 * reference to protect against removal via bm_{entry,status}_write().
138 * Return: binary type list entry on success, NULL on failure
140 static Node
*get_binfmt_handler(struct binfmt_misc
*misc
,
141 struct linux_binprm
*bprm
)
145 read_lock(&misc
->entries_lock
);
146 e
= search_binfmt_handler(misc
, bprm
);
148 refcount_inc(&e
->users
);
149 read_unlock(&misc
->entries_lock
);
154 * put_binfmt_handler - put binary handler node
157 * Free node syncing with load_misc_binary() and defer final free to
158 * load_misc_binary() in case it is using the binary type handler we were
159 * requested to remove.
161 static void put_binfmt_handler(Node
*e
)
163 if (refcount_dec_and_test(&e
->users
)) {
164 if (e
->flags
& MISC_FMT_OPEN_FILE
)
165 filp_close(e
->interp_file
, NULL
);
171 * load_binfmt_misc - load the binfmt_misc of the caller's user namespace
173 * To be called in load_misc_binary() to load the relevant struct binfmt_misc.
174 * If a user namespace doesn't have its own binfmt_misc mount it can make use
175 * of its ancestor's binfmt_misc handlers. This mimicks the behavior of
176 * pre-namespaced binfmt_misc where all registered binfmt_misc handlers where
177 * available to all user and user namespaces on the system.
179 * Return: the binfmt_misc instance of the caller's user namespace
181 static struct binfmt_misc
*load_binfmt_misc(void)
183 const struct user_namespace
*user_ns
;
184 struct binfmt_misc
*misc
;
186 user_ns
= current_user_ns();
188 /* Pairs with smp_store_release() in bm_fill_super(). */
189 misc
= smp_load_acquire(&user_ns
->binfmt_misc
);
193 user_ns
= user_ns
->parent
;
196 return &init_binfmt_misc
;
202 static int load_misc_binary(struct linux_binprm
*bprm
)
205 struct file
*interp_file
= NULL
;
206 int retval
= -ENOEXEC
;
207 struct binfmt_misc
*misc
;
209 misc
= load_binfmt_misc();
213 fmt
= get_binfmt_handler(misc
, bprm
);
217 /* Need to be able to load the file after exec */
219 if (bprm
->interp_flags
& BINPRM_FLAGS_PATH_INACCESSIBLE
)
222 if (fmt
->flags
& MISC_FMT_PRESERVE_ARGV0
) {
223 bprm
->interp_flags
|= BINPRM_FLAGS_PRESERVE_ARGV0
;
225 retval
= remove_arg_zero(bprm
);
230 if (fmt
->flags
& MISC_FMT_OPEN_BINARY
)
231 bprm
->have_execfd
= 1;
233 /* make argv[1] be the path to the binary */
234 retval
= copy_string_kernel(bprm
->interp
, bprm
);
239 /* add the interp as argv[0] */
240 retval
= copy_string_kernel(fmt
->interpreter
, bprm
);
245 /* Update interp in case binfmt_script needs it. */
246 retval
= bprm_change_interp(fmt
->interpreter
, bprm
);
250 if (fmt
->flags
& MISC_FMT_OPEN_FILE
) {
251 interp_file
= file_clone_open(fmt
->interp_file
);
252 if (!IS_ERR(interp_file
))
253 deny_write_access(interp_file
);
255 interp_file
= open_exec(fmt
->interpreter
);
257 retval
= PTR_ERR(interp_file
);
258 if (IS_ERR(interp_file
))
261 bprm
->interpreter
= interp_file
;
262 if (fmt
->flags
& MISC_FMT_CREDENTIALS
)
263 bprm
->execfd_creds
= 1;
269 * If we actually put the node here all concurrent calls to
270 * load_misc_binary() will have finished. We also know
271 * that for the refcount to be zero someone must have concurently
272 * removed the binary type handler from the list and it's our job to
275 put_binfmt_handler(fmt
);
280 /* Command parsers */
283 * parses and copies one argument enclosed in del from *sp to *dp,
284 * recognising the \x special.
285 * returns pointer to the copied argument or NULL in case of an
286 * error (and sets err) or null argument length.
288 static char *scanarg(char *s
, char del
)
292 while ((c
= *s
++) != del
) {
293 if (c
== '\\' && *s
== 'x') {
305 static char *check_special_flags(char *sfs
, Node
*e
)
314 pr_debug("register: flag: P (preserve argv0)\n");
316 e
->flags
|= MISC_FMT_PRESERVE_ARGV0
;
319 pr_debug("register: flag: O (open binary)\n");
321 e
->flags
|= MISC_FMT_OPEN_BINARY
;
324 pr_debug("register: flag: C (preserve creds)\n");
326 /* this flags also implies the
328 e
->flags
|= (MISC_FMT_CREDENTIALS
|
329 MISC_FMT_OPEN_BINARY
);
332 pr_debug("register: flag: F: open interpreter file now\n");
334 e
->flags
|= MISC_FMT_OPEN_FILE
;
345 * This registers a new binary format, it recognises the syntax
346 * ':name:type:offset:magic:mask:interpreter:flags'
347 * where the ':' is the IFS, that can be chosen with the first char
349 static Node
*create_entry(const char __user
*buffer
, size_t count
)
356 pr_debug("register: received %zu bytes\n", count
);
358 /* some sanity checks */
360 if ((count
< 11) || (count
> MAX_REGISTER_LENGTH
))
364 memsize
= sizeof(Node
) + count
+ 8;
365 e
= kmalloc(memsize
, GFP_KERNEL_ACCOUNT
);
369 p
= buf
= (char *)e
+ sizeof(Node
);
371 memset(e
, 0, sizeof(Node
));
372 if (copy_from_user(buf
, buffer
, count
))
375 del
= *p
++; /* delimeter */
377 pr_debug("register: delim: %#x {%c}\n", del
, del
);
379 /* Pad the buffer with the delim to simplify parsing below. */
380 memset(buf
+ count
, del
, 8);
382 /* Parse the 'name' field. */
389 !strcmp(e
->name
, ".") ||
390 !strcmp(e
->name
, "..") ||
391 strchr(e
->name
, '/'))
394 pr_debug("register: name: {%s}\n", e
->name
);
396 /* Parse the 'type' field. */
399 pr_debug("register: type: E (extension)\n");
400 e
->flags
= 1 << Enabled
;
403 pr_debug("register: type: M (magic)\n");
404 e
->flags
= (1 << Enabled
) | (1 << Magic
);
412 if (test_bit(Magic
, &e
->flags
)) {
413 /* Handle the 'M' (magic) format. */
416 /* Parse the 'offset' field. */
422 int r
= kstrtoint(p
, 10, &e
->offset
);
423 if (r
!= 0 || e
->offset
< 0)
429 pr_debug("register: offset: %#x\n", e
->offset
);
431 /* Parse the 'magic' field. */
439 print_hex_dump_bytes(
440 KBUILD_MODNAME
": register: magic[raw]: ",
441 DUMP_PREFIX_NONE
, e
->magic
, p
- e
->magic
);
443 /* Parse the 'mask' field. */
450 pr_debug("register: mask[raw]: none\n");
451 } else if (USE_DEBUG
)
452 print_hex_dump_bytes(
453 KBUILD_MODNAME
": register: mask[raw]: ",
454 DUMP_PREFIX_NONE
, e
->mask
, p
- e
->mask
);
457 * Decode the magic & mask fields.
458 * Note: while we might have accepted embedded NUL bytes from
459 * above, the unescape helpers here will stop at the first one
462 e
->size
= string_unescape_inplace(e
->magic
, UNESCAPE_HEX
);
464 string_unescape_inplace(e
->mask
, UNESCAPE_HEX
) != e
->size
)
466 if (e
->size
> BINPRM_BUF_SIZE
||
467 BINPRM_BUF_SIZE
- e
->size
< e
->offset
)
469 pr_debug("register: magic/mask length: %i\n", e
->size
);
471 print_hex_dump_bytes(
472 KBUILD_MODNAME
": register: magic[decoded]: ",
473 DUMP_PREFIX_NONE
, e
->magic
, e
->size
);
477 char *masked
= kmalloc(e
->size
, GFP_KERNEL_ACCOUNT
);
479 print_hex_dump_bytes(
480 KBUILD_MODNAME
": register: mask[decoded]: ",
481 DUMP_PREFIX_NONE
, e
->mask
, e
->size
);
484 for (i
= 0; i
< e
->size
; ++i
)
485 masked
[i
] = e
->magic
[i
] & e
->mask
[i
];
486 print_hex_dump_bytes(
487 KBUILD_MODNAME
": register: magic[masked]: ",
488 DUMP_PREFIX_NONE
, masked
, e
->size
);
495 /* Handle the 'E' (extension) format. */
497 /* Skip the 'offset' field. */
503 /* Parse the 'magic' field. */
509 if (!e
->magic
[0] || strchr(e
->magic
, '/'))
511 pr_debug("register: extension: {%s}\n", e
->magic
);
513 /* Skip the 'mask' field. */
520 /* Parse the 'interpreter' field. */
526 if (!e
->interpreter
[0])
528 pr_debug("register: interpreter: {%s}\n", e
->interpreter
);
530 /* Parse the 'flags' field. */
531 p
= check_special_flags(p
, e
);
534 if (p
!= buf
+ count
)
544 return ERR_PTR(-EFAULT
);
547 return ERR_PTR(-EINVAL
);
551 * Set status of entry/binfmt_misc:
552 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
554 static int parse_command(const char __user
*buffer
, size_t count
)
560 if (copy_from_user(s
, buffer
, count
))
564 if (s
[count
- 1] == '\n')
566 if (count
== 1 && s
[0] == '0')
568 if (count
== 1 && s
[0] == '1')
570 if (count
== 2 && s
[0] == '-' && s
[1] == '1')
577 static void entry_status(Node
*e
, char *page
)
580 const char *status
= "disabled";
582 if (test_bit(Enabled
, &e
->flags
))
585 if (!VERBOSE_STATUS
) {
586 sprintf(page
, "%s\n", status
);
590 dp
+= sprintf(dp
, "%s\ninterpreter %s\n", status
, e
->interpreter
);
592 /* print the special flags */
593 dp
+= sprintf(dp
, "flags: ");
594 if (e
->flags
& MISC_FMT_PRESERVE_ARGV0
)
596 if (e
->flags
& MISC_FMT_OPEN_BINARY
)
598 if (e
->flags
& MISC_FMT_CREDENTIALS
)
600 if (e
->flags
& MISC_FMT_OPEN_FILE
)
604 if (!test_bit(Magic
, &e
->flags
)) {
605 sprintf(dp
, "extension .%s\n", e
->magic
);
607 dp
+= sprintf(dp
, "offset %i\nmagic ", e
->offset
);
608 dp
= bin2hex(dp
, e
->magic
, e
->size
);
610 dp
+= sprintf(dp
, "\nmask ");
611 dp
= bin2hex(dp
, e
->mask
, e
->size
);
618 static struct inode
*bm_get_inode(struct super_block
*sb
, int mode
)
620 struct inode
*inode
= new_inode(sb
);
623 inode
->i_ino
= get_next_ino();
624 inode
->i_mode
= mode
;
625 simple_inode_init_ts(inode
);
631 * i_binfmt_misc - retrieve struct binfmt_misc from a binfmt_misc inode
632 * @inode: inode of the relevant binfmt_misc instance
634 * This helper retrieves struct binfmt_misc from a binfmt_misc inode. This can
635 * be done without any memory barriers because we are guaranteed that
636 * user_ns->binfmt_misc is fully initialized. It was fully initialized when the
637 * binfmt_misc mount was first created.
639 * Return: struct binfmt_misc of the relevant binfmt_misc instance
641 static struct binfmt_misc
*i_binfmt_misc(struct inode
*inode
)
643 return inode
->i_sb
->s_user_ns
->binfmt_misc
;
647 * bm_evict_inode - cleanup data associated with @inode
648 * @inode: inode to which the data is attached
650 * Cleanup the binary type handler data associated with @inode if a binary type
651 * entry is removed or the filesystem is unmounted and the super block is
654 * If the ->evict call was not caused by a super block shutdown but by a write
655 * to remove the entry or all entries via bm_{entry,status}_write() the entry
656 * will have already been removed from the list. We keep the list_empty() check
657 * to make that explicit.
659 static void bm_evict_inode(struct inode
*inode
)
661 Node
*e
= inode
->i_private
;
666 struct binfmt_misc
*misc
;
668 misc
= i_binfmt_misc(inode
);
669 write_lock(&misc
->entries_lock
);
670 if (!list_empty(&e
->list
))
671 list_del_init(&e
->list
);
672 write_unlock(&misc
->entries_lock
);
673 put_binfmt_handler(e
);
678 * unlink_binfmt_dentry - remove the dentry for the binary type handler
679 * @dentry: dentry associated with the binary type handler
681 * Do the actual filesystem work to remove a dentry for a registered binary
682 * type handler. Since binfmt_misc only allows simple files to be created
683 * directly under the root dentry of the filesystem we ensure that we are
684 * indeed passed a dentry directly beneath the root dentry, that the inode
685 * associated with the root dentry is locked, and that it is a regular file we
686 * are asked to remove.
688 static void unlink_binfmt_dentry(struct dentry
*dentry
)
690 struct dentry
*parent
= dentry
->d_parent
;
691 struct inode
*inode
, *parent_inode
;
693 /* All entries are immediate descendants of the root dentry. */
694 if (WARN_ON_ONCE(dentry
->d_sb
->s_root
!= parent
))
697 /* We only expect to be called on regular files. */
698 inode
= d_inode(dentry
);
699 if (WARN_ON_ONCE(!S_ISREG(inode
->i_mode
)))
702 /* The parent inode must be locked. */
703 parent_inode
= d_inode(parent
);
704 if (WARN_ON_ONCE(!inode_is_locked(parent_inode
)))
707 if (simple_positive(dentry
)) {
709 simple_unlink(parent_inode
, dentry
);
716 * remove_binfmt_handler - remove a binary type handler
717 * @misc: handle to binfmt_misc instance
718 * @e: binary type handler to remove
720 * Remove a binary type handler from the list of binary type handlers and
721 * remove its associated dentry. This is called from
722 * binfmt_{entry,status}_write(). In the future, we might want to think about
723 * adding a proper ->unlink() method to binfmt_misc instead of forcing caller's
724 * to use writes to files in order to delete binary type handlers. But it has
725 * worked for so long that it's not a pressing issue.
727 static void remove_binfmt_handler(struct binfmt_misc
*misc
, Node
*e
)
729 write_lock(&misc
->entries_lock
);
730 list_del_init(&e
->list
);
731 write_unlock(&misc
->entries_lock
);
732 unlink_binfmt_dentry(e
->dentry
);
738 bm_entry_read(struct file
*file
, char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
740 Node
*e
= file_inode(file
)->i_private
;
744 page
= (char *) __get_free_page(GFP_KERNEL
);
748 entry_status(e
, page
);
750 res
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, strlen(page
));
752 free_page((unsigned long) page
);
756 static ssize_t
bm_entry_write(struct file
*file
, const char __user
*buffer
,
757 size_t count
, loff_t
*ppos
)
759 struct inode
*inode
= file_inode(file
);
760 Node
*e
= inode
->i_private
;
761 int res
= parse_command(buffer
, count
);
765 /* Disable this handler. */
766 clear_bit(Enabled
, &e
->flags
);
769 /* Enable this handler. */
770 set_bit(Enabled
, &e
->flags
);
773 /* Delete this handler. */
774 inode
= d_inode(inode
->i_sb
->s_root
);
778 * In order to add new element or remove elements from the list
779 * via bm_{entry,register,status}_write() inode_lock() on the
780 * root inode must be held.
781 * The lock is exclusive ensuring that the list can't be
782 * modified. Only load_misc_binary() can access but does so
783 * read-only. So we only need to take the write lock when we
784 * actually remove the entry from the list.
786 if (!list_empty(&e
->list
))
787 remove_binfmt_handler(i_binfmt_misc(inode
), e
);
798 static const struct file_operations bm_entry_operations
= {
799 .read
= bm_entry_read
,
800 .write
= bm_entry_write
,
801 .llseek
= default_llseek
,
806 static ssize_t
bm_register_write(struct file
*file
, const char __user
*buffer
,
807 size_t count
, loff_t
*ppos
)
811 struct super_block
*sb
= file_inode(file
)->i_sb
;
812 struct dentry
*root
= sb
->s_root
, *dentry
;
813 struct binfmt_misc
*misc
;
815 struct file
*f
= NULL
;
817 e
= create_entry(buffer
, count
);
822 if (e
->flags
& MISC_FMT_OPEN_FILE
) {
823 const struct cred
*old_cred
;
826 * Now that we support unprivileged binfmt_misc mounts make
827 * sure we use the credentials that the register @file was
828 * opened with to also open the interpreter. Before that this
829 * didn't matter much as only a privileged process could open
832 old_cred
= override_creds(file
->f_cred
);
833 f
= open_exec(e
->interpreter
);
834 revert_creds(old_cred
);
836 pr_notice("register: failed to install interpreter file %s\n",
844 inode_lock(d_inode(root
));
845 dentry
= lookup_one_len(e
->name
, root
, strlen(e
->name
));
846 err
= PTR_ERR(dentry
);
851 if (d_really_is_positive(dentry
))
854 inode
= bm_get_inode(sb
, S_IFREG
| 0644);
860 refcount_set(&e
->users
, 1);
861 e
->dentry
= dget(dentry
);
862 inode
->i_private
= e
;
863 inode
->i_fop
= &bm_entry_operations
;
865 d_instantiate(dentry
, inode
);
866 misc
= i_binfmt_misc(inode
);
867 write_lock(&misc
->entries_lock
);
868 list_add(&e
->list
, &misc
->entries
);
869 write_unlock(&misc
->entries_lock
);
875 inode_unlock(d_inode(root
));
886 static const struct file_operations bm_register_operations
= {
887 .write
= bm_register_write
,
888 .llseek
= noop_llseek
,
894 bm_status_read(struct file
*file
, char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
896 struct binfmt_misc
*misc
;
899 misc
= i_binfmt_misc(file_inode(file
));
900 s
= misc
->enabled
? "enabled\n" : "disabled\n";
901 return simple_read_from_buffer(buf
, nbytes
, ppos
, s
, strlen(s
));
904 static ssize_t
bm_status_write(struct file
*file
, const char __user
*buffer
,
905 size_t count
, loff_t
*ppos
)
907 struct binfmt_misc
*misc
;
908 int res
= parse_command(buffer
, count
);
912 misc
= i_binfmt_misc(file_inode(file
));
915 /* Disable all handlers. */
916 misc
->enabled
= false;
919 /* Enable all handlers. */
920 misc
->enabled
= true;
923 /* Delete all handlers. */
924 inode
= d_inode(file_inode(file
)->i_sb
->s_root
);
928 * In order to add new element or remove elements from the list
929 * via bm_{entry,register,status}_write() inode_lock() on the
930 * root inode must be held.
931 * The lock is exclusive ensuring that the list can't be
932 * modified. Only load_misc_binary() can access but does so
933 * read-only. So we only need to take the write lock when we
934 * actually remove the entry from the list.
936 list_for_each_entry_safe(e
, next
, &misc
->entries
, list
)
937 remove_binfmt_handler(misc
, e
);
948 static const struct file_operations bm_status_operations
= {
949 .read
= bm_status_read
,
950 .write
= bm_status_write
,
951 .llseek
= default_llseek
,
954 /* Superblock handling */
956 static void bm_put_super(struct super_block
*sb
)
958 struct user_namespace
*user_ns
= sb
->s_fs_info
;
960 sb
->s_fs_info
= NULL
;
961 put_user_ns(user_ns
);
964 static const struct super_operations s_ops
= {
965 .statfs
= simple_statfs
,
966 .evict_inode
= bm_evict_inode
,
967 .put_super
= bm_put_super
,
970 static int bm_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
973 struct user_namespace
*user_ns
= sb
->s_user_ns
;
974 struct binfmt_misc
*misc
;
975 static const struct tree_descr bm_files
[] = {
976 [2] = {"status", &bm_status_operations
, S_IWUSR
|S_IRUGO
},
977 [3] = {"register", &bm_register_operations
, S_IWUSR
},
981 if (WARN_ON(user_ns
!= current_user_ns()))
985 * Lazily allocate a new binfmt_misc instance for this namespace, i.e.
986 * do it here during the first mount of binfmt_misc. We don't need to
987 * waste memory for every user namespace allocation. It's likely much
988 * more common to not mount a separate binfmt_misc instance than it is
991 * While multiple superblocks can exist they are keyed by userns in
992 * s_fs_info for binfmt_misc. Hence, the vfs guarantees that
993 * bm_fill_super() is called exactly once whenever a binfmt_misc
994 * superblock for a userns is created. This in turn lets us conclude
995 * that when a binfmt_misc superblock is created for the first time for
996 * a userns there's no one racing us. Therefore we don't need any
997 * barriers when we dereference binfmt_misc.
999 misc
= user_ns
->binfmt_misc
;
1002 * If it turns out that most user namespaces actually want to
1003 * register their own binary type handler and therefore all
1004 * create their own separate binfm_misc mounts we should
1005 * consider turning this into a kmem cache.
1007 misc
= kzalloc(sizeof(struct binfmt_misc
), GFP_KERNEL
);
1011 INIT_LIST_HEAD(&misc
->entries
);
1012 rwlock_init(&misc
->entries_lock
);
1014 /* Pairs with smp_load_acquire() in load_binfmt_misc(). */
1015 smp_store_release(&user_ns
->binfmt_misc
, misc
);
1019 * When the binfmt_misc superblock for this userns is shutdown
1020 * ->enabled might have been set to false and we don't reinitialize
1021 * ->enabled again in put_super() as someone might already be mounting
1022 * binfmt_misc again. It also would be pointless since by the time
1023 * ->put_super() is called we know that the binary type list for this
1024 * bintfmt_misc mount is empty making load_misc_binary() return
1025 * -ENOEXEC independent of whether ->enabled is true. Instead, if
1026 * someone mounts binfmt_misc for the first time or again we simply
1027 * reset ->enabled to true.
1029 misc
->enabled
= true;
1031 err
= simple_fill_super(sb
, BINFMTFS_MAGIC
, bm_files
);
1037 static void bm_free(struct fs_context
*fc
)
1040 put_user_ns(fc
->s_fs_info
);
1043 static int bm_get_tree(struct fs_context
*fc
)
1045 return get_tree_keyed(fc
, bm_fill_super
, get_user_ns(fc
->user_ns
));
1048 static const struct fs_context_operations bm_context_ops
= {
1050 .get_tree
= bm_get_tree
,
1053 static int bm_init_fs_context(struct fs_context
*fc
)
1055 fc
->ops
= &bm_context_ops
;
1059 static struct linux_binfmt misc_format
= {
1060 .module
= THIS_MODULE
,
1061 .load_binary
= load_misc_binary
,
1064 static struct file_system_type bm_fs_type
= {
1065 .owner
= THIS_MODULE
,
1066 .name
= "binfmt_misc",
1067 .init_fs_context
= bm_init_fs_context
,
1068 .fs_flags
= FS_USERNS_MOUNT
,
1069 .kill_sb
= kill_litter_super
,
1071 MODULE_ALIAS_FS("binfmt_misc");
1073 static int __init
init_misc_binfmt(void)
1075 int err
= register_filesystem(&bm_fs_type
);
1077 insert_binfmt(&misc_format
);
1081 static void __exit
exit_misc_binfmt(void)
1083 unregister_binfmt(&misc_format
);
1084 unregister_filesystem(&bm_fs_type
);
1087 core_initcall(init_misc_binfmt
);
1088 module_exit(exit_misc_binfmt
);
1089 MODULE_DESCRIPTION("Kernel support for miscellaneous binaries");
1090 MODULE_LICENSE("GPL");