1 /*- 2 * Copyright (c) 2001 The FreeBSD Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/lock.h> 31 #include <sys/malloc.h> 32 #include <sys/mutex.h> 33 #include <sys/proc.h> 34 #include <sys/syscallsubr.h> 35 #include <sys/sysproto.h> 36 #include <sys/systm.h> 37 38 #include <machine/../linux/linux.h> 39 #include <machine/../linux/linux_proto.h> 40 41 #include <compat/linux/linux_util.h> 42 43 DUMMY(setfsuid16); 44 DUMMY(setfsgid16); 45 DUMMY(getresuid16); 46 DUMMY(getresgid16); 47 48 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x) 49 50 int 51 linux_chown16(struct thread *td, struct linux_chown16_args *args) 52 { 53 char *path; 54 int error; 55 56 LCONVPATHEXIST(td, args->path, &path); 57 58 #ifdef DEBUG 59 if (ldebug(chown16)) 60 printf(ARGS(chown16, "%s, %d, %d"), path, args->uid, args->gid); 61 #endif 62 error = kern_chown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid), 63 CAST_NOCHG(args->gid)); 64 LFREEPATH(path); 65 return (error); 66 } 67 68 int 69 linux_lchown16(struct thread *td, struct linux_lchown16_args *args) 70 { 71 char *path; 72 int error; 73 74 LCONVPATHEXIST(td, args->path, &path); 75 76 #ifdef DEBUG 77 if (ldebug(lchown16)) 78 printf(ARGS(lchown16, "%s, %d, %d"), path, args->uid, 79 args->gid); 80 #endif 81 error = kern_lchown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid), 82 CAST_NOCHG(args->gid)); 83 LFREEPATH(path); 84 return (error); 85 } 86 87 int 88 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args) 89 { 90 struct ucred *newcred, *oldcred; 91 l_gid16_t linux_gidset[NGROUPS]; 92 gid_t *bsd_gidset; 93 int ngrp, error; 94 struct proc *p; 95 96 #ifdef DEBUG 97 if (ldebug(setgroups16)) 98 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize); 99 #endif 100 101 ngrp = args->gidsetsize; 102 if (ngrp >= NGROUPS) 103 return (EINVAL); 104 error = copyin((caddr_t)args->gidset, linux_gidset, 105 ngrp * sizeof(l_gid16_t)); 106 if (error) 107 return (error); 108 newcred = crget(); 109 p = td->td_proc; 110 PROC_LOCK(p); 111 oldcred = p->p_ucred; 112 113 /* 114 * cr_groups[0] holds egid. Setting the whole set from 115 * the supplied set will cause egid to be changed too. 116 * Keep cr_groups[0] unchanged to prevent that. 117 */ 118 119 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0) { 120 PROC_UNLOCK(p); 121 crfree(newcred); 122 return (error); 123 } 124 125 crcopy(newcred, oldcred); 126 if (ngrp > 0) { 127 newcred->cr_ngroups = ngrp + 1; 128 129 bsd_gidset = newcred->cr_groups; 130 ngrp--; 131 while (ngrp >= 0) { 132 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 133 ngrp--; 134 } 135 } 136 else 137 newcred->cr_ngroups = 1; 138 139 setsugid(td->td_proc); 140 p->p_ucred = newcred; 141 PROC_UNLOCK(p); 142 crfree(oldcred); 143 return (0); 144 } 145 146 int 147 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args) 148 { 149 struct ucred *cred; 150 l_gid16_t linux_gidset[NGROUPS]; 151 gid_t *bsd_gidset; 152 int bsd_gidsetsz, ngrp, error; 153 154 #ifdef DEBUG 155 if (ldebug(getgroups16)) 156 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize); 157 #endif 158 159 cred = td->td_ucred; 160 bsd_gidset = cred->cr_groups; 161 bsd_gidsetsz = cred->cr_ngroups - 1; 162 163 /* 164 * cr_groups[0] holds egid. Returning the whole set 165 * here will cause a duplicate. Exclude cr_groups[0] 166 * to prevent that. 167 */ 168 169 if ((ngrp = args->gidsetsize) == 0) { 170 td->td_retval[0] = bsd_gidsetsz; 171 return (0); 172 } 173 174 if (ngrp < bsd_gidsetsz) 175 return (EINVAL); 176 177 ngrp = 0; 178 while (ngrp < bsd_gidsetsz) { 179 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 180 ngrp++; 181 } 182 183 error = copyout(linux_gidset, (caddr_t)args->gidset, 184 ngrp * sizeof(l_gid16_t)); 185 if (error) 186 return (error); 187 188 td->td_retval[0] = ngrp; 189 return (0); 190 } 191 192 /* 193 * The FreeBSD native getgid(2) and getuid(2) also modify td->td_retval[1] 194 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that 195 * are assumed to be preserved. The following lightweight syscalls fixes 196 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in 197 * linux_misc.c 198 * 199 * linux_getgid16() - MP SAFE 200 * linux_getuid16() - MP SAFE 201 */ 202 203 int 204 linux_getgid16(struct thread *td, struct linux_getgid16_args *args) 205 { 206 207 td->td_retval[0] = td->td_ucred->cr_rgid; 208 return (0); 209 } 210 211 int 212 linux_getuid16(struct thread *td, struct linux_getuid16_args *args) 213 { 214 215 td->td_retval[0] = td->td_ucred->cr_ruid; 216 return (0); 217 } 218 219 int 220 linux_getegid16(struct thread *td, struct linux_getegid16_args *args) 221 { 222 struct getegid_args bsd; 223 224 return (getegid(td, &bsd)); 225 } 226 227 int 228 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args) 229 { 230 struct geteuid_args bsd; 231 232 return (geteuid(td, &bsd)); 233 } 234 235 int 236 linux_setgid16(struct thread *td, struct linux_setgid16_args *args) 237 { 238 struct setgid_args bsd; 239 240 bsd.gid = args->gid; 241 return (setgid(td, &bsd)); 242 } 243 244 int 245 linux_setuid16(struct thread *td, struct linux_setuid16_args *args) 246 { 247 struct setuid_args bsd; 248 249 bsd.uid = args->uid; 250 return (setuid(td, &bsd)); 251 } 252 253 int 254 linux_setregid16(struct thread *td, struct linux_setregid16_args *args) 255 { 256 struct setregid_args bsd; 257 258 bsd.rgid = CAST_NOCHG(args->rgid); 259 bsd.egid = CAST_NOCHG(args->egid); 260 return (setregid(td, &bsd)); 261 } 262 263 int 264 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args) 265 { 266 struct setreuid_args bsd; 267 268 bsd.ruid = CAST_NOCHG(args->ruid); 269 bsd.euid = CAST_NOCHG(args->euid); 270 return (setreuid(td, &bsd)); 271 } 272 273 int 274 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args) 275 { 276 struct setresgid_args bsd; 277 278 bsd.rgid = CAST_NOCHG(args->rgid); 279 bsd.egid = CAST_NOCHG(args->egid); 280 bsd.sgid = CAST_NOCHG(args->sgid); 281 return (setresgid(td, &bsd)); 282 } 283 284 int 285 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args) 286 { 287 struct setresuid_args bsd; 288 289 bsd.ruid = CAST_NOCHG(args->ruid); 290 bsd.euid = CAST_NOCHG(args->euid); 291 bsd.suid = CAST_NOCHG(args->suid); 292 return (setresuid(td, &bsd)); 293 } 294