xref: /illumos-gate/usr/src/uts/common/sys/exec.h (revision 69a0bf0c658b89d78f2def6997ee0e7d96f13524)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #ifndef _SYS_EXEC_H
31 #define	_SYS_EXEC_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #include <sys/systm.h>
36 #include <vm/seg.h>
37 #include <vm/seg_vn.h>
38 #include <sys/model.h>
39 #include <sys/uio.h>
40 #include <sys/corectl.h>
41 #include <sys/machelf.h>
42 
43 #ifdef	__cplusplus
44 extern "C" {
45 #endif
46 
47 /*
48  * Number of bytes to read for magic string
49  */
50 #define	MAGIC_BYTES	8
51 
52 #define	getexmag(x)	(((x)[0] << 8) + (x)[1])
53 
54 typedef struct execa {
55 	const char *fname;
56 	const char **argp;
57 	const char **envp;
58 } execa_t;
59 
60 typedef struct execenv {
61 	caddr_t ex_bssbase;
62 	caddr_t ex_brkbase;
63 	size_t	ex_brksize;
64 	vnode_t *ex_vp;
65 	short   ex_magic;
66 } execenv_t;
67 
68 #ifdef _KERNEL
69 
70 #define	LOADABLE_EXEC(e)	((e)->exec_lock)
71 #define	LOADED_EXEC(e)		((e)->exec_func)
72 
73 extern int nexectype;		/* number of elements in execsw */
74 extern struct execsw execsw[];
75 extern kmutex_t execsw_lock;
76 
77 /*
78  * User argument structure for passing exec information around between the
79  * common and machine-dependent portions of exec and the exec modules.
80  */
81 typedef struct uarg {
82 	ssize_t	na;
83 	ssize_t	ne;
84 	ssize_t	nc;
85 	ssize_t arglen;
86 	char	*fname;
87 	char	*pathname;
88 	ssize_t	auxsize;
89 	caddr_t	stackend;
90 	size_t	stk_align;
91 	size_t	stk_size;
92 	char	*stk_base;
93 	char	*stk_strp;
94 	int	*stk_offp;
95 	size_t	usrstack_size;
96 	uint_t	stk_prot;
97 	uint_t	dat_prot;
98 	int	traceinval;
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 	char	*emulator;
108 	char	*brandname;
109 	char	*auxp_brand_phdr; /* addr of brand phdr auxv on user stack */
110 } uarg_t;
111 
112 /*
113  * Possible brand actions for exec.
114  */
115 #define	EBA_NONE	0
116 #define	EBA_NATIVE	1
117 #define	EBA_BRAND	2
118 
119 /*
120  * The following macro is a machine dependent encapsulation of
121  * postfix processing to hide the stack direction from elf.c
122  * thereby making the elf.c code machine independent.
123  */
124 #define	execpoststack(ARGS, ARRAYADDR, BYTESIZE) \
125 	(copyout((caddr_t)(ARRAYADDR), (ARGS)->stackend, (BYTESIZE)) ? EFAULT \
126 		: (((ARGS)->stackend += (BYTESIZE)), 0))
127 
128 /*
129  * This provides the current user stack address for an object of size BYTESIZE.
130  * Used to determine the stack address just before applying execpoststack().
131  */
132 #define	stackaddress(ARGS, BYTESIZE)	((ARGS)->stackend)
133 
134 /*
135  * Macro to add attribute/values the aux vector under construction.
136  */
137 /* BEGIN CSTYLED */
138 #if ((_LONG_ALIGNMENT == (2 * _INT_ALIGNMENT)) || \
139      (_POINTER_ALIGNMENT == (2 * _INT_ALIGNMENT)))
140 /* END CSTYLED */
141 /*
142  * This convoluted stuff is necessitated by the fact that there is
143  * potential padding in the aux vector, but not necessarily and
144  * without clearing the padding there is a small, but potential
145  * security hole.
146  */
147 #define	ADDAUX(p, a, v)	{		\
148 		(&(p)->a_type)[1] = 0;	\
149 		(p)->a_type = (a);	\
150 		(p)->a_un.a_val = (v);	\
151 		++(p);			\
152 	}
153 #else
154 #define	ADDAUX(p, a, v)	{			\
155 		(p)->a_type = (a);		\
156 		((p)++)->a_un.a_val = (v);	\
157 	}
158 #endif
159 
160 #define	INTPSZ	MAXPATHLEN
161 typedef struct intpdata {
162 	char	*intp;
163 	char	*intp_name;
164 	char	*intp_arg;
165 } intpdata_t;
166 
167 #define	EXECSETID_SETID		0x1 /* setid exec */
168 #define	EXECSETID_UGIDS		0x2 /* [ug]ids mismatch */
169 #define	EXECSETID_PRIVS		0x4 /* more privs than before */
170 
171 struct execsw {
172 	char	*exec_magic;
173 	int	exec_magoff;
174 	int	exec_maglen;
175 	int	(*exec_func)(struct vnode *vp, struct execa *uap,
176 		    struct uarg *args, struct intpdata *idata, int level,
177 		    long *execsz, int setid, caddr_t exec_file,
178 		    struct cred *cred, int brand_action);
179 	int	(*exec_core)(struct vnode *vp, struct proc *p,
180 		    struct cred *cred, rlim64_t rlimit, int sig,
181 		    core_content_t content);
182 	krwlock_t	*exec_lock;
183 };
184 
185 extern short elfmagic;
186 extern short intpmagic;
187 extern short javamagic;
188 #if defined(__sparc)
189 extern short aout_zmagic;
190 extern short aout_nmagic;
191 extern short aout_omagic;
192 #endif
193 extern short nomagic;
194 
195 extern char elf32magicstr[];
196 extern char elf64magicstr[];
197 extern char intpmagicstr[];
198 extern char javamagicstr[];
199 #if defined(__sparc)
200 extern char aout_nmagicstr[];
201 extern char aout_zmagicstr[];
202 extern char aout_omagicstr[];
203 #endif
204 extern char nomagicstr[];
205 
206 extern int exec_args(execa_t *, uarg_t *, intpdata_t *, void **);
207 extern int exec(const char *fname, const char **argp);
208 extern int exece(const char *fname, const char **argp, const char **envp);
209 extern int exec_common(const char *fname, const char **argp,
210     const char **envp, int brand_action);
211 extern int gexec(vnode_t **vp, struct execa *uap, struct uarg *args,
212     struct intpdata *idata, int level, long *execsz, caddr_t exec_file,
213     struct cred *cred, int brand_action);
214 extern struct execsw *allocate_execsw(char *name, char *magic,
215     size_t magic_size);
216 extern struct execsw *findexecsw(char *magic);
217 extern struct execsw *findexec_by_hdr(char *header);
218 extern struct execsw *findexec_by_magic(char *magic);
219 extern int execpermissions(struct vnode *vp, struct vattr *vattrp,
220     struct uarg *args);
221 extern int execmap(vnode_t *vp, caddr_t addr, size_t len, size_t zfodlen,
222     off_t offset, int prot, int page, uint_t);
223 extern void setexecenv(struct execenv *ep);
224 extern int execopen(struct vnode **vpp, int *fdp);
225 extern int execclose(int fd);
226 extern void setregs(uarg_t *);
227 extern void exec_set_sp(size_t);
228 
229 /*
230  * Utility functions for branded process executing
231  */
232 #if !defined(_ELF32_COMPAT)
233 /*
234  * When compiling 64-bit kernels we don't want these definitions included
235  * when compiling the 32-bit compatability elf code in the elfexec module.
236  */
237 extern int elfexec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
238     long *, int, caddr_t, cred_t *, int);
239 extern int mapexec_brand(vnode_t *, uarg_t *, Ehdr *, Addr *,
240     intptr_t *, caddr_t, int *, caddr_t *, caddr_t *, size_t *);
241 #endif /* !_ELF32_COMPAT */
242 
243 #if defined(_LP64)
244 extern int elf32exec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
245     long *, int, caddr_t, cred_t *, int);
246 extern int mapexec32_brand(vnode_t *, uarg_t *, Elf32_Ehdr *, Elf32_Addr *,
247     intptr_t *, caddr_t, int *, caddr_t *, caddr_t *, size_t *);
248 #endif	/* _LP64 */
249 
250 /*
251  * Utility functions for exec module core routines:
252  */
253 extern int core_seg(proc_t *, vnode_t *, offset_t, caddr_t,
254     size_t, rlim64_t, cred_t *);
255 
256 extern int core_write(vnode_t *, enum uio_seg, offset_t,
257     const void *, size_t, rlim64_t, cred_t *);
258 
259 /* a.out stuff */
260 
261 struct exec;
262 
263 extern caddr_t gettmem(struct exec *exp);
264 extern caddr_t getdmem(struct exec *exp);
265 extern ulong_t getdfile(struct exec *exp);
266 extern uint_t gettfile(struct exec *exp);
267 extern int chkaout(struct exdata *exp);
268 extern void getexinfo(struct exdata *edp_in, struct exdata *edp_out,
269     int *pagetext, int *pagedata);
270 
271 #endif	/* _KERNEL */
272 
273 #ifdef	__cplusplus
274 }
275 #endif
276 
277 #endif /* _SYS_EXEC_H */
278