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 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "namespace.h"
42 #include <sys/types.h>
44 #include <sys/sysctl.h>
57 #include "un-namespace.h"
58 #include "libc_private.h"
60 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
61 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
63 typedef struct bitcmd {
71 #define CMD2_GBITS 0x04
72 #define CMD2_OBITS 0x08
73 #define CMD2_UBITS 0x10
75 static mode_t getumask(void);
76 static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
77 static void compress_mode(BITCMD *);
79 static void dumpmode(BITCMD *);
83 * Given the old mode and an array of bitcmd structures, apply the operations
84 * described in the bitcmd structures to the old mode, and return the new mode.
85 * Note that there is no '=' command; a strict assignment is just a '-' (clear
86 * bits) followed by a '+' (set bits).
89 getmode(const void *bbox, mode_t omode)
92 mode_t clrval, newmode, value;
94 set = (const BITCMD *)bbox;
96 for (value = 0;; set++)
99 * When copying the user, group or other bits around, we "know"
100 * where the bits are in the mode so that we can do shifts to
101 * copy them around. If we don't use shifts, it gets real
102 * grundgy with lots of single bit checks and bit sets.
105 value = (newmode & S_IRWXU) >> 6;
109 value = (newmode & S_IRWXG) >> 3;
113 value = newmode & S_IRWXO;
114 common: if (set->cmd2 & CMD2_CLR) {
116 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
117 if (set->cmd2 & CMD2_UBITS)
118 newmode &= ~((clrval<<6) & set->bits);
119 if (set->cmd2 & CMD2_GBITS)
120 newmode &= ~((clrval<<3) & set->bits);
121 if (set->cmd2 & CMD2_OBITS)
122 newmode &= ~(clrval & set->bits);
124 if (set->cmd2 & CMD2_SET) {
125 if (set->cmd2 & CMD2_UBITS)
126 newmode |= (value<<6) & set->bits;
127 if (set->cmd2 & CMD2_GBITS)
128 newmode |= (value<<3) & set->bits;
129 if (set->cmd2 & CMD2_OBITS)
130 newmode |= value & set->bits;
135 newmode |= set->bits;
139 newmode &= ~set->bits;
143 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
144 newmode |= set->bits;
150 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
156 #define ADDCMD(a, b, c, d) \
157 if (set >= endset) { \
159 setlen += SET_LEN_INCR; \
160 newset = reallocarray(saveset, setlen, sizeof(BITCMD)); \
161 if (newset == NULL) \
163 set = newset + (set - saveset); \
165 endset = newset + (setlen - 2); \
167 set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d))
169 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
172 setmode(const char *p)
176 BITCMD *set, *saveset, *endset;
177 mode_t mask, perm, permXbits, who;
188 * Get a copy of the mask for the permissions that are mask relative.
189 * Flip the bits, we want what's not set.
193 setlen = SET_LEN + 2;
195 if ((set = malloc(setlen * sizeof(BITCMD))) == NULL)
198 endset = set + (setlen - 2);
201 * If an absolute number, get it and return; disallow non-octal digits
204 if (isdigit((unsigned char)*p)) {
206 perml = strtol(p, &ep, 8);
211 if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN))
213 if (perml & ~(STANDARD_BITS|S_ISTXT)) {
217 perm = (mode_t)perml;
218 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
224 * Build list of structures to set/clear/copy bits as described by
225 * each clause of the symbolic mode.
229 /* First, find out which bits might be modified. */
230 for (who = 0;; ++p) {
233 who |= STANDARD_BITS;
236 who |= S_ISUID|S_IRWXU;
239 who |= S_ISGID|S_IRWXG;
249 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
257 for (perm = 0, permXbits = 0;; ++p) {
260 perm |= S_IRUSR|S_IRGRP|S_IROTH;
263 /* If only "other" bits ignore set-id. */
264 if (!who || who & ~S_IRWXO)
265 perm |= S_ISUID|S_ISGID;
268 /* If only "other" bits ignore sticky. */
269 if (!who || who & ~S_IRWXO) {
275 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
278 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
281 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
287 * When ever we hit 'u', 'g', or 'o', we have
288 * to flush out any partial mode that we have,
289 * and then do the copying of the mode bits.
292 ADDCMD(op, who, perm, mask);
297 if (op == '+' && permXbits) {
298 ADDCMD('X', who, permXbits, mask);
301 ADDCMD(*p, who, op, mask);
306 * Add any permissions that we haven't already
309 if (perm || (op == '=' && !equalopdone)) {
312 ADDCMD(op, who, perm, mask);
316 ADDCMD('X', who, permXbits, mask);
331 (void)printf("Before compress_mode()\n");
334 compress_mode(saveset);
336 (void)printf("After compress_mode()\n");
350 sigset_t sigset, sigoset;
356 * First try requesting the umask without temporarily modifying it.
357 * Note that this does not work if the sysctl
358 * security.bsd.unprivileged_proc_debug is set to 0.
361 if (sysctl((int[4]){ CTL_KERN, KERN_PROC, KERN_PROC_UMASK, 0 },
362 4, &smask, &len, NULL, 0) == 0)
366 * Since it's possible that the caller is opening files inside a signal
367 * handler, protect them as best we can.
370 (void)__libc_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
371 (void)umask(mask = umask(0));
372 (void)__libc_sigprocmask(SIG_SETMASK, &sigoset, NULL);
377 addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
382 set->bits = who ? who : STANDARD_BITS;
391 set->bits = (who ? who : mask) & oparg;
399 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
400 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
401 ((who & S_IROTH) ? CMD2_OBITS : 0);
402 set->bits = (mode_t)~0;
404 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
409 set->cmd2 |= CMD2_SET;
410 else if (oparg == '-')
411 set->cmd2 |= CMD2_CLR;
412 else if (oparg == '=')
413 set->cmd2 |= CMD2_SET|CMD2_CLR;
421 dumpmode(BITCMD *set)
423 for (; set->cmd; ++set)
424 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
425 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
426 set->cmd2 & CMD2_CLR ? " CLR" : "",
427 set->cmd2 & CMD2_SET ? " SET" : "",
428 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
429 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
430 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
435 * Given an array of bitcmd structures, compress by compacting consecutive
436 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
437 * 'g' and 'o' commands continue to be separate. They could probably be
438 * compacted, but it's not worth the effort.
441 compress_mode(BITCMD *set)
444 int setbits, clrbits, Xbits, op;
447 /* Copy over any 'u', 'g' and 'o' commands. */
448 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
454 for (setbits = clrbits = Xbits = 0;; nset++) {
455 if ((op = nset->cmd) == '-') {
456 clrbits |= nset->bits;
457 setbits &= ~nset->bits;
458 Xbits &= ~nset->bits;
459 } else if (op == '+') {
460 setbits |= nset->bits;
461 clrbits &= ~nset->bits;
462 Xbits &= ~nset->bits;
463 } else if (op == 'X')
464 Xbits |= nset->bits & ~setbits;