Skip to content

open · chmod · getuid

The three triples are checked exclusively

Owner, group, other — and only one of them applies. Being in the group cannot give the owner back a permission the owner triple denies, which is why chmod 077 locks you out of your own file.

One triple, not three

A mode is nine bits in three groups: what the owner may do, what the group may do, and what everybody else may do. That much everybody knows.

What people get wrong is that the kernel consults exactly one of them. It asks, in order: are you the owner? then only the owner bits apply. Are you in the group? then only the group bits apply. Otherwise, only the other bits.

So the permissions are not additive. chmod 077 gives the owner nothing and everybody else everything, and the owner really is locked out of their own file while a stranger reads it. That is not a bug or an edge case — it is the rule, and it is the shape of every permission surprise there is.

The owner can always chmod it back, because changing a mode is a question about ownership rather than about the mode. That is the only reason this is survivable.

Try this
#include <fcntl.h>
#include <unistd.h>

int main(void) {
  chmod("/work/mine", 0077);
  int fd = open("/work/mine", O_RDONLY);
  return fd;
}

Watch: The errno on the open, and which triple the kernel says it looked at.

the-permission-check

Before you run it

Directories use the same three bits and they mean something different. Read is permission to list the entries. Execute is permission to traverse — to reach something inside by name.

Predict

A directory is mode 0111 — execute only, for everybody. What can you do with it?

the-permission-check

Trace

Run one of the programs on the left.

Everything you do here stays in this browser.Part of liter8.sh