xref: /illumos-gate/usr/src/uts/common/sys/exec.h (revision e6c4e893c6a9849a441fddbb31ea45dc6814ec6b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2022 Garrett D'Amore <garrett@damore.org>
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * Copyright 2019 Joyent, Inc.
32  */
33 
34 #ifndef _SYS_EXEC_H
35 #define	_SYS_EXEC_H
36 
37 #include <sys/systm.h>
38 #include <vm/seg.h>
39 #include <vm/seg_vn.h>
40 #include <sys/model.h>
41 #include <sys/uio.h>
42 #include <sys/corectl.h>
43 #include <sys/machelf.h>
44 
45 #ifdef	__cplusplus
46 extern "C" {
47 #endif
48 
49 /*
50  * Number of bytes to read for magic string
51  */
52 #define	MAGIC_BYTES	8
53 
54 #define	getexmag(x)	(((x)[0] << 8) + (x)[1])
55 
56 typedef struct execa {
57 	const char *fname;
58 	const char **argp;
59 	const char **envp;
60 } execa_t;
61 
62 typedef struct execenv {
63 	caddr_t ex_bssbase;
64 	caddr_t ex_brkbase;
65 	size_t	ex_brksize;
66 	vnode_t *ex_vp;
67 	short   ex_magic;
68 } execenv_t;
69 
70 #ifdef _KERNEL
71 
72 #define	LOADABLE_EXEC(e)	((e)->exec_lock)
73 #define	LOADED_EXEC(e)		((e)->exec_func)
74 
75 
76 /*
77  * User argument structure for passing exec information around between the
78  * common and machine-dependent portions of exec and the exec modules.
79  */
80 typedef struct uarg {
81 	ssize_t	na;
82 	ssize_t	ne;
83 	ssize_t	nc;
84 	ssize_t arglen;
85 	char	*fname;
86 	char	*pathname;
87 	size_t	auxsize;
88 	caddr_t	stackend;
89 	size_t	stk_align;
90 	size_t	stk_size;
91 	char	*stk_base;
92 	char	*stk_strp;
93 	int	*stk_offp;
94 	size_t	usrstack_size;
95 	uint_t	stk_prot;
96 	uint_t	dat_prot;
97 	int	traceinval;
98 	int	addr32;
99 	model_t	to_model;
100 	model_t	from_model;
101 	size_t	to_ptrsize;
102 	size_t	from_ptrsize;
103 	size_t	ncargs;
104 	struct execsw *execswp;
105 	uintptr_t entry;
106 	uintptr_t thrptr;
107 	vnode_t	*ex_vp;
108 	char	*emulator;
109 	char	*brandname;
110 	char	*auxp_auxflags; /* addr of auxflags auxv on the user stack */
111 	char	*auxp_brand; /* address of first brand auxv on user stack */
112 	cred_t	*pfcred;
113 	boolean_t scrubenv;
114 	uintptr_t commpage;
115 } uarg_t;
116 
117 /*
118  * Possible brand actions for exec.
119  */
120 #define	EBA_NONE	0
121 #define	EBA_NATIVE	1
122 #define	EBA_BRAND	2
123 
124 /*
125  * The following macro is a machine dependent encapsulation of
126  * postfix processing to hide the stack direction from elf.c
127  * thereby making the elf.c code machine independent.
128  */
129 #define	execpoststack(ARGS, ARRAYADDR, BYTESIZE) \
130 	(copyout((caddr_t)(ARRAYADDR), (ARGS)->stackend, (BYTESIZE)) ? EFAULT \
131 		: (((ARGS)->stackend += (BYTESIZE)), 0))
132 
133 /*
134  * This provides the current user stack address for an object of size BYTESIZE.
135  * Used to determine the stack address just before applying execpoststack().
136  */
137 #define	stackaddress(ARGS, BYTESIZE)	((ARGS)->stackend)
138 
139 /*
140  * Macro to add attribute/values the aux vector under construction.
141  */
142 /* BEGIN CSTYLED */
143 #if ((_LONG_ALIGNMENT == (2 * _INT_ALIGNMENT)) || \
144      (_POINTER_ALIGNMENT == (2 * _INT_ALIGNMENT)))
145 /* END CSTYLED */
146 /*
147  * This convoluted stuff is necessitated by the fact that there is
148  * potential padding in the aux vector, but not necessarily and
149  * without clearing the padding there is a small, but potential
150  * security hole.
151  */
152 #define	ADDAUX(p, a, v)	{		\
153 		(&(p)->a_type)[1] = 0;	\
154 		(p)->a_type = (a);	\
155 		(p)->a_un.a_val = (v);	\
156 		++(p);			\
157 	}
158 #else
159 #define	ADDAUX(p, a, v)	{			\
160 		(p)->a_type = (a);		\
161 		((p)++)->a_un.a_val = (v);	\
162 	}
163 #endif
164 
165 #define	INTPSZ	MAXPATHLEN
166 #define	INTP_MAXDEPTH	5	/* Nested interpreter depth matches Linux */
167 typedef struct intpdata {
168 	char	*intp;
169 	char	*intp_name[INTP_MAXDEPTH];
170 	char	*intp_arg[INTP_MAXDEPTH];
171 } intpdata_t;
172 
173 #define	EXECSETID_SETID		0x1 /* setid exec */
174 #define	EXECSETID_UGIDS		0x2 /* [ug]ids mismatch */
175 #define	EXECSETID_PRIVS		0x4 /* more privs than before */
176 
177 struct execsw {
178 	char	*exec_magic;
179 	int	exec_magoff;
180 	int	exec_maglen;
181 	int	(*exec_func)(struct vnode *vp, struct execa *uap,
182 		    struct uarg *args, struct intpdata *idata, int level,
183 		    size_t *execsz, int setid, caddr_t exec_file,
184 		    struct cred *cred, int brand_action);
185 	int	(*exec_core)(struct vnode *vp, struct proc *p,
186 		    struct cred *cred, rlim64_t rlimit, int sig,
187 		    core_content_t content);
188 	krwlock_t	*exec_lock;
189 };
190 
191 extern int nexectype;		/* number of elements in execsw */
192 extern struct execsw execsw[];
193 extern kmutex_t execsw_lock;
194 
195 extern short elfmagic;
196 extern short intpmagic;
197 extern short javamagic;
198 extern short nomagic;
199 
200 extern char elf32magicstr[];
201 extern char elf64magicstr[];
202 extern char intpmagicstr[];
203 extern char javamagicstr[];
204 extern char nomagicstr[];
205 
206 extern int exec_args(execa_t *, uarg_t *, intpdata_t *, void **);
207 extern int exece(uintptr_t, const char **, const char **, int);
208 extern int exec_common(const char *, const char **, const char **, vnode_t *,
209     int);
210 extern int gexec(vnode_t **vp, struct execa *uap, struct uarg *args,
211     struct intpdata *idata, int level, size_t *execsz, caddr_t exec_file,
212     struct cred *cred, int brand_action);
213 extern struct execsw *allocate_execsw(char *name, char *magic,
214     size_t magic_size);
215 extern struct execsw *findexecsw(char *magic);
216 extern struct execsw *findexec_by_hdr(char *header);
217 extern struct execsw *findexec_by_magic(char *magic);
218 extern int execpermissions(struct vnode *vp, struct vattr *vattrp,
219     struct uarg *args);
220 extern int execmap(vnode_t *vp, caddr_t addr, size_t len, size_t zfodlen,
221     off_t offset, int prot, int page, uint_t);
222 extern void setexecenv(struct execenv *ep);
223 extern int execopen(struct vnode **vpp, int *fdp);
224 extern int execclose(int fd);
225 extern void setregs(uarg_t *);
226 extern void exec_set_sp(size_t);
227 
228 /*
229  * Utility functions for branded process executing
230  */
231 #if !defined(_ELF32_COMPAT)
232 /*
233  * When compiling 64-bit kernels we don't want these definitions included
234  * when compiling the 32-bit compatability elf code in the elfexec module.
235  */
236 extern int elfexec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
237     size_t *, int, caddr_t, cred_t *, int);
238 extern int mapexec_brand(vnode_t *, uarg_t *, Ehdr *, Addr *,
239     intptr_t *, caddr_t, int *, caddr_t *, caddr_t *, size_t *, uintptr_t *);
240 extern int elfreadhdr(vnode_t *, cred_t *, Ehdr *, uint_t *, caddr_t *,
241     size_t *);
242 #endif /* !_ELF32_COMPAT */
243 
244 #if defined(_LP64)
245 extern int elf32exec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
246     size_t *, int, caddr_t, cred_t *, int);
247 extern int mapexec32_brand(vnode_t *, uarg_t *, Elf32_Ehdr *, Elf32_Addr *,
248     intptr_t *, caddr_t, int *, caddr_t *, caddr_t *, size_t *, uintptr_t *);
249 extern int elf32readhdr(vnode_t *, cred_t *, Elf32_Ehdr *, uint_t *, caddr_t *,
250     size_t *);
251 #endif  /* _LP64 */
252 
253 /*
254  * Utility functions for exec module core routines:
255  */
256 extern int core_seg(proc_t *, vnode_t *, u_offset_t, caddr_t, size_t,
257     rlim64_t, cred_t *);
258 
259 extern int core_write(vnode_t *, enum uio_seg, u_offset_t, const void *,
260     size_t, rlim64_t, cred_t *);
261 
262 #endif	/* _KERNEL */
263 
264 #ifdef	__cplusplus
265 }
266 #endif
267 
268 #endif /* _SYS_EXEC_H */
269