Chapter 10 - OS Security

Updated 5 Oct 2026

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. LogicalVirtualization ทำให้ดูเหมือนใช้คนเดียวVM, Cloud
4. CryptographicEncrypt ข้อมูล/ProcessFull Disk Encryption, FileVault

Levels of Protection (เรียงจากน้อยไปมาก)

  1. No protection — แยกเวลาใช้เอง
  2. Isolation — Process รู้แค่ตัวเอง ไม่รู้จัก Process อื่น
  3. Full sharing or no sharing — Owner ประกาศว่า shared หรือ private
  4. Access Control — กำหนด Access rights แต่ละ User
  5. Sharing by capabilities — Dynamic access rights ตาม User + Context + Object (คล้าย ABAC)
  6. 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 ⭐

Valid address=Base≤address≤Bounds\boxed{\text{Valid address} = \text{Base} \leq \text{address} \leq \text{Bounds}}

  • 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)

Base address ของทุก Segment=Random ทุกครั้งที่รัน\boxed{\text{Base address ของทุก Segment} = \text{Random ทุกครั้งที่รัน}}

ป้องกัน 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     │
└─────────────────────┘

วิธีทำงาน:

  1. Function ถูก Call → Canary ถูกวาง
  2. ก่อน Return → ระบบ Check Canary ว่าเปลี่ยนไหม
  3. ถ้า 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

  1. ใช้ Surge protector หรือ UPS
  2. Update BIOS/UEFI
  3. Update OS
  4. Update Anti-virus/Anti-malware
  5. Update Firewall
  6. Maintain disks
  7. 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 ExploitVulnerability ที่ต้องการ Local access → Escalate privilege
Remote ExploitVulnerability ใน Network server → Remote attacker trigger ได้

8. Windows Security

Mandatory Integrity Controls (Vista+)

  • Objects มี Integrity level: Low, Medium, High, System
  • Subject ต้องมี Integrity เท่ากับหรือสูงกว่า Object ถึง Write ได้

Biba Model: Subject write to object only if integritysubject≥integrityobject\boxed{\text{Biba Model: Subject write to object only if integrity}_{subject} \geq \text{integrity}_{object}}

User Account Control (UAC)

  • Admin ปกติทำงานเป็น Normal user
  • ใช้ Admin rights เฉพาะเมื่อจำเป็น (Prompt ขออนุญาต)

Cryptographic Functions

Functionทำอะไร
EFS (Encrypting File System)Encrypt files/directories
BitLockerFull-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:

  1. Whitelist approved applications
  2. Patch third-party apps & OS
  3. Restrict admin privileges
  4. Defense in Depth

10. Virtualization Security

ประเภทของ Hypervisor

ประเภทคืออะไรตัวอย่างSecurity
Type 1 (Bare-metal)รันตรงบน Hardware โดยไม่มี Host OSVMware ESXi, Hyper-V, Xenปลอดภัยกว่า
Type 2 (Hosted)รันบน Host OSVirtualBox, 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 ยังไม่ถูกแก้ไข

วิธีทำงาน

  1. UEFI BIOS เริ่มต้น และ Verify Bootloader
  2. แต่ละ Component ถูก Measure (เอา Hash ของ Code)
  3. Hash ถูกเก็บใน TPM PCR (Platform Configuration Registers)
  4. ถ้า Component โดนแก้ (Malware/Rootkit) → Hash จะต่าง
  5. ระบบ 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 (ต้องรู้)

AlgorithmSignatureทำอะไร
TEE.Init→(pkTEE,skTEE)\rightarrow (pk_{TEE}, sk_{TEE})สร้าง TEE signing key pair
TEE.Install(p)→eid(p) \rightarrow eidโหลด Program เข้า Enclave
TEE.Resume(eid,f,in)→(out,ρ)(eid, f, in) \rightarrow (out, \rho)รัน Function + ส่งคืน Output + Attestation
TEE.VrfyQuote(pkTEE,p,out,ρ)→0,1(pk_{TEE}, p, out, \rho) \rightarrow {0,1}Verify attestation — 1 ถ้า Valid
TEE.Seal(data)→sealeddata(data) \rightarrow sealed_dataEncrypt ด้วย Hardware-derived key (Unseal ได้แค่ Enclave เดิม)

Data Encryption Flow ใน TEE

Step 1: Enclave Deployment eid←TEE.Install(pEnc)eid \leftarrow \text{TEE.Install}(p_{Enc})

Step 2: Remote Attestation — Enclave พิสูจน์ตัวเองให้ Client ρ←TEE.Quote(H(pEnc), nonce)\rho \leftarrow \text{TEE.Quote}(H(p_{Enc}),\ nonce) TEE.VrfyQuote(pkTEE, pEnc, ρ)=1\text{TEE.VrfyQuote}(pk_{TEE},\ p_{Enc},\ \rho) = 1

Client ตรวจ Proof ก่อนส่งข้อมูล — เหมือนร้านอาหารแสดง Health certificate ก่อนคุณนั่งกิน

Step 3: DH Key Exchange (หลัง Attestation) Ksess←KEX(Client, Enclave)K_{sess} \leftarrow \text{KEX}(\text{Client},\ \text{Enclave})

  • Enclave gen: (ske,pke)(sk_e, pk_e) → ส่ง pkepk_e ให้ Client
  • Client gen: (skc,pkc)(sk_c, pk_c) → ส่ง pkcpk_c ให้ Enclave
  • Enclave: Ksess=DH(ske,pkc)K_{sess} = DH(sk_e, pk_c)
  • Client: Ksess=DH(skc,pke)K_{sess} = DH(sk_c, pk_e)

เหมือนสองคนผสมสีลับของตัวเองออกมาได้สีเดียวกัน (DH analogy)

Step 4: Key Provisioning

CaseวิธีSecurity
Case 1Client ส่ง Key เข้า Enclave แบบ EncKsess(k)\text{Enc}_{K_{sess}}(k)ปกติ
Case 2Enclave Gen Key เองภายใน k←KeyGen(1λ)k \leftarrow \text{KeyGen}(1^\lambda)ปลอดภัยกว่า (Key ไม่เคยออกจาก Enclave)

Case 2 เหมือน Gen PIN ใน ATM เอง — แม้แต่ธนาคารก็ไม่รู้

Step 5-6: Send & Encrypt Data CT=Enc_AES(m,k)CT = \text{Enc\_AES}(m, k)

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 SGXSecure enclaves ใน Intel CPU
ARM TrustZoneSmartphones, IoT
AMD SEVCloud VMs — Encrypt VM memory
Apple Secure EnclaveFace ID, Touch ID, Key storage

14. TPM vs TEE

FeatureTPMTEE
ประเภทHardware security chip (แยกชิ้น)Secure area ภายใน CPU
จุดประสงค์Secure key storage + Boot trustSecure execution ของ Sensitive code
ตำแหน่งSeparate hardware moduleInside CPU
Use caseSecure boot, Disk encryptionSecure 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 ต้องใช้บ่อยระหว่าง ExecutionKey ผูกกับ Platform integrity
Mobile payment, Biometric, DRMSecure boot, BitLocker, Device identity
Confidential computing, IoTFirmware attestation, Anti-bootkit

15. TEE Limitations

Limitationรายละเอียด
Side-Channel AttacksCache timing, Spectre/Meltdown, Page-fault side channels
Limited Enclave MemorySGX EPC ~128–512 MB → ไม่เหมาะกับ Large ML models หรือ Big data

TEE เหมือน Safe room ที่ปลอดภัยมาก แต่ห้องเล็ก และคนนอกบางทีดูได้ว่าเราเปิดประตูบ่อยแค่ไหน (side channels)


16. OWASP Top 10 (2025) — Web Vulnerabilities

  1. Broken Access Control
  2. Security Misconfiguration
  3. Software Supply Chain Failures
  4. Cryptographic Failures
  5. Injection
  6. Insecure Design
  7. Authentication Failures
  8. Software/Data Integrity Failures
  9. Security Logging & Alerting Failures
  10. Mishandling of Exceptional Conditions