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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 1999-2006 Robert N. M. Watson
* All rights reserved.
*
* This software was developed by Robert Watson for the TrustedBSD Project.
*
* 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 AND CONTRIBUTORS ``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.
*/
/*
* Developed by the TrustedBSD Project.
*
* ACL support routines specific to POSIX.1e access control lists. These are
* utility routines for code common across file systems implementing POSIX.1e
* ACLs.
*/
#include <sys/cdefs.h>
#if 0
__FBSDID("$FreeBSD: head/sys/kern/subr_acl_posix1e.c 341827 2018-12-11 19:32:16Z mjg $");
#endif
__KERNEL_RCSID(0, "$NetBSD: subr_acl_posix1e.c,v 1.1 2020/05/16 18:31:50 christos Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/kauth.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/acl.h>
/*
* For the purposes of filesystems maintaining the _OBJ entries in an inode
* with a mode_t field, this routine converts a mode_t entry to an
* acl_perm_t.
*/
acl_perm_t
acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode)
{
acl_perm_t perm = 0;
switch(tag) {
case ACL_USER_OBJ:
if (mode & S_IXUSR)
perm |= ACL_EXECUTE;
if (mode & S_IRUSR)
perm |= ACL_READ;
if (mode & S_IWUSR)
perm |= ACL_WRITE;
return (perm);
case ACL_GROUP_OBJ:
if (mode & S_IXGRP)
perm |= ACL_EXECUTE;
if (mode & S_IRGRP)
perm |= ACL_READ;
if (mode & S_IWGRP)
perm |= ACL_WRITE;
return (perm);
case ACL_OTHER:
if (mode & S_IXOTH)
perm |= ACL_EXECUTE;
if (mode & S_IROTH)
perm |= ACL_READ;
if (mode & S_IWOTH)
perm |= ACL_WRITE;
return (perm);
default:
printf("%s: invalid tag (%u)\n", __func__, tag);
return (0);
}
}
/*
* Given inode information (uid, gid, mode), return an acl entry of the
* appropriate type.
*/
struct acl_entry
acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode)
{
struct acl_entry acl_entry;
acl_entry.ae_tag = tag;
acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode);
acl_entry.ae_entry_type = 0;
acl_entry.ae_flags = 0;
switch(tag) {
case ACL_USER_OBJ:
acl_entry.ae_id = uid;
break;
case ACL_GROUP_OBJ:
acl_entry.ae_id = gid;
break;
case ACL_OTHER:
acl_entry.ae_id = ACL_UNDEFINED_ID;
break;
default:
acl_entry.ae_id = ACL_UNDEFINED_ID;
printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag);
}
return (acl_entry);
}
/*
* Utility function to generate a file mode given appropriate ACL entries.
*/
mode_t
acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry,
struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry)
{
mode_t mode;
mode = 0;
if (acl_user_obj_entry->ae_perm & ACL_EXECUTE)
mode |= S_IXUSR;
if (acl_user_obj_entry->ae_perm & ACL_READ)
mode |= S_IRUSR;
if (acl_user_obj_entry->ae_perm & ACL_WRITE)
mode |= S_IWUSR;
if (acl_group_obj_entry->ae_perm & ACL_EXECUTE)
mode |= S_IXGRP;
if (acl_group_obj_entry->ae_perm & ACL_READ)
mode |= S_IRGRP;
if (acl_group_obj_entry->ae_perm & ACL_WRITE)
mode |= S_IWGRP;
if (acl_other_entry->ae_perm & ACL_EXECUTE)
mode |= S_IXOTH;
if (acl_other_entry->ae_perm & ACL_READ)
mode |= S_IROTH;
if (acl_other_entry->ae_perm & ACL_WRITE)
mode |= S_IWOTH;
return (mode);
}
/*
* Utility function to generate a file mode given a complete POSIX.1e access
* ACL. Note that if the ACL is improperly formed, this may result in a
* panic.
*/
mode_t
acl_posix1e_acl_to_mode(struct acl *acl)
{
struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other;
int i;
/*
* Find the ACL entries relevant to a POSIX permission mode.
*/
acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL;
for (i = 0; i < acl->acl_cnt; i++) {
switch (acl->acl_entry[i].ae_tag) {
case ACL_USER_OBJ:
acl_user_obj = &acl->acl_entry[i];
break;
case ACL_GROUP_OBJ:
acl_group_obj = &acl->acl_entry[i];
break;
case ACL_OTHER:
acl_other = &acl->acl_entry[i];
break;
case ACL_MASK:
acl_mask = &acl->acl_entry[i];
break;
case ACL_USER:
case ACL_GROUP:
break;
default:
panic("acl_posix1e_acl_to_mode: bad ae_tag");
}
}
if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL)
panic("acl_posix1e_acl_to_mode: missing base ae_tags");
/*
* POSIX.1e specifies that if there is an ACL_MASK entry, we replace
* the mode "group" bits with its permissions. If there isn't, we
* use the ACL_GROUP_OBJ permissions.
*/
if (acl_mask != NULL)
return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask,
acl_other));
else
return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj,
acl_other));
}
/*
* Perform a syntactic check of the ACL, sufficient to allow an implementing
* filesystem to determine if it should accept this and rely on the POSIX.1e
* ACL properties.
*/
int
acl_posix1e_check(struct acl *acl)
{
int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group;
int num_acl_mask, num_acl_other, i;
/*
* Verify that the number of entries does not exceed the maximum
* defined for acl_t.
*
* Verify that the correct number of various sorts of ae_tags are
* present:
* Exactly one ACL_USER_OBJ
* Exactly one ACL_GROUP_OBJ
* Exactly one ACL_OTHER
* If any ACL_USER or ACL_GROUP entries appear, then exactly one
* ACL_MASK entry must also appear.
*
* Verify that all ae_perm entries are in ACL_PERM_BITS.
*
* Verify all ae_tag entries are understood by this implementation.
*
* Note: Does not check for uniqueness of qualifier (ae_id) field.
*/
num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group =
num_acl_mask = num_acl_other = 0;
if (acl->acl_cnt > ACL_MAX_ENTRIES)
return (EINVAL);
for (i = 0; i < acl->acl_cnt; i++) {
struct acl_entry *ae = &acl->acl_entry[i];
/*
* Check for a valid tag.
*/
switch(ae->ae_tag) {
case ACL_USER_OBJ:
ae->ae_id = ACL_UNDEFINED_ID; /* XXX */
if (ae->ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_user_obj++;
break;
case ACL_GROUP_OBJ:
ae->ae_id = ACL_UNDEFINED_ID; /* XXX */
if (ae->ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_group_obj++;
break;
case ACL_USER:
if (ae->ae_id == ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_user++;
break;
case ACL_GROUP:
if (ae->ae_id == ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_group++;
break;
case ACL_OTHER:
ae->ae_id = ACL_UNDEFINED_ID; /* XXX */
if (ae->ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_other++;
break;
case ACL_MASK:
ae->ae_id = ACL_UNDEFINED_ID; /* XXX */
if (ae->ae_id != ACL_UNDEFINED_ID)
return (EINVAL);
num_acl_mask++;
break;
default:
return (EINVAL);
}
/*
* Check for valid perm entries.
*/
if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) !=
ACL_PERM_BITS)
return (EINVAL);
}
if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) ||
(num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1))
return (EINVAL);
if (((num_acl_group != 0) || (num_acl_user != 0)) &&
(num_acl_mask != 1))
return (EINVAL);
return (0);
}
/*
* Given a requested mode for a new object, and a default ACL, combine the
* two to produce a new mode. Be careful not to clear any bits that aren't
* intended to be affected by the POSIX.1e ACL. Eventually, this might also
* take the cmask as an argument, if we push that down into
* per-filesystem-code.
*/
mode_t
acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl)
{
mode_t mode;
mode = cmode;
/*
* The current composition policy is that a permission bit must be
* set in *both* the ACL and the requested creation mode for it to
* appear in the resulting mode/ACL. First clear any possibly
* effected bits, then reconstruct.
*/
mode &= ACL_PRESERVE_MASK;
mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl));
return (mode);
}
|