Skip to content

fix: validate key lengths from kernel reply and fix try_again loop cleanup#20

Open
MarkAtwood wants to merge 1 commit intowolfSSL:masterfrom
MarkAtwood:fix/userspace-privkey-length-try-again-leak
Open

fix: validate key lengths from kernel reply and fix try_again loop cleanup#20
MarkAtwood wants to merge 1 commit intowolfSSL:masterfrom
MarkAtwood:fix/userspace-privkey-length-try-again-leak

Conversation

@MarkAtwood
Copy link
Copy Markdown

Summary

Two correctness fixes in user-src/ipc-linux.h for the wolfguard userspace IPC code.

Fix 1: Key length validation in parse_privkey, parse_pubkey, parse_psk

parse_privkey, parse_pubkey, and parse_psk previously used mnl_attr_get_payload_len(attr) as both the allocation size and the copy length, with no check against the expected key size. A kernel response with the wrong payload length would be silently accepted, allocating and storing a buffer of the wrong size.

The fix adds a length check against WG_PRIVATE_KEY_LEN, WG_PUBLIC_KEY_LEN, and WG_SYMMETRIC_KEY_LEN respectively before allocating. On mismatch, the callback returns MNL_CB_ERROR rather than allocating, matching the error-handling pattern used in parse_allowedips. The existing parse_device and parse_peer callbacks already applied this pattern (inline length checks before memcpy); this brings the genkey-specific parsers into conformance.

Fix 2: nlg = NULL after mnlg_socket_close in try_again loops

kernel_generate_privkey, kernel_derive_pubkey, and kernel_generate_psk each have a try_again loop. In the out: cleanup block, mnlg_socket_close(nlg) was called but nlg was not set to NULL afterward. On a goto try_again, the stale (closed/freed) socket pointer remained in nlg. If mnlg_socket_open subsequently fails on the retry, the function returns early — but the out: block's if (nlg) guard is misleading: nlg is non-NULL but invalid.

The fix adds nlg = NULL immediately after mnlg_socket_close(nlg) in each out: block, making the cleanup safe regardless of retry behavior and matching defensive coding conventions.

Files changed

  • user-src/ipc-linux.h: All changes are confined to the #ifndef NO_IPC_LLCRYPTO section (parse callbacks and genkey/pubkey/psk kernel IPC functions).

Copilot AI review requested due to automatic review settings April 17, 2026 20:41
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Correctness improvements for the WireGuard userspace IPC layer by validating key sizes returned from the kernel and hardening retry-loop cleanup, plus new repo configuration/docs for Beads-based issue tracking and agent tooling.

Changes:

  • Validate netlink attribute payload lengths for private/public/psk key parsing before allocating/copying.
  • In genkey/derive/psk retry loops, null out nlg after mnlg_socket_close() to avoid stale-pointer cleanup paths.
  • Add Beads/agent integration files (docs, hooks, config) and update .gitignore for Beads/Dolt artifacts.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
user-src/ipc-linux.h Adds strict key-length validation and makes try_again cleanup safer by clearing nlg after close.
CLAUDE.md Adds AI-agent workflow instructions with Beads integration guidance.
AGENTS.md Adds agent operation guidance (non-interactive shell tips + Beads workflow).
.gitignore Ignores Dolt/Beads artifacts (but also introduces broad *.db ignore).
.claude/settings.json Configures Claude hooks to run bd prime on session start/compact.
.beads/metadata.json Adds Beads backend metadata (Dolt embedded DB config).
.beads/config.yaml Adds Beads configuration template/comments.
.beads/README.md Adds Beads usage/readme documentation for the repo.
.beads/.gitignore Adds Beads runtime/Dolt ignore rules scoped to .beads/.
.beads/hooks/prepare-commit-msg Adds Beads-managed git hook wrapper script.
.beads/hooks/pre-push Adds Beads-managed git hook wrapper script.
.beads/hooks/pre-commit Adds Beads-managed git hook wrapper script.
.beads/hooks/post-merge Adds Beads-managed git hook wrapper script.
.beads/hooks/post-checkout Adds Beads-managed git hook wrapper script.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CLAUDE.md Outdated
@@ -0,0 +1,69 @@
# Project Instructions for AI Agents
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description states that changes are confined to user-src/ipc-linux.h, but this PR also adds multiple Beads/agent configuration and documentation files (e.g., CLAUDE.md, AGENTS.md, .beads/*, .claude/settings.json, and .gitignore). Please update the PR description to reflect these additional changes, or split the Beads/agent integration into a separate PR to keep the scope aligned.

Copilot uses AI. Check for mistakes.
@MarkAtwood MarkAtwood requested a review from douzzer April 17, 2026 21:10
…eanup

In parse_privkey, parse_pubkey, and parse_psk: check that the payload
length from the kernel netlink reply equals the expected key size
(WG_PRIVATE_KEY_LEN, WG_PUBLIC_KEY_LEN, WG_SYMMETRIC_KEY_LEN) before
allocating and copying. Return MNL_CB_ERROR on mismatch.

In kernel_generate_privkey, kernel_derive_pubkey, and
kernel_generate_psk: set nlg = NULL after mnlg_socket_close() in the
out: cleanup block so that a stale pointer cannot be used on a
subsequent try_again retry path.
@MarkAtwood MarkAtwood force-pushed the fix/userspace-privkey-length-try-again-leak branch from 4f4eb2b to b32cb62 Compare April 17, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants