2

Here is a case I don’t understand.

I have a group bar, and this group has two users; foo and bar.

I have a test.txt file, foo is the owner and bar user only can read it.

However, if bar user run the gzip command to this file, the ownership changes from foo to bar.

Therefore, foo cannot touch this file any more.

Is this a security hole?

Logged in as bar

$ whoami
bar
$ cd /home/foo/test
$ ls -al
total 8
drwxrwxr-x 2 foo bar  4096 Jan  6 15:48 . 
drwxrwxr-- 5 foo bar  4096 Jan  6 15:48 ..
-rwxr-xr-x 1 foo foo    0 Jan  6 15:48 test.txt
$ gzip test.txt    
$ ls -al
total 12
drwxrwxr-x 2 foo bar 4096 Jan  6 15:50 . 
drwxrwxr-- 5 foo bar 4096 Jan  6 15:48 ..
-rwxr-xr-x 1 bar  bar   29 Jan  6 15:48 test.txt.gz
$ uname -a
Linux 2.6.18-xenU-ec2-v1.2 #2 SMP x86_64 x86_64 x86_64 GNU/Linux

Logged in as a foo:

$ whoami
foo
$ touch test.txt.gz 
touch: cannot touch `test.txt.gz': Permission denied

3 Answers 3

8

Its not a security hole. Gzip creates a new file and removes the old one. This is governed by the directory permissions and not the permissions on the file. If you remove write permission from the directory your test.txt will be safe from user bar.

The full process with respect to security looks like this:

  1. Gzip uses read permission on original file to obtain a read-only file handle on original file.
  2. Use write permission on directory to create new, empty file.
  3. Read data from original file and write compressed data to new file
  4. Delete old file using write permission on directory.
4

gzip does not change the ownership of a file. It creates a new file (which, by the nature of how Unix-like systems work, is owned by the user running the gzip command) and deletes the original file.

If you want the newly created test.txt.gz file to have the same user and group ownership as the test.txt file, you'll need to use chown -- and on most systems, that means you'll need root access. User bar ordinarily cannot create a file owned by `foo‘.

(gzip can and does set the permission bits on the newly created .gz file to the same as those on the input file, and if you run it as root it will also use chown to set the owner. Source: I just tried it.)

3

That means you'are a member of a different group. gzip creates a new file with your default group. The original file is removed.

HTH

4
  • I think he's wondering why it's owned by the user who runs gzip. Jan 6, 2012 at 21:30
  • gzip can create a new file with directory ownership, but it shuould not be able to remove the existing file with read permission only.
    – bighostkim
    Jan 6, 2012 at 21:33
  • 1
    @bighostkim: yes, it should, as file removal is a modification of the parent directory, which bar does have write access to. Jan 6, 2012 at 22:50
  • What's the solution to OP's question? Dec 5, 2016 at 14:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .