Skip to content

id: The --real flag should only affect -u, -g, and -G #7796

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

frendsick
Copy link
Contributor

This PR fixes the logic for retrieving the actual UID and GID of the calling process when the id command is invoked without any users as arguments. The --real flag should only affect what IDs to report about a user with -u, -g, or -U flags set. Previously, it affected the command whenever no users were specified as an argument.

fixes #7749

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/misc/usage_vs_getopt (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/misc/stdbuf (passes in this run but fails in the 'main' branch)

@frendsick
Copy link
Contributor Author

How should we test the situation where UID != EUID or GID != EGID? I had the idea of mocking the State struct, but the id_print function is hard to test as it only prints to stdout. Also, it uses the Passwd::locate and Group::locate functions internally, meaning even if we could mock the State struct, the tested UIDs and GIDs should be valid on the system for the test to work.

@sylvestre
Copy link
Contributor

Could you please add a test to make sure we don't regress in the future?
Thanks

@frendsick
Copy link
Contributor Author

frendsick commented Apr 20, 2025

Yes, I can. I will probably try to mock the getuid, geteuid, getgid, and getegid functions for the tests. I will return to this probably later today.

@sylvestre
Copy link
Contributor

i don't think you need to do that, you should do that in
https://github.com/uutils/coreutils/blob/main/tests/by-util/test_id.rs

@frendsick
Copy link
Contributor Author

I still don't understand how to run the command so that the UID != EUID or GID != EGID without somehow mocking them.

@frendsick
Copy link
Contributor Author

@sylvestre Did you have any ideas on how this could be tested?

@frendsick frendsick changed the title id: The --real flag should only affect -u, -g, and -U id: The --real flag should only affect -u, -g, and -G Apr 20, 2025
@frendsick
Copy link
Contributor Author

I added a test for different UID and EUID. I didn't figure out a better way than to overwrite the getuid and geteuid functions using the LD_PRELOAD environment variable. Please let me know if you see any issues with this, or if it is not the preferred way of testing this.

Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

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

At the risk of having too many opinions in this thread, maybe we can test it in ways more similar to the original issue? If not then I think we just shouldn't have a test for this. The test you came up with is very clever, but depending on gcc is not great.

@frendsick
Copy link
Contributor Author

@tertsdiepraam The original issue requires elevated privileges and assumes that a system has id installed, which it most likely has. A simpler way to test would be to use setuid and setgid if we were to create a test that requires elevated privileges anyway. There are quite a few tests that use the run_ucmd_as_root function, but it does not work in this case, because if we run id as root, both UID and EUID will be 0.

In my opinion, creating a test that requires elevated privileges as a precondition is not a good idea if there are no other tests that do so, except those that use the run_ucmd_as_root function. If we were to leave the current test as is, gcc could be changed to cc for better portability. It could also be removed if you want.

@frendsick
Copy link
Contributor Author

frendsick commented Apr 20, 2025

The current test also does not break if a system does not have the cc command available, as the unwrap_or_return macro exits gracefully if the command does not work. It should only fail if the compiler exists, but the compilation fails somehow.

@frendsick
Copy link
Contributor Author

I'm open to any decision you make.

@frendsick frendsick requested a review from tertsdiepraam April 20, 2025 21:19
Copy link

GNU testsuite comparison:

GNU test failed: tests/misc/tee. tests/misc/tee is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/misc/stdbuf (passes in this run but fails in the 'main' branch)

@frendsick
Copy link
Contributor Author

@tertsdiepraam The test doesn't work on CI/CD, so I guess it should be removed. What would you like me to do? Should I remove the test, or also try to do a test that requires elevated privileges as a precondition?

@tertsdiepraam
Copy link
Member

I'm ok with no test, but I'll defer to @sul. Maybe he has some ideas

The test is incompatible with some CI/CD targets.

This reverts the following commits:
- 8efcbf9
- 208fa8e
- a498a27
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/usage_vs_getopt (passes in this run but fails in the 'main' branch)

@frendsick
Copy link
Contributor Author

Feel free to merge as is. Please let me know if you have any ideas on how the testing could be performed.

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.

id: Doesn't correctly report EUID
3 participants