xref: /illumos-gate/usr/src/uts/common/sys/exec.h (revision 2b395c3c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cc4b03b5Scasper  * Common Development and Distribution License (the "License").
6cc4b03b5Scasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a0de58d6SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23134a1f4eSCasper H.S. Dik  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
249174bfaaSGarrett D'Amore  * Copyright 2022 Garrett D'Amore <garrett@damore.org>
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
304e18e297SPatrick Mooney /*
314e18e297SPatrick Mooney  * Copyright 2019 Joyent, Inc.
324e18e297SPatrick Mooney  */
334e18e297SPatrick Mooney 
347c478bd9Sstevel@tonic-gate #ifndef _SYS_EXEC_H
357c478bd9Sstevel@tonic-gate #define	_SYS_EXEC_H
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <vm/seg.h>
397c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
407c478bd9Sstevel@tonic-gate #include <sys/model.h>
417c478bd9Sstevel@tonic-gate #include <sys/uio.h>
427c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
43396a100bSedp #include <sys/machelf.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
467c478bd9Sstevel@tonic-gate extern "C" {
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Number of bytes to read for magic string
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate #define	MAGIC_BYTES	8
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	getexmag(x)	(((x)[0] << 8) + (x)[1])
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate typedef struct execa {
577c478bd9Sstevel@tonic-gate 	const char *fname;
587c478bd9Sstevel@tonic-gate 	const char **argp;
597c478bd9Sstevel@tonic-gate 	const char **envp;
607c478bd9Sstevel@tonic-gate } execa_t;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate typedef struct execenv {
637c478bd9Sstevel@tonic-gate 	caddr_t ex_bssbase;
647c478bd9Sstevel@tonic-gate 	caddr_t ex_brkbase;
657c478bd9Sstevel@tonic-gate 	size_t	ex_brksize;
667c478bd9Sstevel@tonic-gate 	vnode_t *ex_vp;
677c478bd9Sstevel@tonic-gate 	short   ex_magic;
687c478bd9Sstevel@tonic-gate } execenv_t;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #ifdef _KERNEL
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	LOADABLE_EXEC(e)	((e)->exec_lock)
737c478bd9Sstevel@tonic-gate #define	LOADED_EXEC(e)		((e)->exec_func)
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * User argument structure for passing exec information around between the
787c478bd9Sstevel@tonic-gate  * common and machine-dependent portions of exec and the exec modules.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate typedef struct uarg {
817c478bd9Sstevel@tonic-gate 	ssize_t	na;
827c478bd9Sstevel@tonic-gate 	ssize_t	ne;
837c478bd9Sstevel@tonic-gate 	ssize_t	nc;
847c478bd9Sstevel@tonic-gate 	ssize_t arglen;
857c478bd9Sstevel@tonic-gate 	char	*fname;
867c478bd9Sstevel@tonic-gate 	char	*pathname;
874e18e297SPatrick Mooney 	size_t	auxsize;
887c478bd9Sstevel@tonic-gate 	caddr_t	stackend;
897c478bd9Sstevel@tonic-gate 	size_t	stk_align;
907c478bd9Sstevel@tonic-gate 	size_t	stk_size;
917c478bd9Sstevel@tonic-gate 	char	*stk_base;
927c478bd9Sstevel@tonic-gate 	char	*stk_strp;
937c478bd9Sstevel@tonic-gate 	int	*stk_offp;
947c478bd9Sstevel@tonic-gate 	size_t	usrstack_size;
957c478bd9Sstevel@tonic-gate 	uint_t	stk_prot;
967c478bd9Sstevel@tonic-gate 	uint_t	dat_prot;
977c478bd9Sstevel@tonic-gate 	int	traceinval;
98a0de58d6SRoger A. Faulkner 	int	addr32;
997c478bd9Sstevel@tonic-gate 	model_t	to_model;
1007c478bd9Sstevel@tonic-gate 	model_t	from_model;
1017c478bd9Sstevel@tonic-gate 	size_t	to_ptrsize;
1027c478bd9Sstevel@tonic-gate 	size_t	from_ptrsize;
1037c478bd9Sstevel@tonic-gate 	size_t	ncargs;
1047c478bd9Sstevel@tonic-gate 	struct execsw *execswp;
1057c478bd9Sstevel@tonic-gate 	uintptr_t entry;
1067c478bd9Sstevel@tonic-gate 	uintptr_t thrptr;
10705d3dc4bSpaulsan 	vnode_t	*ex_vp;
1089acbbeafSnn35248 	char	*emulator;
1099acbbeafSnn35248 	char	*brandname;
110b71d513aSedp 	char	*auxp_auxflags; /* addr of auxflags auxv on the user stack */
11107678296Ssl108498 	char	*auxp_brand; /* address of first brand auxv on user stack */
112134a1f4eSCasper H.S. Dik 	cred_t	*pfcred;
113134a1f4eSCasper H.S. Dik 	boolean_t scrubenv;
1142428aad8SPatrick Mooney 	uintptr_t commpage;
1157c478bd9Sstevel@tonic-gate } uarg_t;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1189acbbeafSnn35248  * Possible brand actions for exec.
1199acbbeafSnn35248  */
1209acbbeafSnn35248 #define	EBA_NONE	0
1219acbbeafSnn35248 #define	EBA_NATIVE	1
1229acbbeafSnn35248 #define	EBA_BRAND	2
1239acbbeafSnn35248 
1249acbbeafSnn35248 /*
1257c478bd9Sstevel@tonic-gate  * The following macro is a machine dependent encapsulation of
1267c478bd9Sstevel@tonic-gate  * postfix processing to hide the stack direction from elf.c
1277c478bd9Sstevel@tonic-gate  * thereby making the elf.c code machine independent.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate #define	execpoststack(ARGS, ARRAYADDR, BYTESIZE) \
1307c478bd9Sstevel@tonic-gate 	(copyout((caddr_t)(ARRAYADDR), (ARGS)->stackend, (BYTESIZE)) ? EFAULT \
1317c478bd9Sstevel@tonic-gate 		: (((ARGS)->stackend += (BYTESIZE)), 0))
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * This provides the current user stack address for an object of size BYTESIZE.
1357c478bd9Sstevel@tonic-gate  * Used to determine the stack address just before applying execpoststack().
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate #define	stackaddress(ARGS, BYTESIZE)	((ARGS)->stackend)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Macro to add attribute/values the aux vector under construction.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */
1437c478bd9Sstevel@tonic-gate #if ((_LONG_ALIGNMENT == (2 * _INT_ALIGNMENT)) || \
1447c478bd9Sstevel@tonic-gate      (_POINTER_ALIGNMENT == (2 * _INT_ALIGNMENT)))
1457c478bd9Sstevel@tonic-gate /* END CSTYLED */
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * This convoluted stuff is necessitated by the fact that there is
1487c478bd9Sstevel@tonic-gate  * potential padding in the aux vector, but not necessarily and
1497c478bd9Sstevel@tonic-gate  * without clearing the padding there is a small, but potential
1507c478bd9Sstevel@tonic-gate  * security hole.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate #define	ADDAUX(p, a, v)	{		\
1537c478bd9Sstevel@tonic-gate 		(&(p)->a_type)[1] = 0;	\
1547c478bd9Sstevel@tonic-gate 		(p)->a_type = (a);	\
1557c478bd9Sstevel@tonic-gate 		(p)->a_un.a_val = (v);	\
1567c478bd9Sstevel@tonic-gate 		++(p);			\
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate #else
1597c478bd9Sstevel@tonic-gate #define	ADDAUX(p, a, v)	{			\
1607c478bd9Sstevel@tonic-gate 		(p)->a_type = (a);		\
1617c478bd9Sstevel@tonic-gate 		((p)++)->a_un.a_val = (v);	\
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate #endif
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #define	INTPSZ	MAXPATHLEN
16693cf283aSJerry Jelinek #define	INTP_MAXDEPTH	5	/* Nested interpreter depth matches Linux */
1677c478bd9Sstevel@tonic-gate typedef struct intpdata {
1687c478bd9Sstevel@tonic-gate 	char	*intp;
16993cf283aSJerry Jelinek 	char	*intp_name[INTP_MAXDEPTH];
17093cf283aSJerry Jelinek 	char	*intp_arg[INTP_MAXDEPTH];
1717c478bd9Sstevel@tonic-gate } intpdata_t;
1727c478bd9Sstevel@tonic-gate 
173cc4b03b5Scasper #define	EXECSETID_SETID		0x1 /* setid exec */
174cc4b03b5Scasper #define	EXECSETID_UGIDS		0x2 /* [ug]ids mismatch */
175cc4b03b5Scasper #define	EXECSETID_PRIVS		0x4 /* more privs than before */
176cc4b03b5Scasper 
1777c478bd9Sstevel@tonic-gate struct execsw {
1787c478bd9Sstevel@tonic-gate 	char	*exec_magic;
1797c478bd9Sstevel@tonic-gate 	int	exec_magoff;
1807c478bd9Sstevel@tonic-gate 	int	exec_maglen;
1817c478bd9Sstevel@tonic-gate 	int	(*exec_func)(struct vnode *vp, struct execa *uap,
1827c478bd9Sstevel@tonic-gate 		    struct uarg *args, struct intpdata *idata, int level,
1834e18e297SPatrick Mooney 		    size_t *execsz, int setid, caddr_t exec_file,
1849acbbeafSnn35248 		    struct cred *cred, int brand_action);
1857c478bd9Sstevel@tonic-gate 	int	(*exec_core)(struct vnode *vp, struct proc *p,
1867c478bd9Sstevel@tonic-gate 		    struct cred *cred, rlim64_t rlimit, int sig,
1877c478bd9Sstevel@tonic-gate 		    core_content_t content);
1887c478bd9Sstevel@tonic-gate 	krwlock_t	*exec_lock;
1897c478bd9Sstevel@tonic-gate };
1907c478bd9Sstevel@tonic-gate 
191d67944fbSScott Rotondo extern int nexectype;		/* number of elements in execsw */
192d67944fbSScott Rotondo extern struct execsw execsw[];
193d67944fbSScott Rotondo extern kmutex_t execsw_lock;
194d67944fbSScott Rotondo 
1957c478bd9Sstevel@tonic-gate extern short elfmagic;
1967c478bd9Sstevel@tonic-gate extern short intpmagic;
1977c478bd9Sstevel@tonic-gate extern short javamagic;
1987c478bd9Sstevel@tonic-gate extern short nomagic;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate extern char elf32magicstr[];
2017c478bd9Sstevel@tonic-gate extern char elf64magicstr[];
2027c478bd9Sstevel@tonic-gate extern char intpmagicstr[];
2037c478bd9Sstevel@tonic-gate extern char javamagicstr[];
2047c478bd9Sstevel@tonic-gate extern char nomagicstr[];
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate extern int exec_args(execa_t *, uarg_t *, intpdata_t *, void **);
207*2b395c3cSAndy Fiddaman extern int exece(uintptr_t, const char **, const char **, int);
208*2b395c3cSAndy Fiddaman extern int exec_common(const char *, const char **, const char **, vnode_t *,
209*2b395c3cSAndy Fiddaman     int);
2107c478bd9Sstevel@tonic-gate extern int gexec(vnode_t **vp, struct execa *uap, struct uarg *args,
2114e18e297SPatrick Mooney     struct intpdata *idata, int level, size_t *execsz, caddr_t exec_file,
2129acbbeafSnn35248     struct cred *cred, int brand_action);
2137c478bd9Sstevel@tonic-gate extern struct execsw *allocate_execsw(char *name, char *magic,
2147c478bd9Sstevel@tonic-gate     size_t magic_size);
2157c478bd9Sstevel@tonic-gate extern struct execsw *findexecsw(char *magic);
2167c478bd9Sstevel@tonic-gate extern struct execsw *findexec_by_hdr(char *header);
2177c478bd9Sstevel@tonic-gate extern struct execsw *findexec_by_magic(char *magic);
2187c478bd9Sstevel@tonic-gate extern int execpermissions(struct vnode *vp, struct vattr *vattrp,
2197c478bd9Sstevel@tonic-gate     struct uarg *args);
2207c478bd9Sstevel@tonic-gate extern int execmap(vnode_t *vp, caddr_t addr, size_t len, size_t zfodlen,
2217c478bd9Sstevel@tonic-gate     off_t offset, int prot, int page, uint_t);
2227c478bd9Sstevel@tonic-gate extern void setexecenv(struct execenv *ep);
2237c478bd9Sstevel@tonic-gate extern int execopen(struct vnode **vpp, int *fdp);
2247c478bd9Sstevel@tonic-gate extern int execclose(int fd);
2257c478bd9Sstevel@tonic-gate extern void setregs(uarg_t *);
2267c478bd9Sstevel@tonic-gate extern void exec_set_sp(size_t);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
229396a100bSedp  * Utility functions for branded process executing
230396a100bSedp  */
231396a100bSedp #if !defined(_ELF32_COMPAT)
232396a100bSedp /*
233396a100bSedp  * When compiling 64-bit kernels we don't want these definitions included
234396a100bSedp  * when compiling the 32-bit compatability elf code in the elfexec module.
235396a100bSedp  */
236396a100bSedp extern int elfexec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
2374e18e297SPatrick Mooney     size_t *, int, caddr_t, cred_t *, int);
238396a100bSedp extern int mapexec_brand(vnode_t *, uarg_t *, Ehdr *, Addr *,
23907678296Ssl108498     intptr_t *, caddr_t, int *, caddr_t *, caddr_t *, size_t *, uintptr_t *);
2404e18e297SPatrick Mooney extern int elfreadhdr(vnode_t *, cred_t *, Ehdr *, uint_t *, caddr_t *,
2414e18e297SPatrick Mooney     size_t *);
242396a100bSedp #endif /* !_ELF32_COMPAT */
243396a100bSedp 
244396a100bSedp #if defined(_LP64)
245396a100bSedp extern int elf32exec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
2464e18e297SPatrick Mooney     size_t *, int, caddr_t, cred_t *, int);
247396a100bSedp extern int mapexec32_brand(vnode_t *, uarg_t *, Elf32_Ehdr *, Elf32_Addr *,
24807678296Ssl108498     intptr_t *, caddr_t, int *, caddr_t *, caddr_t *, size_t *, uintptr_t *);
2494e18e297SPatrick Mooney extern int elf32readhdr(vnode_t *, cred_t *, Elf32_Ehdr *, uint_t *, caddr_t *,
2504e18e297SPatrick Mooney     size_t *);
251396a100bSedp #endif  /* _LP64 */
252396a100bSedp 
253396a100bSedp /*
2547c478bd9Sstevel@tonic-gate  * Utility functions for exec module core routines:
2557c478bd9Sstevel@tonic-gate  */
2564e18e297SPatrick Mooney extern int core_seg(proc_t *, vnode_t *, u_offset_t, caddr_t, size_t,
2574e18e297SPatrick Mooney     rlim64_t, cred_t *);
2587c478bd9Sstevel@tonic-gate 
2594e18e297SPatrick Mooney extern int core_write(vnode_t *, enum uio_seg, u_offset_t, const void *,
2604e18e297SPatrick Mooney     size_t, rlim64_t, cred_t *);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate #endif
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate #endif /* _SYS_EXEC_H */
269