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