Protection and Security in the OS

Users, permissions and access control, privilege separation, memory protection, ASLR and sandboxing.

Est. 35 min Updated Suggest a fix
// Part 01 — Protection model

Security Starts with Separating Authority

An operating system mediates access to files, processes, devices, memory, and network resources. Protection is the mechanism that enforces those boundaries; security is the broader goal of keeping confidentiality, integrity, and availability under a stated threat model. The OS can enforce useful defaults, but an application can still misuse data it is authorised to access.

A practical starting point is least privilege: give a process only the identity and access it needs for its task. Separate the user-facing service from a privileged operation, constrain the files and devices it can reach, and reduce how much authority an error or compromise would expose. Security is layered; permissions, isolation, memory protections, updates, monitoring, and recovery all address different failure modes.

LayerQuestion it answersExample
IdentityWhich user or service is acting?A dedicated account for a background worker
AuthorizationWhich operation is allowed?Read-only access to a configuration file
Privilege separationWhich component needs elevated authority?A small helper performs one protected operation
Memory protectionWhich addresses may this process access?Separate process address spaces and page permissions
IsolationWhich resources are visible or reachable?A sandbox or restricted container
RecoveryHow do we restore trustworthy operation?Patch, rotate credentials, and recover tested backups
💡 Note
Authentication answers "who are you?" Authorization answers "what may you do?" Passing one check does not imply access to every resource; enforce authorization at the resource boundary.
// Part 02 — Users and permissions

Unix Permissions Are a Useful First Boundary

Unix-like systems associate files and processes with user and group identities. Traditional file mode bits describe read, write, and execute access for the owner, group, and others. Directories use execute permission to allow traversal, and write permission to create or remove entries. Permissions combine with ownership, ACLs, mount policy, and other controls; reading one mode string is not a complete access review.

permission-basics.sh
bash
# Inspect your current identity and a file's ownership/mode.
id
ls -l app-config.json

# Set a private read/write mode for the owner only.
chmod 600 app-config.json

# Choose a restrictive default for files created by this shell.
umask 077

A mode of 600 means the owner can read and write, while group and other users have no mode-bit access. It is a reasonable starting point for a local secret file, but secrets should preferably come from an appropriate secret-management system in a deployed service. A restrictive umask changes defaults for newly created files; it does not repair permissions on files that already exist.

🎯 Pro Tip
Review both the file and every parent directory. A private file inside a broadly writable directory may still be replaceable by another user, depending on directory ownership and sticky-bit policy.
// Part 03 — Privilege separation

Keep Privileged Code Small and Specific

The kernel runs in a privileged mode to manage hardware and system resources. Ordinary application code should run with less authority and request specific services through system calls. At the application level, a service can also separate a small privileged helper from a larger parser or network-facing process. The helper should validate the request, perform one narrowly defined operation, and avoid accepting arbitrary commands or paths.

process_identity.c
c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {
    uid_t real_user = getuid();
    uid_t effective_user = geteuid();
    gid_t real_group = getgid();
    gid_t effective_group = getegid();

    printf("real uid=%lu effective uid=%lu\n",
           (unsigned long)real_user, (unsigned long)effective_user);
    printf("real gid=%lu effective gid=%lu\n",
           (unsigned long)real_group, (unsigned long)effective_group);
    return 0;
}

The real identity identifies who started a process; the effective identity is commonly used in permission checks. This example only reports identity. It does not attempt to elevate privileges. Prefer a dedicated service account and explicit permissions over a long-running process with broad administrative authority.

Capabilities can divide some traditionally broad administrative authority into narrower privileges on supported systems. They are still powerful permissions: do not add one just to silence an error. Identify the exact operation, grant the minimum required authority, and document why the process needs it.

// Part 04 — Memory protection

Address Spaces Limit Accidental and Malicious Access

Each process normally receives a virtual address space. The memory-management unit and page tables translate virtual addresses and enforce page permissions such as readable, writable, or executable. A process cannot ordinarily read another process's memory just by guessing an address; access requires a permitted mechanism such as debugging privileges or explicit shared memory. Kernel memory is protected from user-mode access.

Memory protection reduces the effect of many programming errors, but it does not make unsafe code safe. A buffer overrun can still corrupt the current process, and a flaw in a privileged component may cross a boundary. Use memory-safe interfaces where practical, validate lengths, keep toolchains current, and test error paths. Mitigations complement sound coding; they are not substitutes for it.

MechanismPurposeLimit
Per-process address spacesSeparate one process from anotherA permitted sharing/debug path may still expose memory
Page permissionsRestrict reading, writing, or executing memory regionsDo not prevent every logic or data-flow bug
ASLRRandomise locations of selected memory regionsDoes not repair a memory-safety flaw
Non-executable memoryMake selected data pages non-executableDoes not prevent all code-reuse or logic attacks
Privilege separationReduce authority available to a componentA compromised component can still misuse its granted access
// Part 05 — ASLR

Randomisation Adds Uncertainty, Not a Complete Fix

Address Space Layout Randomisation (ASLR) varies locations such as the stack, shared libraries, and other memory regions between executions, where supported and enabled. This makes some address-dependent attacks less predictable. Its effectiveness depends on the platform, executable properties, available entropy, and whether an information leak reveals addresses. ASLR is a defence-in-depth mitigation rather than a guarantee that a vulnerable program is safe.

inspect-aslr.sh
bash
# Read the Linux ASLR setting when procfs exposes it.
if [ -r /proc/sys/kernel/randomize_va_space ]; then
  cat /proc/sys/kernel/randomize_va_space
else
  echo "ASLR setting is not exposed at this path on this system."
fi

This is a read-only inspection. The setting and meaning are OS-specific; do not change a machine-wide security control just to run a demo. On managed systems, ask the administrator and consult the platform documentation before changing security configuration.

// Part 06 — Sandboxing

Constrain What a Program Can Reach

A sandbox restricts a program's access to selected resources. Depending on the platform, it may limit filesystem paths, system calls, network access, devices, process visibility, or resource consumption. A browser tab, mobile app, or build worker can be sandboxed so that a mistake in one component has less reach. Sandboxes depend on their policy and implementation, so they require updates and careful configuration too.

  1. List the files, network destinations, devices, and system operations the workload actually needs.
  2. Deny access by default where the platform supports it, then grant narrowly scoped exceptions.
  3. Run the workload as a non-administrative identity with bounded resources.
  4. Test normal operation and expected failures under the restricted policy.
  5. Review logs and policy changes as part of release and incident response.

For a report-conversion worker, allow reading its input and writing to one output directory; do not mount the whole host filesystem or expose unrelated service credentials. If the worker needs outbound network access, define and monitor that need instead of assuming the default network is harmless.

⚠️ Important
Sandboxing untrusted code requires an isolation boundary appropriate to the threat. A configuration that protects against accidents may not be sufficient for hostile multi-tenant workloads.
// Part 07 — Operational security

Permissions Need Maintenance and Recovery

Access control changes over time: people leave teams, services gain features, and temporary permissions become permanent. Revisit service identities, group membership, secrets, and access logs. Apply security updates through a tested process and know how to roll back safely. Log security-relevant events without writing passwords, tokens, or sensitive user data into general application logs.

ControlQuestion to askEvidence
Account lifecycleAre inactive human and service accounts removed?Owner, purpose, and review date
File accessCan this service read or write only required paths?Ownership, modes, ACLs, and mount policy
PrivilegeDoes a process need its current authority?Documented operation and least-privilege test
UpdatesHow are fixes tested and deployed?Patch records and rollback procedure
RecoveryCan the team restore trusted service and data?A restore exercise and incident runbook

For a financial application, use separate service identities for API, reporting, and deployment tasks. A reporting process that only reads aggregates should not receive permission to alter payment records. Make the intended access visible and test it automatically so a broad permission does not quietly return during deployment.

// Part 08 — Pitfalls

Common OS Protection Mistakes

Treating authentication as authorization

Why: Knowing which user is connected does not decide what that user may access.

Fix: Check the requested action against explicit permissions at the resource boundary.

Running a whole service as administrator

Why: A flaw in any part inherits all of that process authority.

Fix: Use a dedicated low-privilege identity and isolate narrowly privileged operations.

Treating ASLR as a fix for unsafe code

Why: Randomisation raises uncertainty but leaves the underlying defect.

Fix: Fix memory-safety issues and keep mitigations enabled as defence in depth.

Assuming a sandbox is automatically secure

Why: A broad policy, outdated runtime, or shared-kernel risk may defeat its purpose.

Fix: Define a threat model, minimise access, test the boundary, and keep it maintained.

// Part 09 — Practice

Review Access for a Data Export Worker

Practice

Build a small, testable protection plan

  1. Inspect a local file with ls -l and explain the owner, group, and mode bits; do not change files you do not own.
  2. Write an access matrix for an API, reporting worker, and deployment account, covering input, output, and secrets.
  3. Explain how per-process address spaces, page permissions, and ASLR address different risks.
  4. Choose sandbox restrictions for a CSV export worker and list one test for each restriction.
  5. Draft a safe incident checklist for revoking a service credential, applying a fix, and restoring from a tested backup.

Hint: Grant the export worker only the input data and destination it needs. Treat logs and temporary output as sensitive until reviewed.

🎯 Key Takeaways

  • ✓The OS enforces identity and access boundaries over files, processes, memory, and devices.
  • ✓Least privilege limits the damage a process can cause when it fails or is compromised.
  • ✓Unix permissions are one layer; ownership, directory access, ACLs, and mount policy also matter.
  • ✓Memory protection and ASLR are complementary mitigations, not a replacement for safe code.
  • ✓Sandboxing restricts resource access but depends on a precise policy and maintained implementation.
  • ✓Operational security includes account review, updates, careful logs, and tested recovery.
  • For exact permission and mitigation behaviour, consult your operating system's manual pages and official security documentation; details vary by platform.

Next

What comes next

You have covered the OS foundations. Next, practise explaining processes, scheduling, memory, storage, isolation, and security under interview follow-ups.

Lesson 11 · Operating Systems Interview Questions →

HELP US IMPROVE

How was this lesson?

Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.

Choose your feedback

Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.

Share

Discussion

0

Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.

Continue with GitHub
Loading...