Users, groups and permissions

Unit 2 of 5 in Operating systems and the command line.

You will be able to read a Linux permission string and explain who can read, write or run a file and what the setuid bit changes.

Lesson

Three permissions for three kinds of people

Every file has three permissions: read, write and execute. For a directory, read means you may list what is inside, write means you may create and remove files in it, and execute means you may enter it and reach the files inside.

Three kinds of people can have different permissions: the file's owner, other users who are in the file's group, and everyone else. Files get an owner and a group when they are created, and chown and chgrp change them. That gives nine permission bits in all, which is what the string you see in a long directory listing shows.

Which set of permissions applies to you

The system picks one set, not all of them. If the process's effective user ID equals the file's owner, the owner permissions apply. Otherwise, if the file's group matches the process's group or one of its supplementary groups, the group permissions apply. If neither holds, the permissions for everyone else apply.

This order has a practical lesson: being the owner uses the owner bits even if the group bits are more generous. Read the matching set, not the best-looking one.

Who is root, and what the setuid bit does

On traditional Unix systems a privileged process is one whose effective user ID is 0, called superuser or root, and privileged processes bypass all kernel permission checks. Linux later split that power into units called capabilities that can be turned on and off separately, which lets a program have one needed privilege instead of all of them.

Besides read, write and execute there are special bits. The setuid bit makes a program run with the effective user ID of the file's owner instead of the person who started it. That is useful for a few tools and risky for anything else, so a defender checks which programs have it.

Every process has real and effective user and group IDs. The real IDs say who owns the process; the effective IDs are what the kernel uses to decide most permissions.

Default permissions and the umask

New files do not start with every permission. A setting called the umask is applied when a file or directory is created, and the permissions in the umask are turned off from what the program asked for. A stricter umask means new files start more private. Checking the umask for the account that runs a service is a quick hardening step.

Key points

Sources

Practice in the labs

Make users, read permission strings and see how the umask and setuid bit behave, all in a safe sandbox.

Knowledge check

  1. You own a file with permissions that give the owner read only and the group read and write. You are in the group. What can you do? Answer: Read only, because the owner set applies to you as the owner. The system uses the owner permissions when your effective user ID matches the file's owner, even if the group set is more generous (path_resolution(7)).
  2. What does the setuid bit on a program do? Answer: Runs the program with the effective user ID of the file's owner. The GNU Coreutils manual describes setuid as setting the process's effective user ID to that of the file on execution, which is why it needs auditing.
  3. What is a capability in Linux? Answer: A unit of privilege traditionally held by root, which can be enabled and disabled independently. capabilities(7) says Linux divides the privileges traditionally associated with superuser into distinct units that can be independently enabled and disabled.
  4. What does a stricter umask do? Answer: Turns off more permissions on newly created files and directories. umask(2) says permissions in the umask are turned off from the mode requested when a file or directory is created. It does not touch existing files.

Back to the syllabus or see all classes.

Related