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

graftcp sudo does not work #11

Closed
hellobbn opened this issue Dec 27, 2018 · 14 comments
Closed

graftcp sudo does not work #11

hellobbn opened this issue Dec 27, 2018 · 14 comments

Comments

@hellobbn
Copy link

Describe the bug
graftcp sudo does not work, so graftcp yay which uses it will cause error.

To Reproduce
Try command graftcp sudo or graftcp yay

sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid' option set or an NFS file system without root privileges?

Additional context
I found some notes here

For security reasons, the setuid bit and ptrace (used to run binaries under a debugger) cannot both be honored at the same time. Failure to enforce this restriction in the past led to CVE-2001-1384.

So maybe this will not be fixed? (I am not familiar with this)

@hmgle
Copy link
Owner

hmgle commented Dec 27, 2018

sudo graftcp sudo will work.

@hmgle
Copy link
Owner

hmgle commented Dec 27, 2018

By the way, if the graftcp-local is not started by the system, you should run sudo ./graftcp-local to use sudo graftcp sudo.

@hellobbn
Copy link
Author

hellobbn commented Dec 27, 2018

Thanks for your reply!
But some programs will fail if executed by root like makepkg in Arch Linux, instead it will request root later.
Can program of this kind be supported?

➜ yay               
[sudo] password for xxx: 

@hmgle
Copy link
Owner

hmgle commented Dec 27, 2018

You can try this:

$ sudo graftcp bash # or zsh, fish...
$ whoami
root
$ yay

As a root, do anything you wanna do 😁

@hellobbn
Copy link
Author

Hmmmm, yes it could work on some programs. But some forces to run as a non-root, if you run it as root, it will give error immediately.

@hmgle
Copy link
Owner

hmgle commented Dec 30, 2018

Do you mean that the root's environment does not match the non-root's environment, so that it cause the environmental dependency error? You can try sudo -E graftcp bash.

@hellobbn
Copy link
Author

Sorry, but I don't understand.
Let me take makepkg as an example:
if run as root:

[root@hellobbn ~]# makepkg
==> ERROR: Running makepkg as root is not allowed as it can cause permanent,
catastrophic damage to your system.

@hmgle
Copy link
Owner

hmgle commented Dec 31, 2018

@hellobbn Thank you for your example.
I thank it can be solved like this:
Do not run the following script, it will elevate the privileges of the tracked process to root, that is risky.

$ sudo chown root:root graftcp
$ sudo chmod a+s graftcp
# Then you can use graftcp to trace sudo...
$ graftcp bashmakepkg

@hmgle hmgle closed this as completed Jan 3, 2019
@bookyue
Copy link

bookyue commented Sep 2, 2019

Hi, hmgle.
I think hellobbn's problem was about fakeroot.
It gives a fake root environment , to remove the need to become root for a package build.
And graftcp can't work with it. Like yay etc. graftcp yay

sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid' option set or an NFS file system without root privileges?

@hmgle hmgle reopened this Sep 3, 2019
@ManiaciaChao
Copy link

@hellobbn Thank you for your example.
I thank it can be solved like this:

$ sudo chown root:root graftcp
$ sudo chmod a+s graftcp
# Then you can use graftcp to trace sudo...
$ graftcp bash
➜ makepkg

However, this workaround works well in almost every case. To make it easier, you can write a shell script proxy-yay like below:

#! /bin/sh
graftcp sh -c "yay $@"

Then you just need to use proxy-yay instead of yay, or set it as an alias of yay.

@kabehada5s
Copy link

can i use graftcp on alpine?

@fangzhen
Copy link
Contributor

fangzhen commented Jul 6, 2023

Hi,
Error message now reads:

$ graftcp yay
sudo: The "no new privileges" flag is set, which prevents sudo from running as root.
sudo: If sudo is running in a container, you may need to adjust the container configuration to disable the flag.
 -> error refreshing databases - exit status 1

In this commit ,the PR_SET_NO_NEW_PRIVS flag is set, which I think causes the new error message.

A workaround:

        if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
		perror("prctl(PR_SET_NO_NEW_PRIVS)");
		exit(errno);
	}

Remove these lines, and run command like this, which run graftcp as root and yay as a non-root user.

sudo ./graftcp sudo -u <your-username> yay

@hmgle
Copy link
Owner

hmgle commented Jul 6, 2023

Thank you for your feedback.

Introducing PR_SET_NO_NEW_PRIVS indeed brings new restrictions to gain superuser privileges. I'll look into a solution to this problem. If you have any new ideas, feedback is welcome.

Hi, Error message now reads:

$ graftcp yay
sudo: The "no new privileges" flag is set, which prevents sudo from running as root.
sudo: If sudo is running in a container, you may need to adjust the container configuration to disable the flag.
 -> error refreshing databases - exit status 1

In this commit ,the PR_SET_NO_NEW_PRIVS flag is set, which I think causes the new error message.

A workaround:

        if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
		perror("prctl(PR_SET_NO_NEW_PRIVS)");
		exit(errno);
	}

Remove these lines, and run command like this, which run graftcp as root and yay as a non-root user.

sudo ./graftcp sudo -u <your-username> yay

hmgle added a commit that referenced this issue May 31, 2024
When tracing a process that needs to execute setuid(2) or similar
operations that require privilege escalation (e.g., sudo), it is necessary
to avoid calling prctl(PR_SET_NO_NEW_PRIVS, 1). This call would prevent the
tracee process from gaining the required privileges, leading to failures.

Related issue #11 #11 (comment)
@hmgle
Copy link
Owner

hmgle commented May 31, 2024

Hi, Error message now reads:

$ graftcp yay
sudo: The "no new privileges" flag is set, which prevents sudo from running as root.
sudo: If sudo is running in a container, you may need to adjust the container configuration to disable the flag.
 -> error refreshing databases - exit status 1

In this commit ,the PR_SET_NO_NEW_PRIVS flag is set, which I think causes the new error message.

A workaround:

        if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
		perror("prctl(PR_SET_NO_NEW_PRIVS)");
		exit(errno);
	}

Remove these lines, and run command like this, which run graftcp as root and yay as a non-root user.

sudo ./graftcp sudo -u <your-username> yay

@fangzhen I saw the solution implemented in strace and copied the code over. Now the command can be executed perfectly. Thanks to strace.

@hmgle hmgle closed this as completed in a0ed0cd Jun 1, 2024
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

6 participants