2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Dave Borman at Cray Research, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "namespace.h"
36 #include <sys/types.h>
38 #include <sys/sysctl.h>
51 #include "un-namespace.h"
52 #include "libc_private.h"
54 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
55 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
57 typedef struct bitcmd {
65 #define CMD2_GBITS 0x04
66 #define CMD2_OBITS 0x08
67 #define CMD2_UBITS 0x10
69 static mode_t get_current_umask(void);
70 static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
71 static void compress_mode(BITCMD *);
73 static void dumpmode(BITCMD *);
77 * Given the old mode and an array of bitcmd structures, apply the operations
78 * described in the bitcmd structures to the old mode, and return the new mode.
79 * Note that there is no '=' command; a strict assignment is just a '-' (clear
80 * bits) followed by a '+' (set bits).
83 getmode(const void *bbox, mode_t omode)
86 mode_t clrval, newmode, value;
88 set = (const BITCMD *)bbox;
90 for (value = 0;; set++)
93 * When copying the user, group or other bits around, we "know"
94 * where the bits are in the mode so that we can do shifts to
95 * copy them around. If we don't use shifts, it gets real
96 * grundgy with lots of single bit checks and bit sets.
99 value = (newmode & S_IRWXU) >> 6;
103 value = (newmode & S_IRWXG) >> 3;
107 value = newmode & S_IRWXO;
108 common: if (set->cmd2 & CMD2_CLR) {
110 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
111 if (set->cmd2 & CMD2_UBITS)
112 newmode &= ~((clrval<<6) & set->bits);
113 if (set->cmd2 & CMD2_GBITS)
114 newmode &= ~((clrval<<3) & set->bits);
115 if (set->cmd2 & CMD2_OBITS)
116 newmode &= ~(clrval & set->bits);
118 if (set->cmd2 & CMD2_SET) {
119 if (set->cmd2 & CMD2_UBITS)
120 newmode |= (value<<6) & set->bits;
121 if (set->cmd2 & CMD2_GBITS)
122 newmode |= (value<<3) & set->bits;
123 if (set->cmd2 & CMD2_OBITS)
124 newmode |= value & set->bits;
129 newmode |= set->bits;
133 newmode &= ~set->bits;
137 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
138 newmode |= set->bits;
144 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
150 #define ADDCMD(a, b, c, d) \
151 if (set >= endset) { \
153 setlen += SET_LEN_INCR; \
154 newset = reallocarray(saveset, setlen, sizeof(BITCMD)); \
155 if (newset == NULL) \
157 set = newset + (set - saveset); \
159 endset = newset + (setlen - 2); \
161 set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d))
163 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
166 setmode(const char *p)
170 BITCMD *set, *saveset, *endset;
171 mode_t mask, perm, permXbits, who;
182 * Get a copy of the mask for the permissions that are mask relative.
183 * Flip the bits, we want what's not set.
185 mask = ~get_current_umask();
187 setlen = SET_LEN + 2;
189 if ((set = malloc(setlen * sizeof(BITCMD))) == NULL)
192 endset = set + (setlen - 2);
195 * If an absolute number, get it and return; disallow non-octal digits
198 if (isdigit((unsigned char)*p)) {
200 perml = strtol(p, &ep, 8);
205 if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN))
207 if (perml & ~(STANDARD_BITS|S_ISTXT)) {
211 perm = (mode_t)perml;
212 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
218 * Build list of structures to set/clear/copy bits as described by
219 * each clause of the symbolic mode.
223 /* First, find out which bits might be modified. */
224 for (who = 0;; ++p) {
227 who |= STANDARD_BITS;
230 who |= S_ISUID|S_IRWXU;
233 who |= S_ISGID|S_IRWXG;
243 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
251 for (perm = 0, permXbits = 0;; ++p) {
254 perm |= S_IRUSR|S_IRGRP|S_IROTH;
257 /* If only "other" bits ignore set-id. */
258 if (!who || who & ~S_IRWXO)
259 perm |= S_ISUID|S_ISGID;
262 /* If only "other" bits ignore sticky. */
263 if (!who || who & ~S_IRWXO) {
269 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
272 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
275 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
281 * When ever we hit 'u', 'g', or 'o', we have
282 * to flush out any partial mode that we have,
283 * and then do the copying of the mode bits.
286 ADDCMD(op, who, perm, mask);
291 if (op == '+' && permXbits) {
292 ADDCMD('X', who, permXbits, mask);
295 ADDCMD(*p, who, op, mask);
300 * Add any permissions that we haven't already
303 if (perm || (op == '=' && !equalopdone)) {
306 ADDCMD(op, who, perm, mask);
310 ADDCMD('X', who, permXbits, mask);
325 (void)printf("Before compress_mode()\n");
328 compress_mode(saveset);
330 (void)printf("After compress_mode()\n");
342 get_current_umask(void)
344 sigset_t sigset, sigoset;
349 #ifdef KERN_PROC_UMASK
351 * First try requesting the umask without temporarily modifying it.
352 * Note that this does not work if the sysctl
353 * security.bsd.unprivileged_proc_debug is set to 0.
356 if (sysctl((int[4]){ CTL_KERN, KERN_PROC, KERN_PROC_UMASK, 0 },
357 4, &smask, &len, NULL, 0) == 0)
361 * Since it's possible that the caller is opening files inside a signal
362 * handler, protect them as best we can.
365 (void)__libc_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
366 (void)umask(mask = umask(0));
367 (void)__libc_sigprocmask(SIG_SETMASK, &sigoset, NULL);
372 addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
377 set->bits = who ? who : STANDARD_BITS;
386 set->bits = (who ? who : mask) & oparg;
394 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
395 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
396 ((who & S_IROTH) ? CMD2_OBITS : 0);
397 set->bits = (mode_t)~0;
399 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
404 set->cmd2 |= CMD2_SET;
405 else if (oparg == '-')
406 set->cmd2 |= CMD2_CLR;
407 else if (oparg == '=')
408 set->cmd2 |= CMD2_SET|CMD2_CLR;
416 dumpmode(BITCMD *set)
418 for (; set->cmd; ++set)
419 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
420 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
421 set->cmd2 & CMD2_CLR ? " CLR" : "",
422 set->cmd2 & CMD2_SET ? " SET" : "",
423 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
424 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
425 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
430 * Given an array of bitcmd structures, compress by compacting consecutive
431 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
432 * 'g' and 'o' commands continue to be separate. They could probably be
433 * compacted, but it's not worth the effort.
436 compress_mode(BITCMD *set)
439 int setbits, clrbits, Xbits, op;
442 /* Copy over any 'u', 'g' and 'o' commands. */
443 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
449 for (setbits = clrbits = Xbits = 0;; nset++) {
450 if ((op = nset->cmd) == '-') {
451 clrbits |= nset->bits;
452 setbits &= ~nset->bits;
453 Xbits &= ~nset->bits;
454 } else if (op == '+') {
455 setbits |= nset->bits;
456 clrbits &= ~nset->bits;
457 Xbits &= ~nset->bits;
458 } else if (op == 'X')
459 Xbits |= nset->bits & ~setbits;