For testing purposes I set the setuid
to my vim
binary. Now when I open for example /etc/passwd
, modify the file and execute :w
, then I get the E45: 'readonly' option is set (add ! to override)
error message. However, the :w!
saves the changes, i.e it is not a file-system restriction.
Based on what rules vim considers certain files as read-only?
1 Answer
Based on a quick experiment:
# touch readonly
# chmod a-w readonly
Now, vim readonly
will start in readonly mode, because the current user does not have write permission. But, as root or as a regular user (file owner only? untested with other regular users), w!
can bypass this.
Also, from the help, vim
will start with 'readonly'
set if
- vim starts with the
-R
flag - the executable is named
view
When using ":w!" the 'readonly' option is reset for the current buffer, unless the 'Z' flag is in 'cpoptions'. When using the ":view" command the 'readonly' option is set for the newly edited buffer. (
:h 'readonly'
)
-
1That's odd, because
/etc/passwd
hasw
for userroot
:-rw-r--r-- 1 root root 1571 May 8 13:19 /etc/passwd
. I guess it has nothing to do withvim
, but rather how Linux executes thesetuid
binaries.– MartinCommented Jun 19, 2019 at 11:01 -
2@Martin a setuid binary will execute with an EUID ("effective" UID) of root (or the user that owns the binary), but still an UID ("real" UID) of your user. Functions to explicitly check for access (
access(2)
) typically use the real UID. A setuid process that wants to actually run as root will typically executesetreuid(geteuid()):
to reset the real UID appropriately. Hope that helps! Commented Jun 19, 2019 at 23:43
vim
as userroot
and with:w!
I'm able to write the file.:w!
I'm able to save my changes to the file, i.e file system permissions allowroot
user to change the file.