1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2001 The FreeBSD Project 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/fcntl.h> 34 #include <sys/lock.h> 35 #include <sys/malloc.h> 36 #include <sys/mutex.h> 37 #include <sys/priv.h> 38 #include <sys/proc.h> 39 #include <sys/syscallsubr.h> 40 #include <sys/sysproto.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_dtrace.h> 51 #include <compat/linux/linux_util.h> 52 53 /* DTrace init */ 54 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 55 56 /** 57 * DTrace probes in this module. 58 */ 59 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, conv_path, "char *"); 60 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, conv_path, "char *"); 61 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, copyin_error, "int"); 62 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, priv_check_cred_error, "int"); 63 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, copyout_error, "int"); 64 65 DUMMY(setfsuid16); 66 DUMMY(setfsgid16); 67 DUMMY(getresuid16); 68 DUMMY(getresgid16); 69 70 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x) 71 72 int 73 linux_chown16(struct thread *td, struct linux_chown16_args *args) 74 { 75 char *path; 76 int error; 77 78 if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) { 79 error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, 80 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0); 81 } else { 82 LCONVPATHEXIST(args->path, &path); 83 /* 84 * The DTrace probes have to be after the LCONVPATHEXIST, as 85 * LCONVPATHEXIST may return on its own and we do not want to 86 * have a stray entry without the corresponding return. 87 */ 88 LIN_SDT_PROBE1(uid16, linux_chown16, conv_path, path); 89 90 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, 91 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0); 92 LFREEPATH(path); 93 } 94 return (error); 95 } 96 97 int 98 linux_lchown16(struct thread *td, struct linux_lchown16_args *args) 99 { 100 char *path; 101 int error; 102 103 if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) { 104 error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, 105 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW); 106 } else { 107 LCONVPATHEXIST(args->path, &path); 108 109 /* 110 * The DTrace probes have to be after the LCONVPATHEXIST, as 111 * LCONVPATHEXIST may return on its own and we do not want to 112 * have a stray entry without the corresponding return. 113 */ 114 LIN_SDT_PROBE1(uid16, linux_lchown16, conv_path, path); 115 116 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, 117 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW); 118 LFREEPATH(path); 119 } 120 return (error); 121 } 122 123 int 124 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args) 125 { 126 struct ucred *newcred, *oldcred; 127 l_gid16_t *linux_gidset; 128 gid_t *bsd_gidset; 129 int ngrp, error; 130 struct proc *p; 131 132 ngrp = args->gidsetsize; 133 if (ngrp < 0 || ngrp >= ngroups_max + 1) 134 return (EINVAL); 135 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK); 136 error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t)); 137 if (error) { 138 LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error); 139 free(linux_gidset, M_LINUX); 140 return (error); 141 } 142 newcred = crget(); 143 p = td->td_proc; 144 PROC_LOCK(p); 145 oldcred = crcopysafe(p, newcred); 146 147 /* 148 * cr_groups[0] holds egid. Setting the whole set from 149 * the supplied set will cause egid to be changed too. 150 * Keep cr_groups[0] unchanged to prevent that. 151 */ 152 153 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) { 154 PROC_UNLOCK(p); 155 crfree(newcred); 156 157 LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error, 158 error); 159 goto out; 160 } 161 162 if (ngrp > 0) { 163 newcred->cr_ngroups = ngrp + 1; 164 165 bsd_gidset = newcred->cr_groups; 166 ngrp--; 167 while (ngrp >= 0) { 168 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 169 ngrp--; 170 } 171 } 172 else 173 newcred->cr_ngroups = 1; 174 175 setsugid(td->td_proc); 176 proc_set_cred(p, newcred); 177 PROC_UNLOCK(p); 178 crfree(oldcred); 179 error = 0; 180 out: 181 free(linux_gidset, M_LINUX); 182 183 return (error); 184 } 185 186 int 187 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args) 188 { 189 struct ucred *cred; 190 l_gid16_t *linux_gidset; 191 gid_t *bsd_gidset; 192 int bsd_gidsetsz, ngrp, error; 193 194 cred = td->td_ucred; 195 bsd_gidset = cred->cr_groups; 196 bsd_gidsetsz = cred->cr_ngroups - 1; 197 198 /* 199 * cr_groups[0] holds egid. Returning the whole set 200 * here will cause a duplicate. Exclude cr_groups[0] 201 * to prevent that. 202 */ 203 204 if ((ngrp = args->gidsetsize) == 0) { 205 td->td_retval[0] = bsd_gidsetsz; 206 return (0); 207 } 208 209 if (ngrp < bsd_gidsetsz) 210 return (EINVAL); 211 212 ngrp = 0; 213 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset), 214 M_LINUX, M_WAITOK); 215 while (ngrp < bsd_gidsetsz) { 216 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 217 ngrp++; 218 } 219 220 error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t)); 221 free(linux_gidset, M_LINUX); 222 if (error) { 223 LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error); 224 return (error); 225 } 226 227 td->td_retval[0] = ngrp; 228 229 return (0); 230 } 231 232 int 233 linux_getgid16(struct thread *td, struct linux_getgid16_args *args) 234 { 235 236 td->td_retval[0] = td->td_ucred->cr_rgid; 237 238 return (0); 239 } 240 241 int 242 linux_getuid16(struct thread *td, struct linux_getuid16_args *args) 243 { 244 245 td->td_retval[0] = td->td_ucred->cr_ruid; 246 247 return (0); 248 } 249 250 int 251 linux_getegid16(struct thread *td, struct linux_getegid16_args *args) 252 { 253 struct getegid_args bsd; 254 int error; 255 256 error = sys_getegid(td, &bsd); 257 258 return (error); 259 } 260 261 int 262 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args) 263 { 264 struct geteuid_args bsd; 265 int error; 266 267 error = sys_geteuid(td, &bsd); 268 269 return (error); 270 } 271 272 int 273 linux_setgid16(struct thread *td, struct linux_setgid16_args *args) 274 { 275 struct setgid_args bsd; 276 int error; 277 278 bsd.gid = args->gid; 279 error = sys_setgid(td, &bsd); 280 281 return (error); 282 } 283 284 int 285 linux_setuid16(struct thread *td, struct linux_setuid16_args *args) 286 { 287 struct setuid_args bsd; 288 int error; 289 290 bsd.uid = args->uid; 291 error = sys_setuid(td, &bsd); 292 293 return (error); 294 } 295 296 int 297 linux_setregid16(struct thread *td, struct linux_setregid16_args *args) 298 { 299 struct setregid_args bsd; 300 int error; 301 302 bsd.rgid = CAST_NOCHG(args->rgid); 303 bsd.egid = CAST_NOCHG(args->egid); 304 error = sys_setregid(td, &bsd); 305 306 return (error); 307 } 308 309 int 310 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args) 311 { 312 struct setreuid_args bsd; 313 int error; 314 315 bsd.ruid = CAST_NOCHG(args->ruid); 316 bsd.euid = CAST_NOCHG(args->euid); 317 error = sys_setreuid(td, &bsd); 318 319 return (error); 320 } 321 322 int 323 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args) 324 { 325 struct setresgid_args bsd; 326 int error; 327 328 bsd.rgid = CAST_NOCHG(args->rgid); 329 bsd.egid = CAST_NOCHG(args->egid); 330 bsd.sgid = CAST_NOCHG(args->sgid); 331 error = sys_setresgid(td, &bsd); 332 333 return (error); 334 } 335 336 int 337 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args) 338 { 339 struct setresuid_args bsd; 340 int error; 341 342 bsd.ruid = CAST_NOCHG(args->ruid); 343 bsd.euid = CAST_NOCHG(args->euid); 344 bsd.suid = CAST_NOCHG(args->suid); 345 error = sys_setresuid(td, &bsd); 346 347 return (error); 348 } 349