xref: /netbsd-src/sys/compat/linux/common/linux_uselib.c (revision 7eb4355d2e63f8b0e669b2a0bf1fe051f656fd6d)
1 /*	$NetBSD: linux_uselib.c,v 1.32 2014/10/30 16:45:28 maxv Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_uselib.c,v 1.32 2014/10/30 16:45:28 maxv Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/malloc.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
42 #include <sys/mount.h>
43 #include <sys/exec.h>
44 #include <sys/exec_aout.h>
45 
46 #include <sys/mman.h>
47 #include <sys/syscallargs.h>
48 
49 #include <sys/cpu.h>
50 #include <machine/reg.h>
51 
52 #include <compat/linux/common/linux_types.h>
53 #include <compat/linux/common/linux_signal.h>
54 #include <compat/linux/common/linux_util.h>
55 #include <compat/linux/common/linux_exec.h>
56 #include <compat/linux/common/linux_machdep.h>
57 #include <compat/linux/common/linux_ipc.h>
58 #include <compat/linux/common/linux_sem.h>
59 
60 #ifndef EXEC_AOUT
61 /* define EXEC_AOUT to get prototype from linux_syscall.h */
62 #define EXEC_AOUT
63 #endif
64 
65 #include <compat/linux/linux_syscallargs.h>
66 #include <compat/linux/linux_syscall.h>
67 
68 /*
69  * The Linux system call to load shared libraries, a.out version. The
70  * a.out shared libs are just files that are mapped onto a fixed
71  * address in the process' address space. The address is given in
72  * a_entry. Read in the header, set up some VM commands and run them.
73  *
74  * Yes, both text and data are mapped at once, so we're left with
75  * writable text for the shared libs. The Linux crt0 seemed to break
76  * sometimes when data was mapped separately. It munmapped a uselib()
77  * of ld.so by hand, which failed with shared text and data for ld.so
78  * Yuck.
79  *
80  * Because of the problem with ZMAGIC executables (text starts
81  * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
82  * shared libs are not handled very efficiently :-(
83  */
84 
85 int
86 linux_sys_uselib(struct lwp *l, const struct linux_sys_uselib_args *uap, register_t *retval)
87 {
88 	/* {
89 		syscallarg(const char *) path;
90 	} */
91 	long bsize, dsize, tsize, taddr, baddr, daddr;
92 	struct vnode *vp;
93 	struct exec hdr;
94 	struct exec_vmcmd_set vcset;
95 	int i, magic, error;
96 	size_t rem;
97 
98 	error = namei_simple_user(SCARG(uap, path),
99 				NSM_FOLLOW_TRYEMULROOT, &vp);
100 	if (error != 0)
101 		return error;
102 
103 	if (vp->v_type != VREG) {
104 		error = EINVAL;
105 		goto out;
106 	}
107 
108 	if ((error = vn_rdwr(UIO_READ, vp, (void *) &hdr, LINUX_AOUT_HDR_SIZE,
109 			     0, UIO_SYSSPACE, IO_NODELOCKED, l->l_cred,
110 			     &rem, NULL))) {
111 		goto out;
112 	}
113 
114 	if (rem != 0) {
115 		error = ENOEXEC;
116 		goto out;
117 	}
118 
119 	if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE) {
120 		error = ENOEXEC;
121 		goto out;
122 	}
123 
124 	magic = LINUX_N_MAGIC(&hdr);
125 	taddr = hdr.a_entry & (~(PAGE_SIZE - 1));
126 	tsize = hdr.a_text;
127 	daddr = taddr + tsize;
128 	dsize = hdr.a_data + hdr.a_bss;
129 
130 	error = vn_marktext(vp);
131 	if (error)
132 		goto out;
133 
134 	vcset.evs_cnt = 0;
135 	vcset.evs_used = 0;
136 
137 	NEW_VMCMD(&vcset,
138 		  magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
139 		  hdr.a_text + hdr.a_data, taddr,
140 		  vp, LINUX_N_TXTOFF(hdr, magic),
141 		  VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
142 
143 	baddr = roundup(daddr + hdr.a_data, PAGE_SIZE);
144 	bsize = daddr + dsize - baddr;
145         if (bsize > 0) {
146                 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
147                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
148 	}
149 
150 	for (i = 0; i < vcset.evs_used && !error; i++) {
151 		struct exec_vmcmd *vcp;
152 
153 		vcp = &vcset.evs_cmds[i];
154 		error = (*vcp->ev_proc)(l, vcp);
155 	}
156 
157 	kill_vmcmds(&vcset);
158 
159 out:
160 	vrele(vp);
161 	return error;
162 }
163