1 /* $OpenBSD: osf1_prot.c,v 1.3 2007/03/15 10:22:30 art Exp $ */
2 /* $NetBSD: osf1_prot.c,v 1.2 1999/05/05 01:51:35 cgd Exp $ */
3
4 /*
5 * Copyright (c) 1999 Christopher G. Demetriou. 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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou
18 * for the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
36 * All rights reserved.
37 *
38 * Author: Chris G. Demetriou
39 *
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
45 *
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 *
50 * Carnegie Mellon requests users of this software to return to
51 *
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
56 *
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
59 */
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/proc.h>
64 #include <sys/mount.h>
65 #include <sys/syscallargs.h>
66
67 #include <compat/osf1/osf1.h>
68 #include <compat/osf1/osf1_syscallargs.h>
69 #include <compat/osf1/osf1_cvt.h>
70
71 /*
72 * OSF/1 defines _POSIX_SAVED_IDS, which means that our normal
73 * setgid() won't work.
74 *
75 * If you change "uid" to "gid" in the discussion, below, about
76 * setuid(), you'll get a correct description of setgid().
77 */
78 int
79 osf1_sys_setgid(p, v, retval)
80 struct proc *p;
81 void *v;
82 register_t *retval;
83 {
84 struct osf1_sys_setgid_args *uap = v;
85 struct pcred *pc = p->p_cred;
86 gid_t gid = SCARG(uap, gid);
87 int error;
88
89 if ((error = suser(p, 0)) != 0 &&
90 gid != pc->p_rgid && gid != pc->p_svgid)
91 return (error);
92
93 pc->pc_ucred = crcopy(pc->pc_ucred);
94 pc->pc_ucred->cr_gid = gid;
95 if (error == 0) {
96 pc->p_rgid = gid;
97 pc->p_svgid = gid;
98 }
99 atomic_setbits_int(&p->p_flag, P_SUGID);
100 return (0);
101 }
102
103 /*
104 * OSF/1 defines _POSIX_SAVED_IDS, which means that our normal
105 * setuid() won't work.
106 *
107 * Instead, by P1003.1b-1993, setuid() is supposed to work like:
108 * If the process has appropriate [super-user] privileges, the
109 * setuid() function sets the real user ID, effective user
110 * ID, and the saved set-user-ID to uid.
111 * If the process does not have appropriate privileges, but uid
112 * is equal to the real user ID or the saved set-user-ID, the
113 * setuid() function sets the effective user ID to uid; the
114 * real user ID and saved set-user-ID remain unchanged by
115 * this function call.
116 */
117 int
118 osf1_sys_setuid(p, v, retval)
119 struct proc *p;
120 void *v;
121 register_t *retval;
122 {
123 struct osf1_sys_setuid_args *uap = v;
124 struct pcred *pc = p->p_cred;
125 uid_t uid = SCARG(uap, uid);
126 int error;
127
128 if ((error = suser(p, 0)) != 0 &&
129 uid != pc->p_ruid && uid != pc->p_svuid)
130 return (error);
131
132 pc->pc_ucred = crcopy(pc->pc_ucred);
133 pc->pc_ucred->cr_uid = uid;
134 if (error == 0) {
135 (void)chgproccnt(pc->p_ruid, -1);
136 (void)chgproccnt(uid, 1);
137 pc->p_ruid = uid;
138 pc->p_svuid = uid;
139 }
140 atomic_setbits_int(&p->p_flag, P_SUGID);
141 return (0);
142 }