-
-
Notifications
You must be signed in to change notification settings - Fork 645
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
Loader path security #1012
Merged
Merged
Loader path security #1012
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The M1 loader now passes the program executable name directly as x2, which is the third argument to `cosmo()`. From there it is assigned to `__myname`. `GetProgramExecutableName` now always returns `__myname`, and it is `InitProgramExecutableName`’s job to set that variable if it is not already assigned. `InitProgramExecutableName` now dies if `issetugid()` and `__myname` is null, pending a rework of the path-finding logic.
It prints its own name to stderr, not stdout. It also prints `GetProgramExecutableName()` for further debugging convenience.
mrdomino
commented
Dec 15, 2023
mrdomino
changed the title
M1 program name register
M1 loader passes program name via register
Dec 15, 2023
Closed
- Tries `KERN_PROC_PATHNAME` first on NetBSD/FreeBSD. - Tries procfs first, but now only on Linux. (You can have /proc on FreeBSD, but then again, /proc could be anything on non-Linux. An option here is to put non-Linux /proc after the issetugid check.) - If `issetugid()` and none of the secure methods have worked, sets the empty string and returns.
jart
approved these changes
Dec 15, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Thanks!
mrdomino
changed the title
M1 loader passes program name via register
Loader path security
Dec 15, 2023
mrdomino
force-pushed
the
m1-register
branch
2 times, most recently
from
December 15, 2023 16:33
5246a1b
to
173c9aa
Compare
mrdomino
force-pushed
the
m1-register
branch
from
December 15, 2023 16:35
173c9aa
to
7da74f5
Compare
mrdomino
force-pushed
the
m1-register
branch
from
December 15, 2023 17:11
7da74f5
to
133e399
Compare
Using `COSMOPOLITAN_PROGRAM_EXECUTABLE` without a `sys_faccessat` was rightly causing test failures. Now that it is no longer used by the loader, there seems little value in prioritizing it over argv[0], so just lump it in with "_".
mrdomino
force-pushed
the
m1-register
branch
from
December 15, 2023 17:12
133e399
to
42bf4d2
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The ape loader now passes the program executable name directly as a register.
x2
is used on aarch64,%rdx
on x86_64. This is passed as the third argument tocosmo()
(M1) orLaunch
(non-M1) and is assigned to the global__program_executable_name
.GetProgramExecutableName
now returns this global's value, setting it if it is initially null.InitProgramExecutableName
first tries exotic, secure methods:KERN_PROC_PATHNAME
on FreeBSD/NetBSD, and/proc
on Linux. If those produce a reasonable response (i.e., not"/usr/bin/ape"
, which happens with the loader before this change), that is used. Otherwise, ifissetugid()
, the empty string is used. Otherwise, the old argv/envp parsing code is run.The value returned from the loader is always the full absolute path of the binary to be executed, having passed through
realpath
. For the non-M1 loader, this necessitated writingRealPath
, which usesreadlinkat
of"/proc/self/fd/[progfd]"
on Linux,F_GETPATH
on Xnu, and the__realpath
syscall on OpenBSD. On FreeBSD/NetBSD, it punts toGetProgramExecutableName
, which is secure on those OSes.With the loader, all platforms now have a secure program executable name. With no loader or an old loader, everything still works as it did, but setuid/setgid is not supported if the insecure pathfinding code would have been needed.
Fixes #991.