[PR #6965] [MERGED] fix(amneziawg): validate peer subnets before enabling kernel mode #6548

Closed
opened 2026-11-02 04:09:33 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6965
Author: @Toximiner
Created: 10/26/2026
Status: ✅ Merged
Merged: 11/3/2026
Merged by: @MHSanaei

Base: main ← Head: fix/awg-kernel-peer-subnets


📝 Commits (4)

  • 2b3907e fix(amneziawg): reject peers outside kernel interface subnets
  • 4d7680f fix(amneziawg): preserve reconciliation for legacy peer subnets
  • 8bf2424 fix: preserve legacy AWG client recovery operations
  • a46666b Merge remote-tracking branch 'origin/main' into pr/6965

📊 Changes

9 files changed (+286 additions, -9 deletions)

View changed files

📝 internal/amneziawgkern/config.go (+43 -0)
📝 internal/amneziawgkern/config_test.go (+28 -0)
📝 internal/amneziawgkern/manager_test.go (+33 -0)
📝 internal/amneziawgkern/preflight.go (+1 -1)
📝 internal/web/service/client_crud.go (+2 -2)
📝 internal/web/service/client_inbound_apply.go (+38 -5)
📝 internal/web/service/inbound_amneziawg.go (+17 -1)
📝 internal/web/service/inbound_amneziawg_test.go (+40 -0)
📝 internal/web/service/kernel_client_guard_test.go (+84 -0)

📄 Description

Summary

Reject enabling or editing an active AmneziaWG kernel-mode inbound when its peers' IPv4 AllowedIPs fall outside the configured interface subnets. Report the configuration mismatch before persisting an unroutable change.

Why

An inbound restored from a backup can have server subnet 10.9.1.0/24 while its clients still use 10.8.1.x/32. The embedded backend can serve this configuration, but switching to kernel mode currently succeeds and installs an interface route and masquerade rule for 10.9.1.0/24 only. Clients can complete the handshake while internet access fails.

Reproduction on v3.11.0:

  1. Start an embedded AmneziaWG inbound with server subnet 10.9.1.0/24 and an enabled client at 10.8.1.2/32.
  2. Install the kernel module and enable Kernel mode.
  3. Observe that the save succeeds, but the generated route/NAT does not cover the client's source address.

This change reports the offending peer, AllowedIPs and server subnet instead. The same validation covers active client creation, enabling a disabled client, newly added client addresses, and kernel-mode preflight. Both save paths reuse the subnet derivation already used by the client address allocator. Embedded configurations remain editable, and a broken inbound can still be disabled.

Runtime reconciliation deliberately preserves the previous behavior for inconsistent rows already in the database. A legacy stray peer must not prevent correctly addressed peers from starting after a restart, nor freeze updates that revoke other peers after quota or expiry checks. Such a peer still needs its address/server subnet corrected; this PR prevents new invalid interactive changes rather than automatically repairing restored data or changing renewal policy. Single-client updates retain existing addresses on active legacy peers, so metadata edits, renewals, renames and disabling remain possible. Disabled clients and disabled inbounds do not undergo the new subnet check; enabling them validates the active addresses again.

The original source of an inconsistent backup is not established by this PR. Client addresses and keys are not automatically rewritten.

Type of change

  • Bug fix

Areas affected

  • Backend (API endpoints, login, settings)
  • Multi-node (sub-nodes)

How was this tested?

Using Go 1.27.3:

  • New save-validation regression tests failed against the original implementation for local/remote inbound saves and client create/update operations.
  • Tests verify valid host addresses and contained prefixes remain accepted, broader/outside prefixes are rejected on save, and rejected client writes do not persist changes.
  • Runtime regressions cover cold startup from a legacy mixed-subnet configuration and revoking a valid peer while a stray peer remains. Both failed with the initial runtime gate, and pass after its removal.
  • Client recovery regressions cover disabling, renewal, metadata edits, renaming, disabled clients/inbounds, rejecting re-enablement or newly introduced invalid addresses, and repairing an address. The six recovery cases failed on the previous commit and pass with the update.
  • go build ./...
  • go test -p=1 -shuffle=1793050079268997904 -count=1 ./...
  • go test -race -shuffle=on -count=1 ./internal/amneziawgkern
  • go test -race -shuffle=on -count=1 ./internal/web/service -run '^TestKernelAWG'

The frontend embed directory was stubbed with internal/web/dist/.gitkeep, matching the repository's Go CI setup. An initial parallel package run hit a TUIC test's local-port collision; the service package passed when replayed with the same shuffle seed.

Breaking changes

Interactive saves of enabled kernel-mode inbounds, active client creation, client activation and newly added active-client addresses now reject IPv4 AllowedIPs outside every interface subnet with an actionable error. Existing active legacy addresses remain editable without being rewritten; disabling and renewal remain available. Align the server subnet and client addresses before enabling kernel mode. Legacy runtime reconciliation is unchanged. No database migration or client key rotation is performed.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior.
  • go build ./... and the Go test suite pass locally.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/MHSanaei/3x-ui/pull/6965 **Author:** [@Toximiner](https://github.com/Toximiner) **Created:** 10/26/2026 **Status:** ✅ Merged **Merged:** 11/3/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/awg-kernel-peer-subnets` --- ### 📝 Commits (4) - [`2b3907e`](https://github.com/MHSanaei/3x-ui/commit/2b3907eef07ef074efe0834e922d8459668e29de) fix(amneziawg): reject peers outside kernel interface subnets - [`4d7680f`](https://github.com/MHSanaei/3x-ui/commit/4d7680fcb7ad183340cf3d4af4808a0cb4bbb504) fix(amneziawg): preserve reconciliation for legacy peer subnets - [`8bf2424`](https://github.com/MHSanaei/3x-ui/commit/8bf2424b9ee7883fe91911d0d57ae5d8dc260ac8) fix: preserve legacy AWG client recovery operations - [`a46666b`](https://github.com/MHSanaei/3x-ui/commit/a46666b97bf9e0cd58c06027cd3f17385ae9eb69) Merge remote-tracking branch 'origin/main' into pr/6965 ### 📊 Changes **9 files changed** (+286 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `internal/amneziawgkern/config.go` (+43 -0) 📝 `internal/amneziawgkern/config_test.go` (+28 -0) 📝 `internal/amneziawgkern/manager_test.go` (+33 -0) 📝 `internal/amneziawgkern/preflight.go` (+1 -1) 📝 `internal/web/service/client_crud.go` (+2 -2) 📝 `internal/web/service/client_inbound_apply.go` (+38 -5) 📝 `internal/web/service/inbound_amneziawg.go` (+17 -1) 📝 `internal/web/service/inbound_amneziawg_test.go` (+40 -0) 📝 `internal/web/service/kernel_client_guard_test.go` (+84 -0) </details> ### 📄 Description ## Summary Reject enabling or editing an active AmneziaWG kernel-mode inbound when its peers' IPv4 AllowedIPs fall outside the configured interface subnets. Report the configuration mismatch before persisting an unroutable change. ## Why An inbound restored from a backup can have server subnet `10.9.1.0/24` while its clients still use `10.8.1.x/32`. The embedded backend can serve this configuration, but switching to kernel mode currently succeeds and installs an interface route and masquerade rule for `10.9.1.0/24` only. Clients can complete the handshake while internet access fails. Reproduction on v3.11.0: 1. Start an embedded AmneziaWG inbound with server subnet `10.9.1.0/24` and an enabled client at `10.8.1.2/32`. 2. Install the kernel module and enable Kernel mode. 3. Observe that the save succeeds, but the generated route/NAT does not cover the client's source address. This change reports the offending peer, AllowedIPs and server subnet instead. The same validation covers active client creation, enabling a disabled client, newly added client addresses, and kernel-mode preflight. Both save paths reuse the subnet derivation already used by the client address allocator. Embedded configurations remain editable, and a broken inbound can still be disabled. Runtime reconciliation deliberately preserves the previous behavior for inconsistent rows already in the database. A legacy stray peer must not prevent correctly addressed peers from starting after a restart, nor freeze updates that revoke other peers after quota or expiry checks. Such a peer still needs its address/server subnet corrected; this PR prevents new invalid interactive changes rather than automatically repairing restored data or changing renewal policy. Single-client updates retain existing addresses on active legacy peers, so metadata edits, renewals, renames and disabling remain possible. Disabled clients and disabled inbounds do not undergo the new subnet check; enabling them validates the active addresses again. The original source of an inconsistent backup is not established by this PR. Client addresses and keys are not automatically rewritten. ## Type of change - [x] Bug fix ## Areas affected - [x] Backend (API endpoints, login, settings) - [x] Multi-node (sub-nodes) ## How was this tested? Using Go 1.27.3: - New save-validation regression tests failed against the original implementation for local/remote inbound saves and client create/update operations. - Tests verify valid host addresses and contained prefixes remain accepted, broader/outside prefixes are rejected on save, and rejected client writes do not persist changes. - Runtime regressions cover cold startup from a legacy mixed-subnet configuration and revoking a valid peer while a stray peer remains. Both failed with the initial runtime gate, and pass after its removal. - Client recovery regressions cover disabling, renewal, metadata edits, renaming, disabled clients/inbounds, rejecting re-enablement or newly introduced invalid addresses, and repairing an address. The six recovery cases failed on the previous commit and pass with the update. - `go build ./...` - `go test -p=1 -shuffle=1793050079268997904 -count=1 ./...` - `go test -race -shuffle=on -count=1 ./internal/amneziawgkern` - `go test -race -shuffle=on -count=1 ./internal/web/service -run '^TestKernelAWG'` The frontend embed directory was stubbed with `internal/web/dist/.gitkeep`, matching the repository's Go CI setup. An initial parallel package run hit a TUIC test's local-port collision; the service package passed when replayed with the same shuffle seed. ## Breaking changes Interactive saves of enabled kernel-mode inbounds, active client creation, client activation and newly added active-client addresses now reject IPv4 AllowedIPs outside every interface subnet with an actionable error. Existing active legacy addresses remain editable without being rewritten; disabling and renewal remain available. Align the server subnet and client addresses before enabling kernel mode. Legacy runtime reconciliation is unchanged. No database migration or client key rotation is performed. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior. - [x] `go build ./...` and the Go test suite pass locally. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #6965] fix(amneziawg): validate peer subnets before enabling kernel mode to [PR #6965] [MERGED] fix(amneziawg): validate peer subnets before enabling kernel mode 2026-11-07 14:33:55 +07:00
Sign in to join this conversation.
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
MHSanaei/3x-ui#6548
No description provided.