xref: /linux-6.15/include/linux/binfmts.h (revision cc9554e6)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_BINFMTS_H
31da177e4SLinus Torvalds #define _LINUX_BINFMTS_H
41da177e4SLinus Torvalds 
596e02d15SHeiko Carstens #include <linux/sched.h>
6282124d1SAl Viro #include <linux/unistd.h>
7826eba4dSAl Viro #include <asm/exec.h>
8607ca46eSDavid Howells #include <uapi/linux/binfmts.h>
91da177e4SLinus Torvalds 
1028851755SIngo Molnar struct filename;
11a99a3e2eSEric W. Biederman struct coredump_params;
1228851755SIngo Molnar 
1371ce92f3SDan Aloni #define CORENAME_MAX_SIZE 128
1471ce92f3SDan Aloni 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  * This structure is used to hold the arguments that are used when loading binaries.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds struct linux_binprm {
19b6a2fea3SOllie Wild #ifdef CONFIG_MMU
20b6a2fea3SOllie Wild 	struct vm_area_struct *vma;
213c77f845SOleg Nesterov 	unsigned long vma_pages;
22084ebf7cSKees Cook 	unsigned long argmin; /* rlimit marker for copy_strings() */
23b6a2fea3SOllie Wild #else
24b6a2fea3SOllie Wild # define MAX_ARG_PAGES	32
251da177e4SLinus Torvalds 	struct page *page[MAX_ARG_PAGES];
26b6a2fea3SOllie Wild #endif
271da177e4SLinus Torvalds 	struct mm_struct *mm;
281da177e4SLinus Torvalds 	unsigned long p; /* current top of mem */
29a50b0aa4SKirill A. Shutemov 	unsigned int
30b8a61c9eSEric W. Biederman 		/* Should an execfd be passed to userspace? */
31b8a61c9eSEric W. Biederman 		have_execfd:1,
32b8a61c9eSEric W. Biederman 
3356305aa9SEric W. Biederman 		/* Use the creds of a script (see binfmt_misc) */
3456305aa9SEric W. Biederman 		execfd_creds:1,
3546d98eb4SKees Cook 		/*
36b8bff599SEric W. Biederman 		 * Set by bprm_creds_for_exec hook to indicate a
37b8bff599SEric W. Biederman 		 * privilege-gaining exec has happened. Used to set
38b8bff599SEric W. Biederman 		 * AT_SECURE auxv for glibc.
39c425e189SKees Cook 		 */
40eea96732SEric W. Biederman 		secureexec:1,
41eea96732SEric W. Biederman 		/*
421507b7a3SEric W. Biederman 		 * Set when errors can no longer be returned to the
431507b7a3SEric W. Biederman 		 * original userspace.
44eea96732SEric W. Biederman 		 */
45543841d1SKees Cook 		point_of_no_return:1,
46543841d1SKees Cook 		/* Set when "comm" must come from the dentry. */
4721266b8dSLinus Torvalds 		comm_from_dentry:1,
48a5874fdeSMickaël Salaün 		/*
49a5874fdeSMickaël Salaün 		 * Set by user space to check executability according to the
50a5874fdeSMickaël Salaün 		 * caller's environment.
51a5874fdeSMickaël Salaün 		 */
52a5874fdeSMickaël Salaün 		is_check:1;
53b8a61c9eSEric W. Biederman 	struct file *executable; /* Executable to pass to the interpreter */
54bc2bf338SEric W. Biederman 	struct file *interpreter;
551da177e4SLinus Torvalds 	struct file *file;
56a6f76f23SDavid Howells 	struct cred *cred;	/* new credentials */
57a6f76f23SDavid Howells 	int unsafe;		/* how unsafe this exec is (mask of LSM_UNSAFE_*) */
58a6f76f23SDavid Howells 	unsigned int per_clear;	/* bits to clear in current->personality */
591da177e4SLinus Torvalds 	int argc, envc;
60d7627467SDavid Howells 	const char *filename;	/* Name of binary as seen by procps */
61d7627467SDavid Howells 	const char *interp;	/* Name of the binary really executed. Most
621da177e4SLinus Torvalds 				   of the time same as filename, but could be
631da177e4SLinus Torvalds 				   different for binfmt_{misc,script} */
6460d9ad1dSEric W. Biederman 	const char *fdpath;	/* generated filename for execveat */
651da177e4SLinus Torvalds 	unsigned interp_flags;
66b8a61c9eSEric W. Biederman 	int execfd;		/* File descriptor of the executable */
67*cc9554e6SYonatan Goldschmidt 	unsigned long exec;
68c31dbb14SKees Cook 
69c31dbb14SKees Cook 	struct rlimit rlim_stack; /* Saved RLIMIT_STACK used during exec. */
70a6231d19SAlexey Dobriyan 
71a6231d19SAlexey Dobriyan 	char buf[BINPRM_BUF_SIZE];
723859a271SKees Cook } __randomize_layout;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #define BINPRM_FLAGS_ENFORCE_NONDUMP_BIT 0
751da177e4SLinus Torvalds #define BINPRM_FLAGS_ENFORCE_NONDUMP (1 << BINPRM_FLAGS_ENFORCE_NONDUMP_BIT)
761da177e4SLinus Torvalds 
7751f39a1fSDavid Drysdale /* filename of the binary will be inaccessible after exec */
7851f39a1fSDavid Drysdale #define BINPRM_FLAGS_PATH_INACCESSIBLE_BIT 2
7951f39a1fSDavid Drysdale #define BINPRM_FLAGS_PATH_INACCESSIBLE (1 << BINPRM_FLAGS_PATH_INACCESSIBLE_BIT)
8051f39a1fSDavid Drysdale 
812347961bSLaurent Vivier /* preserve argv0 for the interpreter  */
822347961bSLaurent Vivier #define BINPRM_FLAGS_PRESERVE_ARGV0_BIT 3
832347961bSLaurent Vivier #define BINPRM_FLAGS_PRESERVE_ARGV0 (1 << BINPRM_FLAGS_PRESERVE_ARGV0_BIT)
842347961bSLaurent Vivier 
851da177e4SLinus Torvalds /*
861da177e4SLinus Torvalds  * This structure defines the functions that are used to load the binary formats that
871da177e4SLinus Torvalds  * linux accepts.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds struct linux_binfmt {
90e4dc1b14SAlexey Dobriyan 	struct list_head lh;
911da177e4SLinus Torvalds 	struct module *module;
9271613c3bSAl Viro 	int (*load_binary)(struct linux_binprm *);
931da177e4SLinus Torvalds 	int (*load_shlib)(struct file *);
94d65bc29bSAlexey Dobriyan #ifdef CONFIG_COREDUMP
95f6151dfeSMasami Hiramatsu 	int (*core_dump)(struct coredump_params *cprm);
961da177e4SLinus Torvalds 	unsigned long min_coredump;	/* minimal dump size */
97d65bc29bSAlexey Dobriyan #endif
983859a271SKees Cook } __randomize_layout;
991da177e4SLinus Torvalds 
10021ca59b3SChristian Brauner #if IS_ENABLED(CONFIG_BINFMT_MISC)
10121ca59b3SChristian Brauner struct binfmt_misc {
10221ca59b3SChristian Brauner 	struct list_head entries;
10321ca59b3SChristian Brauner 	rwlock_t entries_lock;
10421ca59b3SChristian Brauner 	bool enabled;
10521ca59b3SChristian Brauner } __randomize_layout;
10621ca59b3SChristian Brauner 
10721ca59b3SChristian Brauner extern struct binfmt_misc init_binfmt_misc;
10821ca59b3SChristian Brauner #endif
10921ca59b3SChristian Brauner 
1108fc3dc5aSAl Viro extern void __register_binfmt(struct linux_binfmt *fmt, int insert);
11174641f58SIvan Kokshaysky 
11274641f58SIvan Kokshaysky /* Registration of default binfmt handlers */
register_binfmt(struct linux_binfmt * fmt)1138fc3dc5aSAl Viro static inline void register_binfmt(struct linux_binfmt *fmt)
11474641f58SIvan Kokshaysky {
1158fc3dc5aSAl Viro 	__register_binfmt(fmt, 0);
11674641f58SIvan Kokshaysky }
11774641f58SIvan Kokshaysky /* Same as above, but adds a new binfmt at the top of the list */
insert_binfmt(struct linux_binfmt * fmt)1188fc3dc5aSAl Viro static inline void insert_binfmt(struct linux_binfmt *fmt)
11974641f58SIvan Kokshaysky {
1208fc3dc5aSAl Viro 	__register_binfmt(fmt, 1);
12174641f58SIvan Kokshaysky }
12274641f58SIvan Kokshaysky 
123f6b450d4SAlexey Dobriyan extern void unregister_binfmt(struct linux_binfmt *);
1241da177e4SLinus Torvalds 
125b6a2fea3SOllie Wild extern int __must_check remove_arg_zero(struct linux_binprm *);
1262388777aSEric W. Biederman extern int begin_new_exec(struct linux_binprm * bprm);
127221af7f8SLinus Torvalds extern void setup_new_exec(struct linux_binprm * bprm);
128b8383831SKees Cook extern void finalize_exec(struct linux_binprm *bprm);
1291b5d783cSAl Viro extern void would_dump(struct linux_binprm *, struct file *);
1301da177e4SLinus Torvalds 
131d6e71144SAlan Cox extern int suid_dumpable;
132d6e71144SAlan Cox 
1331da177e4SLinus Torvalds /* Stack area protections */
1341da177e4SLinus Torvalds #define EXSTACK_DEFAULT   0	/* Whatever the arch defaults to */
1351da177e4SLinus Torvalds #define EXSTACK_DISABLE_X 1	/* Disable executable stacks */
1361da177e4SLinus Torvalds #define EXSTACK_ENABLE_X  2	/* Enable executable stacks */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds extern int setup_arg_pages(struct linux_binprm * bprm,
1391da177e4SLinus Torvalds 			   unsigned long stack_top,
1401da177e4SLinus Torvalds 			   int executable_stack);
1417e7ec6a9SNicolas Pitre extern int transfer_args_to_stack(struct linux_binprm *bprm,
1427e7ec6a9SNicolas Pitre 				  unsigned long *sp_location);
143c2315c18SOleg Nesterov extern int bprm_change_interp(const char *interp, struct linux_binprm *bprm);
144986db2d1SChristoph Hellwig int copy_string_kernel(const char *arg, struct linux_binprm *bprm);
145964ee7dfSOleg Nesterov extern void set_binfmt(struct linux_binfmt *new);
1463dc20cb2SAl Viro extern ssize_t read_code(struct file *, unsigned long, loff_t, size_t);
1471da177e4SLinus Torvalds 
148be619f7fSEric W. Biederman int kernel_execve(const char *filename,
149be619f7fSEric W. Biederman 		  const char *const *argv, const char *const *envp);
15028851755SIngo Molnar 
1511da177e4SLinus Torvalds #endif /* _LINUX_BINFMTS_H */
152