1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1993, David Greenman
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef _SYS_IMGACT_H_
33#define	_SYS_IMGACT_H_
34
35#include <sys/_uio.h>
36
37#include <vm/vm.h>
38
39#define MAXSHELLCMDLEN	PAGE_SIZE
40
41struct ucred;
42
43struct image_args {
44	char *buf;		/* pointer to string buffer */
45	void *bufkva;		/* cookie for string buffer KVA */
46	char *begin_argv;	/* beginning of argv in buf */
47	char *begin_envv;	/* (interal use only) beginning of envv in buf,
48				 * access with exec_args_get_begin_envv(). */
49	char *endp;		/* current `end' pointer of arg & env strings */
50	char *fname;            /* pointer to filename of executable (system space) */
51	char *fname_buf;	/* pointer to optional malloc(M_TEMP) buffer */
52	int stringspace;	/* space left in arg & env buffer */
53	int argc;		/* count of argument strings */
54	int envc;		/* count of environment strings */
55	int fd;			/* file descriptor of the executable */
56};
57
58struct image_params {
59	struct proc *proc;		/* our process */
60	struct label *execlabel;	/* optional exec label */
61	struct vnode *vp;		/* pointer to vnode of file to exec */
62	struct vm_object *object;	/* The vm object for this vp */
63	struct vattr *attr;		/* attributes of file */
64	const char *image_header;	/* header of file to exec */
65	unsigned long entry_addr;	/* entry address of target executable */
66	unsigned long reloc_base;	/* load address of image */
67	unsigned long et_dyn_addr;	/* PIE load base */
68	char *interpreter_name;		/* name of the interpreter */
69	void *auxargs;			/* ELF Auxinfo structure pointer */
70	struct sf_buf *firstpage;	/* first page that we mapped */
71	void *ps_strings;		/* pointer to ps_string (user space) */
72	struct image_args *args;	/* system call arguments */
73	struct sysentvec *sysent;	/* system entry vector */
74	void *argv;			/* pointer to argv (user space) */
75	void *envv;			/* pointer to envv (user space) */
76	char *execpath;
77	void *execpathp;
78	char *freepath;
79	void *canary;
80	int canarylen;
81	void *pagesizes;
82	int pagesizeslen;
83	vm_prot_t stack_prot;
84	u_long stack_sz;
85	struct ucred *newcred;		/* new credentials if changing */
86#define IMGACT_SHELL	0x1
87#define IMGACT_BINMISC	0x2
88	unsigned char interpreted;	/* mask of interpreters that have run */
89	bool credential_setid;		/* true if becoming setid */
90	bool vmspace_destroyed;		/* we've blown away original vm space */
91	bool opened;			/* we have opened executable vnode */
92	bool textset;
93	u_int map_flags;
94#define IMGP_ASLR_SHARED_PAGE	0x1
95	uint32_t imgp_flags;
96	struct vnode *interpreter_vp;	/* vnode of the interpreter */
97};
98
99#ifdef _KERNEL
100struct sysentvec;
101struct thread;
102struct vmspace;
103
104int	exec_alloc_args(struct image_args *);
105int	exec_args_add_arg(struct image_args *args, const char *argp,
106	    enum uio_seg segflg);
107int	exec_args_add_env(struct image_args *args, const char *envp,
108	    enum uio_seg segflg);
109int	exec_args_add_fname(struct image_args *args, const char *fname,
110	    enum uio_seg segflg);
111int	exec_args_adjust_args(struct image_args *args, size_t consume,
112	    ssize_t extend);
113char	*exec_args_get_begin_envv(struct image_args *args);
114int	exec_check_permissions(struct image_params *);
115void	exec_cleanup(struct thread *td, struct vmspace *);
116int	exec_copyout_strings(struct image_params *, uintptr_t *);
117void	exec_free_args(struct image_args *);
118int	exec_map_stack(struct image_params *);
119int	exec_new_vmspace(struct image_params *, struct sysentvec *);
120void	exec_setregs(struct thread *, struct image_params *, uintptr_t);
121int	exec_shell_imgact(struct image_params *);
122int	exec_copyin_args(struct image_args *, const char *, enum uio_seg,
123	char **, char **);
124int	pre_execve(struct thread *td, struct vmspace **oldvmspace);
125void	post_execve(struct thread *td, int error, struct vmspace *oldvmspace);
126#endif
127
128#endif /* !_SYS_IMGACT_H_ */
129