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 <sys/cdefs.h>
36 __SCCSID("@(#)setmode.c 8.2 (Berkeley) 3/25/94");
37 __FBSDID("$FreeBSD$");
39 #include "namespace.h"
40 #include <sys/types.h>
42 #include <sys/sysctl.h>
55 #include "un-namespace.h"
56 #include "libc_private.h"
58 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
59 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
61 typedef struct bitcmd {
69 #define CMD2_GBITS 0x04
70 #define CMD2_OBITS 0x08
71 #define CMD2_UBITS 0x10
73 static mode_t getumask(void);
74 static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
75 static void compress_mode(BITCMD *);
77 static void dumpmode(BITCMD *);
81 * Given the old mode and an array of bitcmd structures, apply the operations
82 * described in the bitcmd structures to the old mode, and return the new mode.
83 * Note that there is no '=' command; a strict assignment is just a '-' (clear
84 * bits) followed by a '+' (set bits).
87 getmode(const void *bbox, mode_t omode)
90 mode_t clrval, newmode, value;
92 set = (const BITCMD *)bbox;
94 for (value = 0;; set++)
97 * When copying the user, group or other bits around, we "know"
98 * where the bits are in the mode so that we can do shifts to
99 * copy them around. If we don't use shifts, it gets real
100 * grundgy with lots of single bit checks and bit sets.
103 value = (newmode & S_IRWXU) >> 6;
107 value = (newmode & S_IRWXG) >> 3;
111 value = newmode & S_IRWXO;
112 common: if (set->cmd2 & CMD2_CLR) {
114 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
115 if (set->cmd2 & CMD2_UBITS)
116 newmode &= ~((clrval<<6) & set->bits);
117 if (set->cmd2 & CMD2_GBITS)
118 newmode &= ~((clrval<<3) & set->bits);
119 if (set->cmd2 & CMD2_OBITS)
120 newmode &= ~(clrval & set->bits);
122 if (set->cmd2 & CMD2_SET) {
123 if (set->cmd2 & CMD2_UBITS)
124 newmode |= (value<<6) & set->bits;
125 if (set->cmd2 & CMD2_GBITS)
126 newmode |= (value<<3) & set->bits;
127 if (set->cmd2 & CMD2_OBITS)
128 newmode |= value & set->bits;
133 newmode |= set->bits;
137 newmode &= ~set->bits;
141 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
142 newmode |= set->bits;
148 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
154 #define ADDCMD(a, b, c, d) \
155 if (set >= endset) { \
157 setlen += SET_LEN_INCR; \
158 newset = reallocarray(saveset, setlen, sizeof(BITCMD)); \
159 if (newset == NULL) \
161 set = newset + (set - saveset); \
163 endset = newset + (setlen - 2); \
165 set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d))
167 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
170 setmode(const char *p)
174 BITCMD *set, *saveset, *endset;
175 mode_t mask, perm, permXbits, who;
186 * Get a copy of the mask for the permissions that are mask relative.
187 * Flip the bits, we want what's not set.
191 setlen = SET_LEN + 2;
193 if ((set = malloc(setlen * sizeof(BITCMD))) == NULL)
196 endset = set + (setlen - 2);
199 * If an absolute number, get it and return; disallow non-octal digits
202 if (isdigit((unsigned char)*p)) {
204 perml = strtol(p, &ep, 8);
209 if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN))
211 if (perml & ~(STANDARD_BITS|S_ISTXT)) {
215 perm = (mode_t)perml;
216 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
222 * Build list of structures to set/clear/copy bits as described by
223 * each clause of the symbolic mode.
227 /* First, find out which bits might be modified. */
228 for (who = 0;; ++p) {
231 who |= STANDARD_BITS;
234 who |= S_ISUID|S_IRWXU;
237 who |= S_ISGID|S_IRWXG;
247 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
255 for (perm = 0, permXbits = 0;; ++p) {
258 perm |= S_IRUSR|S_IRGRP|S_IROTH;
261 /* If only "other" bits ignore set-id. */
262 if (!who || who & ~S_IRWXO)
263 perm |= S_ISUID|S_ISGID;
266 /* If only "other" bits ignore sticky. */
267 if (!who || who & ~S_IRWXO) {
273 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
276 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
279 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
285 * When ever we hit 'u', 'g', or 'o', we have
286 * to flush out any partial mode that we have,
287 * and then do the copying of the mode bits.
290 ADDCMD(op, who, perm, mask);
295 if (op == '+' && permXbits) {
296 ADDCMD('X', who, permXbits, mask);
299 ADDCMD(*p, who, op, mask);
304 * Add any permissions that we haven't already
307 if (perm || (op == '=' && !equalopdone)) {
310 ADDCMD(op, who, perm, mask);
314 ADDCMD('X', who, permXbits, mask);
329 (void)printf("Before compress_mode()\n");
332 compress_mode(saveset);
334 (void)printf("After compress_mode()\n");
348 sigset_t sigset, sigoset;
354 * First try requesting the umask without temporarily modifying it.
355 * Note that this does not work if the sysctl
356 * security.bsd.unprivileged_proc_debug is set to 0.
359 if (sysctl((int[4]){ CTL_KERN, KERN_PROC, KERN_PROC_UMASK, 0 },
360 4, &smask, &len, NULL, 0) == 0)
364 * Since it's possible that the caller is opening files inside a signal
365 * handler, protect them as best we can.
368 (void)__libc_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
369 (void)umask(mask = umask(0));
370 (void)__libc_sigprocmask(SIG_SETMASK, &sigoset, NULL);
375 addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
380 set->bits = who ? who : STANDARD_BITS;
389 set->bits = (who ? who : mask) & oparg;
397 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
398 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
399 ((who & S_IROTH) ? CMD2_OBITS : 0);
400 set->bits = (mode_t)~0;
402 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
407 set->cmd2 |= CMD2_SET;
408 else if (oparg == '-')
409 set->cmd2 |= CMD2_CLR;
410 else if (oparg == '=')
411 set->cmd2 |= CMD2_SET|CMD2_CLR;
419 dumpmode(BITCMD *set)
421 for (; set->cmd; ++set)
422 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
423 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
424 set->cmd2 & CMD2_CLR ? " CLR" : "",
425 set->cmd2 & CMD2_SET ? " SET" : "",
426 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
427 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
428 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
433 * Given an array of bitcmd structures, compress by compacting consecutive
434 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
435 * 'g' and 'o' commands continue to be separate. They could probably be
436 * compacted, but it's not worth the effort.
439 compress_mode(BITCMD *set)
442 int setbits, clrbits, Xbits, op;
445 /* Copy over any 'u', 'g' and 'o' commands. */
446 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
452 for (setbits = clrbits = Xbits = 0;; nset++) {
453 if ((op = nset->cmd) == '-') {
454 clrbits |= nset->bits;
455 setbits &= ~nset->bits;
456 Xbits &= ~nset->bits;
457 } else if (op == '+') {
458 setbits |= nset->bits;
459 clrbits &= ~nset->bits;
460 Xbits &= ~nset->bits;
461 } else if (op == 'X')
462 Xbits |= nset->bits & ~setbits;