forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreds.c
260 lines (212 loc) · 5.97 KB
/
creds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* $NetBSD: creds.c,v 1.16 2012/03/15 12:49:36 njoly Exp $ */
/*
* Copyright (c) 2006 Antti Kantee. All Rights Reserved.
*
* Development of this software was supported by the Ulla Tuominen Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if !defined(lint)
__RCSID("$NetBSD: creds.c,v 1.16 2012/03/15 12:49:36 njoly Exp $");
#endif /* !lint */
/*
* Interface for dealing with credits.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <errno.h>
#include <puffs.h>
#include <stdbool.h>
#include <string.h>
#include "puffs_priv.h"
#define UUCCRED(a) (a->pkcr_type == PUFFCRED_TYPE_UUC)
#define INTCRED(a) (a->pkcr_type == PUFFCRED_TYPE_INTERNAL)
int
puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *ruid)
{
PUFFS_MAKEKCRED(pkcr, pcr);
if (!UUCCRED(pkcr)) {
errno = EOPNOTSUPP;
return -1;
}
*ruid = pkcr->pkcr_uuc.cr_uid;
return 0;
}
int
puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *rgid)
{
PUFFS_MAKEKCRED(pkcr, pcr);
if (!UUCCRED(pkcr)) {
errno = EOPNOTSUPP;
return -1;
}
*rgid = pkcr->pkcr_uuc.cr_gid;
return 0;
}
int
puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *rgids, short *ngids)
{
PUFFS_MAKEKCRED(pkcr, pcr);
size_t ncopy;
if (!UUCCRED(pkcr)) {
errno = EOPNOTSUPP;
*ngids = 0;
return -1;
}
ncopy = MIN(*ngids, pkcr->pkcr_uuc.cr_ngroups);
(void)memcpy(rgids, pkcr->pkcr_uuc.cr_groups, sizeof(gid_t) * ncopy);
*ngids = (short)ncopy;
return 0;
}
bool
puffs_cred_isuid(const struct puffs_cred *pcr, uid_t uid)
{
PUFFS_MAKEKCRED(pkcr, pcr);
return UUCCRED(pkcr) && pkcr->pkcr_uuc.cr_uid == uid;
}
bool
puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t gid)
{
PUFFS_MAKEKCRED(pkcr, pcr);
short i;
if (!UUCCRED(pkcr))
return false;
if (pkcr->pkcr_uuc.cr_gid == gid)
return true;
for (i = 0; i < pkcr->pkcr_uuc.cr_ngroups; i++)
if (pkcr->pkcr_uuc.cr_groups[i] == gid)
return true;
return false;
}
bool
puffs_cred_isregular(const struct puffs_cred *pcr)
{
PUFFS_MAKEKCRED(pkcr, pcr);
return UUCCRED(pkcr);
}
bool
puffs_cred_iskernel(const struct puffs_cred *pcr)
{
PUFFS_MAKEKCRED(pkcr, pcr);
return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_NOCRED;
}
bool
puffs_cred_isfs(const struct puffs_cred *pcr)
{
PUFFS_MAKEKCRED(pkcr, pcr);
return INTCRED(pkcr) && pkcr->pkcr_internal == PUFFCRED_CRED_FSCRED;
}
bool
puffs_cred_isjuggernaut(const struct puffs_cred *pcr)
{
return puffs_cred_isuid(pcr, 0) || puffs_cred_iskernel(pcr)
|| puffs_cred_isfs(pcr);
}
/*
* Generic routine for checking file access rights. Modeled after
* vaccess() in the kernel.
*/
int
puffs_access(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
mode_t acc_mode, const struct puffs_cred *pcr)
{
mode_t mask;
/* megapower */
if (puffs_cred_iskernel(pcr) || puffs_cred_isfs(pcr))
return 0;
/* superuser, allow all except exec if *ALL* exec bits are unset */
if (puffs_cred_isuid(pcr, 0)) {
if ((acc_mode & PUFFS_VEXEC) && type != VDIR &&
(file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
return EACCES;
return 0;
}
mask = 0;
/* owner */
if (puffs_cred_isuid(pcr, uid)) {
if (acc_mode & PUFFS_VEXEC)
mask |= S_IXUSR;
if (acc_mode & PUFFS_VREAD)
mask |= S_IRUSR;
if (acc_mode & PUFFS_VWRITE)
mask |= S_IWUSR;
/* group */
} else if (puffs_cred_hasgroup(pcr, gid)) {
if (acc_mode & PUFFS_VEXEC)
mask |= S_IXGRP;
if (acc_mode & PUFFS_VREAD)
mask |= S_IRGRP;
if (acc_mode & PUFFS_VWRITE)
mask |= S_IWGRP;
/* other */
} else {
if (acc_mode & PUFFS_VEXEC)
mask |= S_IXOTH;
if (acc_mode & PUFFS_VREAD)
mask |= S_IROTH;
if (acc_mode & PUFFS_VWRITE)
mask |= S_IWOTH;
}
if ((file_mode & mask) == mask)
return 0;
else
return EACCES;
}
int
puffs_access_chown(uid_t owner, gid_t group, uid_t newowner, gid_t newgroup,
const struct puffs_cred *pcr)
{
if (newowner == (uid_t)PUFFS_VNOVAL)
newowner = owner;
if (newgroup == (gid_t)PUFFS_VNOVAL)
newgroup = group;
if ((!puffs_cred_isuid(pcr, owner) || newowner != owner ||
((newgroup != group && !puffs_cred_hasgroup(pcr, newgroup))))
&& !puffs_cred_isjuggernaut(pcr))
return EPERM;
return 0;
}
int
puffs_access_chmod(uid_t owner, gid_t group, enum vtype type, mode_t mode,
const struct puffs_cred *pcr)
{
if (!puffs_cred_isuid(pcr, owner) && !puffs_cred_isjuggernaut(pcr))
return EPERM;
if (!puffs_cred_isjuggernaut(pcr)) {
if (type != VDIR && (mode & S_ISTXT))
return EFTYPE;
if (!puffs_cred_hasgroup(pcr, group) && (mode & S_ISGID))
return EPERM;
}
return 0;
}
int
puffs_access_times(uid_t uid, gid_t gid, mode_t mode, int va_utimes_null,
const struct puffs_cred *pcr)
{
if (puffs_cred_isuid(pcr, uid) || puffs_cred_isjuggernaut(pcr))
return 0;
if (va_utimes_null == 0)
return EPERM;
return puffs_access(VNON, mode, uid, gid, PUFFS_VWRITE, pcr);
}