xref: /titanic_50/usr/src/uts/common/exec/aout/aout.c (revision 89b43686db1fe9681d80a7cf5662730cb9378cae)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/fpu/fpusystm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/signal.h>
33 #include <sys/cred.h>
34 #include <sys/user.h>
35 #include <sys/errno.h>
36 #include <sys/vnode.h>
37 #include <sys/mman.h>
38 #include <sys/kmem.h>
39 #include <sys/proc.h>
40 #include <sys/pathname.h>
41 #include <sys/cmn_err.h>
42 #include <sys/debug.h>
43 #include <sys/exec.h>
44 #include <sys/exechdr.h>
45 #include <sys/auxv.h>
46 #include <sys/core.h>
47 #include <sys/vmparam.h>
48 #include <sys/archsystm.h>
49 #include <sys/fs/swapnode.h>
50 #include <sys/modctl.h>
51 #include <vm/anon.h>
52 #include <vm/as.h>
53 #include <vm/seg.h>
54 
55 static int aoutexec(vnode_t *vp, execa_t *uap, uarg_t *args,
56     intpdata_t *idatap, int level, long *execsz, int setid,
57     caddr_t exec_file, cred_t *cred, int brand_action);
58 static int get_aout_head(struct vnode **vpp, struct exdata *edp, long *execsz,
59     int *isdyn);
60 static int aoutcore(vnode_t *vp, proc_t *pp, cred_t *credp,
61     rlim64_t rlimit, int sig, core_content_t content);
62 extern int elf32exec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
63     long *, int, caddr_t, cred_t *, int);
64 extern int elf32core(vnode_t *, proc_t *, cred_t *, rlim64_t, int,
65     core_content_t);
66 
67 static struct execsw nesw = {
68 	aout_nmagicstr,
69 	2,
70 	2,
71 	aoutexec,
72 	aoutcore
73 };
74 
75 static struct execsw zesw = {
76 	aout_zmagicstr,
77 	2,
78 	2,
79 	aoutexec,
80 	aoutcore
81 };
82 
83 static struct execsw oesw = {
84 	aout_omagicstr,
85 	2,
86 	2,
87 	aoutexec,
88 	aoutcore
89 };
90 
91 /*
92  * Module linkage information for the kernel.
93  */
94 static struct modlexec nexec = {
95 	&mod_execops, "exec for NMAGIC", &nesw
96 };
97 
98 static struct modlexec zexec = {
99 	&mod_execops, "exec for ZMAGIC", &zesw
100 };
101 
102 static struct modlexec oexec = {
103 	&mod_execops, "exec for OMAGIC", &oesw
104 };
105 
106 static struct modlinkage modlinkage = {
107 	MODREV_1, &nexec, &zexec, &oexec, NULL
108 };
109 
110 int
_init(void)111 _init(void)
112 {
113 	return (mod_install(&modlinkage));
114 }
115 
116 int
_fini(void)117 _fini(void)
118 {
119 	return (mod_remove(&modlinkage));
120 }
121 
122 int
_info(struct modinfo * modinfop)123 _info(struct modinfo *modinfop)
124 {
125 	return (mod_info(&modlinkage, modinfop));
126 }
127 
128 
129 /*ARGSUSED*/
130 static int
aoutexec(vnode_t * vp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,long * execsz,int setid,caddr_t exec_file,cred_t * cred,int brand_action)131 aoutexec(vnode_t *vp, struct execa *uap, struct uarg *args,
132     struct intpdata *idatap, int level, long *execsz, int setid,
133     caddr_t exec_file, cred_t *cred, int brand_action)
134 {
135 	auxv32_t auxflags_auxv32;
136 	int error;
137 	struct exdata edp, edpout;
138 	struct execenv exenv;
139 	proc_t *pp = ttoproc(curthread);
140 	struct vnode *nvp;
141 	int pagetext, pagedata;
142 	int dataprot = PROT_ALL;
143 	int textprot = PROT_ALL & ~PROT_WRITE;
144 	int isdyn;
145 
146 
147 	args->to_model = DATAMODEL_ILP32;
148 	*execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS32-1);
149 
150 	/*
151 	 * Read in and validate the file header.
152 	 */
153 	if (error = get_aout_head(&vp, &edp, execsz, &isdyn))
154 		return (error);
155 
156 	if (error = chkaout(&edp))
157 		return (error);
158 
159 	/*
160 	 * Take a quick look to see if it looks like we will have
161 	 * enough swap space for the program to get started.  This
162 	 * is not a guarantee that we will succeed, but it is definitely
163 	 * better than finding this out after we are committed to the
164 	 * new memory image.  Maybe what is needed is a way to "prereserve"
165 	 * swap space for some segment mappings here.
166 	 *
167 	 * But with shared libraries the process can make it through
168 	 * the exec only to have ld.so fail to get the program going
169 	 * because its mmap's will not be able to succeed if the system
170 	 * is running low on swap space.  In fact this is a far more
171 	 * common failure mode, but we cannot do much about this here
172 	 * other than add some slop to our anonymous memory resources
173 	 * requirements estimate based on some guess since we cannot know
174 	 * what else the program will really need to get to a useful state.
175 	 *
176 	 * XXX - The stack size (clrnd(SSIZE + btopr(nargc))) should also
177 	 * be used when checking for swap space.  This requires some work
178 	 * since nargc is actually determined in exec_args() which is done
179 	 * after this check and hence we punt for now.
180 	 *
181 	 * nargc = SA(nc + (na + 4) * NBPW) + sizeof (struct rwindow);
182 	 */
183 	if (CURRENT_TOTAL_AVAILABLE_SWAP < btopr(edp.ux_dsize) + btopr(SSIZE))
184 		return (ENOMEM);
185 
186 	/*
187 	 * Load the trap 0 interpreter.
188 	 */
189 	if (error = lookupname("/usr/4lib/sbcp", UIO_SYSSPACE, FOLLOW,
190 	    NULLVPP, &nvp)) {
191 		goto done;
192 	}
193 	if (error = elf32exec(nvp, uap, args, idatap, level, execsz,
194 	    setid, exec_file, cred, brand_action)) {
195 		VN_RELE(nvp);
196 		return (error);
197 	}
198 	VN_RELE(nvp);
199 
200 	/*
201 	 * Determine the a.out's characteristics.
202 	 */
203 	getexinfo(&edp, &edpout, &pagetext, &pagedata);
204 
205 	/*
206 	 * Load the a.out's text and data.
207 	 */
208 	if (error = execmap(edp.vp, edp.ux_txtorg, edp.ux_tsize,
209 	    (size_t)0, edp.ux_toffset, textprot, pagetext, 0))
210 		goto done;
211 	if (error = execmap(edp.vp, edp.ux_datorg, edp.ux_dsize,
212 	    edp.ux_bsize, edp.ux_doffset, dataprot, pagedata, 0))
213 		goto done;
214 
215 	exenv.ex_bssbase = (caddr_t)edp.ux_datorg;
216 	exenv.ex_brkbase = (caddr_t)edp.ux_datorg;
217 	exenv.ex_brksize = edp.ux_dsize + edp.ux_bsize;
218 	exenv.ex_magic = edp.ux_mag;
219 	exenv.ex_vp = edp.vp;
220 	setexecenv(&exenv);
221 
222 	/*
223 	 * It's time to manipulate the process aux vectors.
224 	 * We need to update the AT_SUN_AUXFLAGS aux vector to set
225 	 * the AF_SUN_NOPLM flag.
226 	 */
227 	if (copyin(args->auxp_auxflags, &auxflags_auxv32,
228 	    sizeof (auxflags_auxv32)) != 0)
229 		return (EFAULT);
230 
231 	ASSERT(auxflags_auxv32.a_type == AT_SUN_AUXFLAGS);
232 	auxflags_auxv32.a_un.a_val |= AF_SUN_NOPLM;
233 	if (copyout(&auxflags_auxv32, args->auxp_auxflags,
234 	    sizeof (auxflags_auxv32)) != 0)
235 		return (EFAULT);
236 
237 done:
238 	if (error != 0)
239 		psignal(pp, SIGKILL);
240 	else {
241 		/*
242 		 * Ensure that the max fds do not exceed 256 (this is
243 		 * applicable to 4.x binaries, which is why we only
244 		 * do it on a.out files).
245 		 */
246 		struct rlimit64 fdno_rlim;
247 		rctl_alloc_gp_t *gp = rctl_rlimit_set_prealloc(1);
248 
249 		mutex_enter(&curproc->p_lock);
250 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_NOFILE], curproc,
251 		    &fdno_rlim);
252 		if (fdno_rlim.rlim_cur > 256) {
253 			fdno_rlim.rlim_cur = fdno_rlim.rlim_max = 256;
254 			(void) rctl_rlimit_set(rctlproc_legacy[RLIMIT_NOFILE],
255 			    curproc, &fdno_rlim, gp,
256 			    rctlproc_flags[RLIMIT_NOFILE],
257 			    rctlproc_signals[RLIMIT_NOFILE], CRED());
258 		} else if (fdno_rlim.rlim_max > 256) {
259 			fdno_rlim.rlim_max = 256;
260 			(void) rctl_rlimit_set(rctlproc_legacy[RLIMIT_NOFILE],
261 			    curproc, &fdno_rlim, gp,
262 			    rctlproc_flags[RLIMIT_NOFILE],
263 			    rctlproc_signals[RLIMIT_NOFILE], CRED());
264 		}
265 		mutex_exit(&curproc->p_lock);
266 
267 		rctl_prealloc_destroy(gp);
268 	}
269 
270 	return (error);
271 }
272 
273 /*
274  * Read in and validate the file header.
275  */
276 static int
get_aout_head(struct vnode ** vpp,struct exdata * edp,long * execsz,int * isdyn)277 get_aout_head(struct vnode **vpp, struct exdata *edp, long *execsz, int *isdyn)
278 {
279 	struct vnode *vp = *vpp;
280 	struct exec filhdr;
281 	int error;
282 	ssize_t resid;
283 	rlim64_t limit;
284 	rlim64_t roundlimit;
285 
286 	if (error = vn_rdwr(UIO_READ, vp, (caddr_t)&filhdr,
287 	    (ssize_t)sizeof (filhdr), (offset_t)0, UIO_SYSSPACE, 0,
288 	    (rlim64_t)0, CRED(), &resid))
289 		return (error);
290 
291 	if (resid != 0)
292 		return (ENOEXEC);
293 
294 	switch (filhdr.a_magic) {
295 	case OMAGIC:
296 		filhdr.a_data += filhdr.a_text;
297 		filhdr.a_text = 0;
298 		break;
299 	case ZMAGIC:
300 	case NMAGIC:
301 		break;
302 	default:
303 		return (ENOEXEC);
304 	}
305 
306 	/*
307 	 * Check total memory requirements (in pages) for a new process
308 	 * against the available memory or upper limit of memory allowed.
309 	 *
310 	 * For the 64-bit kernel, the limit can be set large enough so that
311 	 * rounding it up to a page can overflow, so we check for btopr()
312 	 * overflowing here by comparing it with the unrounded limit in pages.
313 	 */
314 	*execsz += btopr(filhdr.a_text + filhdr.a_data);
315 	limit = btop(curproc->p_vmem_ctl);
316 	roundlimit = btopr(curproc->p_vmem_ctl);
317 	if ((roundlimit > limit && *execsz > roundlimit) ||
318 	    (roundlimit < limit && *execsz > limit)) {
319 		mutex_enter(&curproc->p_lock);
320 		(void) rctl_action(rctlproc_legacy[RLIMIT_VMEM],
321 		    curproc->p_rctls, curproc, RCA_SAFE);
322 		mutex_exit(&curproc->p_lock);
323 		return (ENOMEM);
324 	}
325 
326 	edp->ux_mach = filhdr.a_machtype;
327 	edp->ux_tsize = filhdr.a_text;
328 	edp->ux_dsize = filhdr.a_data;
329 	edp->ux_bsize = filhdr.a_bss;
330 	edp->ux_mag = filhdr.a_magic;
331 	edp->ux_toffset = gettfile(&filhdr);
332 	edp->ux_doffset = getdfile(&filhdr);
333 	edp->ux_txtorg = gettmem(&filhdr);
334 	edp->ux_datorg = getdmem(&filhdr);
335 	edp->ux_entloc = (caddr_t)(uintptr_t)filhdr.a_entry;
336 	edp->vp = vp;
337 	*isdyn = filhdr.a_dynamic;
338 
339 	return (0);
340 }
341 
342 static int
aoutcore(vnode_t * vp,proc_t * pp,struct cred * credp,rlim64_t rlimit,int sig,core_content_t content)343 aoutcore(vnode_t *vp, proc_t *pp, struct cred *credp, rlim64_t rlimit, int sig,
344     core_content_t content)
345 {
346 	return (elf32core(vp, pp, credp, rlimit, sig, content));
347 }
348