Chapter 10 - OS Security
CSS454 — OS Security (Chapter 10)
เป้าหมาย: เข้าใจว่า OS ต้องป้องกันอะไร, ป้องกัน Memory ยังไง, Harden ระบบยังไง, และ Virtualization + TEE/TPM ทำงานยังไง
1. ทำไม OS Security ถึงสำคัญ?
- OS คือ Controller ของทุก Resource → เป็น Primary target ของการโจมตี
- OS คือ First Line of Defense:
- ป้องกัน User จากกัน
- Identify & Authenticate Users
- จัดการ Fair sharing ของ Hardware
อุปมา: OS เหมือนยามรักษาความปลอดภัยของตึก — ควบคุมว่าใครเข้าได้, ไปที่ไหนได้, และหยุดผู้บุกรุก
OS ต้อง Protect:
- Memory, Networks, Shared I/O devices (disk, printer)
- Shared programs, Shareable data
ถ้าถามว่า OS ต้อง protect อะไร → ตอบ User และ Resource
2. Layered OS Security
┌─────────────────────────────────────┐
│ Subprocesses / User Apps │ ← ยิ่งสูง ยิ่ง Untrusted
├─────────────────────────────────────┤
│ User Processes │
├─────────────────────────────────────┤
│ Compilers, Database Managers │
├─────────────────────────────────────┤
│ Utility Functions │
├─────────────────────────────────────┤
│ File Systems, Device Allocation │
├─────────────────────────────────────┤
│ Scheduling, Sharing, Memory Mgmt │
├─────────────────────────────────────┤
│ Synchronization, Allocation │
├─────────────────────────────────────┤
│ Security Functions │
├─────────────────────────────────────┤
│ Hardware │ ← Foundation
└─────────────────────────────────────┘
- แต่ละ Layer ต้องมี Security measures ของตัวเอง
- แต่ละ Layer เปราะบางต่อการโจมตีจาก Layer ล่าง
อุปมา: เหมือนชั้นในตึก — ถ้า Foundation (hardware) โดนเจาะ ทุกอย่างข้างบนก็ไม่ปลอดภัย (rootkit, bootkit)
3. Types of Separation
OS ใช้ Separation เพื่อแยก Process/User ออกจากกัน
| ประเภท | วิธีทำ | ตัวอย่าง |
|---|---|---|
| 1. Physical | ใช้ Hardware คนละชิ้น | Process A กับ B ใช้ CPU คนละตัว |
| 2. Temporal | รันคนละเวลา | สลับกันรันตาม Security requirement |
| 3. Logical | Virtualization ทำให้ดูเหมือนใช้คนเดียว | VM, Cloud |
| 4. Cryptographic | Encrypt ข้อมูล/Process | Full Disk Encryption, FileVault |
Levels of Protection (เรียงจากน้อยไปมาก)
- No protection — แยกเวลาใช้เอง
- Isolation — Process รู้แค่ตัวเอง ไม่รู้จัก Process อื่น
- Full sharing or no sharing — Owner ประกาศว่า shared หรือ private
- Access Control — กำหนด Access rights แต่ละ User
- Sharing by capabilities — Dynamic access rights ตาม User + Context + Object (คล้าย ABAC)
- Limit use of object — เช่น ดู Document ได้ แต่ Copy ไม่ได้ / ดู Average salary ได้ แต่ดูของ John ไม่ได้
4. Memory and Address Protection
4.1 Fence Protection
- แบ่ง Memory เป็น OS space (0 → n) และ User space (n+1 → High)
- Fence ที่ address
nเป็น Boundary
| OS Space (0 to n) | User Space (n+1 to High) |
↑ Fence
4.2 Fence Registers
- Register เก็บ Fence address → เปลี่ยนได้ยืดหยุ่น
- OS versions ต่างกัน → Fence value ต่างกัน
4.3 Base and Bounds Registers ⭐
- Base Register: Starting address ของ User's program
- Bounds Register: Ending address ของ User's program
- User A, B, C มี Base/Bounds ของตัวเอง → ป้องกัน Isolation ได้ดีมาก
อุปมา: เหมือนรั้วที่ดิน — แต่ละคนมีที่ดินของตัวเอง (Base ถึง Bounds) ก้าวเข้าที่ดินคนอื่นไม่ได้
4.4 Tagged Architecture
- ทุก Word ใน Memory มี Extra bits บอก Access rights
- Bits:
R(Read-only),RW(Read/Write),X(Execute-only) - Set ได้แค่โดย Privileged (OS) instructions
- ถูก Test ทุกครั้งที่ Instruction เข้าถึง Memory นั้น
4.5 Virtual Memory: Segmentation
- Program แบ่งเป็น Segments ตาม Logic (MAIN, DATA, SUB...)
- Segment Translation Table Map ชื่อ Segment → Physical address
- ประโยชน์:
- Address reference ทุกอันถูก Check
- Data classes ต่างกัน → Protection level ต่างกัน
- Share Segment ระหว่าง User ได้ด้วย Access rights ต่างกัน
4.6 Virtual Memory: Paging
- Program แบ่งเป็น Pages ขนาดเท่ากัน (2KB, 8KB, 16KB...)
- Memory แบ่งเป็น Page Frames ขนาดเท่ากัน
- Page Translation Table Map Logical page → Physical frame
- Paging = Efficient memory management
4.7 Combined Paging + Segmentation
- แต่ละ Segment มี Page Translation Table ของตัวเอง
- Address: Segment → Page Table → Physical Address
- รวมข้อดีของทั้งสองแบบ
- Security ดีกว่า แต่ Performance ขึ้นกับขนาด Program
5. Memory Corruption Protection
5.1 Address Space Layout Randomization (ASLR)
ป้องกัน Buffer Overflow:
- ปกติ: Attacker รู้ว่า Shellcode อยู่ที่ไหน → กระโดดไปได้เลย
- มี ASLR: Stack address เปลี่ยนทุกรัน → เดาไม่ถูก → โปรแกรม Crash
ป้องกัน Return-to-libc:
- ปกติ: Attacker ใช้
system("/bin/sh")จาก libc แทนการ Inject code - มี ASLR: libc address เปลี่ยนทุกรัน → ต้อง Leak base address ก่อน (ยากมาก)
5.2 Stack Canary
Stack Canary = Random value เล็กๆ วางไว้บน Stack ก่อน Return address
Stack Layout:
┌─────────────────────┐
│ Local Variables │
│ [ buffer[16] ] │
├─────────────────────┤
│ Canary (random) │ ← ตัวนี้แหละ
├─────────────────────┤
│ Return Address │
└─────────────────────┘
วิธีทำงาน:
- Function ถูก Call → Canary ถูกวาง
- ก่อน Return → ระบบ Check Canary ว่าเปลี่ยนไหม
- ถ้า Canary เปลี่ยน (โดน Buffer overflow) → Program abort ทันที ก่อนใช้ Return address ที่โดน Corrupt
Concept คล้าย Hash — ถ้าค่าเปลี่ยน = โดนแตะ
5.3 Buffer Overflow — Code Example
Unsafe (ห้ามใช้!):
void vulnerable() {
char buffer[16];
gets(buffer); // ⚠️ ไม่มี bounds checking เลย!
}Safe:
void safe() {
char buffer[16];
fgets(buffer, sizeof(buffer), stdin); // ✅ จำกัดจำนวน chars
}C = "จัดการ Memory เอง" 💀 | Swift = "ระบบจัดการให้" 🍎
6. OS Hardening
OS Hardening = Configure OS ให้ Secure, Update, สร้าง Policies, ลบ Apps/Services ที่ไม่จำเป็น → ลด Attack surface
6 ขั้นตอนหลัก
Step 1: Install & Patch OS
- ติดตั้ง OS แล้ว Patch ทันที ก่อนเปิดใช้งาน
- ระบบที่ยังไม่ Patch = เป้าโจมตีง่ายมาก
Step 2: Remove Unnecessary Services/Apps/Protocols
"FEWER IS BETTER!"
- ยิ่งมี Software น้อย → Attack surface ยิ่งเล็ก
- อย่าใช้ Default configuration — Default เน้น Ease-of-use ไม่ใช่ Security
- ติดตั้งเพิ่มทีหลังเมื่อต้องการ
Step 3: Configure Users, Groups & Authentication
- Elevated privileges → จำกัดเฉพาะคนที่จำเป็น เฉพาะเมื่อจำเป็น
- Default accounts ที่ไม่ใช้ → Remove หรือ Disable
- กำหนด Authentication credential policies
Step 4: Configure Resource Controls
- ตั้ง Appropriate permissions บน Data และ Resources
- ใช้ Security hardening guides เป็น Reference
Step 5: Install Additional Security Controls
- Anti-virus, Host-based firewalls
- IDS/IPS, Application whitelisting
Step 6: Test System Security
- ใช้ Checklist จาก Security hardening guides
- Scan หา Known vulnerabilities
- ทำหลัง Initial hardening และทำซ้ำเป็นระยะ
7 Steps Well-Maintained Computer
- ใช้ Surge protector หรือ UPS
- Update BIOS/UEFI
- Update OS
- Update Anti-virus/Anti-malware
- Update Firewall
- Maintain disks
- Create system image (Backup)
7. Linux/Unix Security
- Patch management — อัพเดท Security patches สม่ำเสมอ
- Config files อยู่ที่
/etcหรือ Installation tree - Dot files (
.bashrc,.vimrc,.gitconfig) — Hidden config ใน Home directory ของ User แต่ละคน
Exploit Types
| Type | คืออะไร |
|---|---|
| Local Exploit | Vulnerability ที่ต้องการ Local access → Escalate privilege |
| Remote Exploit | Vulnerability ใน Network server → Remote attacker trigger ได้ |
8. Windows Security
Mandatory Integrity Controls (Vista+)
- Objects มี Integrity level: Low, Medium, High, System
- Subject ต้องมี Integrity เท่ากับหรือสูงกว่า Object ถึง Write ได้
User Account Control (UAC)
- Admin ปกติทำงานเป็น Normal user
- ใช้ Admin rights เฉพาะเมื่อจำเป็น (Prompt ขออนุญาต)
Cryptographic Functions
| Function | ทำอะไร |
|---|---|
| EFS (Encrypting File System) | Encrypt files/directories |
| BitLocker | Full-disk encryption ด้วย AES |
Logon Process
- Admin (Admin Approval Mode): ได้ทั้ง Full Admin Token + Standard User Token → Explorer.exe รันด้วย Standard token
- Standard User: ได้แค่ Standard User Token → Explorer.exe รันด้วย Limited privilege
9. Defense in Depth
"ใช้หลาย Layer ของ Defense เพื่อรับมือปัญหา Technical, Personnel, และ Operational"
Attack
↓
┌────────────────────────────┐
│ Hardware Firewall/Router │
├────────────────────────────┤
│ OS / Software Firewall │
├────────────────────────────┤
│ Antivirus / Antimalware │
├────────────────────────────┤
│ Security Patches │
├────────────────────────────┤
│ User Account Controls │
└────────────────────────────┘
↓ Data (Core)
Layers: Policies → Physical → Perimeter → Internal Network → Host → Application → Data
อุปมา: เหมือนหัวหอม — Attacker ต้องเจาะทะลุทุก Layer เพื่อถึง Core
ASD Top 4 Prevention Strategies:
- Whitelist approved applications
- Patch third-party apps & OS
- Restrict admin privileges
- Defense in Depth
10. Virtualization Security
ประเภทของ Hypervisor
| ประเภท | คืออะไร | ตัวอย่าง | Security |
|---|---|---|---|
| Type 1 (Bare-metal) | รันตรงบน Hardware โดยไม่มี Host OS | VMware ESXi, Hyper-V, Xen | ปลอดภัยกว่า |
| Type 2 (Hosted) | รันบน Host OS | VirtualBox, VMware Workstation | ปลอดภัยน้อยกว่า |
Virtualization Security Issues
- Guest OS Isolation — ป้องกันไม่ให้ Guest เข้า Resource ที่ไม่ได้รับอนุญาต
- Hypervisor Monitoring — Hypervisor มี Privileged access ถึงทุก Guest → ถ้าถูกเจาะ = เสียทุก VM
- Image/Snapshot Management — Attacker พยายาม View/Modify VM images
Securing Virtualization
Hypervisor Security:
- ใช้ Type 1 hypervisor
- Minimize attack surface (disable unused services)
- Patch regularly
- ใช้ TPM + Secure Boot
VM Hardening:
- ใช้ Hardened VM Images
- Isolate VMs
- ติดตั้ง Anti-malware ใน VM
Network Security:
- Virtual Firewalls — ระหว่าง VM หรือที่ vSwitch
- Microsegmentation — SDN/VMware NSX สำหรับ Per-VM policy
- Secure vSwitches — ป้องกัน Promiscuous mode, MAC spoofing, Forged transmissions
Security Tools:
- Virtualization-aware AV — Trend Micro Deep Security
- VM Introspection (VMI) — Inspect VM จาก Hypervisor โดยไม่ต้องมี Agent
- SIEM — รวม Virtualization logs สำหรับ Correlation
11. TPM (Trusted Platform Module)
Hardware security chip ฝังอยู่ใน Motherboard (หรือ Virtual ใน Cloud) Apple equivalent: T2 chip / Secure Enclave
TPM Functions
- Secure store cryptographic keys
- Random number generation (PRNG)
- Secure hashing and signing
- Integrity measurements ระหว่าง Boot
- Device identity attestation
Computer
├── CPU + OS + Apps
└── TPM Chip
├── Secure Key Storage
├── PCR Registers (บันทึก Boot hashes)
├── Cryptographic Unit
├── Random Generator
└── Trusted Boot
Virtual TPM (vTPM)
- Cloud providers ให้ vTPM (Azure, AWS NitroTPM)
- ทำ Boot integrity และ Encryption ได้เหมือน Physical TPM
12. Trusted Boot (Measured Boot)
Boot process integrity checking — ตรวจสอบว่าทุก Component ที่โหลดตอน Boot ยังไม่ถูกแก้ไข
วิธีทำงาน
- UEFI BIOS เริ่มต้น และ Verify Bootloader
- แต่ละ Component ถูก Measure (เอา Hash ของ Code)
- Hash ถูกเก็บใน TPM PCR (Platform Configuration Registers)
- ถ้า Component โดนแก้ (Malware/Rootkit) → Hash จะต่าง
- ระบบ Refuse to boot หรือ Alert security tools
อุปมา: เหมือน Tamper-evident seal — ถ้ามีการเปิดระหว่าง Boot chain ก็จะรู้
13. TEE (Trusted Execution Environment)
Secure area ภายใน CPU — Execute sensitive code ใน Isolated environment ที่แม้ OS/Hypervisor จะโดนเจาะก็ยังปลอดภัย
CPU
├── Normal World (OS, Apps) ← ถูกเจาะได้
└── Secure World (TEE) ← Protected แม้ OS จะ Compromised
├── Isolated Memory
├── Crypto Keys
└── Trusted Apps
อุปมา: เหมือนห้องกระจกกันกระสุน — มองเห็นได้ว่ารันอยู่ แต่แตะหรือแก้ไขไม่ได้
TEE Algorithms (ต้องรู้)
| Algorithm | Signature | ทำอะไร |
|---|---|---|
| TEE.Init | สร้าง TEE signing key pair | |
| TEE.Install | โหลด Program เข้า Enclave | |
| TEE.Resume | รัน Function + ส่งคืน Output + Attestation | |
| TEE.VrfyQuote | Verify attestation — 1 ถ้า Valid | |
| TEE.Seal | Encrypt ด้วย Hardware-derived key (Unseal ได้แค่ Enclave เดิม) |
Data Encryption Flow ใน TEE
Step 1: Enclave Deployment
Step 2: Remote Attestation — Enclave พิสูจน์ตัวเองให้ Client
Client ตรวจ Proof ก่อนส่งข้อมูล — เหมือนร้านอาหารแสดง Health certificate ก่อนคุณนั่งกิน
Step 3: DH Key Exchange (หลัง Attestation)
- Enclave gen: → ส่ง ให้ Client
- Client gen: → ส่ง ให้ Enclave
- Enclave:
- Client:
เหมือนสองคนผสมสีลับของตัวเองออกมาได้สีเดียวกัน (DH analogy)
Step 4: Key Provisioning
| Case | วิธี | Security |
|---|---|---|
| Case 1 | Client ส่ง Key เข้า Enclave แบบ | ปกติ |
| Case 2 | Enclave Gen Key เองภายใน | ปลอดภัยกว่า (Key ไม่เคยออกจาก Enclave) |
Case 2 เหมือน Gen PIN ใน ATM เอง — แม้แต่ธนาคารก็ไม่รู้
Step 5-6: Send & Encrypt Data
Intel SGX — Enclave Memory (EPC)
- Each enclave มี Isolated memory แต่ Share EPC Pool กับ Enclave อื่น
- EPC size จำกัด (~128 MB เก่า, ~256 MB+ ใหม่)
เหมือน Apartment ในตึก — ห้องล็อกของตัวเอง แต่ที่จอดรถ (EPC) ใช้ร่วมกัน
TEE Technologies
| Technology | ใช้ที่ไหน |
|---|---|
| Intel SGX | Secure enclaves ใน Intel CPU |
| ARM TrustZone | Smartphones, IoT |
| AMD SEV | Cloud VMs — Encrypt VM memory |
| Apple Secure Enclave | Face ID, Touch ID, Key storage |
14. TPM vs TEE
| Feature | TPM | TEE |
|---|---|---|
| ประเภท | Hardware security chip (แยกชิ้น) | Secure area ภายใน CPU |
| จุดประสงค์ | Secure key storage + Boot trust | Secure execution ของ Sensitive code |
| ตำแหน่ง | Separate hardware module | Inside CPU |
| Use case | Secure boot, Disk encryption | Secure apps, Confidential computing |
TPM + TEE ทำงานด้วยกัน
1. TPM → Verify system integrity ตอน Boot
2. Secure Boot → โหลด Trusted OS
3. TEE → รัน Sensitive operations ปลอดภัย
TPM = ยามที่ทางเข้าตึก (Check ตอน Boot) TEE = Executive office ที่ล็อกอยู่ข้างใน (ที่ทำงาน Sensitive จริงๆ)
เมื่อไหรใช้อะไร?
| ใช้ TEE เมื่อ... | ใช้ TPM เมื่อ... |
|---|---|
| Key ต้องใช้บ่อยระหว่าง Execution | Key ผูกกับ Platform integrity |
| Mobile payment, Biometric, DRM | Secure boot, BitLocker, Device identity |
| Confidential computing, IoT | Firmware attestation, Anti-bootkit |
15. TEE Limitations
| Limitation | รายละเอียด |
|---|---|
| Side-Channel Attacks | Cache timing, Spectre/Meltdown, Page-fault side channels |
| Limited Enclave Memory | SGX EPC ~128–512 MB → ไม่เหมาะกับ Large ML models หรือ Big data |
TEE เหมือน Safe room ที่ปลอดภัยมาก แต่ห้องเล็ก และคนนอกบางทีดูได้ว่าเราเปิดประตูบ่อยแค่ไหน (side channels)
16. OWASP Top 10 (2025) — Web Vulnerabilities
- Broken Access Control
- Security Misconfiguration
- Software Supply Chain Failures
- Cryptographic Failures
- Injection
- Insecure Design
- Authentication Failures
- Software/Data Integrity Failures
- Security Logging & Alerting Failures
- Mishandling of Exceptional Conditions