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 "opt_compat.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/proc.h> 36 #include <sys/sysproto.h> 37 38 #include <machine/../linux/linux.h> 39 #include <machine/../linux/linux_proto.h> 40 #include <compat/linux/linux_util.h> 41 42 DUMMY(setfsuid16); 43 DUMMY(setfsgid16); 44 DUMMY(getresuid16); 45 DUMMY(getresgid16); 46 47 #define CAST_NOCHG(x) (x == 0xFFFF) ? -1 : x; 48 49 int 50 linux_chown16(struct thread *td, struct linux_chown16_args *args) 51 { 52 struct chown_args bsd; 53 caddr_t sg; 54 55 sg = stackgap_init(); 56 CHECKALTEXIST(td, &sg, args->path); 57 58 #ifdef DEBUG 59 if (ldebug(chown16)) 60 printf(ARGS(chown16, "%s, %d, %d"), args->path, args->uid, 61 args->gid); 62 #endif 63 64 bsd.path = args->path; 65 bsd.uid = CAST_NOCHG(args->uid); 66 bsd.gid = CAST_NOCHG(args->gid); 67 return (chown(td, &bsd)); 68 } 69 70 int 71 linux_lchown16(struct thread *td, struct linux_lchown16_args *args) 72 { 73 struct lchown_args bsd; 74 caddr_t sg; 75 76 sg = stackgap_init(); 77 CHECKALTEXIST(td, &sg, args->path); 78 79 #ifdef DEBUG 80 if (ldebug(lchown16)) 81 printf(ARGS(lchown16, "%s, %d, %d"), args->path, args->uid, 82 args->gid); 83 #endif 84 85 bsd.path = args->path; 86 bsd.uid = CAST_NOCHG(args->uid); 87 bsd.gid = CAST_NOCHG(args->gid); 88 return (lchown(td, &bsd)); 89 } 90 91 int 92 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args) 93 { 94 struct ucred *newcred, *oldcred; 95 l_gid16_t linux_gidset[NGROUPS]; 96 gid_t *bsd_gidset; 97 int ngrp, error; 98 struct proc *p; 99 100 #ifdef DEBUG 101 if (ldebug(setgroups16)) 102 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize); 103 #endif 104 105 ngrp = args->gidsetsize; 106 if (ngrp >= NGROUPS) 107 return (EINVAL); 108 error = copyin((caddr_t)args->gidset, linux_gidset, 109 ngrp * sizeof(l_gid16_t)); 110 if (error) 111 return (error); 112 newcred = crget(); 113 p = td->td_proc; 114 PROC_LOCK(p); 115 oldcred = p->p_ucred; 116 117 /* 118 * cr_groups[0] holds egid. Setting the whole set from 119 * the supplied set will cause egid to be changed too. 120 * Keep cr_groups[0] unchanged to prevent that. 121 */ 122 123 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0) { 124 PROC_UNLOCK(p); 125 crfree(newcred); 126 return (error); 127 } 128 129 crcopy(newcred, oldcred); 130 if (ngrp > 0) { 131 newcred->cr_ngroups = ngrp + 1; 132 133 bsd_gidset = newcred->cr_groups; 134 ngrp--; 135 while (ngrp >= 0) { 136 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 137 ngrp--; 138 } 139 } 140 else 141 newcred->cr_ngroups = 1; 142 143 setsugid(td->td_proc); 144 p->p_ucred = newcred; 145 PROC_UNLOCK(p); 146 crfree(oldcred); 147 return (0); 148 } 149 150 int 151 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args) 152 { 153 struct ucred *cred; 154 l_gid16_t linux_gidset[NGROUPS]; 155 gid_t *bsd_gidset; 156 int bsd_gidsetsz, ngrp, error; 157 158 #ifdef DEBUG 159 if (ldebug(getgroups16)) 160 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize); 161 #endif 162 163 cred = td->td_ucred; 164 bsd_gidset = cred->cr_groups; 165 bsd_gidsetsz = cred->cr_ngroups - 1; 166 167 /* 168 * cr_groups[0] holds egid. Returning the whole set 169 * here will cause a duplicate. Exclude cr_groups[0] 170 * to prevent that. 171 */ 172 173 if ((ngrp = args->gidsetsize) == 0) { 174 td->td_retval[0] = bsd_gidsetsz; 175 return (0); 176 } 177 178 if (ngrp < bsd_gidsetsz) 179 return (EINVAL); 180 181 ngrp = 0; 182 while (ngrp < bsd_gidsetsz) { 183 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 184 ngrp++; 185 } 186 187 error = copyout(linux_gidset, (caddr_t)args->gidset, 188 ngrp * sizeof(l_gid16_t)); 189 if (error) 190 return (error); 191 192 td->td_retval[0] = ngrp; 193 return (0); 194 } 195 196 /* 197 * The FreeBSD native getgid(2) and getuid(2) also modify td->td_retval[1] 198 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that 199 * are assumed to be preserved. The following lightweight syscalls fixes 200 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in 201 * linux_misc.c 202 * 203 * linux_getgid16() - MP SAFE 204 * linux_getuid16() - MP SAFE 205 */ 206 207 int 208 linux_getgid16(struct thread *td, struct linux_getgid16_args *args) 209 { 210 211 td->td_retval[0] = td->td_ucred->cr_rgid; 212 return (0); 213 } 214 215 int 216 linux_getuid16(struct thread *td, struct linux_getuid16_args *args) 217 { 218 219 td->td_retval[0] = td->td_ucred->cr_ruid; 220 return (0); 221 } 222 223 int 224 linux_getegid16(struct thread *td, struct linux_getegid16_args *args) 225 { 226 struct getegid_args bsd; 227 228 return (getegid(td, &bsd)); 229 } 230 231 int 232 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args) 233 { 234 struct geteuid_args bsd; 235 236 return (geteuid(td, &bsd)); 237 } 238 239 int 240 linux_setgid16(struct thread *td, struct linux_setgid16_args *args) 241 { 242 struct setgid_args bsd; 243 244 bsd.gid = args->gid; 245 return (setgid(td, &bsd)); 246 } 247 248 int 249 linux_setuid16(struct thread *td, struct linux_setuid16_args *args) 250 { 251 struct setuid_args bsd; 252 253 bsd.uid = args->uid; 254 return (setuid(td, &bsd)); 255 } 256 257 int 258 linux_setregid16(struct thread *td, struct linux_setregid16_args *args) 259 { 260 struct setregid_args bsd; 261 262 bsd.rgid = CAST_NOCHG(args->rgid); 263 bsd.egid = CAST_NOCHG(args->egid); 264 return (setregid(td, &bsd)); 265 } 266 267 int 268 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args) 269 { 270 struct setreuid_args bsd; 271 272 bsd.ruid = CAST_NOCHG(args->ruid); 273 bsd.euid = CAST_NOCHG(args->euid); 274 return (setreuid(td, &bsd)); 275 } 276 277 int 278 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args) 279 { 280 struct setresgid_args bsd; 281 282 bsd.rgid = CAST_NOCHG(args->rgid); 283 bsd.egid = CAST_NOCHG(args->egid); 284 bsd.sgid = CAST_NOCHG(args->sgid); 285 return (setresgid(td, &bsd)); 286 } 287 288 int 289 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args) 290 { 291 struct setresuid_args bsd; 292 293 bsd.ruid = CAST_NOCHG(args->ruid); 294 bsd.euid = CAST_NOCHG(args->euid); 295 bsd.suid = CAST_NOCHG(args->suid); 296 return (setresuid(td, &bsd)); 297 } 298