xref: /NextBSD/sys/compat/cloudabi64/cloudabi64_module.c (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
1 /*-
2  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/imgact.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/proc.h>
34 #include <sys/smp.h>
35 #include <sys/sysctl.h>
36 #include <sys/sysent.h>
37 #include <sys/systm.h>
38 
39 #include <compat/cloudabi64/cloudabi64_syscalldefs.h>
40 #include <compat/cloudabi64/cloudabi64_util.h>
41 
42 register_t *
cloudabi64_copyout_strings(struct image_params * imgp)43 cloudabi64_copyout_strings(struct image_params *imgp)
44 {
45 	struct image_args *args;
46 	uintptr_t begin;
47 	size_t len;
48 
49 	/* Copy out program arguments. */
50 	args = imgp->args;
51 	len = args->begin_envv - args->begin_argv;
52 	begin = rounddown2(imgp->sysent->sv_usrstack - len, sizeof(register_t));
53 	copyout(args->begin_argv, (void *)begin, len);
54 	return ((register_t *)begin);
55 }
56 
57 int
cloudabi64_fixup(register_t ** stack_base,struct image_params * imgp)58 cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
59 {
60 	char canarybuf[64];
61 	Elf64_Auxargs *args;
62 	struct thread *td;
63 	void *argdata, *canary;
64 	size_t argdatalen;
65 	int error;
66 
67 	/*
68 	 * CloudABI executables do not store the FreeBSD OS release
69 	 * number in their header. Set the OS release number to the
70 	 * latest version of FreeBSD, so that system calls behave as if
71 	 * called natively.
72 	 */
73 	td = curthread;
74 	td->td_proc->p_osrel = __FreeBSD_version;
75 
76 	/* Store canary for stack smashing protection. */
77 	argdata = *stack_base;
78 	arc4rand(canarybuf, sizeof(canarybuf), 0);
79 	*stack_base -= howmany(sizeof(canarybuf), sizeof(register_t));
80 	canary = *stack_base;
81 	error = copyout(canarybuf, canary, sizeof(canarybuf));
82 	if (error != 0)
83 		return (error);
84 
85 	/*
86 	 * Compute length of program arguments. As the argument data is
87 	 * binary safe, we had to add a trailing null byte in
88 	 * exec_copyin_data_fds(). Undo this by reducing the length.
89 	 */
90 	args = (Elf64_Auxargs *)imgp->auxargs;
91 	argdatalen = imgp->args->begin_envv - imgp->args->begin_argv;
92 	if (argdatalen > 0)
93 		--argdatalen;
94 
95 	/* Write out an auxiliary vector. */
96 	cloudabi64_auxv_t auxv[] = {
97 #define	VAL(type, val)	{ .a_type = (type), .a_val = (val) }
98 #define	PTR(type, ptr)	{ .a_type = (type), .a_ptr = (uintptr_t)(ptr) }
99 		PTR(CLOUDABI_AT_ARGDATA, argdata),
100 		VAL(CLOUDABI_AT_ARGDATALEN, argdatalen),
101 		PTR(CLOUDABI_AT_CANARY, canary),
102 		VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)),
103 		VAL(CLOUDABI_AT_NCPUS, mp_ncpus),
104 		VAL(CLOUDABI_AT_PAGESZ, args->pagesz),
105 		PTR(CLOUDABI_AT_PHDR, args->phdr),
106 		VAL(CLOUDABI_AT_PHNUM, args->phnum),
107 		VAL(CLOUDABI_AT_TID, td->td_tid),
108 #undef VAL
109 #undef PTR
110 		{ .a_type = CLOUDABI_AT_NULL },
111 	};
112 	*stack_base -= howmany(sizeof(auxv), sizeof(register_t));
113 	return (copyout(auxv, *stack_base, sizeof(auxv)));
114 }
115 
116 static int
cloudabi64_modevent(module_t mod,int type,void * data)117 cloudabi64_modevent(module_t mod, int type, void *data)
118 {
119 
120 	switch (type) {
121 	case MOD_LOAD:
122 		if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) {
123 			printf("Failed to add CloudABI ELF brand handler\n");
124 			return (EINVAL);
125 		}
126 		return (0);
127 	case MOD_UNLOAD:
128 		if (elf64_brand_inuse(&cloudabi64_brand))
129 			return (EBUSY);
130 		if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) {
131 			printf("Failed to remove CloudABI ELF brand handler\n");
132 			return (EINVAL);
133 		}
134 		return (0);
135 	default:
136 		return (EOPNOTSUPP);
137 	}
138 }
139 
140 static moduledata_t cloudabi64_module = {
141 	"cloudabi64",
142 	cloudabi64_modevent,
143 	NULL
144 };
145 
146 DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY);
147 MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1);
148 FEATURE(cloudabi64, "CloudABI 64bit support");
149