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