-
Notifications
You must be signed in to change notification settings - Fork 18
proc allow root and fakeroot guests to switch to any UID/GID #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| */ | ||
|
|
||
| #include <sys/types.h> /* gid_t */ | ||
| #include <sys/wait.h> /* waitpid */ | ||
| #include <unistd.h> /* getgroups */ | ||
|
|
||
| #include "test-harness.h" | ||
|
|
@@ -67,14 +68,22 @@ int main(void) | |
| TEST("setuid(expected) succeeds"); | ||
| EXPECT_TRUE(raw_syscall1(__NR_setuid, expected_id) == 0, "setuid failed"); | ||
|
|
||
| /* setuid: setting to arbitrary value must fail with -EPERM */ | ||
| TEST("setuid(other) returns -EPERM"); | ||
| /* setuid: setting to arbitrary value must fail with -EPERM for non-root */ | ||
| TEST("setuid(other) returns expected status"); | ||
| { | ||
| long rc = raw_syscall1(__NR_setuid, expected_id == 0 ? 1000 : 0); | ||
| if (rc == -1) /* -EPERM */ | ||
| PASS(); | ||
| pid_t pid = fork(); | ||
| if (pid == 0) { | ||
| long rc = raw_syscall1(__NR_setuid, expected_id == 0 ? 1000 : 0); | ||
| _exit(rc == 0 ? 0 : 1); | ||
| } | ||
| int status; | ||
| waitpid(pid, &status, 0); | ||
| if (expected_id == 0) | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, | ||
| "setuid(1000) failed for root"); | ||
| else | ||
| FAIL("expected -EPERM"); | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 1, | ||
| "expected -EPERM"); | ||
| } | ||
|
|
||
| /* setresuid: swap euid (no-op but must succeed) */ | ||
|
|
@@ -86,15 +95,23 @@ int main(void) | |
| } | ||
|
|
||
| /* setresuid: set euid to a value not in {ruid, euid, suid} */ | ||
| TEST("setresuid(-1,other,-1) returns -EPERM"); | ||
| TEST("setresuid(-1,other,-1) returns expected status"); | ||
| { | ||
| long rc = | ||
| raw_syscall3(__NR_setresuid, (long) (unsigned) -1, | ||
| expected_id == 0 ? 1000 : 42, (long) (unsigned) -1); | ||
| if (rc == -1) /* -EPERM */ | ||
| PASS(); | ||
| pid_t pid = fork(); | ||
| if (pid == 0) { | ||
| long rc = raw_syscall3(__NR_setresuid, (long) (unsigned) -1, | ||
| expected_id == 0 ? 1000 : 42, | ||
| (long) (unsigned) -1); | ||
| _exit(rc == 0 ? 0 : 1); | ||
| } | ||
| int status; | ||
| waitpid(pid, &status, 0); | ||
| if (expected_id == 0) | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, | ||
| "setresuid failed for root"); | ||
| else | ||
| FAIL("expected -EPERM"); | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 1, | ||
| "expected -EPERM"); | ||
| } | ||
|
|
||
| /* setreuid: ruid can only be set to current ruid or euid */ | ||
|
|
@@ -105,14 +122,22 @@ int main(void) | |
| EXPECT_TRUE(rc == 0, "setreuid(ruid,-1) failed"); | ||
| } | ||
|
|
||
| TEST("setreuid(other,-1) returns -EPERM"); | ||
| TEST("setreuid(other,-1) returns expected status"); | ||
| { | ||
| long rc = raw_syscall2(__NR_setreuid, expected_id == 0 ? 1000 : 42, | ||
| (long) (unsigned) -1); | ||
| if (rc == -1) /* -EPERM */ | ||
| PASS(); | ||
| pid_t pid = fork(); | ||
| if (pid == 0) { | ||
| long rc = raw_syscall2(__NR_setreuid, expected_id == 0 ? 1000 : 42, | ||
| (long) (unsigned) -1); | ||
| _exit(rc == 0 ? 0 : 1); | ||
| } | ||
| int status; | ||
| waitpid(pid, &status, 0); | ||
| if (expected_id == 0) | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, | ||
| "setreuid failed for root"); | ||
| else | ||
| FAIL("expected -EPERM"); | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 1, | ||
| "expected -EPERM"); | ||
| } | ||
|
|
||
| /* getresgid */ | ||
|
|
@@ -130,9 +155,22 @@ int main(void) | |
| TEST("setgid(expected) succeeds"); | ||
| EXPECT_TRUE(raw_syscall1(__NR_setgid, expected_id) == 0, "setgid failed"); | ||
|
|
||
| TEST("setgid(other) returns -EPERM"); | ||
| EXPECT_TRUE(raw_syscall1(__NR_setgid, expected_id == 0 ? 1000 : 0) == -1, | ||
| "expected -EPERM"); | ||
| TEST("setgid(other) returns expected status"); | ||
| { | ||
| pid_t pid = fork(); | ||
| if (pid == 0) { | ||
| long rc = raw_syscall1(__NR_setgid, expected_id == 0 ? 1000 : 0); | ||
| _exit(rc == 0 ? 0 : 1); | ||
| } | ||
| int status; | ||
| waitpid(pid, &status, 0); | ||
| if (expected_id == 0) | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, | ||
| "setgid(1000) failed for root"); | ||
| else | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 1, | ||
| "expected -EPERM"); | ||
| } | ||
|
|
||
| /* setfsuid / setfsgid: Linux contract is to return the previous fsuid / | ||
| * fsgid. | ||
|
|
@@ -238,6 +276,42 @@ int main(void) | |
| EXPECT_TRUE(rc > 0 && (mask & 1), "CPU0 not in mask"); | ||
| } | ||
|
|
||
| if (expected_id == 0) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These root-only assertions never run in CI: test-credentials isn't in tests/manifest.txt (not part of make check), test-matrix.sh invokes it without --fakeroot (default guest UID 1000), and it's listed in QEMU_SKIP for the real-root qemu lane — so expected_id == 0 is unreachable everywhere. Running it manually with --fakeroot fails 7/27 cases: the earlier setuid(other)/setgid(other) root-branch calls (lines 73/156) now permanently drop identity to 1000 per this PR's own "reset all three fields" fix, but the later setfsuid/setfsgid/getgroups assertions still assume the stale expected_id == 0 identity. Please wire --fakeroot into a test runner so this doesn't silently bit-rot, and fix the stale-identity assumption in those later checks.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
| TEST("privileged setreuid(1000, -1) sets real ID"); | ||
| { | ||
| pid_t pid = fork(); | ||
| if (pid == 0) { | ||
| long rc = | ||
| raw_syscall2(__NR_setreuid, 1000, (long) (unsigned) -1); | ||
| _exit(rc == 0 ? 0 : 1); | ||
| } | ||
| int status; | ||
| waitpid(pid, &status, 0); | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, | ||
| "setreuid(1000, -1) failed"); | ||
| } | ||
|
|
||
| TEST("privileged setuid(1000) resets ruid/euid/suid"); | ||
| { | ||
| pid_t pid = fork(); | ||
| if (pid == 0) { | ||
| long rc = raw_syscall1(__NR_setuid, 1000); | ||
| if (rc != 0) | ||
| _exit(1); | ||
| unsigned int ruid = 0, euid = 0, suid = 0; | ||
| long grc = raw_syscall3(__NR_getresuid, (long) &ruid, | ||
| (long) &euid, (long) &suid); | ||
| if (grc == 0 && ruid == 1000 && euid == 1000 && suid == 1000) | ||
| _exit(0); | ||
| _exit(2); | ||
| } | ||
| int status; | ||
| waitpid(pid, &status, 0); | ||
| EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, | ||
| "uid triple not fully updated"); | ||
| } | ||
| } | ||
|
|
||
| SUMMARY("test-credentials"); | ||
| return fails ? 1 : 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.