User Mode vs Kernel Mode - Privilege Levels
Quick Reference (TL;DR)
User Mode (Ring 3): Applications run here. Limited privileges, cannot access hardware directly, cannot disable interrupts, memory protection enforced.
Kernel Mode (Ring 0): OS kernel runs here. Full privileges, direct hardware access, can disable interrupts, can modify page tables.
Transition: User β Kernel via system calls (trap instruction). Kernel β User via return from interrupt.
1. Clear Definition
User Mode (Ring 3)
The unprivileged mode where applications run. Limited access to hardware, enforced memory protection, cannot execute privileged instructions.
Kernel Mode (Ring 0)
The privileged mode where the OS kernel runs. Full access to hardware, can modify system state, can execute all CPU instructions.
π‘ Key Insight: This separation is fundamental to OS security and stability. User programs cannot crash the system or access other processes' memory.
2. Core Concepts
CPU Privilege Levels (Rings)
x86 Architecture (most common):
- Ring 0: Kernel mode (highest privilege)
- Ring 1-2: Rarely used (hypervisors, device drivers)
- Ring 3: User mode (lowest privilege)
ARM Architecture:
- EL0: User mode (Exception Level 0)
- EL1: Kernel mode (Exception Level 1)
- EL2: Hypervisor mode
- EL3: Secure monitor mode
What Kernel Mode Can Do That User Mode Can't
Privileged Operations:
- Disable interrupts:
CLIinstruction (Clear Interrupt Flag) - Modify page tables: Change memory mappings
- Access I/O ports: Direct hardware access
- Halt CPU:
HLTinstruction - Change privilege level: Switch to user mode
- Access control registers: CR0, CR3, etc.
- Modify interrupt descriptor table: Change interrupt handlers
Example:
// User mode - This will cause a fault
cli(); // Clear interrupts - PRIVILEGED INSTRUCTION
// Result: General Protection Fault
// Kernel mode - This is allowed
cli(); // OK - we're in kernel mode
Why Kernel Mode Exists
Security:
- Prevents applications from accessing other processes' memory
- Prevents applications from crashing the system
- Enforces access control
Stability:
- Kernel code is trusted and tested
- User code bugs don't affect kernel
- Isolation between processes
Resource Management:
- Kernel controls hardware resources
- Prevents resource exhaustion
- Enforces fair scheduling
How Privilege Escalation Bugs Happen
Common Vulnerabilities:
-
Buffer Overflow in Kernel:
- Attacker overwrites kernel stack
- Changes return address to malicious code
- Code runs in kernel mode β full system access
-
Use-After-Free:
- Kernel frees memory
- Attacker reallocates and writes malicious data
- Kernel uses freed memory β executes attacker code
-
Integer Overflow:
- Attacker provides large input
- Integer wraps around
- Kernel accesses wrong memory location
-
Race Conditions:
- Attacker exploits timing window
- Kernel checks permission, then uses resource
- Attacker changes state between check and use
Example Attack Flow:
1. Attacker finds kernel vulnerability
2. Exploits bug to execute code in kernel mode
3. Code runs with kernel privileges
4. Attacker can:
- Modify any memory
- Disable security features
- Install rootkit
- Access all processes
3. Use Cases
User Mode Operations
- Application logic execution
- Standard library calls
- User-space computations
- File I/O (via system calls)
- Network operations (via system calls)
Kernel Mode Operations
- System call handling
- Interrupt handling
- Process scheduling
- Memory management (page tables)
- Device driver operations
- Security enforcement
4. Advantages & Disadvantages
User Mode Advantages
β
Security: Cannot crash system or access other processes
β
Isolation: Processes are isolated from each other
β
Stability: Bugs in user code don't affect kernel
β
Portability: Same code works on different OS
User Mode Disadvantages
β Limited access: Cannot access hardware directly
β Overhead: System calls required for kernel services
β Performance: Mode transitions are expensive
Kernel Mode Advantages
β
Full control: Direct hardware access
β
Performance: No mode transitions needed
β
Efficiency: Can optimize critical paths
Kernel Mode Disadvantages
β Responsibility: Bugs can crash entire system
β Security risk: Full system access
β Complexity: Must be carefully written
5. Best Practices
- Minimize kernel code: Less code = fewer bugs
- Validate all inputs: Never trust user data
- Use least privilege: Only use kernel mode when necessary
- Defense in depth: Multiple security layers
- Code review: Kernel code must be carefully reviewed
6. Common Pitfalls
β οΈ Mistake: Thinking user mode can access hardware (it can't, must use system calls)
β οΈ Mistake: Assuming kernel mode is "safe" (it's powerful but risky)
β οΈ Mistake: Not understanding why system calls are slow (mode transition)
β οΈ Mistake: Confusing user mode with "user space" (related but different concepts)
7. Interview Tips
Common Questions:
- "What's the difference between user mode and kernel mode?"
- "Why can't user programs disable interrupts?"
- "Can kernel code crash the OS? Why?"
- "How does privilege escalation work?"
Key Points:
- Privilege levels: Ring 0 (kernel) vs Ring 3 (user)
- What kernel can do: Disable interrupts, modify page tables, access hardware
- Security: Separation prevents user code from crashing system
- Transitions: System calls switch from user to kernel mode
8. Related Topics
- System Calls (Topic 4): How user mode calls kernel mode
- Process Management (Topic 5): How kernel manages user processes
- Security & Protection (Topic 16): How privilege levels enforce security
9. Visual Aids
Privilege Ring Diagram
βββββββββββββββββββ
β Ring 0 β β Kernel Mode
β (Kernel) β Full Privileges
βββββββββββββββββββ€
β Ring 1-2 β β Rarely Used
βββββββββββββββββββ€
β Ring 3 β β User Mode
β (Applications)β Limited Privileges
βββββββββββββββββββ
Mode Transition
User Mode Kernel Mode
β β
β System Call (trap) β
ββββββββββββββββββββββββββββ>β
β β Execute privileged
β β operation
β Return from interrupt β
β<ββββββββββββββββββββββββββββ€
β β
Memory Protection
User Process A (Ring 3)
βββββββββββββββββββ
β User Memory β β Can only access own memory
βββββββββββββββββββ
Kernel (Ring 0)
βββββββββββββββββββ
β Kernel Memory β β Can access all memory
β + All Process β
β Memory β
βββββββββββββββββββ
10. Quick Reference Summary
| Aspect | User Mode (Ring 3) | Kernel Mode (Ring 0) |
|---|---|---|
| Privilege | Low | High |
| Hardware Access | Via system calls | Direct |
| Interrupts | Cannot disable | Can disable |
| Memory | Own process only | All memory |
| Crash Impact | Own process | Entire system |
| Code Location | Applications | OS kernel |
| Security | Enforced | Trusted |
Key Principle: User mode is restricted for security; kernel mode has full control for functionality.