[PR #272] fix: retain session keys for the maximum configured lifetime #272

Open
opened 2026-10-27 06:20:56 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: master ← Head: codex/key-lifetime-regression


📝 Commits (2)

  • 8f90300 test: add native key lifetime regression coverage
  • 54a362c fix: use maximum lifetime for session key cleanup

📊 Changes

4 files changed (+503 additions, -2 deletions)

View changed files

📝 src/timers.c (+3 -2)
➕ tests/README.md (+67 -0)
➕ tests/key-cleanup.py (+117 -0)
➕ tests/key-lifetime.py (+316 -0)

📄 Description

Summary

 on(session_derived)
-  cleanup_deadline = 3 * random(RejectAfterTime.lo, RejectAfterTime.hi)
+  cleanup_deadline = 3 * RejectAfterTime.hi

For a wide RejectAfterTime range such as 1-210, wg_timers_session_derived() can currently schedule timer_zero_key_material after just 3 seconds, while the keys should remain usable for up to 210 seconds. Snapshot the configured range with READ_ONCE and use its upper bound, matching amneziawg-go's keychainExpireTime(). The zero/default and fixed-value cases retain their current deadlines.

Add a deterministic callback test using the actual C function/range helpers with a timer/RNG shim, plus an opt-in native RX/TX integration test. The latter reproduces #227 / #240 with fresh Noise IKpsk2 sessions and deliberately ignores rehandshake requests, so rekeying cannot hide an incorrect expiration boundary.

This addresses the session-derived cleanup path, complementing the separate fixes in #238 (failed-handshake cleanup), #239 (refresh clamp), and #240 (data-path lifetime). It does not duplicate those code changes. Passing the complete native --expect fixed suite requires the related data-path corrections as well; this timer-only change does not fix all of #227 by itself.

Evidence

  • Before: key-cleanup.py --source <original src/timers.c> deterministically fails when the RNG shim selects a valid low endpoint:
    1-210 RNG=lo: cleanup=3, expected=630 seconds
    2-65535 RNG=lo: cleanup=6, expected=196605 seconds
    
    After: the actual patched callback passes the default, fixed, wide and maximum-range cases with both RNG endpoints:
    PASS: 8 cleanup deadline cases (including lowest RNG output)
    
  • Before (native): the unmodified module still permits RX/TX with the same session keys at age 6 seconds for RejectAfterTime = 4.
    After (native, combined candidate): expiry works for 4 and 4-8; with 1-210, RX/TX works at age 185 seconds and expires after 210 seconds. The combined candidate includes the RX/TX upper-bound change, this cleanup change, failed-handshake cleanup correction and refresh clamp. Failed initial handshakes or initial RX/TX are errors, not successful expiration tests.
  • Native integration executed against a module based on v3.1.20260906 / 4569c4c67f3a, Linux 7.2.12-x64v3-xanmod1, Python 3 + cryptography. The published native script is AST-equivalent to that regression (imports split and a hash lambda rewritten as a function). The callback test compiles with -Wall -Wextra -Werror; Python compilation, Black and Ruff pass.
  • No tests are wired into CI. Callback tests need a C compiler and no root; native tests need root and an installed module, and long mode takes about four minutes. A single native wide-range run does not exhaust all random outcomes; the callback test forces the previously problematic RNG endpoints.

Merge Danger

Door: two-way

The code change affects only the session-derived key-material cleanup deadline. Reverting restores the prior choice. No protocol, config-format, or UAPI changes.

Blast Radius: kernel

Wide ranges can retain key material longer than a randomly chosen low deadline, up to the existing three-times-maximum lifetime policy used by the Go implementation. Root-only integration tests use generated keys in a fresh network namespace, reject an existing namespace with their reserved name, and clean up on exit/timeout. They do not modify host routes, firewall rules, or existing peers. Callback shims validate the calculation, not kernel scheduling/concurrency.


🔄 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/272 **Author:** [@YoungReckless4](https://github.com/YoungReckless4) **Created:** 10/25/2026 **Status:** 🔄 Open **Base:** `master` ← **Head:** `codex/key-lifetime-regression` --- ### 📝 Commits (2) - [`8f90300`](https://github.com/amnezia-vpn/amneziawg-linux-kernel-module/commit/8f90300fb2ba284ddedc0766617b166c824b39a2) test: add native key lifetime regression coverage - [`54a362c`](https://github.com/amnezia-vpn/amneziawg-linux-kernel-module/commit/54a362c3055aa715301092b936c1b5ddd9751abb) fix: use maximum lifetime for session key cleanup ### 📊 Changes **4 files changed** (+503 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `src/timers.c` (+3 -2) ➕ `tests/README.md` (+67 -0) ➕ `tests/key-cleanup.py` (+117 -0) ➕ `tests/key-lifetime.py` (+316 -0) </details> ### 📄 Description ## Summary ```diff on(session_derived) - cleanup_deadline = 3 * random(RejectAfterTime.lo, RejectAfterTime.hi) + cleanup_deadline = 3 * RejectAfterTime.hi ``` For a wide `RejectAfterTime` range such as `1-210`, `wg_timers_session_derived()` can currently schedule `timer_zero_key_material` after just 3 seconds, while the keys should remain usable for up to 210 seconds. Snapshot the configured range with `READ_ONCE` and use its upper bound, matching [amneziawg-go's keychainExpireTime()](https://github.com/amnezia-vpn/amneziawg-go/blob/master/device/timers.go). The zero/default and fixed-value cases retain their current deadlines. Add a deterministic callback test using the actual C function/range helpers with a timer/RNG shim, plus an opt-in native RX/TX integration test. The latter reproduces #227 / #240 with fresh Noise IKpsk2 sessions and deliberately ignores rehandshake requests, so rekeying cannot hide an incorrect expiration boundary. This addresses the session-derived cleanup path, complementing the separate fixes in #238 (failed-handshake cleanup), #239 (refresh clamp), and #240 (data-path lifetime). It does not duplicate those code changes. Passing the complete native `--expect fixed` suite requires the related data-path corrections as well; this timer-only change does not fix all of #227 by itself. ## Evidence - **Before:** `key-cleanup.py --source <original src/timers.c>` deterministically fails when the RNG shim selects a valid low endpoint: ```text 1-210 RNG=lo: cleanup=3, expected=630 seconds 2-65535 RNG=lo: cleanup=6, expected=196605 seconds ``` **After:** the actual patched callback passes the default, fixed, wide and maximum-range cases with both RNG endpoints: ```text PASS: 8 cleanup deadline cases (including lowest RNG output) ``` - **Before (native):** the unmodified module still permits RX/TX with the same session keys at age 6 seconds for `RejectAfterTime = 4`. **After (native, combined candidate):** expiry works for `4` and `4-8`; with `1-210`, RX/TX works at age 185 seconds and expires after 210 seconds. The combined candidate includes the RX/TX upper-bound change, this cleanup change, failed-handshake cleanup correction and refresh clamp. Failed initial handshakes or initial RX/TX are errors, not successful expiration tests. - Native integration executed against a module based on `v3.1.20260906` / `4569c4c67f3a`, Linux `7.2.12-x64v3-xanmod1`, Python 3 + `cryptography`. The published native script is AST-equivalent to that regression (imports split and a hash lambda rewritten as a function). The callback test compiles with `-Wall -Wextra -Werror`; Python compilation, Black and Ruff pass. - No tests are wired into CI. Callback tests need a C compiler and no root; native tests need root and an installed module, and long mode takes about four minutes. A single native wide-range run does not exhaust all random outcomes; the callback test forces the previously problematic RNG endpoints. ## Merge Danger **Door:** two-way The code change affects only the session-derived key-material cleanup deadline. Reverting restores the prior choice. No protocol, config-format, or UAPI changes. **Blast Radius:** kernel Wide ranges can retain key material longer than a randomly chosen low deadline, up to the existing three-times-maximum lifetime policy used by the Go implementation. Root-only integration tests use generated keys in a fresh network namespace, reject an existing namespace with their reserved name, and clean up on exit/timeout. They do not modify host routes, firewall rules, or existing peers. Callback shims validate the calculation, not kernel scheduling/concurrency. --- <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#272
No description provided.