| 1 | /*- |
| 2 | * Copyright (c) 2001 The FreeBSD Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 | * SUCH DAMAGE. |
| 25 | * |
| 26 | * $FreeBSD: src/sys/compat/linux/linux_uid16.c,v 1.4.2.1 2001/10/21 03:57:35 marcel Exp $ |
| 27 | * $DragonFly: src/sys/emulation/linux/linux_uid16.c,v 1.3 2003/06/23 17:55:26 dillon Exp $ |
| 28 | */ |
| 29 | |
| 30 | #include "opt_compat.h" |
| 31 | |
| 32 | #include <sys/param.h> |
| 33 | #include <sys/systm.h> |
| 34 | #include <sys/proc.h> |
| 35 | #include <sys/sysproto.h> |
| 36 | |
| 37 | #include <machine/../linux/linux.h> |
| 38 | #include <machine/../linux/linux_proto.h> |
| 39 | #include <compat/linux/linux_util.h> |
| 40 | |
| 41 | DUMMY(setfsuid16); |
| 42 | DUMMY(setfsgid16); |
| 43 | DUMMY(getresuid16); |
| 44 | DUMMY(getresgid16); |
| 45 | |
| 46 | #define CAST_NOCHG(x) (x == 0xFFFF) ? -1 : x; |
| 47 | |
| 48 | int |
| 49 | linux_chown16(struct linux_chown16_args *args) |
| 50 | { |
| 51 | struct chown_args bsd; |
| 52 | caddr_t sg; |
| 53 | |
| 54 | sg = stackgap_init(); |
| 55 | CHECKALTEXIST(&sg, args->path); |
| 56 | |
| 57 | #ifdef DEBUG |
| 58 | if (ldebug(chown16)) |
| 59 | printf(ARGS(chown16, "%s, %d, %d"), args->path, args->uid, |
| 60 | args->gid); |
| 61 | #endif |
| 62 | |
| 63 | bsd.path = args->path; |
| 64 | bsd.uid = CAST_NOCHG(args->uid); |
| 65 | bsd.gid = CAST_NOCHG(args->gid); |
| 66 | return (chown(&bsd)); |
| 67 | } |
| 68 | |
| 69 | int |
| 70 | linux_lchown16(struct linux_lchown16_args *args) |
| 71 | { |
| 72 | struct lchown_args bsd; |
| 73 | caddr_t sg; |
| 74 | |
| 75 | sg = stackgap_init(); |
| 76 | CHECKALTEXIST(&sg, args->path); |
| 77 | |
| 78 | #ifdef DEBUG |
| 79 | if (ldebug(lchown16)) |
| 80 | printf(ARGS(lchown16, "%s, %d, %d"), args->path, args->uid, |
| 81 | args->gid); |
| 82 | #endif |
| 83 | |
| 84 | bsd.path = args->path; |
| 85 | bsd.uid = CAST_NOCHG(args->uid); |
| 86 | bsd.gid = CAST_NOCHG(args->gid); |
| 87 | return (lchown(&bsd)); |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | linux_setgroups16(struct linux_setgroups16_args *args) |
| 92 | { |
| 93 | struct proc *p = curproc; |
| 94 | struct ucred *newcred, *oldcred; |
| 95 | l_gid16_t linux_gidset[NGROUPS]; |
| 96 | gid_t *bsd_gidset; |
| 97 | int ngrp, error; |
| 98 | |
| 99 | #ifdef DEBUG |
| 100 | if (ldebug(setgroups16)) |
| 101 | printf(ARGS(setgroups16, "%d, *"), args->gidsetsize); |
| 102 | #endif |
| 103 | |
| 104 | ngrp = args->gidsetsize; |
| 105 | oldcred = p->p_ucred; |
| 106 | |
| 107 | /* |
| 108 | * cr_groups[0] holds egid. Setting the whole set from |
| 109 | * the supplied set will cause egid to be changed too. |
| 110 | * Keep cr_groups[0] unchanged to prevent that. |
| 111 | */ |
| 112 | |
| 113 | if ((error = suser_xxx(oldcred, PRISON_ROOT)) != 0) |
| 114 | return (error); |
| 115 | |
| 116 | if (ngrp >= NGROUPS) |
| 117 | return (EINVAL); |
| 118 | |
| 119 | newcred = crdup(oldcred); |
| 120 | if (ngrp > 0) { |
| 121 | error = copyin((caddr_t)args->gidset, linux_gidset, |
| 122 | ngrp * sizeof(l_gid16_t)); |
| 123 | if (error) |
| 124 | return (error); |
| 125 | |
| 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(); |
| 139 | p->p_ucred = newcred; |
| 140 | crfree(oldcred); |
| 141 | return (0); |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | linux_getgroups16(struct linux_getgroups16_args *args) |
| 146 | { |
| 147 | struct proc *p = curproc; |
| 148 | struct ucred *cred; |
| 149 | l_gid16_t linux_gidset[NGROUPS]; |
| 150 | gid_t *bsd_gidset; |
| 151 | int bsd_gidsetsz, ngrp, error; |
| 152 | |
| 153 | #ifdef DEBUG |
| 154 | if (ldebug(getgroups16)) |
| 155 | printf(ARGS(getgroups16, "%d, *"), args->gidsetsize); |
| 156 | #endif |
| 157 | |
| 158 | cred = p->p_ucred; |
| 159 | bsd_gidset = cred->cr_groups; |
| 160 | bsd_gidsetsz = cred->cr_ngroups - 1; |
| 161 | |
| 162 | /* |
| 163 | * cr_groups[0] holds egid. Returning the whole set |
| 164 | * here will cause a duplicate. Exclude cr_groups[0] |
| 165 | * to prevent that. |
| 166 | */ |
| 167 | |
| 168 | if ((ngrp = args->gidsetsize) == 0) { |
| 169 | p->p_retval[0] = bsd_gidsetsz; |
| 170 | return (0); |
| 171 | } |
| 172 | |
| 173 | if (ngrp < bsd_gidsetsz) |
| 174 | return (EINVAL); |
| 175 | |
| 176 | ngrp = 0; |
| 177 | while (ngrp < bsd_gidsetsz) { |
| 178 | linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; |
| 179 | ngrp++; |
| 180 | } |
| 181 | |
| 182 | error = copyout(linux_gidset, (caddr_t)args->gidset, |
| 183 | ngrp * sizeof(l_gid16_t)); |
| 184 | if (error) |
| 185 | return (error); |
| 186 | |
| 187 | p->p_retval[0] = ngrp; |
| 188 | return (0); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1] |
| 193 | * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that |
| 194 | * are assumed to be preserved. The following lightweight syscalls fixes |
| 195 | * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in |
| 196 | * linux_misc.c |
| 197 | * |
| 198 | * linux_getgid16() - MP SAFE |
| 199 | * linux_getuid16() - MP SAFE |
| 200 | */ |
| 201 | |
| 202 | int |
| 203 | linux_getgid16(struct linux_getgid16_args *args) |
| 204 | { |
| 205 | struct proc *p = curproc; |
| 206 | |
| 207 | p->p_retval[0] = p->p_ucred->cr_rgid; |
| 208 | return (0); |
| 209 | } |
| 210 | |
| 211 | int |
| 212 | linux_getuid16(struct linux_getuid16_args *args) |
| 213 | { |
| 214 | struct proc *p = curproc; |
| 215 | |
| 216 | p->p_retval[0] = p->p_ucred->cr_ruid; |
| 217 | return (0); |
| 218 | } |
| 219 | |
| 220 | int |
| 221 | linux_getegid16(struct linux_getegid16_args *args) |
| 222 | { |
| 223 | struct getegid_args bsd; |
| 224 | |
| 225 | return (getegid(&bsd)); |
| 226 | } |
| 227 | |
| 228 | int |
| 229 | linux_geteuid16(struct linux_geteuid16_args *args) |
| 230 | { |
| 231 | struct geteuid_args bsd; |
| 232 | |
| 233 | return (geteuid(&bsd)); |
| 234 | } |
| 235 | |
| 236 | int |
| 237 | linux_setgid16(struct linux_setgid16_args *args) |
| 238 | { |
| 239 | struct setgid_args bsd; |
| 240 | |
| 241 | bsd.gid = args->gid; |
| 242 | return (setgid(&bsd)); |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | linux_setuid16(struct linux_setuid16_args *args) |
| 247 | { |
| 248 | struct setuid_args bsd; |
| 249 | |
| 250 | bsd.uid = args->uid; |
| 251 | return (setuid(&bsd)); |
| 252 | } |
| 253 | |
| 254 | int |
| 255 | linux_setregid16(struct linux_setregid16_args *args) |
| 256 | { |
| 257 | struct setregid_args bsd; |
| 258 | |
| 259 | bsd.rgid = CAST_NOCHG(args->rgid); |
| 260 | bsd.egid = CAST_NOCHG(args->egid); |
| 261 | return (setregid(&bsd)); |
| 262 | } |
| 263 | |
| 264 | int |
| 265 | linux_setreuid16(struct linux_setreuid16_args *args) |
| 266 | { |
| 267 | struct setreuid_args bsd; |
| 268 | |
| 269 | bsd.ruid = CAST_NOCHG(args->ruid); |
| 270 | bsd.euid = CAST_NOCHG(args->euid); |
| 271 | return (setreuid(&bsd)); |
| 272 | } |
| 273 | |
| 274 | int |
| 275 | linux_setresgid16(struct linux_setresgid16_args *args) |
| 276 | { |
| 277 | struct setresgid_args bsd; |
| 278 | |
| 279 | bsd.rgid = CAST_NOCHG(args->rgid); |
| 280 | bsd.egid = CAST_NOCHG(args->egid); |
| 281 | bsd.sgid = CAST_NOCHG(args->sgid); |
| 282 | return (setresgid(&bsd)); |
| 283 | } |
| 284 | |
| 285 | int |
| 286 | linux_setresuid16(struct linux_setresuid16_args *args) |
| 287 | { |
| 288 | struct setresuid_args bsd; |
| 289 | |
| 290 | bsd.ruid = CAST_NOCHG(args->ruid); |
| 291 | bsd.euid = CAST_NOCHG(args->euid); |
| 292 | bsd.suid = CAST_NOCHG(args->suid); |
| 293 | return (setresuid(&bsd)); |
| 294 | } |