Bug 1895831 - passwd command truncates files instead of atomic moves
Summary: passwd command truncates files instead of atomic moves
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: shadow-utils
Version: rawhide
Hardware: All
OS: Linux
unspecified
medium
Target Milestone: ---
Assignee: Iker Pedrosa
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2020-11-09 08:35 UTC by Коренберг Марк
Modified: 2020-11-09 08:55 UTC (History)
3 users (show)

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2020-11-09 08:55:45 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

Description Коренберг Марк 2020-11-09 08:35:45 UTC
See https://bugzilla.redhat.com/show_bug.cgi?id=1862056


According to strace, it opens /etc/passwd with O_TRUNC and writes new contents. If power outage happened at this point, FS may reveal en empty file. In order to overcome, it MUST:

1. open temporary file in the same dir (i.e. in /etc) securely like mkostemp()
2. copy ownerhsip info, permissions, possibly some attributes (selinux? xattr?) from current file to temporary one
3. fill new file with new data
4. fdatasync(tmpfile)
5. close(tmpfile)
5. rename("/etc/passwd.tmp", "/etc/passwd")
6. dirfd = open("/etc", O_DIRECTORY|O_RDONLY)
7. fdatasync(dirfd)
8. close(dirfd)

This is the only way to secure(safe) replace a file.

If possible, it's better to:
1. Do all the operations using *at syscalls with opened "/etc" at the start of the change operation.
2. use openat(O_TEMPFILE)+ linkat("/proc/self/fd/%d") instead of rename() in order not to leave temporary files on accidental poweroff. If not, remove all temporary files left from the previous call after gaining the main lock.

Comment 1 Tomas Mraz 2020-11-09 08:44:50 UTC
Where do you see that? shadow-utils should not open /etc/passwd with O_TRUNC at all. Could you attach the strace where this would be visible?

Comment 2 Коренберг Марк 2020-11-09 08:55:45 UTC
Sorry, my fault. It does not open /etc/passwd with O_TRUNC.


Note You need to log in before you can comment on or make changes to this bug.