By the end of this lesson you can
- Explain kernel mode, user mode and why system calls exist
- Trace how a process is created and read a process tree like an analyst
- Describe virtual memory and the protections ASLR, DEP and stack canaries
- Compare file systems and explain inodes, the MFT and timestamps
- Explain users, permissions, least privilege and privilege escalation
What an operating system actually does
An operating system has two jobs. It is a referee that shares the CPU, memory, disk and network fairly between dozens of programs, and it is a translator that gives programs simple commands like "open this file" instead of making each one talk to disk controllers directly.
The core of the OS is the kernel. It runs in a privileged CPU mode (kernel mode, often called ring 0 on x86) where it can touch any memory and any device. Everything else, including your browser, terminal and antivirus user interface, runs in user mode (ring 3) with restricted rights.
| Kernel design | Idea | Examples |
|---|---|---|
| Monolithic | most OS services (drivers, file systems, networking) run inside the kernel for speed | Linux |
| Hybrid | a monolithic style kernel with some microkernel ideas and parts moved out | Windows NT family, macOS (XNU) |
| Microkernel | only the bare minimum in the kernel; drivers and services run as separate user processes | QNX, seL4, MINIX 3 |
System calls: the only door into the kernel
When a program wants something only the kernel can do, it makes a system call: it puts a call number and arguments in registers and executes a special instruction (syscall on x86-64) that switches the CPU into kernel mode at a fixed entry point. The kernel checks permissions, does the work and returns.
| Task | Linux system call | Windows (documented API, then native call) |
|---|---|---|
| Open a file | openat | CreateFileW then NtCreateFile |
| Read data | read | ReadFile then NtReadFile |
| Start a program | fork / clone then execve | CreateProcessW then NtCreateUserProcess |
| Network connection | socket, connect | Winsock connect, handled by the AFD driver |
| Allocate memory | mmap, brk | VirtualAlloc then NtAllocateVirtualMemory |
You can watch system calls happen. On Linux, strace prints every call a program makes:
$ strace -e trace=openat,execve cat /etc/hostname
execve("/usr/bin/cat", ["cat", "/etc/hostname"], ...) = 0
openat(AT_FDCWD, "/etc/hostname", O_RDONLY) = 3
cwm-labProcesses and threads: programs come alive
A program is a file on disk. A process is that program loaded into memory and running, with its own private memory, open files, a security identity and at least one thread (a single line of execution the scheduler can run). Threads in the same process share its memory.
Every process gets a PID (process ID) and remembers its parent's PID (PPID). Together they form a tree, and that tree is one of the best tools a defender has.
$ ps -ef --forest | tail -5
student 1201 1190 0 09:30 pts/0 00:00:00 -bash
student 1388 1201 0 09:41 pts/0 00:00:00 \_ ps -ef --forestMemory management: every process lives in its own illusion
Each process believes it has a huge, private address space starting near zero. That is virtual memory. The CPU's memory management unit (MMU) translates every virtual address into a real physical RAM address using page tables the kernel maintains. Memory is handled in pages, usually 4 KiB each.
- Isolation: process A simply has no mapping for process B's pages, so it cannot read them. That is a security boundary built into hardware.
- Swapping: rarely used pages can be moved to disk (a swap partition on Linux,
pagefile.syson Windows) and brought back when touched. - Permissions per page: each page can be readable, writable and executable, or not.
Built in defences against memory attacks
| Defence | What it does | What it stops |
|---|---|---|
| DEP / NX bit | marks stack and heap pages as non-executable | running shellcode placed in a data buffer |
| ASLR | randomises where the stack, heap, libraries and program load | hard coded addresses in exploits |
| Stack canaries | a secret value before the return address, checked before returning | simple stack buffer overflows |
| CFG, CET shadow stack | check that indirect calls and returns go to valid places | return oriented programming |
File systems: how names become bytes
A file system turns a raw partition into folders and files. It stores the data, and separately the metadata: name, size, owner, permissions, timestamps and the list of blocks holding the content.
- Linux ext4 keeps metadata in an inode per file. The file name lives in the directory entry that points to the inode, which is why one file can have several names (hard links). See inode numbers with
ls -i. - Windows NTFS keeps a record for every file in the Master File Table (
$MFT), a goldmine for forensic investigators.
| File system | Used by | Notes for security |
|---|---|---|
| NTFS | Windows system drives | permissions (ACLs), journaling, encryption via BitLocker or EFS, alternate data streams |
| ext4 | most Linux distributions | Unix permissions, journaling, inodes |
| XFS, Btrfs | RHEL family servers, Fedora, some NAS boxes | Btrfs has snapshots useful for ransomware recovery |
| APFS | macOS, iOS | built in encryption and snapshots |
| FAT32 | old pen drives, EFI System Partition | no permissions at all, 4 GiB maximum file size |
| exFAT | SD cards, large pen drives | no permissions, no journal |
Timestamps tell stories
File systems record when a file was modified, accessed, had its metadata changed, and (on NTFS and newer ext4) created. Investigators build timelines from these to reconstruct an attack minute by minute.
Users, permissions and privilege
Every process runs as some user, and the OS checks that identity on every file open and system call. That is what stops one user from reading another's files.
| Linux and macOS | Windows | |
|---|---|---|
| Most powerful account | root, user ID 0 | NT AUTHORITY\SYSTEM for the OS, plus members of Administrators |
| Identity number | UID and GID | SID, for example S-1-5-18 is SYSTEM |
| Who may do what | owner, group, others with read, write, execute bits | access control lists (ACLs) on each object |
| Temporary elevation | sudo | User Account Control (UAC) prompt |
| Extra mandatory controls | SELinux, AppArmor | integrity levels, AppLocker, WDAC |
Most permission systems you meet are discretionary access control (DAC): the owner of a file decides who else can use it. Mandatory access control (MAC) adds rules set by the administrator that even owners cannot override, which is why a compromised web server process under SELinux still cannot read home folders.
Privilege escalation
Attackers usually land as a low privileged user, for example through a phishing document or a web bug, and then try to climb. Vertical escalation means becoming admin or root. Horizontal means becoming another user at the same level, perhaps one with access to payroll files.
Flashcards: do you remember?
Tap a card to flip it. Try to answer in your head first; recalling is what makes it stick.
Quick quiz
Instant feedback on every answer. Score 70% or more to pass this lesson's quiz.
Q1A program wants to read a file. What must it do?
User mode code cannot touch hardware. The kernel mediates every file access.
Q2Which process tree is the strongest sign of a malicious document?
Office apps rarely need to start PowerShell. It is a common macro malware pattern.
Q3cat /proc/sys/kernel/randomize_va_space prints 0. What does that mean?
0 means no randomisation. 2 is the normal full setting.
Q4Why can malware avoid user mode API hooks?
Direct or indirect syscalls skip hooked functions in ntdll.dll.
Q5A web server process is compromised, but SELinux prevents it reading users' home folders. That is an example of:
MAC policies are set by the administrator and apply even if file permissions would allow access.
Q6An attacker logged in as a normal user becomes root. This is:
Vertical means moving to a higher privilege level.
Hands-on lab
Lab: watch the OS at work
You need: Your Ubuntu VM from lesson 1.1 (restore the clean-install snapshot if you broke it). Optional: your own Windows PC.
Only on machines you own. Do this lab on your own VM or computer. Scanning, testing or accessing anyone else's system without written permission can be an offence under the Information Technology Act, 2000 (for example Sections 43 and 66).
See the process tree
$ ps -ef --forest | less $ pstree -p | head -30 $ echo $$$$is your shell's PID. Find it in the tree and follow the parents up to PID 1. What is PID 1 called?Trace system calls
$ sudo apt install -y strace $ strace -c ls /etc > /dev/null $ strace -e trace=openat cat /etc/hostnameThe
-csummary shows how many calls of each type a tiny command makes. Note how many filescatopens before it reaches the one you asked for (libraries and locale files).Look at a process's memory map
$ cat /proc/self/maps | head -20 $ cat /proc/sys/kernel/randomize_va_space $ cat /proc/self/maps | grep stack; cat /proc/self/maps | grep stackRun the last line twice: the stack address changes each run. That is ASLR working. Find the pages marked
r-xp(code) andrw-p(data).Inodes, links and timestamps
$ cd ~ && echo hello > a.txt $ ln a.txt b.txt && ls -li a.txt b.txt $ stat a.txt $ touch -d '2020-01-01 10:00' a.txt && stat a.txtBoth names share one inode number. The
touch -dline is a harmless timestomp on your own file: notice which timestamp changed and which one (Change) still shows the real time. Investigators use exactly that mismatch.Users and permissions
$ id $ sudo useradd -m labuser $ sudo -u labuser cat /home/student/a.txt $ chmod 600 ~/a.txt && sudo -u labuser cat /home/student/a.txt $ sudo userdel -r labuserNotice the second read fails with Permission denied (if your home folder is not readable by others the first may fail too, which is also a lesson). Replace
studentwith your own user name.Windows: the same ideas
On your own Windows PC open Task Manager, Details tab, right click a column header, Select columns, and add Command line and Elevated. Then download Process Explorer from Microsoft Sysinternals (learn.microsoft.com/sysinternals) and view the process tree. Find
lsass.exeand check its parent.
You are done when: You can name PID 1, show ASLR changing addresses, explain why two file names had one inode, and point to a process's parent on both Linux and Windows.
Key takeaways
- The kernel is the referee; system calls are the only legitimate way in.
- Read processes as a family tree: parent, path and user tell you if something is wrong.
- Virtual memory isolates processes. ASLR, DEP and canaries make memory bugs harder, not impossible.
- Metadata and timestamps are evidence, and attackers try to fake them.
- Least privilege limits how far one compromised account can go.
Finished the sections, quiz and lab? Mark the lesson complete. Progress is saved only in this browser.