blob: 4a20b7517dd036c1fba31ab024ec9ee773d0e9ec [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_BINFMTS_H
3#define _LINUX_BINFMTS_H
4
Heiko Carstens96e02d12012-02-04 10:47:10 +01005#include <linux/sched.h>
Al Viro282124d2012-09-30 13:20:09 -04006#include <linux/unistd.h>
Al Viro826eba42012-08-03 12:14:44 +04007#include <asm/exec.h>
David Howells607ca462012-10-13 10:46:48 +01008#include <uapi/linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Ingo Molnar28851752017-02-05 14:24:31 +010010struct filename;
11
Dan Aloni71ce92f2007-05-16 22:11:16 -070012#define CORENAME_MAX_SIZE 128
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * This structure is used to hold the arguments that are used when loading binaries.
16 */
Mikael Petterssonf670d0e2011-01-12 17:00:02 -080017struct linux_binprm {
Ollie Wildb6a2fea2007-07-19 01:48:16 -070018#ifdef CONFIG_MMU
19 struct vm_area_struct *vma;
Oleg Nesterov3c77f842010-11-30 20:55:34 +010020 unsigned long vma_pages;
Ollie Wildb6a2fea2007-07-19 01:48:16 -070021#else
22# define MAX_ARG_PAGES 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct page *page[MAX_ARG_PAGES];
Ollie Wildb6a2fea2007-07-19 01:48:16 -070024#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 struct mm_struct *mm;
26 unsigned long p; /* current top of mem */
Oleg Nesterov655c16a2019-01-03 15:28:11 -080027 unsigned long argmin; /* rlimit marker for copy_strings() */
Kirill A. Shutemova50b0aa2009-04-02 16:58:29 -070028 unsigned int
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -050029 /* Should an execfd be passed to userspace? */
30 have_execfd:1,
31
Eric W. Biederman56305aa2020-05-29 22:00:54 -050032 /* Use the creds of a script (see binfmt_misc) */
33 execfd_creds:1,
Kees Cook46d98eb2017-07-18 15:25:27 -070034 /*
Eric W. Biedermanb8bff592020-03-22 15:46:24 -050035 * Set by bprm_creds_for_exec hook to indicate a
36 * privilege-gaining exec has happened. Used to set
37 * AT_SECURE auxv for glibc.
Kees Cookc425e182017-07-18 15:25:22 -070038 */
Eric W. Biedermaneea96732020-03-25 10:03:36 -050039 secureexec:1,
40 /*
Eric W. Biederman1507b7a2020-04-02 18:17:50 -050041 * Set when errors can no longer be returned to the
42 * original userspace.
Eric W. Biedermaneea96732020-03-25 10:03:36 -050043 */
Eric W. Biederman1507b7a2020-04-02 18:17:50 -050044 point_of_no_return:1;
Kirill A. Shutemov53112482008-10-15 22:02:37 -070045#ifdef __alpha__
46 unsigned int taso:1;
47#endif
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -050048 struct file * executable; /* Executable to pass to the interpreter */
Eric W. Biedermanbc2bf332020-05-18 18:43:20 -050049 struct file * interpreter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct file * file;
David Howellsa6f76f22008-11-14 10:39:24 +110051 struct cred *cred; /* new credentials */
52 int unsafe; /* how unsafe this exec is (mask of LSM_UNSAFE_*) */
53 unsigned int per_clear; /* bits to clear in current->personality */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int argc, envc;
David Howellsd7627462010-08-17 23:52:56 +010055 const char * filename; /* Name of binary as seen by procps */
56 const char * interp; /* Name of the binary really executed. Most
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 of the time same as filename, but could be
58 different for binfmt_{misc,script} */
59 unsigned interp_flags;
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -050060 int execfd; /* File descriptor of the executable */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 unsigned long loader, exec;
Kees Cookc31dbb142018-04-10 16:35:01 -070062
63 struct rlimit rlim_stack; /* Saved RLIMIT_STACK used during exec. */
Alexey Dobriyana6231d12019-05-14 15:44:40 -070064
65 char buf[BINPRM_BUF_SIZE];
Kees Cook3859a272016-10-28 01:22:25 -070066} __randomize_layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define BINPRM_FLAGS_ENFORCE_NONDUMP_BIT 0
69#define BINPRM_FLAGS_ENFORCE_NONDUMP (1 << BINPRM_FLAGS_ENFORCE_NONDUMP_BIT)
70
David Drysdale51f39a12014-12-12 16:57:29 -080071/* filename of the binary will be inaccessible after exec */
72#define BINPRM_FLAGS_PATH_INACCESSIBLE_BIT 2
73#define BINPRM_FLAGS_PATH_INACCESSIBLE (1 << BINPRM_FLAGS_PATH_INACCESSIBLE_BIT)
74
Masami Hiramatsuf6151df2009-12-17 15:27:16 -080075/* Function parameter for binfmt->coredump */
76struct coredump_params {
Eric W. Biedermanae7795b2018-09-25 11:27:20 +020077 const kernel_siginfo_t *siginfo;
Masami Hiramatsuf6151df2009-12-17 15:27:16 -080078 struct pt_regs *regs;
79 struct file *file;
80 unsigned long limit;
Masami Hiramatsu30736a42010-03-05 13:44:12 -080081 unsigned long mm_flags;
Al Viroecc8c772013-10-05 15:32:35 -040082 loff_t written;
Mateusz Guzik1607f092016-06-05 23:14:14 +020083 loff_t pos;
Masami Hiramatsuf6151df2009-12-17 15:27:16 -080084};
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * This structure defines the functions that are used to load the binary formats that
88 * linux accepts.
89 */
90struct linux_binfmt {
Alexey Dobriyane4dc1b12007-10-16 23:26:03 -070091 struct list_head lh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct module *module;
Al Viro71613c32012-10-20 22:00:48 -040093 int (*load_binary)(struct linux_binprm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int (*load_shlib)(struct file *);
Masami Hiramatsuf6151df2009-12-17 15:27:16 -080095 int (*core_dump)(struct coredump_params *cprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned long min_coredump; /* minimal dump size */
Kees Cook3859a272016-10-28 01:22:25 -070097} __randomize_layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Al Viro8fc3dc52012-03-17 03:05:16 -040099extern void __register_binfmt(struct linux_binfmt *fmt, int insert);
Ivan Kokshaysky74641f52009-04-30 15:08:49 -0700100
101/* Registration of default binfmt handlers */
Al Viro8fc3dc52012-03-17 03:05:16 -0400102static inline void register_binfmt(struct linux_binfmt *fmt)
Ivan Kokshaysky74641f52009-04-30 15:08:49 -0700103{
Al Viro8fc3dc52012-03-17 03:05:16 -0400104 __register_binfmt(fmt, 0);
Ivan Kokshaysky74641f52009-04-30 15:08:49 -0700105}
106/* Same as above, but adds a new binfmt at the top of the list */
Al Viro8fc3dc52012-03-17 03:05:16 -0400107static inline void insert_binfmt(struct linux_binfmt *fmt)
Ivan Kokshaysky74641f52009-04-30 15:08:49 -0700108{
Al Viro8fc3dc52012-03-17 03:05:16 -0400109 __register_binfmt(fmt, 1);
Ivan Kokshaysky74641f52009-04-30 15:08:49 -0700110}
111
Alexey Dobriyanf6b450d2007-10-16 23:26:04 -0700112extern void unregister_binfmt(struct linux_binfmt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700114extern int __must_check remove_arg_zero(struct linux_binprm *);
Eric W. Biederman23887772020-05-03 07:54:10 -0500115extern int begin_new_exec(struct linux_binprm * bprm);
Linus Torvalds221af7f2010-01-28 22:14:42 -0800116extern void setup_new_exec(struct linux_binprm * bprm);
Kees Cookb8383832018-04-10 16:34:57 -0700117extern void finalize_exec(struct linux_binprm *bprm);
Al Viro1b5d7832011-06-19 12:49:47 -0400118extern void would_dump(struct linux_binprm *, struct file *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Alan Coxd6e71142005-06-23 00:09:43 -0700120extern int suid_dumpable;
Alan Coxd6e71142005-06-23 00:09:43 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Stack area protections */
123#define EXSTACK_DEFAULT 0 /* Whatever the arch defaults to */
124#define EXSTACK_DISABLE_X 1 /* Disable executable stacks */
125#define EXSTACK_ENABLE_X 2 /* Enable executable stacks */
126
127extern int setup_arg_pages(struct linux_binprm * bprm,
128 unsigned long stack_top,
129 int executable_stack);
Nicolas Pitre7e7ec6a2016-07-24 11:30:18 -0400130extern int transfer_args_to_stack(struct linux_binprm *bprm,
131 unsigned long *sp_location);
Oleg Nesterovc2315c12017-10-03 16:15:42 -0700132extern int bprm_change_interp(const char *interp, struct linux_binprm *bprm);
Christoph Hellwig986db2d2020-06-04 16:51:14 -0700133int copy_string_kernel(const char *arg, struct linux_binprm *bprm);
Oleg Nesterov964ee7d2009-09-23 15:56:59 -0700134extern void set_binfmt(struct linux_binfmt *new);
Al Viro3dc20cb2013-04-13 20:31:37 -0400135extern ssize_t read_code(struct file *, unsigned long, loff_t, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Ingo Molnar28851752017-02-05 14:24:31 +0100137extern int do_execve(struct filename *,
138 const char __user * const __user *,
139 const char __user * const __user *);
140extern int do_execveat(int, struct filename *,
141 const char __user * const __user *,
142 const char __user * const __user *,
143 int);
Alexei Starovoitov449325b2018-05-21 19:22:29 -0700144int do_execve_file(struct file *file, void *__argv, void *__envp);
Ingo Molnar28851752017-02-05 14:24:31 +0100145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#endif /* _LINUX_BINFMTS_H */