Permission/setuid/setgid handling
cover Permission/setuid/setgid handling
Victor Kamensky noticed and submitted a change where a file could have +s defined and this would be set in the real filesystem. This potentially opens up a condition where a script or executable could be run as a specific user's uid or gid which could lead to unintended consequences.
Victor's change filters out setuid/setgid from the on-disk permissions.
I the course of working on this issue (including writing test cases), it was observed that there was an existing bug where if the user did a mkdir followed by a +s on the directory the Linux kernel would prevent the +s mode. This had the side effect of the +s not being present when the directory was later scanned. (+s is only allowed on root owned directories.) This bug has never been reported but is now fixed as well.
This lead to the case where we also updated the documentation in the code to make it more clear what PSEUDO_DB_MODE and PSEUDO_FS_MODE do and how they need to be kept in sync.
These changes have been pushed to pseudo master branch.
Mark Hatle (5):
Makefile.in: Add pseudo_client.h as a dependency pseudo_client.h: Make it clear both macros must be updated together
tests: Add test that returned stat is correct pseudo_client.h: Add +s to PSEUDO_DB_MODE for mkdir
tests: Add setuid permission check
Victor Kamensky (1):
client: permissions drop setuid and setgid
Makefile.in | 2 +- pseudo_client.h | 13 ++++- test/test-db-mode.c | 117 ++++++++++++++++++++++++++++++++++++++++ test/test-db-mode.sh | 10 ++++ test/test-setuid-permissions.sh | 58 ++++++++++++++++++++ 5 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 test/test-db-mode.c create mode 100755 test/test-db-mode.sh create mode 100755 test/test-setuid-permissions.sh
Signature · 1 line
1.8.3.1
PATCH 1/6 Makefile.in: Add pseudo_client.h as a dependency
From: Mark Hatle <[email protected]>
Updating parts of the .h did not trigger a rebuild.
--- Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile.in +1 -1
@@ -158,7 +158,7 @@ pseudo_tables.o: pseudo_tables.c $(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -c -o pseudo_tables.o pseudo_tables.c # no-strict-aliasing is needed for the function pointer trickery. -pseudo_wrappers.o: $(GUTS) pseudo_wrappers.c pseudo_wrapfuncs.c pseudo_wrapfuncs.h pseudo_tables.h +pseudo_wrappers.o: $(GUTS) pseudo_wrappers.c pseudo_wrapfuncs.c pseudo_wrapfuncs.h pseudo_tables.h pseudo_client.h $(CC) -fno-strict-aliasing $(CFLAGS) $(CFLAGS_PSEUDO) -D_GNU_SOURCE -c -o pseudo_wrappers.o pseudo_wrappers.c offsets32:
Signature · 1 line
1.8.3.1
PATCH 2/6 pseudo_client.h: Make it clear both macros must be updated together
In reply to Mark HatleFrom: Mark Hatle <[email protected]>
Both PSEUDO_FS_MODE and PSEUDO_DB_MODE must be updated together. The first defined the masking for the mode on the disk, while the second filters the masking so we know which modes from the disk can be trusted.
--- pseudo_client.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
pseudo_client.h +4 -1
@@ -87,7 +87,10 @@ extern int read_pidfile(FILE *, int *);* When doing anything which actually writes to the filesystem, we add in * the user read/write/execute bits. When storing to the database, though, * we mask out any such bits which weren't in the original mode. +* +* Note: PSEUDO_DB_MODE must be kept in sync with PSEUDO_FS_MODE, as the +* former defined which filesystem mode bits must be loaded from the DB. */ #define PSEUDO_FS_MODE (mode, isdir) (((mode) | S_IRUSR | S_IWUSR | ((isdir) ? S_IXUSR : 0)) & ~(S_IWGRP | S_IWOTH)) -#define PSEUDO_DB_MODE (fs_mode, user_mode) (((fs_mode) & ~0722) | ((user_mode & 0722))) +#define PSEUDO_DB_MODE (fs_mode, user_mode) (((fs_mode) & ~(S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH)) | ((user_mode) & (S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH)))
Signature · 1 line
1.8.3.1
PATCH 3/6 tests: Add test that returned stat is correct
In reply to Mark HatleFrom: Mark Hatle <[email protected]>
There are concerns that if PSEUDO_FS_MODE and PSEUDO_DB_MODE get out of sync with each other that invalid modules could be returned in some cases.
AI-Generated: Implemented with the assistance of github CoPilot (Claude Opus 4.6)
--- test/test-db-mode.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/test-db-mode.sh | 10 +++++ 2 files changed, 127 insertions(+) create mode 100644 test/test-db-mode.c create mode 100755 test/test-db-mode.sh
test/test-db-mode.c new file +117 -0
@@ -0,0 +1,117 @@ +/* +* Test that PSEUDO_DB_MODE correctly reconstructs permissions at file +* creation time. PSEUDO_DB_MODE is used when a file is created (open +* with O_CREAT, mkdir, etc.) to recover the intended mode from the +* filesystem mode (which has been mangled by PSEUDO_FS_MODE). +* +* If PSEUDO_DB_MODE is broken, the mode stored in the database at +* creation time will be wrong, which we detect by stat'ing immediately +* after creation (before any chmod). +* +* SPDX-License-Identifier: LGPL-2.1-only +*/ + +#include <sys/stat.h> +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> + +static int test_creat_mode (mode_t mode) { +const char *path ="test-db-mode-tmp" ; +struct stat st; + +/* Remove any prior file */ +unlink (path); + +/* Clear umask so it doesn't interfere */ +mode_t old_umask =umask (0 ); + +int fd =open (path,O_CREAT |O_WRONLY |O_EXCL , mode); +if (fd <0 ) { +perror ("open" ); +umask (old_umask); +return 1 ; + } +close (fd); + +if (stat (path, &st) !=0 ) { +perror ("stat" ); +unlink (path); +umask (old_umask); +return 1 ; + } + +mode_t got = st.st_mode &07777 ; +unlink (path); +umask (old_umask); + +if (got != mode) { +fprintf (stderr,"FAIL: open(O_CREAT, 0%04o) -> stat 0%04o\n" , mode, got); +return 1 ; + } +return 0 ; +} + +static int test_mkdir_mode (mode_t mode) { +const char *path ="test-db-mode-dir-tmp" ; +struct stat st; + +rmdir (path); + +mode_t old_umask =umask (0 ); + +if (mkdir (path, mode) !=0 ) { +perror ("mkdir" ); +umask (old_umask); +return 1 ; + } + +if (stat (path, &st) !=0 ) { +perror ("stat" ); +rmdir (path); +umask (old_umask); +return 1 ; + } + +mode_t got = st.st_mode &07777 ; +rmdir (path); +umask (old_umask); + +if (got != mode) { +fprintf (stderr,"FAIL: mkdir(0%04o) -> stat 0%04o\n" , mode, got); +return 1 ; + } +return 0 ; +} + +int main (void ) { +int failures =0 ; +int total =0 ; + +/* Test all octet values simultaneously: 00000, 01111, 02222, ..., 07777 */ +mode_t modes[] = { +00000 ,01111 ,02222 ,03333 ,04444 ,05555 ,06666 ,07777 , + }; +int num_modes =sizeof (modes) /sizeof (modes[0 ]); + +for (int i =0 ; i < num_modes; i++) { + total++; +if (test_creat_mode (modes[i]) !=0 ) + failures++; + } + +for (int i =0 ; i < num_modes; i++) { + total++; +if (test_mkdir_mode (modes[i]) !=0 ) + failures++; + } + +if (failures >0 ) { +fprintf (stderr,"%d/%d mode tests failed\n" , failures, total); +return 2 ; + } +return 0 ; +}
test/test-db-mode.sh new file +10 -0
@@ -0,0 +1,10 @@ +#!/bin/bash +# +# SPDX-License-Identifier: LGPL-2.1-only +# +# Test that PSEUDO_DB_MODE correctly reconstructs permission bits +# at file/directory creation time. + +trap "rm -rf test-db-mode-tmp test-db-mode-dir-tmp" EXIT + +./test/test-db-mode
Signature · 1 line
1.8.3.1
PATCH 4/6 pseudo_client.h: Add +s to PSEUDO_DB_MODE for mkdir
In reply to Mark HatleFrom: Mark Hatle <[email protected]>
During a mkdir the kernel will automatically filter out S_ISUID and S_ISGID for non-root users.
This would result in an emulated ug+s on a directory losing the +s.
This resolves the issue, and we need to remember to account for it in the future.
AI-Generated: Implemented with the assistance of github CoPilot (Claude Opus 4.6)
--- pseudo_client.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
pseudo_client.h +4 -1
@@ -90,7 +90,10 @@ extern int read_pidfile(FILE *, int *);* * Note: PSEUDO_DB_MODE must be kept in sync with PSEUDO_FS_MODE, as the * former defined which filesystem mode bits must be loaded from the DB. +* +* Note: S_ISUID and S_ISGID may be stripped during a mkdir as a user, +* account for this in PSEUDO_DB_MODE. */ #define PSEUDO_FS_MODE (mode, isdir) (((mode) | S_IRUSR | S_IWUSR | ((isdir) ? S_IXUSR : 0)) & ~(S_IWGRP | S_IWOTH)) -#define PSEUDO_DB_MODE (fs_mode, user_mode) (((fs_mode) & ~(S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH)) | ((user_mode) & (S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH))) +#define PSEUDO_DB_MODE (fs_mode, user_mode) (((fs_mode) & ~(S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH | S_ISUID | S_ISGID)) | ((user_mode) & (S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH | S_ISUID | S_ISGID)))
Signature · 1 line
1.8.3.1
PATCH 5/6 tests: Add setuid permission check
In reply to Mark HatleFrom: Mark Hatle <[email protected]>
Verify that +s permissions are not leaking into real filesystem.
AI-Generated: Implemented with the assistance of github CoPilot (Claude Opus 4.6)
--- test/test-setuid-permissions.sh | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 test/test-setuid-permissions.sh
test/test-setuid-permissions.sh new file +58 -0
@@ -0,0 +1,58 @@ +#!/bin/bash +# +# SPDX-License-Identifier: LGPL-2.1-only +# +set -e + +# Verify that setuid/setgid bits tracked by pseudo do not +# bleed into the real filesystem. +# +# Return vals: +# +# 2 - Setuid/setgid bits found on real file +# 1 - Unexpected command error +# 0 - Pass + +mode() { + stat -c "%a" "$1" +} + +trap "rm -f testfile" EXIT + +test_mode() { + local octal_mode="$1" + local expected_pseudo="$2" + local expected_real="$3" + + chmod $octal_mode testfile + + # Under pseudo, verify mode is as requested + local pseudo_mode=$(mode testfile) + if [ "$pseudo_mode" != "$expected_pseudo" ]; then + echo "FAIL: pseudo mode $pseudo_mode != expected $expected_pseudo (chmod $octal_mode)" + exit 1 + fi + + # Check without pseudo - real file must NOT have setuid/setgid + local real_mode=$(PSEUDO_DISABLED=1 stat -c "%a" testfile) + if [ "$real_mode" != "$expected_real" ]; then + echo "FAIL: real mode $real_mode != expected $expected_real (chmod $octal_mode)" + exit 2 + fi +} + +touch testfile + +# Test setuid only (4755) +test_mode 4755 4755 755 + +# Test setgid only (2755) +test_mode 2755 2755 755 + +# Test setuid + setgid (6755) +test_mode 6755 6755 755 + +# Test setuid + setgid + other base perms (6644) +test_mode 6644 6644 644 + +exit 0
Signature · 1 line
1.8.3.1
PATCH 6/6 client: permissions drop setuid and setgid
In reply to Mark HatleFrom: Victor Kamensky <[email protected]>
Otherwise, in some situation, where code running under pseudo set setuid and setgit permission bits, they bleed into underlying files, and may result with executables that have setuid and setgid bit(s) on on real files that are owned by user who run pseudo.
Added comment to indicate why we also filter setuid/setgid.
--- pseudo_client.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
pseudo_client.h +4 -1
@@ -88,12 +88,15 @@ extern int read_pidfile(FILE *, int *);* the user read/write/execute bits. When storing to the database, though, * we mask out any such bits which weren't in the original mode. * +* Additional setuid/setgid modes are also filteed as they are unsafe as +* a real executable may allow someone to setuid/gid as the running user. +* * Note: PSEUDO_DB_MODE must be kept in sync with PSEUDO_FS_MODE, as the * former defined which filesystem mode bits must be loaded from the DB. * * Note: S_ISUID and S_ISGID may be stripped during a mkdir as a user, * account for this in PSEUDO_DB_MODE. */ -#define PSEUDO_FS_MODE (mode, isdir) (((mode) | S_IRUSR | S_IWUSR | ((isdir) ? S_IXUSR : 0)) & ~(S_IWGRP | S_IWOTH)) +#define PSEUDO_FS_MODE (mode, isdir) (((mode) | S_IRUSR | S_IWUSR | ((isdir) ? S_IXUSR : 0)) & ~(S_IWGRP | S_IWOTH | S_ISUID | S_ISGID))#define PSEUDO_DB_MODE (fs_mode, user_mode) (((fs_mode) & ~(S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH | S_ISUID | S_ISGID)) | ((user_mode) & (S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IWOTH | S_ISUID | S_ISGID)))
Signature · 1 line
1.8.3.1