Skip to content

userdel: keep a mail spool the user does not own - #401

Open
pierre-warnier wants to merge 5 commits into
mainfrom
fix-400-userdel-spool-owner
Open

pierre-warnier wants to merge 5 commits into
mainfrom
fix-400-userdel-spool-owner

Conversation

@pierre-warnier

Copy link
Copy Markdown
Collaborator

Fixes #400

Stacked on #396, which changes the same code. The base is fix-392-userdel-missing for now; it will be retargeted to main once #396 lands.

Ubuntu 26.04 behaviour

Probed in ubuntu:26.04 with passwd 1:4.17.4-2ubuntu3, each case with a home the user owns:

/var/mail/LOGIN flags exit stderr spool account, home
regular file owned by root -r 12 userdel: /var/mail/LOGIN not owned by LOGIN, not removing kept removed
regular file owned by root -r -f 0 none removed removed
symlink to a root-owned file -r 12 same message link and file kept removed
symlink to a root-owned file -r -f 0 none link removed, file kept removed
symlink to a file LOGIN owns -r and -r -f 0 none link removed, file kept removed

Nothing goes to stdout. Ownership is that of the file the link points to (the links were root's own in every case), and only the link is ever unlinked.

Change

remove_mail_spool opens a descriptor on the spool's directory, stats the entry through it (following a link, so a dangling one is still "not found", as in #396), and unlinks through that same descriptor. Without -f, a spool owned by another uid is kept and said on stderr; the home is still removed and the exit status is 12. The same probe run against this build gives the same table.

Tests: five new cases in tests/by-util/test_userdel.rs; the two refusal cases fail before the fix (exit 0 instead of 12). The man page and CHANGELOG are updated.

Reviews welcome. Without objection, this will be merged after 7 days.

Comment thread src/uu/userdel/src/userdel.rs Outdated

if spool_kept {
// Already said above, in its place before anything about the home.
return Err(shadow_core::cli::AlreadyPrinted(exit_codes::CANT_REMOVE_HOME).into());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this early return skips the selinux remove_login, the nscd invalidation and the audit log in uumain, while the account is already gone.
could the 12 be returned only after those, no?
please add a test too

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 5310d60: the 12 is now returned after the SELinux mapping, the nscd invalidation and the audit record have run, as Ubuntu 26.04 also logs the deletion in this case. The new test checks the exit 12, that the account is gone, and that the DEL_USER audit line is written; it fails on the previous code (79671f9 moves the /dev/log listener into the shared test helpers for it).

Comment thread src/uu/userdel/src/userdel.rs Outdated
/// Remove the mail spool at `mail` unless it belongs to someone other than
/// `uid` and `force` is not given.
///
/// Ownership is that of the file the entry leads to: Ubuntu 26.04's `userdel

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please make it shorter, the PR description already has the Ubuntu table

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Shortened to three lines in 6ddbe39.

Comment thread src/uu/userdel/src/userdel.rs Outdated
if !force && target.st_uid != uid {
return Spool::NotOwned;
}
// A failed unlink has never been reported here; that is not this check's to change.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

not sure this comment helps, could you please drop it or just say why we ignore the error?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reduced to one line in 6ddbe39: the error was already ignored before this PR, while upstream warns and exits 12, which is a separate fix.

Comment thread tests/by-util/test_userdel.rs Outdated
/// `kind` is `"file"` for a regular spool, or `"link"` for a spool that is a
/// symlink to a file outside /var/mail; that file, or the spool itself, is
/// owned by `owner`.
fn spool_case(kind: &str, owner: u32) -> SpoolCase {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could kind be a bool or a small enum instead of a string?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is a bool now, as there are only two cases (83115ec).

@pierre-warnier
pierre-warnier force-pushed the fix-392-userdel-missing branch from 7783c18 to f87bf97 Compare December 5, 2026 08:52
@pierre-warnier
pierre-warnier force-pushed the fix-400-userdel-spool-owner branch from f603a28 to 83115ec Compare December 5, 2026 08:59
@pierre-warnier
pierre-warnier force-pushed the fix-392-userdel-missing branch from f87bf97 to 678d679 Compare December 5, 2026 09:57
@pierre-warnier
pierre-warnier changed the base branch from fix-392-userdel-missing to main December 5, 2026 10:49
With -r, a mail spool that the user does not own is now kept, as
upstream does: "SPOOL not owned by LOGIN, not removing" on stderr, the
home still removed, exit 12. -f removes it. A spool that is a symlink is
judged by the file it points to, and only the link is ever removed.

The spool is looked up once through a descriptor on /var/mail, and the
unlink goes through that same descriptor.

Fixes #400
usermod's audit test had it to itself. A second test that listens would
find the socket taken by the first and skip without a word, so the
listener moves to the common helpers and takes a lock that keeps the
next test waiting until the socket is gone.
The account is already removed when -r gets to the spool and the home,
so returning there skipped the SELinux login mapping, the nscd
invalidation and the audit record, all of which concern the account.
What is kept is now said where it was, and exit 12 is returned once
those have run. Ubuntu 26.04's userdel logs the deletion in this case
as well.
@pierre-warnier
pierre-warnier force-pushed the fix-400-userdel-spool-owner branch from 83115ec to 0befa85 Compare December 5, 2026 10:49
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.

userdel -r removes a mail spool that is not the user's

2 participants