1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* 28*0Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/param.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/sysmacros.h> 36*0Sstevel@tonic-gate #include <sys/systm.h> 37*0Sstevel@tonic-gate #include <sys/tuneable.h> 38*0Sstevel@tonic-gate #include <sys/cred_impl.h> 39*0Sstevel@tonic-gate #include <sys/errno.h> 40*0Sstevel@tonic-gate #include <sys/proc.h> 41*0Sstevel@tonic-gate #include <sys/signal.h> 42*0Sstevel@tonic-gate #include <sys/debug.h> 43*0Sstevel@tonic-gate #include <sys/policy.h> 44*0Sstevel@tonic-gate #include <sys/zone.h> 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate int 47*0Sstevel@tonic-gate setuid(uid_t uid) 48*0Sstevel@tonic-gate { 49*0Sstevel@tonic-gate register proc_t *p; 50*0Sstevel@tonic-gate int error; 51*0Sstevel@tonic-gate int do_nocd = 0; 52*0Sstevel@tonic-gate int uidchge = 0; 53*0Sstevel@tonic-gate cred_t *cr, *newcr; 54*0Sstevel@tonic-gate uid_t oldruid = uid; 55*0Sstevel@tonic-gate zoneid_t zoneid = getzoneid(); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate if (uid < 0 || uid > MAXUID) 58*0Sstevel@tonic-gate return (set_errno(EINVAL)); 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate /* 61*0Sstevel@tonic-gate * Need to pre-allocate the new cred structure before grabbing 62*0Sstevel@tonic-gate * the p_crlock mutex. 63*0Sstevel@tonic-gate */ 64*0Sstevel@tonic-gate newcr = cralloc(); 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate p = ttoproc(curthread); 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate retry: 69*0Sstevel@tonic-gate mutex_enter(&p->p_crlock); 70*0Sstevel@tonic-gate cr = p->p_cred; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate if ((uid == cr->cr_ruid || uid == cr->cr_suid) && 73*0Sstevel@tonic-gate secpolicy_allow_setid(cr, uid, B_TRUE) != 0) { 74*0Sstevel@tonic-gate error = 0; 75*0Sstevel@tonic-gate crcopy_to(cr, newcr); 76*0Sstevel@tonic-gate p->p_cred = newcr; 77*0Sstevel@tonic-gate newcr->cr_uid = uid; 78*0Sstevel@tonic-gate } else if ((error = secpolicy_allow_setid(cr, uid, B_FALSE)) == 0) { 79*0Sstevel@tonic-gate if (!uidchge && uid != cr->cr_ruid) { 80*0Sstevel@tonic-gate /* 81*0Sstevel@tonic-gate * The ruid of the process is going to change. In order 82*0Sstevel@tonic-gate * to avoid a race condition involving the 83*0Sstevel@tonic-gate * process-count associated with the newly given ruid, 84*0Sstevel@tonic-gate * we increment the count before assigning the 85*0Sstevel@tonic-gate * credential to the process. 86*0Sstevel@tonic-gate * To do that, we'll have to take pidlock, so we first 87*0Sstevel@tonic-gate * release p_crlock. 88*0Sstevel@tonic-gate */ 89*0Sstevel@tonic-gate mutex_exit(&p->p_crlock); 90*0Sstevel@tonic-gate uidchge = 1; 91*0Sstevel@tonic-gate mutex_enter(&pidlock); 92*0Sstevel@tonic-gate upcount_inc(uid, zoneid); 93*0Sstevel@tonic-gate mutex_exit(&pidlock); 94*0Sstevel@tonic-gate /* 95*0Sstevel@tonic-gate * As we released p_crlock we can't rely on the cr 96*0Sstevel@tonic-gate * we read. So retry the whole thing. 97*0Sstevel@tonic-gate */ 98*0Sstevel@tonic-gate goto retry; 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate /* 101*0Sstevel@tonic-gate * A privileged process that gives up its privilege 102*0Sstevel@tonic-gate * must be marked to produce no core dump. 103*0Sstevel@tonic-gate */ 104*0Sstevel@tonic-gate if (cr->cr_uid != uid || 105*0Sstevel@tonic-gate cr->cr_ruid != uid || 106*0Sstevel@tonic-gate cr->cr_suid != uid) 107*0Sstevel@tonic-gate do_nocd = 1; 108*0Sstevel@tonic-gate oldruid = cr->cr_ruid; 109*0Sstevel@tonic-gate crcopy_to(cr, newcr); 110*0Sstevel@tonic-gate p->p_cred = newcr; 111*0Sstevel@tonic-gate newcr->cr_ruid = uid; 112*0Sstevel@tonic-gate newcr->cr_suid = uid; 113*0Sstevel@tonic-gate newcr->cr_uid = uid; 114*0Sstevel@tonic-gate ASSERT(uid != oldruid ? uidchge : 1); 115*0Sstevel@tonic-gate } else 116*0Sstevel@tonic-gate crfree(newcr); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate mutex_exit(&p->p_crlock); 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate /* 121*0Sstevel@tonic-gate * We decrement the number of processes associated with the oldruid 122*0Sstevel@tonic-gate * to match the increment above, even if the ruid of the process 123*0Sstevel@tonic-gate * did not change or an error occurred (oldruid == uid). 124*0Sstevel@tonic-gate */ 125*0Sstevel@tonic-gate if (uidchge) { 126*0Sstevel@tonic-gate mutex_enter(&pidlock); 127*0Sstevel@tonic-gate upcount_dec(oldruid, zoneid); 128*0Sstevel@tonic-gate mutex_exit(&pidlock); 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate if (error == 0) { 132*0Sstevel@tonic-gate if (do_nocd) { 133*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 134*0Sstevel@tonic-gate p->p_flag |= SNOCD; 135*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate crset(p, newcr); /* broadcast to process threads */ 138*0Sstevel@tonic-gate return (0); 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate return (set_errno(error)); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate int64_t 144*0Sstevel@tonic-gate getuid(void) 145*0Sstevel@tonic-gate { 146*0Sstevel@tonic-gate rval_t r; 147*0Sstevel@tonic-gate cred_t *cr; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate cr = curthread->t_cred; 150*0Sstevel@tonic-gate r.r_val1 = cr->cr_ruid; 151*0Sstevel@tonic-gate r.r_val2 = cr->cr_uid; 152*0Sstevel@tonic-gate return (r.r_vals); 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate int 156*0Sstevel@tonic-gate seteuid(uid_t uid) 157*0Sstevel@tonic-gate { 158*0Sstevel@tonic-gate register proc_t *p; 159*0Sstevel@tonic-gate int error = EPERM; 160*0Sstevel@tonic-gate int do_nocd = 0; 161*0Sstevel@tonic-gate cred_t *cr, *newcr; 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate if (uid < 0 || uid > MAXUID) 164*0Sstevel@tonic-gate return (set_errno(EINVAL)); 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /* 167*0Sstevel@tonic-gate * Need to pre-allocate the new cred structure before grabbing 168*0Sstevel@tonic-gate * the p_crlock mutex. 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate newcr = cralloc(); 171*0Sstevel@tonic-gate p = ttoproc(curthread); 172*0Sstevel@tonic-gate mutex_enter(&p->p_crlock); 173*0Sstevel@tonic-gate cr = p->p_cred; 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate if (uid == cr->cr_ruid || uid == cr->cr_uid || uid == cr->cr_suid || 176*0Sstevel@tonic-gate (error = secpolicy_allow_setid(cr, uid, B_FALSE)) == 0) { 177*0Sstevel@tonic-gate /* 178*0Sstevel@tonic-gate * A privileged process that makes itself look like a 179*0Sstevel@tonic-gate * set-uid process must be marked to produce no core dump, 180*0Sstevel@tonic-gate * if the effective uid did changed. 181*0Sstevel@tonic-gate */ 182*0Sstevel@tonic-gate if (cr->cr_uid != uid && error == 0) 183*0Sstevel@tonic-gate do_nocd = 1; 184*0Sstevel@tonic-gate error = 0; 185*0Sstevel@tonic-gate crcopy_to(cr, newcr); 186*0Sstevel@tonic-gate p->p_cred = newcr; 187*0Sstevel@tonic-gate newcr->cr_uid = uid; 188*0Sstevel@tonic-gate } else 189*0Sstevel@tonic-gate crfree(newcr); 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate mutex_exit(&p->p_crlock); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate if (error == 0) { 194*0Sstevel@tonic-gate if (do_nocd) { 195*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 196*0Sstevel@tonic-gate p->p_flag |= SNOCD; 197*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate crset(p, newcr); /* broadcast to process threads */ 200*0Sstevel@tonic-gate return (0); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate return (set_errno(error)); 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate /* 206*0Sstevel@tonic-gate * Buy-back from SunOS 4.x 207*0Sstevel@tonic-gate * 208*0Sstevel@tonic-gate * Like setuid() and seteuid() combined -except- that non-root users 209*0Sstevel@tonic-gate * can change cr_ruid to cr_uid, and the semantics of cr_suid are 210*0Sstevel@tonic-gate * subtly different. 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate int 213*0Sstevel@tonic-gate setreuid(uid_t ruid, uid_t euid) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate proc_t *p; 216*0Sstevel@tonic-gate int error = 0; 217*0Sstevel@tonic-gate int do_nocd = 0; 218*0Sstevel@tonic-gate int uidchge = 0; 219*0Sstevel@tonic-gate uid_t oldruid = ruid; 220*0Sstevel@tonic-gate cred_t *cr, *newcr; 221*0Sstevel@tonic-gate zoneid_t zoneid = getzoneid(); 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate if ((ruid != -1 && (ruid < 0 || ruid > MAXUID)) || 224*0Sstevel@tonic-gate (euid != -1 && (euid < 0 || euid > MAXUID))) 225*0Sstevel@tonic-gate return (set_errno(EINVAL)); 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate /* 228*0Sstevel@tonic-gate * Need to pre-allocate the new cred structure before grabbing 229*0Sstevel@tonic-gate * the p_crlock mutex. 230*0Sstevel@tonic-gate */ 231*0Sstevel@tonic-gate newcr = cralloc(); 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate p = ttoproc(curthread); 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate retry: 236*0Sstevel@tonic-gate mutex_enter(&p->p_crlock); 237*0Sstevel@tonic-gate cr = p->p_cred; 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate if (ruid != -1 && ruid != cr->cr_ruid && ruid != cr->cr_uid && 240*0Sstevel@tonic-gate secpolicy_allow_setid(cr, ruid, B_FALSE) != 0) { 241*0Sstevel@tonic-gate error = EPERM; 242*0Sstevel@tonic-gate } else if (euid != -1 && 243*0Sstevel@tonic-gate euid != cr->cr_ruid && euid != cr->cr_uid && 244*0Sstevel@tonic-gate euid != cr->cr_suid && secpolicy_allow_setid(cr, euid, B_FALSE)) { 245*0Sstevel@tonic-gate error = EPERM; 246*0Sstevel@tonic-gate } else { 247*0Sstevel@tonic-gate if (!uidchge && ruid != -1 && cr->cr_ruid != ruid) { 248*0Sstevel@tonic-gate /* 249*0Sstevel@tonic-gate * The ruid of the process is going to change. In order 250*0Sstevel@tonic-gate * to avoid a race condition involving the 251*0Sstevel@tonic-gate * process-count associated with the newly given ruid, 252*0Sstevel@tonic-gate * we increment the count before assigning the 253*0Sstevel@tonic-gate * credential to the process. 254*0Sstevel@tonic-gate * To do that, we'll have to take pidlock, so we first 255*0Sstevel@tonic-gate * release p_crlock. 256*0Sstevel@tonic-gate */ 257*0Sstevel@tonic-gate mutex_exit(&p->p_crlock); 258*0Sstevel@tonic-gate uidchge = 1; 259*0Sstevel@tonic-gate mutex_enter(&pidlock); 260*0Sstevel@tonic-gate upcount_inc(ruid, zoneid); 261*0Sstevel@tonic-gate mutex_exit(&pidlock); 262*0Sstevel@tonic-gate /* 263*0Sstevel@tonic-gate * As we released p_crlock we can't rely on the cr 264*0Sstevel@tonic-gate * we read. So retry the whole thing. 265*0Sstevel@tonic-gate */ 266*0Sstevel@tonic-gate goto retry; 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate crhold(cr); 269*0Sstevel@tonic-gate crcopy_to(cr, newcr); 270*0Sstevel@tonic-gate p->p_cred = newcr; 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate if (euid != -1) 273*0Sstevel@tonic-gate newcr->cr_uid = euid; 274*0Sstevel@tonic-gate if (ruid != -1) { 275*0Sstevel@tonic-gate oldruid = newcr->cr_ruid; 276*0Sstevel@tonic-gate newcr->cr_ruid = ruid; 277*0Sstevel@tonic-gate ASSERT(ruid != oldruid ? uidchge : 1); 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate /* 280*0Sstevel@tonic-gate * "If the real uid is being changed, or the effective uid is 281*0Sstevel@tonic-gate * being changed to a value not equal to the real uid, the 282*0Sstevel@tonic-gate * saved uid is set to the new effective uid." 283*0Sstevel@tonic-gate */ 284*0Sstevel@tonic-gate if (ruid != -1 || 285*0Sstevel@tonic-gate (euid != -1 && newcr->cr_uid != newcr->cr_ruid)) 286*0Sstevel@tonic-gate newcr->cr_suid = newcr->cr_uid; 287*0Sstevel@tonic-gate /* 288*0Sstevel@tonic-gate * A process that gives up its privilege 289*0Sstevel@tonic-gate * must be marked to produce no core dump. 290*0Sstevel@tonic-gate */ 291*0Sstevel@tonic-gate if ((cr->cr_uid != newcr->cr_uid || 292*0Sstevel@tonic-gate cr->cr_ruid != newcr->cr_ruid || 293*0Sstevel@tonic-gate cr->cr_suid != newcr->cr_suid)) 294*0Sstevel@tonic-gate do_nocd = 1; 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate crfree(cr); 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate mutex_exit(&p->p_crlock); 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate /* 301*0Sstevel@tonic-gate * We decrement the number of processes associated with the oldruid 302*0Sstevel@tonic-gate * to match the increment above, even if the ruid of the process 303*0Sstevel@tonic-gate * did not change or an error occurred (oldruid == uid). 304*0Sstevel@tonic-gate */ 305*0Sstevel@tonic-gate if (uidchge) { 306*0Sstevel@tonic-gate ASSERT(oldruid != -1 && ruid != -1); 307*0Sstevel@tonic-gate mutex_enter(&pidlock); 308*0Sstevel@tonic-gate upcount_dec(oldruid, zoneid); 309*0Sstevel@tonic-gate mutex_exit(&pidlock); 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate if (error == 0) { 313*0Sstevel@tonic-gate if (do_nocd) { 314*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 315*0Sstevel@tonic-gate p->p_flag |= SNOCD; 316*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate crset(p, newcr); /* broadcast to process threads */ 319*0Sstevel@tonic-gate return (0); 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate crfree(newcr); 322*0Sstevel@tonic-gate return (set_errno(error)); 323*0Sstevel@tonic-gate } 324