linux32_uid16.c revision 1.2 1 1.2 ad /* $NetBSD: linux32_uid16.c,v 1.2 2008/11/19 18:36:04 ad Exp $ */
2 1.1 njoly
3 1.1 njoly /*-
4 1.1 njoly * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 1.1 njoly * Copyright (c) 2008 Nicolas Joly, all rights reserved.
6 1.1 njoly *
7 1.1 njoly * Redistribution and use in source and binary forms, with or without
8 1.1 njoly * modification, are permitted provided that the following conditions
9 1.1 njoly * are met:
10 1.1 njoly * 1. Redistributions of source code must retain the above copyright
11 1.1 njoly * notice, this list of conditions and the following disclaimer.
12 1.1 njoly * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 njoly * notice, this list of conditions and the following disclaimer in the
14 1.1 njoly * documentation and/or other materials provided with the distribution.
15 1.1 njoly * 3. All advertising materials mentioning features or use of this software
16 1.1 njoly * must display the following acknowledgement:
17 1.1 njoly * This product includes software developed by Emmanuel Dreyfus
18 1.1 njoly * 4. The name of the author may not be used to endorse or promote
19 1.1 njoly * products derived from this software without specific prior written
20 1.1 njoly * permission.
21 1.1 njoly *
22 1.1 njoly * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
23 1.1 njoly * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 1.1 njoly * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1 njoly * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
26 1.1 njoly * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 njoly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 njoly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 njoly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 njoly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 njoly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1 njoly * POSSIBILITY OF SUCH DAMAGE.
33 1.1 njoly */
34 1.1 njoly
35 1.1 njoly #include <sys/cdefs.h>
36 1.2 ad __KERNEL_RCSID(0, "$NetBSD: linux32_uid16.c,v 1.2 2008/11/19 18:36:04 ad Exp $");
37 1.1 njoly
38 1.1 njoly #include <sys/param.h>
39 1.1 njoly #include <sys/proc.h>
40 1.1 njoly #include <sys/kauth.h>
41 1.1 njoly
42 1.1 njoly #include <compat/netbsd32/netbsd32.h>
43 1.1 njoly
44 1.1 njoly #include <compat/linux/common/linux_types.h>
45 1.1 njoly #include <compat/linux/common/linux_signal.h>
46 1.2 ad #include <compat/linux/common/linux_ipc.h>
47 1.2 ad #include <compat/linux/common/linux_sem.h>
48 1.1 njoly #include <compat/linux/linux_syscallargs.h>
49 1.1 njoly
50 1.1 njoly #include <compat/linux32/common/linux32_types.h>
51 1.1 njoly #include <compat/linux32/common/linux32_signal.h>
52 1.1 njoly #include <compat/linux32/common/linux32_machdep.h>
53 1.1 njoly #include <compat/linux32/linux32_syscallargs.h>
54 1.1 njoly
55 1.1 njoly #define LINUX32TOBSD_UID(u) \
56 1.1 njoly (((u) == (linux32_uid16_t)-1) ? -1 : (u))
57 1.1 njoly #define LINUX32TOBSD_GID(g) \
58 1.1 njoly (((g) == (linux32_gid16_t)-1) ? -1 : (g))
59 1.1 njoly
60 1.1 njoly #define BSDTOLINUX32_UID(u) \
61 1.1 njoly (((u) & ~0xffff) ? (linux32_uid16_t)65534 : (linux32_uid16_t)(u))
62 1.1 njoly #define BSDTOLINUX32_GID(g) \
63 1.1 njoly (((g) & ~0xffff) ? (linux32_gid16_t)65534 : (linux32_gid16_t)(g))
64 1.1 njoly
65 1.1 njoly int
66 1.1 njoly linux32_sys_chown16(struct lwp *l, const struct linux32_sys_chown16_args *uap, register_t *retval)
67 1.1 njoly {
68 1.1 njoly /* {
69 1.1 njoly syscallarg(const netbsd32_charp) path;
70 1.1 njoly syscallarg(linux32_uid16_t) uid;
71 1.1 njoly syscallarg(linux32_gid16_t) gid;
72 1.1 njoly } */
73 1.1 njoly struct sys___posix_chown_args ua;
74 1.1 njoly
75 1.1 njoly NETBSD32TOP_UAP(path, const char);
76 1.1 njoly SCARG(&ua, uid) = LINUX32TOBSD_UID(SCARG(uap, uid));
77 1.1 njoly SCARG(&ua, gid) = LINUX32TOBSD_GID(SCARG(uap, gid));
78 1.1 njoly
79 1.1 njoly return sys___posix_chown(l, &ua, retval);
80 1.1 njoly }
81 1.1 njoly
82 1.1 njoly int
83 1.1 njoly linux32_sys_lchown16(struct lwp *l, const struct linux32_sys_lchown16_args *uap, register_t *retval)
84 1.1 njoly {
85 1.1 njoly /* {
86 1.1 njoly syscallarg(const netbsd32_charp) path;
87 1.1 njoly syscallarg(linux32_uid16_t) uid;
88 1.1 njoly syscallarg(linux32_gid16_t) gid;
89 1.1 njoly } */
90 1.1 njoly struct sys___posix_lchown_args ua;
91 1.1 njoly
92 1.1 njoly NETBSD32TOP_UAP(path, const char);
93 1.1 njoly SCARG(&ua, uid) = LINUX32TOBSD_UID(SCARG(uap, uid));
94 1.1 njoly SCARG(&ua, gid) = LINUX32TOBSD_GID(SCARG(uap, gid));
95 1.1 njoly
96 1.1 njoly return sys___posix_lchown(l, &ua, retval);
97 1.1 njoly }
98 1.1 njoly
99 1.1 njoly int
100 1.1 njoly linux32_sys_fchown16(struct lwp *l, const struct linux32_sys_fchown16_args *uap, register_t *retval)
101 1.1 njoly {
102 1.1 njoly /* {
103 1.1 njoly syscallarg(int) fd;
104 1.1 njoly syscallarg(linux32_uid16_t) uid;
105 1.1 njoly syscallarg(linux32_gid16_t) gid;
106 1.1 njoly } */
107 1.1 njoly struct sys___posix_fchown_args ua;
108 1.1 njoly
109 1.1 njoly SCARG(&ua, fd) = SCARG(uap, fd);
110 1.1 njoly SCARG(&ua, uid) = LINUX32TOBSD_UID(SCARG(uap, uid));
111 1.1 njoly SCARG(&ua, gid) = LINUX32TOBSD_GID(SCARG(uap, gid));
112 1.1 njoly
113 1.1 njoly return sys___posix_fchown(l, &ua, retval);
114 1.1 njoly }
115 1.1 njoly
116 1.1 njoly int
117 1.1 njoly linux32_sys_getgroups16(struct lwp *l, const struct linux32_sys_getgroups16_args *uap, register_t *retval)
118 1.1 njoly {
119 1.1 njoly /* {
120 1.1 njoly syscallarg(int) gidsetsize;
121 1.1 njoly syscallarg(linux32_gid16p_t) gidset;
122 1.1 njoly } */
123 1.1 njoly struct linux_sys_getgroups16_args ua;
124 1.1 njoly
125 1.1 njoly NETBSD32TO64_UAP(gidsetsize);
126 1.1 njoly NETBSD32TOP_UAP(gidset, linux_gid16_t);
127 1.1 njoly
128 1.1 njoly return linux_sys_getgroups16(l, &ua, retval);
129 1.1 njoly }
130 1.1 njoly
131 1.1 njoly int
132 1.1 njoly linux32_sys_setgroups16(struct lwp *l, const struct linux32_sys_setgroups16_args *uap, register_t *retval)
133 1.1 njoly {
134 1.1 njoly /* {
135 1.1 njoly syscallarg(int) gidsetsize;
136 1.1 njoly syscallarg(linux32_gid16p_t) gidset;
137 1.1 njoly } */
138 1.1 njoly struct linux_sys_setgroups16_args ua;
139 1.1 njoly
140 1.1 njoly NETBSD32TO64_UAP(gidsetsize);
141 1.1 njoly NETBSD32TOP_UAP(gidset, linux_gid16_t);
142 1.1 njoly
143 1.1 njoly return linux_sys_setgroups16(l, &ua, retval);
144 1.1 njoly }
145 1.1 njoly
146 1.1 njoly int
147 1.1 njoly linux32_sys_setreuid16(struct lwp *l, const struct linux32_sys_setreuid16_args *uap, register_t *retval)
148 1.1 njoly {
149 1.1 njoly /* {
150 1.1 njoly syscallarg(linux32_uid16_t) ruid;
151 1.1 njoly syscallarg(linux32_uid16_t) euid;
152 1.1 njoly } */
153 1.1 njoly struct sys_setreuid_args bsa;
154 1.1 njoly
155 1.1 njoly SCARG(&bsa, ruid) = LINUX32TOBSD_UID(SCARG(uap, ruid));
156 1.1 njoly SCARG(&bsa, euid) = LINUX32TOBSD_UID(SCARG(uap, euid));
157 1.1 njoly
158 1.1 njoly return sys_setreuid(l, &bsa, retval);
159 1.1 njoly }
160 1.1 njoly
161 1.1 njoly int
162 1.1 njoly linux32_sys_setregid16(struct lwp *l, const struct linux32_sys_setregid16_args *uap, register_t *retval)
163 1.1 njoly {
164 1.1 njoly /* {
165 1.1 njoly syscallarg(linux32_gid16_t) rgid;
166 1.1 njoly syscallarg(linux32_gid16_t) egid;
167 1.1 njoly } */
168 1.1 njoly struct sys_setregid_args bsa;
169 1.1 njoly
170 1.1 njoly SCARG(&bsa, rgid) = LINUX32TOBSD_GID(SCARG(uap, rgid));
171 1.1 njoly SCARG(&bsa, egid) = LINUX32TOBSD_GID(SCARG(uap, egid));
172 1.1 njoly
173 1.1 njoly return sys_setregid(l, &bsa, retval);
174 1.1 njoly }
175 1.1 njoly
176 1.1 njoly int
177 1.1 njoly linux32_sys_setresuid16(struct lwp *l, const struct linux32_sys_setresuid16_args *uap, register_t *retval)
178 1.1 njoly {
179 1.1 njoly /* {
180 1.1 njoly syscallarg(linux32_uid16_t) ruid;
181 1.1 njoly syscallarg(linux32_uid16_t) euid;
182 1.1 njoly syscallarg(linux32_uid16_t) suid;
183 1.1 njoly } */
184 1.1 njoly struct linux32_sys_setresuid_args lsa;
185 1.1 njoly
186 1.1 njoly SCARG(&lsa, ruid) = LINUX32TOBSD_UID(SCARG(uap, ruid));
187 1.1 njoly SCARG(&lsa, euid) = LINUX32TOBSD_UID(SCARG(uap, euid));
188 1.1 njoly SCARG(&lsa, suid) = LINUX32TOBSD_UID(SCARG(uap, suid));
189 1.1 njoly
190 1.1 njoly return linux32_sys_setresuid(l, &lsa, retval);
191 1.1 njoly }
192 1.1 njoly
193 1.1 njoly int
194 1.1 njoly linux32_sys_setresgid16(struct lwp *l, const struct linux32_sys_setresgid16_args *uap, register_t *retval)
195 1.1 njoly {
196 1.1 njoly /* {
197 1.1 njoly syscallarg(linux32_gid16_t) rgid;
198 1.1 njoly syscallarg(linux32_gid16_t) egid;
199 1.1 njoly syscallarg(linux32_gid16_t) sgid;
200 1.1 njoly } */
201 1.1 njoly struct linux32_sys_setresgid_args lsa;
202 1.1 njoly
203 1.1 njoly SCARG(&lsa, rgid) = LINUX32TOBSD_GID(SCARG(uap, rgid));
204 1.1 njoly SCARG(&lsa, egid) = LINUX32TOBSD_GID(SCARG(uap, egid));
205 1.1 njoly SCARG(&lsa, sgid) = LINUX32TOBSD_GID(SCARG(uap, sgid));
206 1.1 njoly
207 1.1 njoly return linux32_sys_setresgid(l, &lsa, retval);
208 1.1 njoly }
209 1.1 njoly
210 1.1 njoly int
211 1.1 njoly linux32_sys_getresuid16(struct lwp *l, const struct linux32_sys_getresuid16_args *uap, register_t *retval)
212 1.1 njoly {
213 1.1 njoly /* {
214 1.1 njoly syscallarg(linux32_uid16p_t) ruid;
215 1.1 njoly syscallarg(linux32_uid16p_t) euid;
216 1.1 njoly syscallarg(linux32_uid16p_t) suid;
217 1.1 njoly } */
218 1.1 njoly kauth_cred_t pc = l->l_cred;
219 1.1 njoly int error;
220 1.1 njoly uid_t buid;
221 1.1 njoly linux32_uid16_t luid;
222 1.1 njoly
223 1.1 njoly buid = kauth_cred_getuid(pc);
224 1.1 njoly luid = BSDTOLINUX32_UID(buid);
225 1.1 njoly if ((error = copyout(&luid, SCARG_P32(uap, ruid), sizeof(luid))) != 0)
226 1.1 njoly return error;
227 1.1 njoly
228 1.1 njoly buid = kauth_cred_geteuid(pc);
229 1.1 njoly luid = BSDTOLINUX32_UID(buid);
230 1.1 njoly if ((error = copyout(&luid, SCARG_P32(uap, euid), sizeof(luid))) != 0)
231 1.1 njoly return error;
232 1.1 njoly
233 1.1 njoly buid = kauth_cred_getsvuid(pc);
234 1.1 njoly luid = BSDTOLINUX32_UID(buid);
235 1.1 njoly return (copyout(&luid, SCARG_P32(uap, suid), sizeof(luid)));
236 1.1 njoly }
237 1.1 njoly
238 1.1 njoly int
239 1.1 njoly linux32_sys_getresgid16(struct lwp *l, const struct linux32_sys_getresgid16_args *uap, register_t *retval)
240 1.1 njoly {
241 1.1 njoly /* {
242 1.1 njoly syscallarg(linux32_gid16p_t) rgid;
243 1.1 njoly syscallarg(linux32_gid16p_t) egid;
244 1.1 njoly syscallarg(linux32_gid16p_t) sgid;
245 1.1 njoly } */
246 1.1 njoly kauth_cred_t pc = l->l_cred;
247 1.1 njoly int error;
248 1.1 njoly gid_t bgid;
249 1.1 njoly linux32_gid16_t lgid;
250 1.1 njoly
251 1.1 njoly bgid = kauth_cred_getgid(pc);
252 1.1 njoly lgid = BSDTOLINUX32_GID(bgid);
253 1.1 njoly if ((error = copyout(&lgid, SCARG_P32(uap, rgid), sizeof(lgid))) != 0)
254 1.1 njoly return error;
255 1.1 njoly
256 1.1 njoly bgid = kauth_cred_getegid(pc);
257 1.1 njoly lgid = BSDTOLINUX32_GID(bgid);
258 1.1 njoly if ((error = copyout(&lgid, SCARG_P32(uap, egid), sizeof(lgid))) != 0)
259 1.1 njoly return error;
260 1.1 njoly
261 1.1 njoly bgid = kauth_cred_getsvgid(pc);
262 1.1 njoly lgid = BSDTOLINUX32_GID(bgid);
263 1.1 njoly return (copyout(&lgid, SCARG_P32(uap, sgid), sizeof(lgid)));
264 1.1 njoly }
265