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