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

EPERM when deleting the stage2 file when launched with sudo #7

Open
vfsfitvnm opened this issue Jan 21, 2021 · 2 comments
Open

EPERM when deleting the stage2 file when launched with sudo #7

vfsfitvnm opened this issue Jan 21, 2021 · 2 comments

Comments

@vfsfitvnm
Copy link

For obvious reasons, the stage2*.bin file will be created as root when launching the script with sudo. In this case, the shellcode (that will run as non privileged user) won't be able to delete the stage2*.bin file (EPERM).

I guess that the file should be chowned so the owner becomes the non-root user.

A quick fix is adding this snippet as soon as the file is created.

if os.getuid() == 0:
    uid = int(os.environ.get("SUDO_UID"))
    guid = int(os.environ.get("SUDO_GID"))
    os.chown(stage2_path, uid, guid)
@DavidBuchanan314
Copy link
Owner

The target process may be owned by a different user altogether, so your proposed fix would not work in that case. I think we should read the uid of the target process via /proc/<PID>/loginuid, and leave the gid unchanged. I'll implement this at some point soon™

@vfsfitvnm
Copy link
Author

The target process may be owned by a different user altogether, so your proposed fix would not work in that case. I think we should read the uid of the target process via /proc/<PID>/loginuid, and leave the gid unchanged. I'll implement this at some point soon™

You are correct, I didn't realize that.
However, would you consider parsing /proc/<PID>/status instead of loginuid? It seems to be a more portable solution (loginuid does not work for daemons and it's not present on Android - not much relevant I guess).

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

No branches or pull requests

2 participants