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 int ngrp, error; 91 struct proc *p; 92 93 ngrp = args->gidsetsize; 94 if (ngrp < 0 || ngrp >= ngroups_max) 95 return (EINVAL); 96 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK); 97 error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t)); 98 if (error) { 99 LIN_SDT_PROBE1(uid16, linux_setgroups16, copyin_error, error); 100 free(linux_gidset, M_LINUX); 101 return (error); 102 } 103 newcred = crget(); 104 p = td->td_proc; 105 PROC_LOCK(p); 106 oldcred = crcopysafe(p, newcred); 107 108 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) { 109 PROC_UNLOCK(p); 110 crfree(newcred); 111 112 LIN_SDT_PROBE1(uid16, linux_setgroups16, priv_check_cred_error, 113 error); 114 goto out; 115 } 116 117 newcred->cr_ngroups = ngrp; 118 for (int i = 0; i < ngrp; i++) 119 newcred->cr_groups[i] = linux_gidset[i]; 120 newcred->cr_flags |= CRED_FLAG_GROUPSET; 121 122 setsugid(td->td_proc); 123 proc_set_cred(p, newcred); 124 PROC_UNLOCK(p); 125 crfree(oldcred); 126 error = 0; 127 out: 128 free(linux_gidset, M_LINUX); 129 130 return (error); 131 } 132 133 int 134 linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args) 135 { 136 struct ucred *cred; 137 l_gid16_t *linux_gidset; 138 gid_t *bsd_gidset; 139 int bsd_gidsetsz, ngrp, error; 140 141 cred = td->td_ucred; 142 bsd_gidset = cred->cr_groups; 143 bsd_gidsetsz = cred->cr_ngroups; 144 145 if ((ngrp = args->gidsetsize) == 0) { 146 td->td_retval[0] = bsd_gidsetsz; 147 return (0); 148 } 149 150 if (ngrp < bsd_gidsetsz) 151 return (EINVAL); 152 153 ngrp = 0; 154 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset), 155 M_LINUX, M_WAITOK); 156 while (ngrp < bsd_gidsetsz) { 157 linux_gidset[ngrp] = bsd_gidset[ngrp]; 158 ngrp++; 159 } 160 161 error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t)); 162 free(linux_gidset, M_LINUX); 163 if (error) { 164 LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error); 165 return (error); 166 } 167 168 td->td_retval[0] = ngrp; 169 170 return (0); 171 } 172 173 int 174 linux_getgid16(struct thread *td, struct linux_getgid16_args *args) 175 { 176 177 td->td_retval[0] = td->td_ucred->cr_rgid; 178 179 return (0); 180 } 181 182 int 183 linux_getuid16(struct thread *td, struct linux_getuid16_args *args) 184 { 185 186 td->td_retval[0] = td->td_ucred->cr_ruid; 187 188 return (0); 189 } 190 191 int 192 linux_getegid16(struct thread *td, struct linux_getegid16_args *args) 193 { 194 struct getegid_args bsd; 195 int error; 196 197 error = sys_getegid(td, &bsd); 198 199 return (error); 200 } 201 202 int 203 linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args) 204 { 205 struct geteuid_args bsd; 206 int error; 207 208 error = sys_geteuid(td, &bsd); 209 210 return (error); 211 } 212 213 int 214 linux_setgid16(struct thread *td, struct linux_setgid16_args *args) 215 { 216 struct setgid_args bsd; 217 int error; 218 219 bsd.gid = args->gid; 220 error = sys_setgid(td, &bsd); 221 222 return (error); 223 } 224 225 int 226 linux_setuid16(struct thread *td, struct linux_setuid16_args *args) 227 { 228 struct setuid_args bsd; 229 int error; 230 231 bsd.uid = args->uid; 232 error = sys_setuid(td, &bsd); 233 234 return (error); 235 } 236 237 int 238 linux_setregid16(struct thread *td, struct linux_setregid16_args *args) 239 { 240 struct setregid_args bsd; 241 int error; 242 243 bsd.rgid = CAST_NOCHG(args->rgid); 244 bsd.egid = CAST_NOCHG(args->egid); 245 error = sys_setregid(td, &bsd); 246 247 return (error); 248 } 249 250 int 251 linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args) 252 { 253 struct setreuid_args bsd; 254 int error; 255 256 bsd.ruid = CAST_NOCHG(args->ruid); 257 bsd.euid = CAST_NOCHG(args->euid); 258 error = sys_setreuid(td, &bsd); 259 260 return (error); 261 } 262 263 int 264 linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args) 265 { 266 struct setresgid_args bsd; 267 int error; 268 269 bsd.rgid = CAST_NOCHG(args->rgid); 270 bsd.egid = CAST_NOCHG(args->egid); 271 bsd.sgid = CAST_NOCHG(args->sgid); 272 error = sys_setresgid(td, &bsd); 273 274 return (error); 275 } 276 277 int 278 linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args) 279 { 280 struct setresuid_args bsd; 281 int error; 282 283 bsd.ruid = CAST_NOCHG(args->ruid); 284 bsd.euid = CAST_NOCHG(args->euid); 285 bsd.suid = CAST_NOCHG(args->suid); 286 error = sys_setresuid(td, &bsd); 287 288 return (error); 289 } 290