These are the notes for Cybersecurity Fundamentals (defensive), episode 9. Last time we learned how the world names and scores vulnerabilities. Today we go down to the operating system (OS): the base software, like Linux or Windows, that runs every other program and shares the computer between them. The question is simple: who is allowed to do what on a computer? You'll invent each idea from a real-life problem: kernel mode vs user mode, processes and system calls, users and groups, Linux file permissions (rwx, chmod, chown, umask), ACLs, setuid/setgid, root and Administrator, sudo, Windows UAC, SIDs and access tokens, services and daemons, privilege escalation (as a concept), patching, hardening, CIS Benchmarks and attack surface reduction. Every term is explained the first time it appears, and the Glossary at the end defines them all again.
The family, files and server are made up. Every code sample runs as-is; outputs are real (Python 3 on Linux). The Linux outputs quoted below (ls -l, Permission denied messages) are real, from a Linux machine on 28 Oct 2026.
The problems you'll solve
| Problem | Idea it teaches |
|---|---|
| One buggy app could crash or read everything | Kernel mode vs user mode, processes, system calls |
| The whole family shares one account | Users, groups, owner/group/other, rwx |
| Files that anyone can change | chmod numbers, chown, why 777 is never the fix |
| New files come out readable by everyone | umask |
| Three permission slots aren't enough | ACLs (and Windows DACLs) |
| Change your own password in a root-only file | setuid, setgid, privilege escalation |
| Working as root all day | Root/Administrator, least privilege, sudo, UAC, SIDs, access tokens |
| Programs that run all the time | Services and daemons with their own accounts |
| A server with six open doors and no updates | Patching, hardening, CIS Benchmarks, attack surface reduction |
Problem 1: kernel mode and user mode
If every program could touch any memory and any hardware, one buggy app could crash the machine and one malicious app could read every other app's passwords. A bank solves the same problem:
| At a bank | In the OS | Meaning |
|---|---|---|
| Customers in the hall | User mode | Where normal apps run: restricted, each sees only its own memory |
| The vault and its staff | Kernel mode | Where the kernel (the core of the OS) runs, with full access to memory and hardware |
| Ask at the counter | System call | An app's request to the kernel to do something for it (read a file, send data) |
A process is a running program; each gets its own private memory space. The processor itself enforces the two modes. When a process makes a system call, the kernel checks: which user is this process running as, and is that user allowed? Everything else in this episode is about that check.
Problem 2: users and groups
The family laptop has one login, shared by papa, mummy and Chotu. Chotu opens papa's salary sheet and deletes the tax files by mistake; afterwards the records just say "family".
Demo 1: one shared account
# one shared account for the whole family
files = {"diary.txt": "family", "salary.xlsx": "family",
"homework.doc": "family"}
def can_read(user, f):
return True # everyone IS the same account
for f in files:
print(f"{f:13} readable by Chotu: {can_read('chotu', f)}")
Output:
diary.txt readable by Chotu: True
salary.xlsx readable by Chotu: True
homework.doc readable by Chotu: True
One account means no way to tell people apart, so no way to protect anything, and no record of who did what (accountability, episode 2).
The fix: every person gets their own user account; people who share files go into a group (a named set of users, like family or kids). Every file has an owner, a group, and nine permission bits: read, write and execute (run as a program), for the owner, the group, and everyone else (other).
Demo 1b: the Linux permission check
import stat
def can(user, groups, want, owner, group, mode):
if user == owner:
bits = mode >> 6 # owner's 3 bits
elif group in groups:
bits = mode >> 3 # group's 3 bits
else:
bits = mode # everyone else
return bool(bits & {"r": 4, "w": 2, "x": 1}[want])
# file: diary.txt, owner papa, group family, mode 640
m = 0o640
print(stat.filemode(stat.S_IFREG | m))
print("papa write:", can("papa", ["family"], "w", "papa", "family", m))
print("mummy read: ", can("mummy", ["family"], "r", "papa", "family", m))
print("mummy write:", can("mummy", ["family"], "w", "papa", "family", m))
print("chotu read: ", can("chotu", ["kids"], "r", "papa", "family", m))
Output:
-rw-r-----
papa write: True
mummy read: True
mummy write: False
chotu read: False
Exactly like Linux: if you're the owner, use the owner's bits; else if you're in the file's group, the group's bits; else the "other" bits. (>> shifts the bits right so the three we want end up at the bottom; & tests one bit. 0o640 is an octal, base-8, number.) Note that the real kernel picks only one of the three sets: if you're the owner, the group bits don't apply to you even if they're more generous.
Reading permission strings
- rw- r-- ---
type owner group other
| Letter | Number | On a file | On a directory (folder) |
|---|---|---|---|
| r | 4 | read the contents | list the names inside |
| w | 2 | change the contents | add, delete or rename files inside |
| x | 1 | run it as a program | enter it (cd) and reach files inside |
Add the numbers per group: rw- = 4+2 = 6, r-- = 4, --- = 0, so rw-r----- = 640. The first character is the type: - file, d directory.
Problem 3: setting permissions (chmod, chown)
chmod (change mode) sets permissions; chown (change owner) sets a file's owner and group, e.g. chown papa:family diary.txt.
Demo 2: common modes
import stat
for mode in (0o777, 0o755, 0o644, 0o640, 0o600):
print(oct(mode)[2:], stat.filemode(stat.S_IFREG | mode))
Output:
777 -rwxrwxrwx
755 -rwxr-xr-x
644 -rw-r--r--
640 -rw-r-----
600 -rw-------
| Mode | Use |
|---|---|
| 755 | Programs and scripts: owner changes, everyone runs |
| 644 | Normal files: owner writes, everyone reads |
| 640 | Shared with a group only |
| 600 | Secrets: private keys, password files |
| 777 | Almost never: anyone can change it |
The 777 trap. A script fails with:
sh: 1: ./run.sh: Permission denied
(real message, script with mode 644). People "fix" it with chmod 777 run.sh. It works, and now any user, or any hacked program on the machine, can change that script, which may later run as root. The right fix is the smallest change that works: chmod u+x run.sh adds execute for the owner only (u = user/owner, g = group, o = other, a = all).
Problem 4: new files readable by everyone (umask)
Papa saves a new file without running chmod and it comes out -rw-rw-rw-. Programs usually ask for 666 for new files (777 for new directories), and the OS removes the bits listed in the umask (user file-creation mask).
Demo 3: umask with real files
import os, stat
for mask in (0o000, 0o022, 0o077):
os.umask(mask) # set this process's umask
name = f"report_{oct(mask)[2:]}.txt"
with open(name, "w") as f: # open() asks for 666
f.write("hi")
mode = os.stat(name).st_mode
print(f"umask {mask:03o} -> {stat.filemode(mode)}")
os.remove(name)
Output:
umask 000 -> -rw-rw-rw-
umask 022 -> -rw-r--r--
umask 077 -> -rw-------
| umask | New file | New directory | Use |
|---|---|---|---|
| 000 | 666 rw-rw-rw- |
777 | Dangerous |
| 022 | 644 rw-r--r-- |
755 | The usual default |
| 027 | 640 rw-r----- |
750 | Group can read, others nothing |
| 077 | 600 rw------- |
700 | Private by default |
Problem 5: three slots aren't enough (ACLs)
Papa wants his accountant, and only her, to read salary.xlsx, without adding her to the family group. Owner/group/other has just three slots.
An ACL (access control list) is a list of extra entries, each saying "this user (or group) gets these permissions". On Linux (where the filesystem supports it), setfacl -m u:accountant:r salary.xlsx adds read for user accountant, and getfacl salary.xlsx lists the entries. On Windows, every file and folder has an ACL called the DACL (discretionary access control list), which you see in the file's Properties, Security tab.
Problem 6: setuid and setgid
On Linux, password hashes (one-way scrambles of passwords, episode 14) live in /etc/shadow. On a real machine:
$ stat -c '%A %U %n' /etc/shadow
-rw-r----- root /etc/shadow
$ cat /etc/shadow # as an ordinary user
cat: /etc/shadow: Permission denied
So how does an ordinary user change their own password? The passwd program has a special bit:
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 64152 May 30 2024 /usr/bin/passwd
That s is the setuid bit (set user ID): when anyone runs the program, it runs with the rights of the file's owner (root). passwd is written carefully to let you change only your own password. setgid (set group ID) does the same for the group; on a directory, it makes new files inside belong to the directory's group, so a team's files stay shared.
Demo 4: how setuid and setgid look
import stat
files = [("passwd", 0o4755), ("vim", 0o4755), ("shared/", 0o2775)]
for name, mode in files:
kind = stat.S_IFDIR if name.endswith("/") else stat.S_IFREG
print(f"{name:8} {stat.filemode(kind | mode)}")
Output:
passwd -rwsr-xr-x
vim -rwsr-xr-x
shared/ drwxrwsr-x
The extra leading digit: 4 = setuid, 2 = setgid (and 1 = the "sticky bit", used on shared folders like /tmp so users can only delete their own files).
Why the vim line is dangerous: a text editor that runs as root lets any user open and change any file, including /etc/shadow. Gaining more rights than you were given is privilege escalation, and a careless setuid bit is a classic cause. Defenders regularly list every setuid file (for example find / -perm -4000 -type f) and remove the bit from anything that shouldn't have it.
Problem 7: root, sudo and UAC
Linux has a superuser, root (user ID 0), that can do anything; Windows has the built-in Administrator account and the Administrators group. Ravi works as root all day: one typo in a delete command wipes the system, and a malicious download instantly gets full control. Least privilege (episode 3): work as a normal user and use admin power only when needed.
| Linux: sudo | Windows: UAC (User Account Control) |
|---|---|
sudo <command> runs one command as root |
When an administrator signs in, Windows creates two access tokens: a standard user one and an administrator one |
| Asks for your own password | Apps start with the standard token |
Only users allowed in the sudoers file (edit it with visudo) |
Needing admin rights shows a consent prompt ("Do you want to allow this app to make changes?") on the secure desktop, which other programs can't reach |
| Every use is logged | Standard users get a credential prompt: an admin must type their password |
$ sudo apt upgrade
[sudo] password for ravi:
How Windows knows who you are: SIDs and access tokens
- A SID (security identifier) is a unique ID for every user, group and computer account; Windows uses SIDs, not names, to make decisions. Well-known examples:
S-1-5-32-544= Administrators group,S-1-5-32-545= Users group,S-1-5-18= Local System (the account the OS itself uses). The built-in Administrator account's SID ends in 500. - When you sign in and your password is verified, Windows builds an access token: your SID, your groups' SIDs, a logon SID, and your privileges (special rights such as shutting down the system). Every process you run gets a copy.
- When a process opens a file, Windows compares the process's token with the file's DACL: the same idea as Linux's "which user is this process, and what does the list allow?"
Problem 8: services and daemons
Programs that run all the time in the background, with no window (web server, database, print system), are services on Windows and daemons on Linux. Like a night watchman with only the gate key, a good service runs as its own low-privilege account (e.g. www-data for a web server), never as root or Local System unless it truly needs to. If the web server is hacked, the attacker gets only what www-data can do: least privilege limits the blast radius (how much damage one compromise can cause).
Problem 9: patching and hardening
ChaiGo's new web server was installed with defaults: six network services listening, each on a port (a numbered network door), and no updates for a year (full of n-days from episode 8). Telnet is an old remote-login protocol that sends passwords as plain text; SSH is the secure replacement; HTTPS serves the website securely.
Demo 5: count the doors
listening = {"ssh": 22, "https": 443, "telnet": 23, "ftp": 21,
"printer": 631, "old-admin": 8080}
needed = {"ssh", "https"} # what this web server is FOR
extra = sorted(set(listening) - needed)
print("open ports:", sorted(listening.values()))
print("not needed:", extra)
print("doors:", len(listening), "->", len(needed), "after hardening")
Output:
open ports: [21, 22, 23, 443, 631, 8080]
not needed: ['ftp', 'old-admin', 'printer', 'telnet']
doors: 6 -> 2 after hardening
| Practice | Meaning |
|---|---|
| Patching | Installing updates quickly and on a schedule, so known bugs get closed |
| Hardening | Changing a system's default settings to make it harder to attack |
| Attack surface reduction | Removing unused services, accounts, software and open ports: fewer doors, less to defend |
| CIS Benchmarks | Free, step-by-step secure-configuration guides from the Center for Internet Security, written through a community consensus process (12,000+ professionals); 100+ benchmarks across 25+ vendor product families |
On a real Linux server you'd see listening ports with ss -tlnp and disable a service with systemctl disable --now <service>; on Windows, the Services console and Windows Update.
The whole chain
| Step | Problem | You invented |
|---|---|---|
| 1 | One app could crash everything | Kernel vs user mode, system calls |
| 2 | One family account | Users, groups, rwx |
| 3 | 777 and too-open new files | chmod, chown, umask |
| 4 | Three slots | ACLs |
| 5 | Change your own password | setuid, setgid |
| 6 | Root all day | sudo, UAC, SIDs, tokens |
| 7 | Always-on programs | Services with their own accounts |
| 8 | Six open doors | Patching, hardening, CIS |
Common mistakes (and what they print)
| Mistake | What you see | Why it's wrong | Fix |
|---|---|---|---|
./run.sh with mode 644 |
sh: 1: ./run.sh: Permission denied |
No execute bit | chmod u+x run.sh |
chmod 777 run.sh |
-rwxrwxrwx |
Anyone can change it | 755 for programs, 644 for files |
umask 000 |
umask 000 -> -rw-rw-rw- |
New files writable by everyone | umask 022 (or 077 for private) |
| Writing to a file you don't own | sh: 1: cannot create run.sh: Permission denied |
No write bit for you | Ask the owner, or use the right group |
cat /etc/shadow as a normal user |
cat: /etc/shadow: Permission denied |
Working as intended | Use passwd; sudo only if you must |
| Private key with 644 | -rw-r--r-- ... id_ed25519 |
Others on the machine can read your key | chmod 600 |
| Logging in as root/Administrator daily | Malware gets full control | No least privilege | Normal account + sudo/UAC |
Mini project: a permission auditor
Flag world-writable files, unexpected setuid programs and readable private keys in a (made-up) listing.
import stat
# a made-up listing: (path, owner, mode)
files = [("/usr/bin/passwd", "root", 0o104755),
("/usr/local/bin/backup.sh", "root", 0o100777),
("/home/ravi/.ssh/id_ed25519", "ravi", 0o100644),
("/opt/tools/vim", "root", 0o104755),
("/var/www/index.html", "www-data", 0o100644),
("/srv/team", "root", 0o042775)]
EXPECTED_SETUID = {"/usr/bin/passwd"}
for path, owner, mode in files:
problems = []
if mode & stat.S_IWOTH:
problems.append("world-writable")
if mode & stat.S_ISUID and path not in EXPECTED_SETUID:
problems.append("unexpected setuid")
if "/.ssh/id_" in path and mode & 0o077:
problems.append("private key readable by others")
flag = "; ".join(problems) or "ok"
print(f"{stat.filemode(mode)} {owner:8} {path:28} {flag}")
Output:
-rwsr-xr-x root /usr/bin/passwd ok
-rwxrwxrwx root /usr/local/bin/backup.sh world-writable
-rw-r--r-- ravi /home/ravi/.ssh/id_ed25519 private key readable by others
-rwsr-xr-x root /opt/tools/vim unexpected setuid
-rw-r--r-- www-data /var/www/index.html ok
drwxrwsr-x root /srv/team ok
(0o100000 marks a regular file and 0o040000 a directory, the same bits stat.S_IFREG and stat.S_IFDIR add.) To audit a real machine you own, replace the list with os.walk and os.stat.
Practice
1. Work it out: what number is -rwxr-x---? Then check who can run run.sh with that mode (owner ravi, group devs).
Solution
rwx = 7, r-x = 5, --- = 0, so 750.
import stat
def can(user, groups, want, owner, group, mode):
if user == owner:
bits = mode >> 6
elif group in groups:
bits = mode >> 3
else:
bits = mode
return bool(bits & {"r": 4, "w": 2, "x": 1}[want])
m = 0o750 # run.sh: owner ravi, group devs
print(stat.filemode(stat.S_IFREG | m))
print("ravi run:", can("ravi", ["devs"], "x", "ravi", "devs", m))
print("asha run:", can("asha", ["devs"], "x", "ravi", "devs", m))
print("asha edit:", can("asha", ["devs"], "w", "ravi", "devs", m))
print("guest run:", can("guest", ["guests"], "x", "ravi", "devs", m))
Output:
-rwxr-x---
ravi run: True
asha run: True
asha edit: False
guest run: False
2. umask 027. What modes do a new file and a new directory get?
Solution
import os, stat
os.umask(0o027)
with open("r.txt", "w") as f:
f.write("hi")
os.mkdir("d")
print(stat.filemode(os.stat("r.txt").st_mode))
print(stat.filemode(os.stat("d").st_mode))
os.remove("r.txt"); os.rmdir("d")
Output:
-rw-r-----
drwxr-x---
666 minus 027 → 640 for files; 777 minus 027 → 750 for directories.
3. Which tool? (a) Give one extra user read access to one file without changing groups. (b) Let staff run one admin command without sharing the root password. (c) Let users change their own password stored in a root-only file.
Solution
(a) An ACL (setfacl; on Windows, the DACL). (b) sudo with a sudoers rule (on Windows, UAC elevation). (c) A carefully written setuid program (passwd).
4. Harden it. A small office PC runs: file sharing, remote desktop, a game server and a printer service. Only printing is needed. What do you do?
Solution
Disable remote desktop, file sharing and the game server (attack surface reduction), keep printing, install all pending updates (patching), make sure the daily user is not an administrator, and follow the CIS Benchmark for that OS.
Quick quiz
-rw-r----- 1 papa family 2048 salary.xlsx. Chotu is only in kids; mummy is in family. Who can read it? A. Only papa B. Papa and mummy C. Everyone D. Only Chotu
Answer
B, papa and mummy. Owner papa has rw-; group family has r--; Chotu falls into "other", which has ---. Mode 640.
Interview questions
- What's the difference between kernel mode and user mode? Kernel mode has full access to hardware and memory and runs the OS core; user mode is restricted, each process sees only its own memory and must use system calls to ask the kernel for anything.
- Explain Linux permissions
-rwxr-x---. A regular file; owner can read/write/execute; group can read/execute; others nothing. Octal 750. - What does
xmean on a directory? Permission to enter it and access files inside (traverse);ronly lists names. - What is umask and what does 022 produce? A mask of bits removed from new files' requested mode; with 022, files become 644 and directories 755.
- Why is
chmod 777dangerous? Anyone on the system can modify (and run) the file; if it's later run by root or a service, that's privilege escalation. - What is setuid, and why audit it? A program runs with its owner's privileges; legitimate for tools like
passwd, but an unexpected setuid-root binary gives every user root's power. - What's the difference between
suandsudo?suswitches to another user (often root) and needs that user's password;sudoruns individual commands as root using your own password, controlled by sudoers and logged. - How does UAC work? Admins get a filtered standard-user token and a full admin token at sign-in; apps run with the standard token until the user consents (or provides admin credentials) on the secure desktop.
- What is a SID and an access token? A SID uniquely identifies a security principal (user, group, computer). An access token, created at logon, holds the user's SID, group SIDs and privileges; every process carries one and Windows checks it against objects' DACLs.
- What is system hardening? Name three steps. Reducing a system's attack surface and changing insecure defaults: remove unused services/software/accounts, patch regularly, enforce least privilege, follow CIS Benchmarks.
Glossary
| Term | Meaning in plain words | Example |
|---|---|---|
| Operating system (OS) | Base software that runs programs and shares the computer between them | Linux, Windows |
| Kernel | The core of the OS, with full hardware access | The Linux kernel |
| Kernel mode | The CPU mode with full access, used by the kernel | Drivers, memory management |
| User mode | The restricted CPU mode normal apps run in | Your browser |
| Process | A running program with its own memory | Each open app |
| System call | An app's request to the kernel | "Open this file" |
| User account | An identity on the system for one person or service | papa, www-data |
| Group | A named set of users that share permissions | family |
| Owner / group / other | The three classes Linux permissions apply to | Papa / family / everyone else |
| Permission bits (rwx) | Read (4), write (2), execute (1) for each class | rw-r----- = 640 |
| Octal | Base-8 numbers, used for permission modes | 0o640 |
| chmod | Change a file's permission mode | chmod 600 key |
| chown | Change a file's owner and group | chown papa:family diary.txt |
| umask | Bits removed from new files' permissions | 022 → files 644 |
| ACL | Access control list: extra per-user/group permission entries | setfacl -m u:accountant:r salary.xlsx |
| DACL | Windows' discretionary ACL on every file and folder | Security tab in Properties |
| Password hash | A one-way scramble of a password, stored instead of the password | Lines in /etc/shadow |
| setuid | A program runs with its owner's rights | /usr/bin/passwd (-rwsr-xr-x) |
| setgid | Runs with the group's rights; on a directory, new files inherit its group | drwxrwsr-x shared folder |
| Sticky bit | On a shared directory, users can delete only their own files | /tmp |
| Privilege escalation | Gaining more rights than you were given | A setuid-root text editor |
| Root | Linux superuser, user ID 0 | Can read any file |
| Administrator | Windows' built-in admin account / the Administrators group | SID ending in 500 / S-1-5-32-544 |
| Least privilege | Give only the access needed, only when needed (episode 3) | Daily work as a normal user |
| sudo | Run one command as root with your own password; logged | sudo apt upgrade |
| sudoers | The file listing who may use sudo for what | Edited with visudo |
| UAC | Windows User Account Control: admin power only after consent | "Allow this app to make changes?" |
| Consent / credential prompt | UAC asks admins to approve, or standard users for admin credentials | Yes/No vs password |
| Secure desktop | A separate desktop for UAC prompts that other programs can't reach | The dimmed screen |
| SID | Security identifier: a unique ID for a user, group or computer | S-1-5-18 Local System |
| Access token | A user's SIDs and privileges, copied into every process they start | Built at sign-in |
| Privilege (Windows) | A special system-wide right in a token | Shut down the system |
| Service / daemon | A background program with no window | Web server, print system |
| Blast radius | How much damage one compromise can cause | Limited to www-data's files |
| Port | A numbered network "door" a service listens on | 443 for HTTPS |
| Telnet | Old remote login that sends passwords in plain text | Port 23 |
| SSH | Secure, encrypted remote login | Port 22 |
| Patching | Installing updates to fix known bugs | Monthly updates |
| Hardening | Changing defaults to make a system harder to attack | Turning off telnet |
| Attack surface reduction | Removing unused services, accounts, software, ports | 6 doors → 2 |
| CIS Benchmarks | Free, consensus-built secure configuration guides from the Center for Internet Security | A Linux or Windows benchmark |
Summary
- Apps run in user mode and ask the kernel for everything through system calls; the kernel checks which user the process runs as.
- Give every person their own account; share through groups. Read permissions as owner / group / other, each rwx (4/2/1).
- Set the smallest permissions that work: 755 programs, 644 files, 600 secrets; 777 is never the fix. A sensible umask (022 or 077) keeps new files safe. Use ACLs for exceptions.
- setuid/setgid are powerful; audit them, because a careless one is privilege escalation.
- Don't work as root/Administrator: use sudo or UAC. Windows decides access with SIDs, access tokens and DACLs.
- Run services/daemons under their own low-privilege accounts.
- Patch fast, harden with CIS Benchmarks, and reduce the attack surface.
Next episode: cryptography foundations: plaintext, ciphertext, keys, brute force, randomness, and why Base64 is not encryption.
Sources: Microsoft Learn, "How User Account Control works" and "Access tokens"; ADSecurity.org, "Interesting Windows Computer & Active Directory Well-Known Security Identifiers (SIDs)"; Center for Internet Security, "CIS Benchmarks" overview. Linux outputs from a Linux machine on 28 Oct 2026. Fetched 28 Oct 2026.