xref: /illumos-gate/usr/src/uts/common/sys/brand.h (revision 621b6cf75a027e4c06f2a1b3207abb5f24539b14)
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) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019 Joyent, Inc.
25  */
26 
27 #ifndef _SYS_BRAND_H
28 #define	_SYS_BRAND_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/proc.h>
35 #include <sys/exec.h>
36 #include <sys/modctl.h>
37 #include <sys/types.h>
38 
39 /*
40  * All Brands supported by this kernel must use BRAND_VER_1.
41  */
42 #define	BRAND_VER_1	1
43 
44 /*
45  * sub-commands to brandsys.
46  * 1 - 128 are for common commands
47  * 128+ are available for brand-specific commands.
48  */
49 #define	B_REGISTER		1
50 #define	B_TTYMODES		2
51 #define	B_ELFDATA		3
52 #define	B_EXEC_NATIVE		4
53 #define	B_EXEC_BRAND		5
54 #define	B_TRUSS_POINT		6
55 
56 /*
57  * Structure used by zoneadmd to communicate the name of a brand and the
58  * supporting brand module into the kernel.
59  */
60 struct brand_attr {
61 	char	ba_brandname[MAXNAMELEN];
62 	char	ba_modname[MAXPATHLEN];
63 };
64 
65 /* What we call the native brand. */
66 #define	NATIVE_BRAND_NAME	"native"
67 
68 /* What we call the labeled brand. */
69 #define	LABELED_BRAND_NAME	"labeled"
70 
71 /*
72  * Aux vector containing lddata pointer of brand library linkmap.
73  * Used by common {brand}_librtld_db.
74  */
75 #define	AT_SUN_BRAND_COMMON_LDDATA	AT_SUN_BRAND_AUX1
76 
77 /*
78  * Information needed by the brand library to launch an executable.
79  */
80 typedef struct brand_elf_data {
81 	ulong_t		sed_phdr;
82 	ulong_t		sed_phent;
83 	ulong_t		sed_phnum;
84 	ulong_t		sed_entry;
85 	ulong_t		sed_base;
86 	ulong_t		sed_ldentry;
87 	ulong_t		sed_lddata;
88 } brand_elf_data_t;
89 
90 /*
91  * Common structure used to register a branded processes
92  */
93 typedef struct brand_proc_reg {
94 	uint_t		sbr_version;	/* version number */
95 	caddr_t		sbr_handler;	/* base address of handler */
96 } brand_proc_reg_t;
97 
98 #ifdef	_KERNEL
99 
100 struct proc;
101 struct uarg;
102 struct brand_mach_ops;
103 struct intpdata;
104 struct execa;
105 
106 struct brand_ops {
107 	void	(*b_init_brand_data)(zone_t *);
108 	void	(*b_free_brand_data)(zone_t *);
109 	int	(*b_brandsys)(int, int64_t *, uintptr_t, uintptr_t, uintptr_t,
110 		uintptr_t, uintptr_t, uintptr_t);
111 	void	(*b_setbrand)(struct proc *);
112 	int	(*b_getattr)(zone_t *, int, void *, size_t *);
113 	int	(*b_setattr)(zone_t *, int, void *, size_t);
114 	void	(*b_copy_procdata)(struct proc *, struct proc *);
115 	void	(*b_proc_exit)(struct proc *, klwp_t *);
116 	void	(*b_exec)();
117 	void	(*b_lwp_setrval)(klwp_t *, int, int);
118 	int	(*b_initlwp)(klwp_t *);
119 	void	(*b_forklwp)(klwp_t *, klwp_t *);
120 	void	(*b_freelwp)(klwp_t *);
121 	void	(*b_lwpexit)(klwp_t *);
122 	int	(*b_elfexec)(struct vnode *, struct execa *, struct uarg *,
123 		struct intpdata *, int, size_t *, int, caddr_t, struct cred *,
124 		int);
125 	void	(*b_sigset_native_to_brand)(sigset_t *);
126 	void	(*b_sigset_brand_to_native)(sigset_t *);
127 	int	b_nsig;
128 };
129 
130 /*
131  * The b_version field must always be the first entry in this struct.
132  */
133 typedef struct brand {
134 	int			b_version;
135 	char			*b_name;
136 	struct brand_ops	*b_ops;
137 	struct brand_mach_ops	*b_machops;
138 } brand_t;
139 
140 extern brand_t native_brand;
141 
142 /*
143  * Convenience macros
144  */
145 #define	lwptolwpbrand(l)	((l)->lwp_brand)
146 #define	ttolwpbrand(t)		(lwptolwpbrand(ttolwp(t)))
147 #define	PROC_IS_BRANDED(p)	((p)->p_brand != &native_brand)
148 #define	ZONE_IS_BRANDED(z)	((z)->zone_brand != &native_brand)
149 #define	BROP(p)			((p)->p_brand->b_ops)
150 #define	ZBROP(z)		((z)->zone_brand->b_ops)
151 #define	BRMOP(p)		((p)->p_brand->b_machops)
152 #define	SIGSET_NATIVE_TO_BRAND(sigset)				\
153 	if (PROC_IS_BRANDED(curproc) &&				\
154 	    BROP(curproc)->b_sigset_native_to_brand)		\
155 		BROP(curproc)->b_sigset_native_to_brand(sigset)
156 #define	SIGSET_BRAND_TO_NATIVE(sigset)				\
157 	if (PROC_IS_BRANDED(curproc) &&				\
158 	    BROP(curproc)->b_sigset_brand_to_native)		\
159 		BROP(curproc)->b_sigset_brand_to_native(sigset)
160 
161 extern void	brand_init();
162 extern int	brand_register(brand_t *);
163 extern int	brand_unregister(brand_t *);
164 extern brand_t	*brand_register_zone(struct brand_attr *);
165 extern brand_t	*brand_find_name(char *);
166 extern void	brand_unregister_zone(brand_t *);
167 extern int	brand_zone_count(brand_t *);
168 extern void	brand_setbrand(proc_t *);
169 extern void	brand_clearbrand(proc_t *, boolean_t);
170 
171 /*
172  * The following functions can be shared among kernel brand modules which
173  * implement Solaris-derived brands, all of which need to do similar tasks to
174  * manage the brand.
175  */
176 extern int	brand_solaris_cmd(int, uintptr_t, uintptr_t, uintptr_t,
177 		    struct brand *, int);
178 extern void	brand_solaris_copy_procdata(proc_t *, proc_t *,
179 		    struct brand *);
180 extern int	brand_solaris_elfexec(vnode_t *, execa_t *, uarg_t *,
181 		    intpdata_t *, int, size_t *, int, caddr_t, cred_t *, int,
182 		    struct brand *, char *, char *, char *, char *, char *);
183 extern void	brand_solaris_exec(struct brand *);
184 extern int	brand_solaris_fini(char **, struct modlinkage *,
185 		    struct brand *);
186 extern void	brand_solaris_forklwp(klwp_t *, klwp_t *, struct brand *);
187 extern void	brand_solaris_freelwp(klwp_t *, struct brand *);
188 extern int	brand_solaris_initlwp(klwp_t *, struct brand *);
189 extern void	brand_solaris_lwpexit(klwp_t *, struct brand *);
190 extern void	brand_solaris_proc_exit(struct proc *, klwp_t *,
191 		    struct brand *);
192 extern void	brand_solaris_setbrand(proc_t *, struct brand *);
193 
194 #if defined(_SYSCALL32)
195 typedef struct brand_elf_data32 {
196 	uint32_t	sed_phdr;
197 	uint32_t	sed_phent;
198 	uint32_t	sed_phnum;
199 	uint32_t	sed_entry;
200 	uint32_t	sed_base;
201 	uint32_t	sed_ldentry;
202 	uint32_t	sed_lddata;
203 } brand_elf_data32_t;
204 
205 typedef struct brand_common_reg32 {
206 	uint32_t	sbr_version;	/* version number */
207 	caddr32_t	sbr_handler;	/* base address of handler */
208 } brand_common_reg32_t;
209 #endif /* _SYSCALL32 */
210 
211 /*
212  * Common information associated with all branded processes
213  */
214 typedef struct brand_proc_data {
215 	caddr_t		spd_handler;	/* address of user-space handler */
216 	brand_elf_data_t spd_elf_data;	/* common ELF data for branded app. */
217 } brand_proc_data_t;
218 
219 #define	BRAND_NATIVE_DIR	"/.SUNWnative/"
220 #define	BRAND_NATIVE_LINKER32	BRAND_NATIVE_DIR "lib/ld.so.1"
221 #define	BRAND_NATIVE_LINKER64	BRAND_NATIVE_DIR "lib/64/ld.so.1"
222 
223 #endif	/* _KERNEL */
224 
225 #ifdef	__cplusplus
226 }
227 #endif
228 
229 #endif	/* _SYS_BRAND_H */
230