Articles / Linux

  • linux
  • permissions
  • file-descriptors
  • chmod
  • security
  • debugging

Why a Linux Process Can Read a File After chmod 000

You run chmod 000 on a file, and a process that was already reading it carries on regardless. Nothing is broken. Linux checks permissions when a file is opened, not on every read, so revoking the mode bits never revokes an open file descriptor.

Seeing it happen

Try this as an ordinary user. Root would ignore the mode bits entirely, which muddies the demo.

echo "the secret" > secret.txt
exec 3<secret.txt      # open it on fd 3 in this shell
chmod 000 secret.txt
cat secret.txt         # cat: secret.txt: Permission denied
cat <&3                # the secret

The first cat asks the kernel to open the path, and the kernel says no. The second inherits fd 3, which was opened while the file was still readable, so no new check happens.

Where the check actually lives

An open() call does the permission work: it walks the path, checks search permission on each directory, then checks the file's mode against your credentials and the requested access (read, write or both). If that passes, you get a file descriptor pointing at an open file description that records the access mode you were granted.

After that, read() only asks one question: was this descriptor opened for reading? It does not go back to the inode's mode bits. That is also why the cost of a read stays small; re-checking credentials on every call would be wasteful.

Quick detour: why not just /proc/self/fd/3?

You might think cat /proc/self/fd/3 would also work. It does not. Opening that path is a brand new open() on the same inode, so the kernel runs the full permission check again and returns EACCES. The link is a shortcut to the file, not to your existing grant. Handy to know when someone suggests it as a "clever" way to bypass a mode change.

It is not only chmod

The same rule explains several things that look odd at first:

  • Remove a user from a group, and their existing shells and daemons keep the old group membership until they restart or log in again, because credentials are fixed per process.
  • Change ownership with chown, and open descriptors still work.
  • Change the directory to chmod 000, and a process holding an fd inside it still reads the file, because the directory search check happens during path lookup only.
  • A mmap() mapping survives close() on the descriptor, so the process can still read the pages afterwards.
  • Pass the descriptor to another process over a Unix socket, and the receiver gets the access without ever passing a check.

A process opened for writing can also keep writing, and even ftruncate() the file, after you take write permission away.

The same thing from Go

package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	f, err := os.Open("secret.txt")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	// Run "chmod 000 secret.txt" from another terminal now, then press Enter.
	fmt.Scanln()

	b, err := io.ReadAll(f)
	fmt.Printf("%q %v\n", b, err) // still reads the content, err is nil
}

Reopening the path with os.Open after the chmod would fail. The existing *os.File is untouched.

Finding who still has it open

If you changed a mode to lock someone out, check who is still holding the file:

  • lsof secret.txt lists processes with the file open.
  • fuser -v secret.txt gives a shorter answer.
  • ls -l /proc/PID/fd shows the descriptors of one process.
  • /proc/PID/maps shows mappings that outlive a closed descriptor.

The deleted-file case works the same way, by the way: the data lives while anything references it. Permissions are just another thing that is looked at once.

What actually cuts a process off

Linux has no revoke(). BSD systems have one that forcibly invalidates descriptors for a file, but Linux never gained an equivalent. So your options are blunt:

  1. Stop or restart the process, so its descriptors close and its credentials refresh.
  2. Kill it, if it is something you do not trust. Closing a descriptor from outside is possible with a debugger, but that is fragile and not something to rely on.
  3. If the file holds a secret, rotate the secret. Assume anything that held it open could have copied it.

That last point is the real lesson for security work. Changing permissions controls future opens only. If a process might already have read the data, chmod was never going to be the fix.