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
$ 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 . 
-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
$ touch test.txt.gz 
touch: cannot touch `test.txt.gz': Permission denied

migrated from stackoverflow.com Jan 8 '12 at 11:21

This question came from our site for professional and enthusiast programmers.


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.

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.)


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


  • I think he's wondering why it's owned by the user who runs gzip. – Edward Thomson Jan 6 '12 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 '12 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. – Edward Thomson Jan 6 '12 at 22:50
  • What's the solution to OP's question? – IgorGanapolsky Dec 5 '16 at 14:34

Your Answer

