Skip to content

fix(netlink): harden route segment parsing and accept short GET payloads#1781

Merged
fslongjin merged 2 commits intoDragonOS-Community:masterfrom
fslongjin:fix-netlink-short-message-validation
Feb 11, 2026
Merged

fix(netlink): harden route segment parsing and accept short GET payloads#1781
fslongjin merged 2 commits intoDragonOS-Community:masterfrom
fslongjin:fix-netlink-short-message-validation

Conversation

@fslongjin
Copy link
Member

Prevent kernel panics on malformed netlink route messages by adding strict length validation before reading segment body CType bytes. Add compatibility handling for short RTM_GETLINK/RTM_GETADDR/RTM_GETROUTE payloads (rtgenmsg-sized) and keep parsing bounded by nlmsg_len. Add a c_unitest regression test that covers truncated and rtgen-sized netlink route requests.

FIXED: #1780

Prevent kernel panics on malformed netlink route messages by adding strict length
validation before reading segment body CType bytes. Add compatibility handling for
short RTM_GETLINK/RTM_GETADDR/RTM_GETROUTE payloads (rtgenmsg-sized) and keep
parsing bounded by nlmsg_len. Add a c_unitest regression test that covers truncated
and rtgen-sized netlink route requests.

Signed-off-by: longjin <longjin@DragonOS.org>
@fslongjin
Copy link
Member Author

@claude review

@github-actions github-actions bot added the Bug fix A bug is fixed in this pull request label Feb 10, 2026
@dragonos-community-ai-pr-reviewer
Copy link

dragonos-community-ai-pr-reviewer bot commented Feb 10, 2026

Claude finished @fslongjin's task in 5m 48s —— View job


Code Review for PR #1781: fix(netlink): harden route segment parsing and accept short GET payloads

I've completed my review of the changes to harden route segment parsing and add compatibility for short GET payloads. Overall, the changes look good and address the kernel panic issue. Here's my detailed feedback:

Todo List:

  • Read repository's CLAUDE.md file for repo-specific instructions
  • Examine changed files to understand the PR changes
  • Analyze code for bugs, security issues, performance problems
  • Check for coding standards and best practices
  • Provide comprehensive review feedback with specific file paths and line numbers
  • Update GitHub comment with review results

Summary of Changes

  1. Hardened length validation in kernel/src/net/socket/netlink/message/segment/mod.rs:

    • Added checks for ctype_size before reading CType bytes
    • Added validation that total_len >= total_len_with_padding
  2. Added compatibility for short GET payloads in kernel/src/net/socket/netlink/route/message/segment/mod.rs:

    • Added segment length validation before slicing payload buffer
    • Added special handling for RTM_GETLINK, RTM_GETADDR, RTM_GETROUTE with payloads smaller than their respective struct sizes
    • New helper functions (read_short_getlink_segment, etc.) that parse only the address family byte (rtgenmsg-style)
  3. Added regression test in user/apps/c_unitest/test_netlink_short_msg.c:

    • Tests various short payload scenarios to ensure no kernel panics

Positive Aspects

Good defensive programming with comprehensive length checks
Handles edge cases correctly (empty payloads, truncated messages)
Maintains compatibility with Linux behavior for rtgenmsg-sized GET requests
Clean separation of short-payload handling into helper functions
Regression test covers the exact scenarios the fix addresses

Issues Found

1. Potential Alignment Issues (Security Risk)

File: kernel/src/net/socket/netlink/message/segment/mod.rs:82
Issue: Unsafe pointer cast without alignment guarantees

let c_type = unsafe { *(c_type_bytes.as_ptr() as *const Self::CType) };

The buffer c_type_bytes may not be properly aligned for Self::CType, causing undefined behavior on some architectures. Similar issue exists in kernel/src/net/socket/netlink/route/message/segment/mod.rs:89 with CMsgSegHdr reading.

Recommendation: Use core::ptr::read_unaligned() for reading from potentially unaligned buffers:

let c_type = unsafe { core::ptr::read_unaligned(c_type_bytes.as_ptr() as *const Self::CType) };

Fix this →

2. Redundant Length Check

File: kernel/src/net/socket/netlink/message/segment/mod.rs:78-80
Issue: The condition total_len < ctype_size || buf.len() < ctype_size is redundant since buf.len() == total_len (the payload buffer length equals total_len).

Recommendation: Simplify to if total_len < ctype_size for clarity.

3. Missing Safety Comments

Issue: unsafe blocks lack // SAFETY: comments explaining why the operations are safe.

Recommendation: Add safety comments documenting alignment assumptions and buffer size guarantees.

4. Hardcoded Interface Type

File: kernel/src/net/socket/netlink/route/message/segment/mod.rs:162
Issue: InterfaceType::NETROM is hardcoded for short GETLINK segments. This may be incorrect for some interface types.

Recommendation: Consider using InterfaceType::UNSPEC or making it configurable based on the request context.

Suggestions for Improvement

  1. Consider using read_unaligned consistently throughout the netlink parsing codebase where buffers may not be properly aligned.

  2. Add more detailed error messages in validation failures to aid debugging (e.g., distinguishing "buffer too small" from "invalid family").

  3. Consider adding bounds checks for attribute parsing in the short segment helpers (though currently they use empty attribute vectors).

Minor Nitpicks

  • File: kernel/src/net/socket/netlink/route/message/segment/mod.rs:179
    family: family as i32 - consider using i32::from(family) or family.into() if AddressFamily implements Into<i32>.

  • Test file could check for specific error returns rather than just "no kernel panic", but the current approach is reasonable for a regression test.

Conclusion

The PR effectively addresses kernel panic vulnerabilities and improves robustness. With the alignment fixes and minor improvements suggested above, this will be a solid contribution.

CI Status: Tests are currently running (3 passed, 3 in progress). Build check is in progress.

@github-actions github-actions bot added the test Unitest/User space test label Feb 10, 2026
Update netlink segment parsing to use `core::ptr::read_unaligned` for reading CType and header structures. This change ensures proper handling of potentially unaligned memory, improving robustness against malformed netlink messages.

Signed-off-by: longjin <longjin@DragonOS.org>
@fslongjin fslongjin merged commit c820bf9 into DragonOS-Community:master Feb 11, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request test Unitest/User space test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG REPORT] Netlink Segment Parsing Buffer Overflow Panic

1 participant