Skip to content
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

sanity-check set-id interpreter script paths #1029

Merged
merged 9 commits into from
Dec 31, 2023
Merged

Conversation

mrdomino
Copy link
Collaborator

@mrdomino mrdomino commented Dec 18, 2023

This actually implements proposal 2a, rolled back from #1024, of this:

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

From the first commit:

There is an argument to be made for trapping in [a] constructor here,
and there is also an argument to be made for failing open like we did
previously. We pick a middle ground: GetProgramExecutableName returns
empty string. Logic being that ensuring the sanity of whatever set-id
script mechanism user is running is above our pay grade, but the path
TOCTOU between loader and binary is not.

The check is fairly stringent; we do it in a constructor, and we make
sure that there actually is an open fd higher than 2 that matches the
passed path.

There is an argument to be made for trapping in the constructor here,
and there is also an argument to be made for failing open like we did
previously. We pick a middle ground: GetProgramExecutableName returns
empty string. Logic being that ensuring the sanity of whatever set-id
script mechanism user is running is above our pay grade, but the path
TOCTOU between loader and binary is not.
By the logic of the previous commit, it's not our job to verify that the
user's whacked out set-id configuration is sane, only that the path they
passed does not refer to a different file now than it did when ape found
it. This is generally true of the files under /dev/fd in our case.

So we just check that the file is in /dev/fd/, that it starts with not a
'.', and that it does not contain a '/'.

Since we don't care whether there actually is an fd by that name, we can
do the check when called and save a constructor.
Again, the details of the system's set-id interpreter script setup are
above our pay grade; we only care whether the path can vary between us
and the loader.
Blank line needed to prevent clang-format from making `DevFd` more than
80 characters long. Possible clang-format bug?
@mrdomino
Copy link
Collaborator Author

Tested on M1 Mac as so:

% chmod u+s /usr/local/bin/ape
% chown root /usr/local/bin/ape
% ape - o/aarch64/examples/printargs.com printargs.com
[… GetProgramExecutableName = "" …]
% ape - /dev/fd/3 printargs.com 3<o/aarch64/examples/printargs.com
[… GetProgramExecutableName = "/dev/fd/3" …]

And obviously confirmed that things like /dev/fd/../../path/to/printargs.com do not break it.

Copy link
Owner

@jart jart left a comment

Choose a reason for hiding this comment

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

Looks good. Passes fleet. Thanks for using clang-format.

@jart jart merged commit 10b4693 into jart:master Dec 31, 2023
5 checks passed
@mrdomino mrdomino deleted the setugid-check branch December 31, 2023 16:05
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