1 /*        $NetBSD: exec_aout.c,v 1.43 2024/12/06 16:19:41 riastradh Exp $       */
2 
3 /*
4  * Copyright (c) 1993, 1994 Christopher G. Demetriou
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christopher G. Demetriou.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.43 2024/12/06 16:19:41 riastradh Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 
39 #include <sys/exec.h>
40 #include <sys/exec_aout.h>
41 #include <sys/module.h>
42 #include <sys/proc.h>
43 #include <sys/resourcevar.h>
44 #include <sys/sdt.h>
45 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 
48 #include <uvm/uvm_extern.h>
49 
50 MODULE(MODULE_CLASS_EXEC, exec_aout, NULL);
51 
52 static struct execsw exec_aout_execsw = {
53           .es_hdrsz = sizeof(struct exec),
54           .es_makecmds = exec_aout_makecmds,
55           .u = {
56                     .elf_probe_func = NULL,
57           },
58           .es_emul = &emul_netbsd,
59           .es_prio = EXECSW_PRIO_ANY,
60           .es_arglen = 0,
61           .es_copyargs = copyargs,
62           .es_setregs = NULL,
63           .es_coredump = coredump_netbsd,
64           .es_setup_stack = exec_setup_stack,
65 };
66 
67 static int
exec_aout_modcmd(modcmd_t cmd,void * arg)68 exec_aout_modcmd(modcmd_t cmd, void *arg)
69 {
70 
71           switch (cmd) {
72           case MODULE_CMD_INIT:
73                     return exec_add(&exec_aout_execsw, 1);
74 
75           case MODULE_CMD_FINI:
76                     return exec_remove(&exec_aout_execsw, 1);
77 
78           default:
79                     return SET_ERROR(ENOTTY);
80         }
81 }
82 
83 /*
84  * exec_aout_makecmds(): Check if it's an a.out-format executable.
85  *
86  * Given a lwp pointer and an exec package pointer, see if the referent
87  * of the epp is in a.out format.  First check 'standard' magic numbers for
88  * this architecture.  If that fails, try a CPU-dependent hook.
89  *
90  * This function, in the former case, or the hook, in the latter, is
91  * responsible for creating a set of vmcmds which can be used to build
92  * the process's vm space and inserting them into the exec package.
93  */
94 
95 int
exec_aout_makecmds(struct lwp * l,struct exec_package * epp)96 exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
97 {
98           u_long midmag, magic;
99           u_short mid;
100           int error;
101           struct exec *execp = epp->ep_hdr;
102 
103           if (epp->ep_hdrvalid < sizeof(struct exec))
104                     return SET_ERROR(ENOEXEC);
105 
106           midmag = ntohl(execp->a_midmag);
107           mid = (midmag >> 16) & 0x3ff;
108           magic = midmag & 0xffff;
109 
110           midmag = mid << 16 | magic;
111 
112           switch (midmag) {
113           case (MID_MACHINE << 16) | ZMAGIC:
114                     error = exec_aout_prep_zmagic(l, epp);
115                     break;
116           case (MID_MACHINE << 16) | NMAGIC:
117                     error = exec_aout_prep_nmagic(l, epp);
118                     break;
119           case (MID_MACHINE << 16) | OMAGIC:
120                     error = exec_aout_prep_omagic(l, epp);
121                     break;
122           default:
123                     error = cpu_exec_aout_makecmds(l, epp);
124           }
125 
126           if (error)
127                     kill_vmcmds(&epp->ep_vmcmds);
128           else
129                     epp->ep_flags &= ~EXEC_TOPDOWN_VM;
130 
131           return error;
132 }
133 
134 /*
135  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
136  *
137  * First, set of the various offsets/lengths in the exec package.
138  *
139  * Then, mark the text image busy (so it can be demand paged) or error
140  * out if this is not possible.  Finally, set up vmcmds for the
141  * text, data, bss, and stack segments.
142  */
143 
144 int
exec_aout_prep_zmagic(struct lwp * l,struct exec_package * epp)145 exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
146 {
147           struct exec *execp = epp->ep_hdr;
148           int error;
149 
150           epp->ep_taddr = AOUT_LDPGSZ;
151           epp->ep_tsize = execp->a_text;
152           epp->ep_daddr = epp->ep_taddr + execp->a_text;
153           epp->ep_dsize = execp->a_data + execp->a_bss;
154           epp->ep_entry = execp->a_entry;
155 
156           error = vn_marktext(epp->ep_vp);
157           if (error)
158                     return (error);
159 
160           /* set up command for text segment */
161           NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
162               epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
163 
164           /* set up command for data segment */
165           NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
166               epp->ep_daddr, epp->ep_vp, execp->a_text,
167               VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
168 
169           /* set up command for bss segment */
170           if (execp->a_bss > 0)
171                     NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
172                         epp->ep_daddr + execp->a_data, NULLVP, 0,
173                         VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
174 
175           return (*epp->ep_esch->es_setup_stack)(l, epp);
176 }
177 
178 /*
179  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
180  */
181 
182 int
exec_aout_prep_nmagic(struct lwp * l,struct exec_package * epp)183 exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
184 {
185           struct exec *execp = epp->ep_hdr;
186           long bsize, baddr;
187 
188           epp->ep_taddr = AOUT_LDPGSZ;
189           epp->ep_tsize = execp->a_text;
190           epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
191           epp->ep_dsize = execp->a_data + execp->a_bss;
192           epp->ep_entry = execp->a_entry;
193 
194           /* set up command for text segment */
195           NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
196               epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
197               VM_PROT_READ|VM_PROT_EXECUTE);
198 
199           /* set up command for data segment */
200           NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
201               epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
202               VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
203 
204           /* set up command for bss segment */
205           baddr = round_page(epp->ep_daddr + execp->a_data);
206           bsize = epp->ep_daddr + epp->ep_dsize - baddr;
207           if (bsize > 0)
208                     NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
209                         NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
210 
211           return (*epp->ep_esch->es_setup_stack)(l, epp);
212 }
213 
214 /*
215  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
216  */
217 
218 int
exec_aout_prep_omagic(struct lwp * l,struct exec_package * epp)219 exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
220 {
221           struct exec *execp = epp->ep_hdr;
222           long dsize, bsize, baddr;
223 
224           epp->ep_taddr = AOUT_LDPGSZ;
225           epp->ep_tsize = execp->a_text;
226           epp->ep_daddr = epp->ep_taddr + execp->a_text;
227           epp->ep_dsize = execp->a_data + execp->a_bss;
228           epp->ep_entry = execp->a_entry;
229 
230           /* set up command for text and data segments */
231           NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
232               execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
233               sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
234 
235           /* set up command for bss segment */
236           baddr = round_page(epp->ep_daddr + execp->a_data);
237           bsize = epp->ep_daddr + epp->ep_dsize - baddr;
238           if (bsize > 0)
239                     NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
240                         NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
241 
242           /*
243            * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
244            * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
245            * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
246            * respectively to page boundaries.
247            * Compensate `ep_dsize' for the amount of data covered by the last
248            * text page.
249            */
250           dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
251           epp->ep_dsize = (dsize > 0) ? dsize : 0;
252           return (*epp->ep_esch->es_setup_stack)(l, epp);
253 }
254