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 "opt_compat.h" 33 34 #include <sys/fcntl.h> 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/priv.h> 41 #include <sys/proc.h> 42 #include <sys/sdt.h> 43 #include <sys/syscallsubr.h> 44 #include <sys/sysproto.h> 45 #include <sys/systm.h> 46 47 #ifdef COMPAT_LINUX32 48 #include <machine/../linux32/linux.h> 49 #include <machine/../linux32/linux32_proto.h> 50 #else 51 #include <machine/../linux/linux.h> 52 #include <machine/../linux/linux_proto.h> 53 #endif 54 55 #include <compat/linux/linux_dtrace.h> 56 #include <compat/linux/linux_util.h> 57 58 /* DTrace init */ 59 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE); 60 61 /** 62 * DTrace probes in this module. 63 */ 64 LIN_SDT_PROBE_DEFINE1(uid16, linux_chown16, conv_path, "char *"); 65 LIN_SDT_PROBE_DEFINE1(uid16, linux_lchown16, conv_path, "char *"); 66 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, copyin_error, "int"); 67 LIN_SDT_PROBE_DEFINE1(uid16, linux_setgroups16, priv_check_cred_error, "int"); 68 LIN_SDT_PROBE_DEFINE1(uid16, linux_getgroups16, copyout_error, "int"); 69 70 DUMMY(setfsuid16); 71 DUMMY(setfsgid16); 72 DUMMY(getresuid16); 73 DUMMY(getresgid16); 74 75 #define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x) 76 77 int 78 linux_chown16(struct thread *td, struct linux_chown16_args *args) 79 { 80 char *path; 81 int error; 82 83 if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) { 84 error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, 85 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0); 86 } else { 87 LCONVPATHEXIST(td, args->path, &path); 88 /* 89 * The DTrace probes have to be after the LCONVPATHEXIST, as 90 * LCONVPATHEXIST may return on its own and we do not want to 91 * have a stray entry without the corresponding return. 92 */ 93 LIN_SDT_PROBE1(uid16, linux_chown16, conv_path, path); 94 95 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, 96 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), 0); 97 LFREEPATH(path); 98 } 99 return (error); 100 } 101 102 int 103 linux_lchown16(struct thread *td, struct linux_lchown16_args *args) 104 { 105 char *path; 106 int error; 107 108 if (!LUSECONVPATH(td) && !SDT_PROBES_ENABLED()) { 109 error = kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, 110 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW); 111 } else { 112 LCONVPATHEXIST(td, args->path, &path); 113 114 /* 115 * The DTrace probes have to be after the LCONVPATHEXIST, as 116 * LCONVPATHEXIST may return on its own and we do not want to 117 * have a stray entry without the corresponding return. 118 */ 119 LIN_SDT_PROBE1(uid16, linux_lchown16, conv_path, path); 120 121 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, 122 CAST_NOCHG(args->uid), CAST_NOCHG(args->gid), AT_SYMLINK_NOFOLLOW); 123 LFREEPATH(path); 124 } 125 return (error); 126 } 127 128 int 129 linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args) 130 { 131 struct ucred *newcred, *oldcred; 132 l_gid16_t *linux_gidset; 133 gid_t *bsd_gidset; 134 int ngrp, error; 135 struct proc *p; 136 137 ngrp = args->gidsetsize; 138 if (ngrp < 0 || ngrp >= ngroups_max + 1) 139 return (EINVAL); 140 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK); 141 error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t)); 142 if (error) { 143 LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error); 144 free(linux_gidset, M_LINUX); 145 return (error); 146 } 147 newcred = crget(); 148 p = td->td_proc; 149 PROC_LOCK(p); 150 oldcred = crcopysafe(p, newcred); 151 152 /* 153 * cr_groups[0] holds egid. Setting the whole set from 154 * the supplied set will cause egid to be changed too. 155 * Keep cr_groups[0] unchanged to prevent that. 156 */ 157 158 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) { 159 PROC_UNLOCK(p); 160 crfree(newcred); 161 162 LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error, 163 error); 164 goto out; 165 } 166 167 if (ngrp > 0) { 168 newcred->cr_ngroups = ngrp + 1; 169 170 bsd_gidset = newcred->cr_groups; 171 ngrp--; 172 while (ngrp >= 0) { 173 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 174 ngrp--; 175 } 176 } 177 else 178 newcred->cr_ngroups = 1; 179 180 setsugid(td->td_proc); 181 proc_set_cred(p, newcred); 182 PROC_UNLOCK(p); 183 crfree(oldcred); 184 error = 0; 185 out: 186 free(linux_gidset, M_LINUX); 187 188 return (error); 189 } 190 191 int 192 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args) 193 { 194 struct ucred *cred; 195 l_gid16_t *linux_gidset; 196 gid_t *bsd_gidset; 197 int bsd_gidsetsz, ngrp, error; 198 199 cred = td->td_ucred; 200 bsd_gidset = cred->cr_groups; 201 bsd_gidsetsz = cred->cr_ngroups - 1; 202 203 /* 204 * cr_groups[0] holds egid. Returning the whole set 205 * here will cause a duplicate. Exclude cr_groups[0] 206 * to prevent that. 207 */ 208 209 if ((ngrp = args->gidsetsize) == 0) { 210 td->td_retval[0] = bsd_gidsetsz; 211 return (0); 212 } 213 214 if (ngrp < bsd_gidsetsz) 215 return (EINVAL); 216 217 ngrp = 0; 218 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset), 219 M_LINUX, M_WAITOK); 220 while (ngrp < bsd_gidsetsz) { 221 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 222 ngrp++; 223 } 224 225 error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t)); 226 free(linux_gidset, M_LINUX); 227 if (error) { 228 LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error); 229 return (error); 230 } 231 232 td->td_retval[0] = ngrp; 233 234 return (0); 235 } 236 237 int 238 linux_getgid16(struct thread *td, struct linux_getgid16_args *args) 239 { 240 241 td->td_retval[0] = td->td_ucred->cr_rgid; 242 243 return (0); 244 } 245 246 int 247 linux_getuid16(struct thread *td, struct linux_getuid16_args *args) 248 { 249 250 td->td_retval[0] = td->td_ucred->cr_ruid; 251 252 return (0); 253 } 254 255 int 256 linux_getegid16(struct thread *td, struct linux_getegid16_args *args) 257 { 258 struct getegid_args bsd; 259 int error; 260 261 error = sys_getegid(td, &bsd); 262 263 return (error); 264 } 265 266 int 267 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args) 268 { 269 struct geteuid_args bsd; 270 int error; 271 272 error = sys_geteuid(td, &bsd); 273 274 return (error); 275 } 276 277 int 278 linux_setgid16(struct thread *td, struct linux_setgid16_args *args) 279 { 280 struct setgid_args bsd; 281 int error; 282 283 bsd.gid = args->gid; 284 error = sys_setgid(td, &bsd); 285 286 return (error); 287 } 288 289 int 290 linux_setuid16(struct thread *td, struct linux_setuid16_args *args) 291 { 292 struct setuid_args bsd; 293 int error; 294 295 bsd.uid = args->uid; 296 error = sys_setuid(td, &bsd); 297 298 return (error); 299 } 300 301 int 302 linux_setregid16(struct thread *td, struct linux_setregid16_args *args) 303 { 304 struct setregid_args bsd; 305 int error; 306 307 bsd.rgid = CAST_NOCHG(args->rgid); 308 bsd.egid = CAST_NOCHG(args->egid); 309 error = sys_setregid(td, &bsd); 310 311 return (error); 312 } 313 314 int 315 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args) 316 { 317 struct setreuid_args bsd; 318 int error; 319 320 bsd.ruid = CAST_NOCHG(args->ruid); 321 bsd.euid = CAST_NOCHG(args->euid); 322 error = sys_setreuid(td, &bsd); 323 324 return (error); 325 } 326 327 int 328 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args) 329 { 330 struct setresgid_args bsd; 331 int error; 332 333 bsd.rgid = CAST_NOCHG(args->rgid); 334 bsd.egid = CAST_NOCHG(args->egid); 335 bsd.sgid = CAST_NOCHG(args->sgid); 336 error = sys_setresgid(td, &bsd); 337 338 return (error); 339 } 340 341 int 342 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args) 343 { 344 struct setresuid_args bsd; 345 int error; 346 347 bsd.ruid = CAST_NOCHG(args->ruid); 348 bsd.euid = CAST_NOCHG(args->euid); 349 bsd.suid = CAST_NOCHG(args->suid); 350 error = sys_setresuid(td, &bsd); 351 352 return (error); 353 } 354