[PR #273] fix: harden obfuscation updates and handshake error handling #273

Open
opened 2026-10-30 04:59:56 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/amnezia-vpn/amneziawg-linux-kernel-module/pull/273
Author: @YoungReckless4
Created: 10/29/2026
Status: 🔄 Open

Base: master ← Head: codex/awg31-kernel-hardening


📝 Commits (1)

  • 293ce22 fix: harden obfuscation updates and handshake error handling

📊 Changes

15 files changed (+734 additions, -60 deletions)

View changed files

📝 src/device.c (+1 -1)
📝 src/device.h (+2 -1)
📝 src/header_protection.c (+12 -8)
📝 src/header_protection.h (+3 -2)
📝 src/junk.c (+33 -28)
📝 src/netlink.c (+42 -13)
📝 src/receive.c (+1 -1)
📝 src/send.c (+11 -6)
📝 src/type.h (+8 -0)
➕ tests/README.md (+83 -0)
➕ tests/build-hardening-kernel.sh (+27 -0)
➕ tests/cps-parser.py (+78 -0)
➕ tests/cps-shim.h (+60 -0)
➕ tests/hardening-vm.py (+97 -0)
➕ tests/kernel-hardening.py (+276 -0)

📄 Description

Summary

Obfuscation configuration errors and allocation failures can corrupt memory or crash the kernel. HPK packet reception also acquires a sleeping lock from the UDP receive path.
The malformed-CPS and invalid-range cases enter through administrative netlink configuration.

 HPK snapshot
-  rwsem in softirq; nested read acquisition in netlink dump
+  BH-safe rwlock; one acquisition per snapshot
 CPS update
-  discard the old packet before validating/allocating its replacement
+  validate bounded lengths, prepare a replacement, then publish under the lock
 Junk handshake prelude
-  unchecked allocation and independently changing Jmin/Jmax
+  checked allocation, one range snapshot, automatic retry after ENOMEM

Reject reversed device/peer ranges through netlink, including partial Jmin/Jmax updates. Preserve valid settings, packet encoding and the configured junk sequence; an allocation failure defers the attempt rather than omitting configured junk packets.

Evidence

  • Before: pinned v3.1.20260906 fails all three native regression cases for range validation, CPS update preservation and HPK RX locking. The debug kernel reports BUG: sleeping function called from invalid context and Invalid wait context in awg_header_protection_init.
    After: all six native cases pass in an offline, two-vCPU Linux 6.6.87 QEMU VM with KASAN, lockdep and DEBUG_ATOMIC_SLEEP. They include bidirectional traffic in ordinary WireGuard, legacy AWG and HPK-enabled AWG 3.1 modes.
  • Before: stack-filtered FAILSLAB injection at the junk-buffer allocation produces a KASAN NULL dereference and kernel panic.
    After: the same injection is consumed, the attempt retries automatically, and all six configured junk packets precede the handshake. No BUG/KASAN/Oops/RCU diagnostics.
  • Before: the actual CPS parser loses the old packet after an invalid update and accepts negative lengths.
    After: four ASan/UBSan groups pass, including aggregate limits and every allocation-failure point.
  • Module builds pass on Linux 6.6.87 and 7.2.13-x64v3-xanmod1 (Clang 21). This change applies alongside the existing RejectAfterTime patch; that separate patch is not included here.

Reproduction helpers and their limits are documented in tests/README.md. The tests do not establish a cause for historical client stalls or measure production/mobile-network reliability.

Merge Danger

Door: two-way

Revert the module change to roll back; no database or client-key migration is required.

Blast Radius: networking

Changes affect HPK locking, CPS configuration publication and handshake retries. Reversed ranges and CPS packets exceeding the IPv4 UDP payload limit now fail explicitly. Valid packet formats and configured obfuscation parameters remain unchanged.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/amnezia-vpn/amneziawg-linux-kernel-module/pull/273 **Author:** [@YoungReckless4](https://github.com/YoungReckless4) **Created:** 10/29/2026 **Status:** 🔄 Open **Base:** `master` ← **Head:** `codex/awg31-kernel-hardening` --- ### 📝 Commits (1) - [`293ce22`](https://github.com/amnezia-vpn/amneziawg-linux-kernel-module/commit/293ce22b4130516a17cc20bbb5743cbcd802c59b) fix: harden obfuscation updates and handshake error handling ### 📊 Changes **15 files changed** (+734 additions, -60 deletions) <details> <summary>View changed files</summary> 📝 `src/device.c` (+1 -1) 📝 `src/device.h` (+2 -1) 📝 `src/header_protection.c` (+12 -8) 📝 `src/header_protection.h` (+3 -2) 📝 `src/junk.c` (+33 -28) 📝 `src/netlink.c` (+42 -13) 📝 `src/receive.c` (+1 -1) 📝 `src/send.c` (+11 -6) 📝 `src/type.h` (+8 -0) ➕ `tests/README.md` (+83 -0) ➕ `tests/build-hardening-kernel.sh` (+27 -0) ➕ `tests/cps-parser.py` (+78 -0) ➕ `tests/cps-shim.h` (+60 -0) ➕ `tests/hardening-vm.py` (+97 -0) ➕ `tests/kernel-hardening.py` (+276 -0) </details> ### 📄 Description ## Summary Obfuscation configuration errors and allocation failures can corrupt memory or crash the kernel. HPK packet reception also acquires a sleeping lock from the UDP receive path. The malformed-CPS and invalid-range cases enter through administrative netlink configuration. ```diff HPK snapshot - rwsem in softirq; nested read acquisition in netlink dump + BH-safe rwlock; one acquisition per snapshot CPS update - discard the old packet before validating/allocating its replacement + validate bounded lengths, prepare a replacement, then publish under the lock Junk handshake prelude - unchecked allocation and independently changing Jmin/Jmax + checked allocation, one range snapshot, automatic retry after ENOMEM ``` Reject reversed device/peer ranges through netlink, including partial Jmin/Jmax updates. Preserve valid settings, packet encoding and the configured junk sequence; an allocation failure defers the attempt rather than omitting configured junk packets. ## Evidence - **Before:** pinned `v3.1.20260906` fails all three native regression cases for range validation, CPS update preservation and HPK RX locking. The debug kernel reports `BUG: sleeping function called from invalid context` and `Invalid wait context` in `awg_header_protection_init`. **After:** all six native cases pass in an offline, two-vCPU Linux 6.6.87 QEMU VM with KASAN, lockdep and DEBUG_ATOMIC_SLEEP. They include bidirectional traffic in ordinary WireGuard, legacy AWG and HPK-enabled AWG 3.1 modes. - **Before:** stack-filtered FAILSLAB injection at the junk-buffer allocation produces a KASAN NULL dereference and kernel panic. **After:** the same injection is consumed, the attempt retries automatically, and all six configured junk packets precede the handshake. No BUG/KASAN/Oops/RCU diagnostics. - **Before:** the actual CPS parser loses the old packet after an invalid update and accepts negative lengths. **After:** four ASan/UBSan groups pass, including aggregate limits and every allocation-failure point. - Module builds pass on Linux 6.6.87 and `7.2.13-x64v3-xanmod1` (Clang 21). This change applies alongside the existing RejectAfterTime patch; that separate patch is not included here. Reproduction helpers and their limits are documented in `tests/README.md`. The tests do not establish a cause for historical client stalls or measure production/mobile-network reliability. ## Merge Danger **Door:** two-way Revert the module change to roll back; no database or client-key migration is required. **Blast Radius:** networking Changes affect HPK locking, CPS configuration publication and handshake retries. Reversed ranges and CPS packets exceeding the IPv4 UDP payload limit now fail explicitly. Valid packet formats and configured obfuscation parameters remain unchanged. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
amnezia-vpn/amneziawg-linux-kernel-module#273
No description provided.