Clan x86

Technical (Development, Security, etc.) => Unix / Linux Discussion => Topic started by: mynameistmp on September 19, 2005, 03:20:55 AM

Title: TIP: Immutable files
Post by: mynameistmp on September 19, 2005, 03:20:55 AM
In the ext2 and ext3 filesystems there are a number of additional file attributes available that are beyond the standard bits accessible by chmod. Here is an example of a neat one:

Quote
sh-3.00$ cat test.txt; ls -l test.txt; id
x86
-rw-r--r--  1 tmp users 4 2005-09-19 00:33 test.txt
uid=1000(tmp) gid=100(users) groups=100(users),11(floppy),17(audio),18(video),19(cdrom)
sh-3.00$ rm -f test.txt
rm: cannot remove `test.txt': Operation not permitted

Seems strange. It's a typical text file. My UID is the owner of this file, but I can't delete it. Alright, let's try with root:

Quote
root@tmp:/home/tmp# id ; rm -f test.txt
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy)
rm: cannot remove `test.txt': Operation not permitted

The problem is that this file is 'immutable'. For those of you who aren't familiar with this you should check out the man page on lsattr and chattr. All we need to do is get rid of the immutable attribute and we're off to the races:

Quote
root@tmp:/home/tmp# lsattr test.txt
----i-------- test.txt
root@tmp:/home/tmp# chattr -i test.txt
root@tmp:/home/tmp# rm test.txt

While this flag is set any attempts to unlink, overwrite, rename, or append to the file will fail.
Title: Re: TIP: Immutable files
Post by: Newby on September 19, 2005, 09:26:26 PM
That's awesome. Thanks for the tip. :)
Title: Re: TIP: Immutable files
Post by: Sidoh on September 19, 2005, 10:37:51 PM
Indeed!  Thanks, tmp.  :)
Title: Re: TIP: Immutable files
Post by: Quik on September 20, 2005, 10:30:39 PM
Hrm, was curious how you did this. Might be helpful.
Title: Re: TIP: Immutable files
Post by: mfqr on September 21, 2005, 10:15:22 PM
cool tip :). thanks.