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 /* Interpreter selection staged by a bpf binfmt_misc handler. */
16 struct binfmt_misc_bpf {
17 /* interpreters the matched entry bound, selectable by name */
18 const struct list_head *bpf_interps;
19 const char *bpf_interp; /* interpreter selected by a bpf handler */
20 struct file *bpf_interp_file; /* the bound interpreter it selected */
21 const char *bpf_interp_arg; /* interpreter argument from a bpf handler */
22 u64 bpf_flags; /* enum bpf_binprm_flags from a bpf handler */
23 };
24
25 /*
26 * This structure is used to hold the arguments that are used when loading binaries.
27 */
28 struct linux_binprm {
29 #ifdef CONFIG_MMU
30 struct vm_area_struct *vma;
31 unsigned long vma_pages;
32 unsigned long argmin; /* rlimit marker for copy_strings() */
33 #else
34 # define MAX_ARG_PAGES 32
35 struct page *page[MAX_ARG_PAGES];
36 #endif
37 struct mm_struct *mm;
38 struct mm_struct *old_mm; /* replaced address space, freed by setup_new_exec() */
39 /* user_ns published to task->exec_state at execve, narrowed by would_dump(). */
40 struct user_namespace *user_ns;
41 unsigned long p; /* current top of mem */
42 unsigned int
43 /* Should an execfd be passed to userspace? */
44 have_execfd:1,
45
46 /* Use the creds of a script (see binfmt_misc) */
47 execfd_creds:1,
48 /*
49 * Set by bprm_creds_for_exec hook to indicate a
50 * privilege-gaining exec has happened. Used to set
51 * AT_SECURE auxv for glibc.
52 */
53 secureexec:1,
54 /*
55 * Set when errors can no longer be returned to the
56 * original userspace.
57 */
58 point_of_no_return:1,
59 /* Set when "comm" must come from the dentry. */
60 comm_from_dentry:1,
61 /*
62 * Set by user space to check executability according to the
63 * caller's environment.
64 */
65 is_check:1;
66 struct file *executable; /* Executable to pass to the interpreter */
67 struct file *interpreter;
68 struct file *loader;
69 struct file *file;
70 struct cred *cred; /* new credentials */
71 int unsafe; /* how unsafe this exec is (mask of LSM_UNSAFE_*) */
72 unsigned int per_clear; /* bits to clear in current->personality */
73 int argc, envc;
74 const char *filename; /* Name of binary as seen by procps */
75 const char *interp; /* Name of the binary really executed. Most
76 of the time same as filename, but could be
77 different for binfmt_{misc,script} */
78 const char *fdpath; /* generated filename for execveat */
79 struct binfmt_misc_bpf; /* bpf handler interpreter selection */
80 unsigned interp_flags;
81 int execfd; /* File descriptor of the executable */
82 unsigned long exec;
83
84 struct rlimit rlim_stack; /* Saved RLIMIT_STACK used during exec. */
85
86 char buf[BINPRM_BUF_SIZE];
87 } __randomize_layout;
88
89 #define BINPRM_FLAGS_ENFORCE_NONDUMP_BIT 0
90 #define BINPRM_FLAGS_ENFORCE_NONDUMP (1 << BINPRM_FLAGS_ENFORCE_NONDUMP_BIT)
91
92 /* filename of the binary will be inaccessible after exec */
93 #define BINPRM_FLAGS_PATH_INACCESSIBLE_BIT 2
94 #define BINPRM_FLAGS_PATH_INACCESSIBLE (1 << BINPRM_FLAGS_PATH_INACCESSIBLE_BIT)
95
96 /* preserve argv0 for the interpreter */
97 #define BINPRM_FLAGS_PRESERVE_ARGV0_BIT 3
98 #define BINPRM_FLAGS_PRESERVE_ARGV0 (1 << BINPRM_FLAGS_PRESERVE_ARGV0_BIT)
99
100 /* binfmt_misc dispatched to the interpreter transparently */
101 #define BINPRM_FLAGS_TRANSPARENT_INTERP_BIT 4
102 #define BINPRM_FLAGS_TRANSPARENT_INTERP (1 << BINPRM_FLAGS_TRANSPARENT_INTERP_BIT)
103
104 /**
105 * bprm_at_flags - the AT_FLAGS this invocation implies
106 * @bprm: binary that is being executed
107 *
108 * Tell the program on the receiving end which dispatch contract it got.
109 *
110 * Return: the AT_FLAGS value for this exec
111 */
bprm_at_flags(const struct linux_binprm * bprm)112 static inline unsigned long bprm_at_flags(const struct linux_binprm *bprm)
113 {
114 /* Transparency preserves the whole argv, argv[0] included. */
115 if (bprm->interp_flags & BINPRM_FLAGS_TRANSPARENT_INTERP)
116 return AT_FLAGS_TRANSPARENT_INTERP;
117 if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0)
118 return AT_FLAGS_PRESERVE_ARGV0;
119 return 0;
120 }
121
122 /*
123 * This structure defines the functions that are used to load the binary formats that
124 * linux accepts.
125 */
126 struct linux_binfmt {
127 struct list_head lh;
128 struct module *module;
129 int (*load_binary)(struct linux_binprm *);
130 #ifdef CONFIG_COREDUMP
131 /* Returns true if the whole coredump was written. */
132 bool (*core_dump)(struct coredump_params *cprm);
133 unsigned long min_coredump; /* minimal dump size */
134 #endif
135 } __randomize_layout;
136
137 #if IS_ENABLED(CONFIG_BINFMT_MISC)
138 struct binfmt_misc {
139 struct hlist_head entries;
140 spinlock_t entries_lock;
141 bool enabled;
142 } __randomize_layout;
143
144 extern struct binfmt_misc init_binfmt_misc;
145 #endif
146
147 extern void __register_binfmt(struct linux_binfmt *fmt, int insert);
148
149 /* Registration of default binfmt handlers */
register_binfmt(struct linux_binfmt * fmt)150 static inline void register_binfmt(struct linux_binfmt *fmt)
151 {
152 __register_binfmt(fmt, 0);
153 }
154 /* Same as above, but adds a new binfmt at the top of the list */
insert_binfmt(struct linux_binfmt * fmt)155 static inline void insert_binfmt(struct linux_binfmt *fmt)
156 {
157 __register_binfmt(fmt, 1);
158 }
159
160 extern void unregister_binfmt(struct linux_binfmt *);
161
162 extern int __must_check remove_arg_zero(struct linux_binprm *);
163 extern int begin_new_exec(struct linux_binprm * bprm);
164 extern void setup_new_exec(struct linux_binprm * bprm);
165 extern void finalize_exec(struct linux_binprm *bprm);
166 extern void would_dump(struct linux_binprm *, struct file *);
167 struct file *bprm_open_interpreter(struct linux_binprm *bprm, const char *path);
168 void bprm_drop_loader(struct linux_binprm *bprm);
169
170 extern int suid_dumpable;
171
172 /* Stack area protections */
173 #define EXSTACK_DEFAULT 0 /* Whatever the arch defaults to */
174 #define EXSTACK_DISABLE_X 1 /* Disable executable stacks */
175 #define EXSTACK_ENABLE_X 2 /* Enable executable stacks */
176
177 extern int setup_arg_pages(struct linux_binprm * bprm,
178 unsigned long stack_top,
179 int executable_stack);
180 extern int transfer_args_to_stack(struct linux_binprm *bprm,
181 unsigned long *sp_location);
182 extern int bprm_change_interp(const char *interp, struct linux_binprm *bprm);
183 int copy_string_kernel(const char *arg, struct linux_binprm *bprm);
184 extern void set_binfmt(struct linux_binfmt *new);
185 extern ssize_t read_code(struct file *, unsigned long, loff_t, size_t);
186
187 int kernel_execve(const char *filename,
188 const char *const *argv, const char *const *envp);
189
190 #endif /* _LINUX_BINFMTS_H */
191