xref: /linux-6.15/include/linux/binfmts.h (revision faf2d88e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BINFMTS_H
3 #define _LINUX_BINFMTS_H
4 
5 #include <linux/sched.h>
6 #include <linux/unistd.h>
7 #include <asm/exec.h>
8 #include <uapi/linux/binfmts.h>
9 
10 struct filename;
11 struct coredump_params;
12 
13 #define CORENAME_MAX_SIZE 128
14 
15 /*
16  * This structure is used to hold the arguments that are used when loading binaries.
17  */
18 struct linux_binprm {
19 #ifdef CONFIG_MMU
20 	struct vm_area_struct *vma;
21 	unsigned long vma_pages;
22 	unsigned long argmin; /* rlimit marker for copy_strings() */
23 #else
24 # define MAX_ARG_PAGES	32
25 	struct page *page[MAX_ARG_PAGES];
26 #endif
27 	struct mm_struct *mm;
28 	unsigned long p; /* current top of mem */
29 	unsigned int
30 		/* Should an execfd be passed to userspace? */
31 		have_execfd:1,
32 
33 		/* Use the creds of a script (see binfmt_misc) */
34 		execfd_creds:1,
35 		/*
36 		 * Set by bprm_creds_for_exec hook to indicate a
37 		 * privilege-gaining exec has happened. Used to set
38 		 * AT_SECURE auxv for glibc.
39 		 */
40 		secureexec:1,
41 		/*
42 		 * Set when errors can no longer be returned to the
43 		 * original userspace.
44 		 */
45 		point_of_no_return:1,
46 		/*
47 		 * Set by user space to check executability according to the
48 		 * caller's environment.
49 		 */
50 		is_check:1;
51 	struct file *executable; /* Executable to pass to the interpreter */
52 	struct file *interpreter;
53 	struct file *file;
54 	struct cred *cred;	/* new credentials */
55 	int unsafe;		/* how unsafe this exec is (mask of LSM_UNSAFE_*) */
56 	unsigned int per_clear;	/* bits to clear in current->personality */
57 	int argc, envc;
58 	const char *filename;	/* Name of binary as seen by procps */
59 	const char *interp;	/* Name of the binary really executed. Most
60 				   of the time same as filename, but could be
61 				   different for binfmt_{misc,script} */
62 	const char *fdpath;	/* generated filename for execveat */
63 	unsigned interp_flags;
64 	int execfd;		/* File descriptor of the executable */
65 	unsigned long loader, exec;
66 
67 	struct rlimit rlim_stack; /* Saved RLIMIT_STACK used during exec. */
68 
69 	char buf[BINPRM_BUF_SIZE];
70 } __randomize_layout;
71 
72 #define BINPRM_FLAGS_ENFORCE_NONDUMP_BIT 0
73 #define BINPRM_FLAGS_ENFORCE_NONDUMP (1 << BINPRM_FLAGS_ENFORCE_NONDUMP_BIT)
74 
75 /* filename of the binary will be inaccessible after exec */
76 #define BINPRM_FLAGS_PATH_INACCESSIBLE_BIT 2
77 #define BINPRM_FLAGS_PATH_INACCESSIBLE (1 << BINPRM_FLAGS_PATH_INACCESSIBLE_BIT)
78 
79 /* preserve argv0 for the interpreter  */
80 #define BINPRM_FLAGS_PRESERVE_ARGV0_BIT 3
81 #define BINPRM_FLAGS_PRESERVE_ARGV0 (1 << BINPRM_FLAGS_PRESERVE_ARGV0_BIT)
82 
83 /*
84  * This structure defines the functions that are used to load the binary formats that
85  * linux accepts.
86  */
87 struct linux_binfmt {
88 	struct list_head lh;
89 	struct module *module;
90 	int (*load_binary)(struct linux_binprm *);
91 	int (*load_shlib)(struct file *);
92 #ifdef CONFIG_COREDUMP
93 	int (*core_dump)(struct coredump_params *cprm);
94 	unsigned long min_coredump;	/* minimal dump size */
95 #endif
96 } __randomize_layout;
97 
98 #if IS_ENABLED(CONFIG_BINFMT_MISC)
99 struct binfmt_misc {
100 	struct list_head entries;
101 	rwlock_t entries_lock;
102 	bool enabled;
103 } __randomize_layout;
104 
105 extern struct binfmt_misc init_binfmt_misc;
106 #endif
107 
108 extern void __register_binfmt(struct linux_binfmt *fmt, int insert);
109 
110 /* Registration of default binfmt handlers */
111 static inline void register_binfmt(struct linux_binfmt *fmt)
112 {
113 	__register_binfmt(fmt, 0);
114 }
115 /* Same as above, but adds a new binfmt at the top of the list */
116 static inline void insert_binfmt(struct linux_binfmt *fmt)
117 {
118 	__register_binfmt(fmt, 1);
119 }
120 
121 extern void unregister_binfmt(struct linux_binfmt *);
122 
123 extern int __must_check remove_arg_zero(struct linux_binprm *);
124 extern int begin_new_exec(struct linux_binprm * bprm);
125 extern void setup_new_exec(struct linux_binprm * bprm);
126 extern void finalize_exec(struct linux_binprm *bprm);
127 extern void would_dump(struct linux_binprm *, struct file *);
128 
129 extern int suid_dumpable;
130 
131 /* Stack area protections */
132 #define EXSTACK_DEFAULT   0	/* Whatever the arch defaults to */
133 #define EXSTACK_DISABLE_X 1	/* Disable executable stacks */
134 #define EXSTACK_ENABLE_X  2	/* Enable executable stacks */
135 
136 extern int setup_arg_pages(struct linux_binprm * bprm,
137 			   unsigned long stack_top,
138 			   int executable_stack);
139 extern int transfer_args_to_stack(struct linux_binprm *bprm,
140 				  unsigned long *sp_location);
141 extern int bprm_change_interp(const char *interp, struct linux_binprm *bprm);
142 int copy_string_kernel(const char *arg, struct linux_binprm *bprm);
143 extern void set_binfmt(struct linux_binfmt *new);
144 extern ssize_t read_code(struct file *, unsigned long, loff_t, size_t);
145 
146 int kernel_execve(const char *filename,
147 		  const char *const *argv, const char *const *envp);
148 
149 #endif /* _LINUX_BINFMTS_H */
150