With an analyzing program I built, I ran into a bug where a user had a primary group set to a since then deleted group (no longer in the /etc/group file). To scope the impact I ran some tests and run into what appears to be some strange behaviors:
It appears that for the rights check in sudoers, both the /etc/group file and the primary group of the user are searched separately and the primary group therefore doesn't have to be an actual group?
Question: Is this correct? What am I missing? I'm not sure why the client had a primary group deleted, but this doesn't seem right.
Device specs:
Linux Ubuntu 20.04.1
5.13.0-1017-azure x86_64
Steps to reproduce:
Create new user: sudo useradd testuser
This created both the user and a separate new group with the same name, which became its primary group.
Check groups of new user: groups testuser
Result: 'testuser : testuser'
Delete the new group via command: sudo groupdel testuser
Got the following error message: groupdel: cannot remove the primary group of user 'testuser'
Delete the new group via manual file adjustment: sudo nano /etc/group
This did work as I could simply delete the last line with the relevant group and save the file
Check groups of new user again: groups testuser
Result: 'testuser : groups: cannot find name for group ID 1003
1003'
Try to login as the new user, after removing the group Success, could still login
Try to execute sudo command with the new user Failed as expected: 'testuser is not in the sudoers file. This incident will be reported'
Log back in as privileged account (which does have sudo rights)
Give the group name removed in step 4 sudoer rights in the sudoers file: sudo nano /etc/sudoers
Then add this to the file:
%testuser ALL=(ALL:ALL) ALL
Login as new user and execute sudo command again Still failed: 'testuser is not in the sudoers file. This incident will be reported'
Give the group id removed in step 4 sudoer rights in the sudoers file: sudo nano /etc/sudoers
Then add this to the file:
%#1003 ALL=(ALL:ALL) ALL
Login as new user and execute sudo command again Success, user now has SUDO rights (I had really hoped this not to be the case)
Create a new group: sudo groupadd testforid
New group created with ID 1003
Check groups of new user again: groups testuser
Result: 'testuser : testforid'
Yes, deleted groups still work as primary groups; in fact, groups which have never created work as primary groups.
This is because all that matters ultimately for users and groups is their identifiers. The mapping between a user and its primary group, e.g. in /etc/passwd
, associates identifiers. In your example, testuser
was assigned primary group 1003; whether that group was given a name (in /etc/group
in your case) was irrelevant. When you granted group 1003 sudo
privileges, testuser
was granted those privileges through that group.
User names and identifiers follow the same rule: all that matters ultimately is the user identifier. Files owned by a given user are tied to that user through the identifier, not the name, and that association persists when the user is removed. Files can also be created with a user identifier which has no matching user on the system.
Multiple user names and group names can also be associated with the same user and group id respectively; theres no requirement for the mappings to be bijective. Thus permissions etc. can be defined in terms of user/group identifiers, and those identifiers can have 0 or more names associated with them.