Skip to content

Remove realpath/getcwd from loaders #1024

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

Merged
merged 10 commits into from
Dec 18, 2023
Merged

Conversation

mrdomino
Copy link
Collaborator

@mrdomino mrdomino commented Dec 17, 2023

This implements proposals 1 and 2a from this gist:

https://gist.github.com/mrdomino/2222cab61715fd527e82e036ba4156b1

The only reason to use realpath from the loader was to try to prevent a
TOCTOU between the loader and the binary. But this is only a real issue
in set-id contexts, and in those cases there is already a canonical way
to do it: /dev/fd, passed by the kernel to the loader, so all we have
to do is pass that along to the binary.

Aside from realpath, there is no reason to absolutize the path we supply
to the binary, since it can call getcwd as well as we can, and on non-
M1 the binary is in a much better position to make that call.

Since we no longer absolutize the path, the binary does need to do this,
so we make its argv-parsing code generic and apply that to the different
possible places the path could come from. This means that _ is finally
usable as a relative path, as a nice side benefit.

The M1 realpath code had a significant bug - it uses the wrong offset to
truncate the .ape in the $prog.ape case.

This PR also fixes a regression in ape $progname out of $PATH on the
two BSDs (Free and Net) that did not implement RealPath.

Noticed a bug in the process, pathlen is out of step with n
(Do not merge without GetProgramExecutableName change)
TryPath with com==1 tries both non-com and com, so the checks were
redundant.

With com==0, there is no reason to copy to the buffer if the initial
access fails.
Merges branches 'm1-realpath', 'loader-realpath' and
'getprogramexecutablename-realpath' into no-realpath
The loader implementing it was never minted, so it seems reasonable not
to keep support for it indefinitely. But it seems impolite to break the
prerelease loaders people may have been using between releases.
Copy link
Collaborator Author

@mrdomino mrdomino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI Github does not render the three-way merge in 0734b9c correctly. That commit should show as empty; it only pulls in the commits before it and makes no changes itself.

If we got __program_executable_name, it means the /proc-esque methods
are going to return the path of the loader rather than the path of the
binary, so do not use them in this case.

If the passed path is absolute, use it as-is. Otherwise, copy it into
the buffer with getcwd prepended. The sys_faccessat call should be
redundant; if TryPath fails, it probably failed because getcwd() was too
long, so just use the relative path and accept that chdir will break
things.

On that note, TryPath now fails prior to the sys_faccessat if getcwd()
is too long, and otherwise behaves the way the code used to. The version
I wrote before this change would have always succeeded on a truncated
path assuming the relative path could have been accessed. The original
version would have succeeded on a truncated path if there happened to be
a file at that path that could be accessed.

This change also backs out the setugid loader check for /dev/fd path.
For now, we fail open; if for some reason we are invoked set-id by the
loader, we just assume that the user knows what they are doing and do
not attempt to second-guess them, beyond politely declining to trust
argv[0] or the environment.
@mrdomino mrdomino merged commit 2a11a09 into jart:master Dec 18, 2023
@mrdomino mrdomino deleted the no-realpath branch December 18, 2023 20:01
mrdomino added a commit to mrdomino/cosmopolitan that referenced this pull request Dec 22, 2023
Returns to passing os as `%rcx`, stack pointer as `%rdx` as it was
before jart#1024. exe is now passed as `%r8`, and is moved to `%rdx`.

os is passed on aarch64, from both loader.c and ape-m1.c. It is
currently unused.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants