Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Gain owner of a file using vim :w!

5 views
Skip to first unread message

Beco

unread,
May 22, 2013, 6:50:01 PM5/22/13
to

Dear users,

I'm astonished by this (maybe I'm naive and I'm missing something).

Yesterday as root I saved a file skel.bashrc in my /home/beco user, owned by root, group root.

Today I edited it, logged as beco, and vi told me "warning, read only!". I edited anyway, just to test, and saved with :w!

After that I checked the file and it has changed to owner beco, group beco.

How is that possible?

Thanks,
Beco




--
Dr Beco
A.I. researcher

"Sometimes the heart sees what is invisible to the eye." (H. Jackson Brown Jr.)

staticsafe

unread,
May 22, 2013, 8:30:02 PM5/22/13
to
According to the vim docs [0]:

:w[rite]! [++opt] Like ":write", but forcefully write when 'readonly' is
set or there is another reason why writing was
refused.
Note: This may change the permission and ownership of
the file and break (symbolic) links. Add the 'W' flag
to 'cpoptions' to avoid this.

I'm not sure how this works. What were the permissions on the file before you edited it?

[0] - http://vimdoc.sourceforge.net/htmldoc/editing.html#writing
--
staticsafe
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
Please don't top post - http://goo.gl/YrmAb
Don't CC me! I'm subscribed to whatever list I just posted on.


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/2013052300...@uriel.asininetech.com

Paul Condon

unread,
May 22, 2013, 8:40:02 PM5/22/13
to
I did not know about this, and it is suprising, but ...
I have frequently used another flaw in the read-only security to get a
copy with write permission. I open a new document in a new window, and
then use my mouse to select the whole text in the window holding the
read-only original. Then I paste that text into the new, empty document.

The read-only feature of the UNIX paradigm is not a way of enforcing
intellectual property rights. It is just a way of reminding oneself to
be careful and not clobber something that one really needs to keep.


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/519D6545...@gmail.com

Joe Riel

unread,
May 22, 2013, 8:50:01 PM5/22/13
to
Beco <r...@beco.cc> writes:

> Dear users,
>
> I'm astonished by this (maybe I'm naive and I'm missing something).
>
> Yesterday as root I saved a file skel.bashrc in my /home/beco user, owned by
> root, group root.
>
> Today I edited it, logged as beco, and vi told me "warning, read only!". I
> edited anyway, just to test, and saved with :w!
>
> After that I checked the file and it has changed to owner beco, group beco.
>
> How is that possible?

You have write permission to the directory, so you can delete the file
and create a new one, with yourself as the owner. That is, essentially,
what vi is doing.

--
Joe Riel


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/87bo82c...@san.rr.com

John Hasler

unread,
May 22, 2013, 11:20:01 PM5/22/13
to
Joe Riel writes:
> You have write permission to the directory, so you can delete the file
> and create a new one, with yourself as the owner. That is,
> essentially, what vi is doing.

Note that if the file had had a hardlink in another directory, say one
owned by root, that link (and therefor the actual file) would not have
been deleted. The effect would have been to replace the link in his
directory with a copy owned by him, leaving the original file and the
other link unchanged.
--
John Hasler


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/87ppwi9...@thumper.dhh.gt.org

Andrei POPESCU

unread,
May 23, 2013, 2:50:01 AM5/23/13
to
[sent privately by mistake]

On Mi, 22 mai 13, 19:48:37, Beco wrote:
> Dear users,
>
> I'm astonished by this (maybe I'm naive and I'm missing something).
>
> Yesterday as root I saved a file skel.bashrc in my /home/beco user, owned
> by root, group root.
>
> Today I edited it, logged as beco, and vi told me "warning, read only!". I
> edited anyway, just to test, and saved with :w!
>
> After that I checked the file and it has changed to owner beco, group beco.
>
> How is that possible?

Check this out:

amp@sid:~$ sudo touch tmp/testfile
amp@sid:~$ ls -l tmp/testfile
-rw-r--r-- 1 root root 0 May 23 01:58 tmp/testfile
amp@sid:~$ rm tmp/testfile
rm: remove write-protected regular empty file `tmp/testfile'? y
amp@sid:~$ ls -l tmp/testfile
ls: cannot access tmp/testfile: No such file or directory
amp@sid:~$

AFAIR it has to do with the fact that you own the directory and rm just
deletes the directory entry for that file.

With vi(m) (and I assume most other editors) this works because when you
edit a file you don't work on the actual file, but on a copy of it. When
you save it vi(m) replaces the original file with the changed copy
(effectively rm/rename), because in case of a crash/power failure/etc.
you still have the original and hopefully even most of the changed file
(depending on autosave settings).

Hope this explains,
Andrei
--
If you can't explain it simply, you don't understand it well enough.
(Albert Einstein)
signature.asc

Frank Miles

unread,
May 23, 2013, 1:20:01 PM5/23/13
to
On Thu, 23 May 2013 00:50:01 +0200, Beco wrote:

> Dear users,
>
> I'm astonished by this (maybe I'm naive and I'm missing something).
>
> Yesterday as root I saved a file skel.bashrc in my /home/beco user,
> owned by root, group root.
>
> Today I edited it, logged as beco, and vi told me "warning, read only!".
> I edited anyway, just to test, and saved with :w!
>
> After that I checked the file and it has changed to owner beco, group
> beco.
>
> How is that possible?
>
> Thanks,
> Beco

Did you want the modified file to remain owned by root? That would, of course,
be _more_ dangerous!


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/knlgup$sno$1...@dont-email.me

Kevin Chadwick

unread,
Jul 29, 2013, 1:50:02 PM7/29/13
to
> I'm not sure how this works. What were the permissions on the file before you edited it?

Yeah, you sure your not accessing an sftp with suid dir permissions.

I get permission denied.

Also setting chattr +ias on a file as root prevents the folder
shenanigans

On OpenBSD setting chflags schg means you would need to reboot or
defeat the very secure kernel.

I understand how the folder thing could trick you and I would guess
whether it is a bug has been debated many times coming down to inodes
vs logic but as for read-only and IPR how could you expect any
different, you can prevent others except root reading with standard
chmod?

--
_______________________________________________________________________

'Write programs that do one thing and do it well. Write programs to work
together. Write programs to handle text streams, because that is a
universal interface'

(Doug McIlroy)
_______________________________________________________________________


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/305833....@smtp101.mail.ir2.yahoo.com
0 new messages