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