Protection and Security
Protection is the mechanism that decides whether an access is allowed; security is the policy about what should be allowed, plus confidence the mechanism cannot be bypassed. Mixing the two up is how systems with correct permissions still get broken into.
Protection vs Security
Operating system protection and security are two related jobs. Protection is the mechanism: the kernel code that checks each access against a rule. Security is the wider question: which rules are right, who a user really is, and whether the mechanism has holes. A machine with perfect permission checks and a password shared by everyone is well protected and not secure.
Every access-control question has the same shape. A subject, a process acting for a user, asks to perform an operation on an object, such as a file, a socket or a memory region. A reference monitor in the kernel checks the rule and allows or denies it.
For that check to mean anything it must be non-bypassable (no other path to the object), tamper-proof (a subject cannot edit the rules that bind it) and small enough to verify. Hardware provides the first two: the MMU checks every memory access, and user mode cannot reprogram it. Code alone cannot do this, because the checking code would run with the same privilege it is trying to limit.
- Protection: mechanism; security: policy plus assurance
- Subject, object, and a rule checked by a reference monitor
- The monitor must be non-bypassable, tamper-proof and verifiable
The Access Matrix, ACLs and Capabilities
The model is the access matrix: one row per subject (or protection domain), one column per object, and the allowed operations in each cell. No system stores it whole, because it is huge and almost entirely empty, so real systems slice it.
Slicing by column gives an access control list: each object carries the list of who may do what to it. 'Who can read this file?' is instant; 'what can this user reach?' needs a scan of every object. NTFS and POSIX ACLs work this way.
Slicing by row gives capabilities: each subject holds unforgeable tokens that grant specific rights. Passing on authority is easy, taking it back is hard, because every copy must be found. Unix file descriptors behave like capabilities: once a file is open, the descriptor keeps working even if the permissions change.
Classic Unix permissions are a compressed ACL: three classes (owner, group, other) times three bits (read, write, execute), nine bits in all. The kernel picks the first class that matches, so an owner can get fewer rights than the group. On a directory, x allows passing through and w allows creating and deleting entries, which is why write access to a directory lets you delete a file you cannot read, unless the sticky bit is set.
| Access control list | Capability | |
|---|---|---|
| Stored with | The object | The subject |
| Answers quickly | Who may access this file? | What may this subject reach? |
| Answers slowly | What can this user reach? | Who can reach this object? |
| Revocation | Easy — edit the list | Hard — find every copy |
| Delegation | Awkward | Natural — pass the token |
| Example | POSIX permissions, NTFS ACLs | Unix file descriptors |
- ACLs slice the matrix by object; capabilities by subject
- File descriptors keep working after a permission change
- Unix picks the first matching class: owner, group, then other
Least Privilege, Authentication and setuid
The principle of least privilege says each component should hold only the authority its job needs. It does not prevent a break-in; it bounds it. A hacked web server running as www-data cannot read /etc/shadow, so one bug becomes an incident instead of a full compromise. In practice that means dropping root after binding a port, one user per service, containers with reduced capabilities, and seccomp filters that allow only the system calls a process needs.
Authentication establishes who a subject is: something known (a password), held (a hardware key) or measured (a fingerprint). Passwords are stored as salted hashes from a deliberately slow function such as bcrypt, scrypt or Argon2, so an attacker who steals the file cannot test billions of guesses per second. Multi-factor login works because stealing two different kinds of proof is much harder than one.
setuid is the controlled exception. A setuid-root program runs with root's authority whoever starts it, which is how passwd lets a user change their own line in a root-owned file. It is also the classic escalation route: a bug in a setuid program is a bug with root behind it. Modern systems keep such programs few and grant single capabilities, such as CAP_NET_BIND_SERVICE, instead of all of root.
- Least privilege limits damage; it does not prevent the bug
- Store passwords with a slow, salted hash
- setuid programs must be few and carefully written
Terms, operations, and practical uses
The model
- SubjectA process acting for a user.
- ObjectA file, socket or memory region.
- Reference monitorNon-bypassable, tamper-proof and verifiable.
- Protection vs securityMechanism versus policy plus assurance.
Access control
- Access matrixSubjects by objects; too large and sparse to store.
- ACLStored with the object: who may touch this file.
- CapabilityHeld by the subject: an unforgeable token of rights.
- Unix bitsOwner, group, other; the first matching class decides.
Privilege
- Least privilegeBounds the damage of the bug you did not prevent.
- setuidRuns as the file's owner; the classic escalation target.
- CapabilitiesCAP_NET_BIND_SERVICE instead of all of root.
- seccompLimits a process to the system calls it needs.
Permission checks on /etc/shadow (root:shadow 0640)
OWNER, GROUP, OTHER = 6, 4, 0 # mode 0640: rw- r-- ---
READ, WRITE = 4, 2
shadow = {"owner": "root", "group": "shadow", "mode": (OWNER, GROUP, OTHER)}
subjects = {
"root": {"uid": 0, "groups": ["root"]},
"alice": {"uid": 1000, "groups": ["shadow"]}, # group can read only
"www-data": {"uid": 33, "groups": ["www-data"]}, # neither owner nor group
}
def permitted(who, obj, want):
s = subjects[who]
if s["uid"] == 0:
return True, "" # root bypasses the check
bits = obj["mode"][0] if who == obj["owner"] else \
obj["mode"][1] if obj["group"] in s["groups"] else obj["mode"][2]
if bits & want:
return True, ""
return False, "no write" if want == WRITE else "no read"
results = []
for who, want in (("root", WRITE), ("alice", WRITE), ("www-data", READ)):
ok, why = permitted(who, shadow, want)
results.append(f"{who}:" + ("allow" if ok else f"deny({why})"))
# least privilege held: the web server could not read the password file
breached = permitted("www-data", shadow, READ)[0]
print(" ".join(results) + " | least privilege "
+ ("breached" if breached else "held"))#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int READ = 4, WRITE = 2;
struct Subject { int uid; vector<string> groups; };
struct Object { string owner, group; int mode[3]; };
map<string, Subject> subjects = {
{"root", {0, {"root"}}},
{"alice", {1000, {"shadow"}}}, // group can read only
{"www-data", {33, {"www-data"}}}, // neither owner nor group
};
Object shadowFile{"root", "shadow", {6, 4, 0}}; // mode 0640
pair<bool, string> permitted(const string& who, const Object& obj, int want) {
Subject s = subjects[who];
if (s.uid == 0) return {true, ""}; // root bypasses the check
int bits;
if (who == obj.owner) bits = obj.mode[0];
else if (find(s.groups.begin(), s.groups.end(), obj.group) != s.groups.end())
bits = obj.mode[1];
else bits = obj.mode[2];
if (bits & want) return {true, ""};
return {false, want == WRITE ? "no write" : "no read"};
}
int main() {
vector<pair<string, int>> checks = {{"root", WRITE}, {"alice", WRITE}, {"www-data", READ}};
string out;
for (auto& c : checks) {
auto r = permitted(c.first, shadowFile, c.second);
out += c.first + ":" + (r.first ? "allow" : "deny(" + r.second + ")") + " ";
}
bool breached = permitted("www-data", shadowFile, READ).first;
cout << out << "| least privilege " << (breached ? "breached" : "held") << "\n";
}import java.util.List;
import java.util.Map;
class Main {
static final int READ = 4, WRITE = 2;
record Subject(int uid, List<String> groups) {}
record Obj(String owner, String group, int[] mode) {}
static Map<String, Subject> subjects = Map.of(
"root", new Subject(0, List.of("root")),
"alice", new Subject(1000, List.of("shadow")), // read only
"www-data", new Subject(33, List.of("www-data"))); // neither
static Obj shadow = new Obj("root", "shadow", new int[]{6, 4, 0}); // 0640
static String[] permitted(String who, Obj obj, int want) {
Subject s = subjects.get(who);
if (s.uid() == 0) return new String[]{"true", ""}; // root bypasses
int bits;
if (who.equals(obj.owner())) bits = obj.mode()[0];
else if (s.groups().contains(obj.group())) bits = obj.mode()[1];
else bits = obj.mode()[2];
if ((bits & want) != 0) return new String[]{"true", ""};
return new String[]{"false", want == WRITE ? "no write" : "no read"};
}
public static void main(String[] args) {
String[] who = {"root", "alice", "www-data"};
int[] want = {WRITE, WRITE, READ};
StringBuilder out = new StringBuilder();
for (int i = 0; i < who.length; i++) {
String[] r = permitted(who[i], shadow, want[i]);
out.append(who[i]).append(":")
.append(r[0].equals("true") ? "allow" : "deny(" + r[1] + ")").append(" ");
}
boolean breached = permitted("www-data", shadow, READ)[0].equals("true");
System.out.println(out + "| least privilege " + (breached ? "breached" : "held"));
}
}Step through it
Running on /etc/shadow, owner root, group shadow, mode 0640
Common Threats and Defences
Program threats misuse code a user runs. A Trojan horse does something harmful while appearing useful; a trap door is a hidden way in left by its author; a logic bomb waits for a trigger such as a date. System threats spread: a virus attaches to other programs, and a worm copies itself across a network on its own, as the 1988 Morris worm did through a buffer overflow.
A buffer overflow writes past an array on the stack into the saved return address and redirects execution. Each standard defence removes one precondition: stack canaries detect the overwrite, DEP/NX stops injected data from running as code, and ASLR hides the addresses that code-reuse attacks need. None is complete alone, so they are layered. Memory-safe languages remove the whole class of bug.
Isolation also extends beyond processes. Containers share one kernel, so a kernel bug breaks out of all of them; virtual machines share far less. Side channels such as Spectre and Meltdown leaked data across these boundaries through CPU timing, without breaking any permission check, which shows a correct mechanism can still leak.
| Defence | Removes | Defeated by |
|---|---|---|
| Stack canary | Silent overwrite of the return address | Overwriting a pointer instead; leaking the canary |
| DEP / NX | Executing injected data | Return-oriented programming |
| ASLR | Knowing where code lives | An information leak revealing addresses |
| Memory-safe language | The whole bug class | (nothing in this class) |
- Program threats: Trojan horse, trap door, logic bomb
- System threats: viruses and worms
- Canaries, DEP and ASLR each block one step of an exploit