Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

privsep: Ensure we recv for real after a successful recv MSG_PEEK#556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
rsmarples wants to merge7 commits intomaster
base:master
Choose a base branch
Loading
fromprivsep-recvmsg

Conversation

@rsmarples
Copy link
Member

Adjust the code flow so that the same errors would be caught after the final recv.
This ensures we read what is really meant for us and not something silly.

Shouldfix#555.

Adjust the code flow so that the same errors would be caughtafter the final recv.This ensures we read what is really meant for us and notsomething silly.Shouldfix#555.
@coderabbitai
Copy link

coderabbitaibot commentedNov 17, 2025
edited
Loading

Walkthrough

Reworked privsep message I/O to a two-phase recvmsg flow (peek sizing then mandatory consume), moved per-message buffers to optional heap allocation, removed per-process data cleanup hooks, and removed two fields from the public process struct.

Changes

Cohort / File(s)Summary
Privsep root I/O and buffer handling
src/privsep-root.c
Replaced single MSG_PEEK read with msghdr/iovec + recvmsg(MSG_PEEK
Process struct and cleanup behavior
src/privsep.h,src/privsep.c
Removedpsp_data andpsp_freedata fields fromstruct ps_process; removed runtime cleanup ofpsp_data inps_freeprocess, so per-process data callback/path no longer invoked during process free.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Review focus:
    • Correctness of new two-step recvmsg control flow and guarantee that peeked messages are always consumed (including on error paths).
    • Memory semantics aroundpsr_mallocdata /psr_data allocation, ownership, and absence of the previous in-place mdata lifecycle.
    • Error mapping and logging for MSG_TRUNC, short reads, and total-length mismatches.
    • Effects of removingpsp_data /psp_freedata and skipping per-process data cleanup on callers and lifecycle (ensure no leaked resources or dangling pointers).
    • Call sites tops_root_writeerror and other modified APIs to confirm consistent zero-length payload handling.

Possibly related PRs

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check nameStatusExplanationResolution
Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.You can run@coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check nameStatusExplanation
Title check✅ PassedThe title accurately describes the main change: ensuring a real recv occurs after MSG_PEEK, which aligns with the core code modifications switching from simple MSG_PEEK to a two-phase recv approach.
Description check✅ PassedThe description is related to the changeset, explaining that the code flow is adjusted to re-check error conditions after a real recv following MSG_PEEK, which matches the implementation details.
Linked Issues check✅ PassedThe PR implements a two-phase recv approach (MSG_PEEK followed by real recv) that addresses the buffer handling issue reported in#555 by ensuring proper error detection on the final recv path.
Out of Scope Changes check✅ PassedAll changes are directly related to the recv mechanism and buffer handling objectives: modifications to privsep-root.c for two-phase recv, removal of cleanup callbacks in privsep.c/h consistent with the new allocation strategy.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branchprivsep-recvmsg

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and betweencd75b08 and16a0bfb.

📒 Files selected for processing (3)
  • src/privsep-root.c (6 hunks)
  • src/privsep.c (0 hunks)
  • src/privsep.h (0 hunks)
💤 Files with no reviewable changes (2)
  • src/privsep.h
  • src/privsep.c
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/privsep-root.c
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
  • GitHub Check: netbsd (--disable-arp, -DSMALL)
  • GitHub Check: netbsd (--disable-ipv4ll)
  • GitHub Check: netbsd (--disable-ipv4ll, -DSMALL)
  • GitHub Check: netbsd (--disable-dhcp6, -DSMALL)
  • GitHub Check: netbsd (--disable-ipv6)
  • GitHub Check: netbsd (--disable-dhcp6)
  • GitHub Check: netbsd (--disable-ipv6, -DSMALL)
  • GitHub Check: netbsd (--disable-arp)
  • GitHub Check: netbsd (--disable-ipv4, -DSMALL)
  • GitHub Check: netbsd (-DSMALL)
  • GitHub Check: netbsd (--disable-ipv4)
  • GitHub Check: netbsd
  • GitHub Check: freebsd
  • GitHub Check: openbsd

Thanks for usingCodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment@coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitaicoderabbitaibot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between385ff24 andf4446d5.

📒 Files selected for processing (1)
  • src/privsep-root.c (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
  • GitHub Check: netbsd (--disable-ipv6)
  • GitHub Check: netbsd (--disable-ipv6, -DSMALL)
  • GitHub Check: netbsd (-DSMALL)
  • GitHub Check: netbsd (--disable-dhcp6, -DSMALL)
  • GitHub Check: netbsd (--disable-dhcp6)
  • GitHub Check: netbsd (--disable-ipv4ll)
  • GitHub Check: netbsd (--disable-ipv4ll, -DSMALL)
  • GitHub Check: netbsd (--disable-ipv4)
  • GitHub Check: netbsd (--disable-arp, -DSMALL)
  • GitHub Check: netbsd (--disable-ipv4, -DSMALL)
  • GitHub Check: netbsd (--disable-arp)
  • GitHub Check: netbsd
  • GitHub Check: freebsd
  • GitHub Check: openbsd

Copy link

@coderabbitaicoderabbitaibot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
src/privsep-root.c (1)

129-138:Critical: Fix MSG_WAITALL hang by using incoming message size.

Lines 134 and 137 setiov_len from the context buffer sizes (psr_ctx->psr_mdatalen,psr_ctx->psr_datalen), which can be larger than the actual incoming message sizepsr_error->psr_datalen. WhenMSG_WAITALL is used with aniov_len larger than available data,recvmsg blocks indefinitely waiting for bytes that will never arrive, wedging the privileged proxy and causing cascading failures like "No buffer space available."

This is the same critical issue identified in the past review and must be fixed.

Apply this diff to clampiov_len to the actual incoming message size:

 if (psr_error->psr_datalen != 0) { if (psr_ctx->psr_usemdata) { iov[1].iov_base = psr_ctx->psr_mdata;-/* psr_mdatalen could be smaller then psr_datalen- * if the above malloc failed. */-iov[1].iov_len =-    MIN(psr_ctx->psr_mdatalen, psr_ctx->psr_datalen);+/* Clamp to actual incoming size to avoid MSG_WAITALL hang.+ * If buffer is too small, MSG_TRUNC will catch it. */+iov[1].iov_len = psr_error->psr_datalen;+if (iov[1].iov_len > psr_ctx->psr_mdatalen)+iov[1].iov_len = psr_ctx->psr_mdatalen; } else { iov[1].iov_base = psr_ctx->psr_data;-iov[1].iov_len = psr_ctx->psr_datalen;+iov[1].iov_len = psr_error->psr_datalen;+if (iov[1].iov_len > psr_ctx->psr_datalen)+iov[1].iov_len = psr_ctx->psr_datalen; } }
🧹 Nitpick comments (1)
src/privsep-root.c (1)

102-111:Consider removing MSG_WAITALL from the MSG_PEEK operation.

UsingMSG_WAITALL withMSG_PEEK may cause unnecessary blocking when the message header hasn't fully arrived yet. Since this is just a peek operation to determine buffer size, and you're checking for short reads immediately after (line 110),MSG_PEEK alone should suffice.

Apply this diff:

-len = recvmsg(fd, &msg, MSG_PEEK | MSG_WAITALL);+len = recvmsg(fd, &msg, MSG_PEEK);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and betweenf4446d5 and834a322.

📒 Files selected for processing (1)
  • src/privsep-root.c (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
  • GitHub Check: netbsd (--disable-dhcp6, -DSMALL)
  • GitHub Check: netbsd (--disable-ipv6, -DSMALL)
  • GitHub Check: netbsd (--disable-dhcp6)
  • GitHub Check: netbsd (--disable-ipv6)
  • GitHub Check: netbsd (--disable-ipv4ll)
  • GitHub Check: netbsd (--disable-ipv4ll, -DSMALL)
  • GitHub Check: netbsd (--disable-arp)
  • GitHub Check: netbsd (-DSMALL)
  • GitHub Check: netbsd (--disable-ipv4)
  • GitHub Check: netbsd (--disable-arp, -DSMALL)
  • GitHub Check: netbsd
  • GitHub Check: netbsd (--disable-ipv4, -DSMALL)
  • GitHub Check: freebsd
  • GitHub Check: openbsd
🔇 Additional comments (2)
src/privsep-root.c (2)

113-127:LGTM! Proper handling of allocation failure.

The allocation failure handling is correct: whenrealloc fails, the code leaves the smaller buffer in place and relies onMSG_TRUNC detection in the subsequentrecvmsg to signalENOBUFS. This ensures the message is properly discarded from the queue.


141-151:LGTM! Comprehensive validation of received message.

The final receive path correctly:

  • Centralizes the actual message consumption for both early failures and normal flow
  • Validates minimum header size
  • Detects truncation viaMSG_TRUNC and returnsENOBUFS
  • Ensures exact size match between received data and expected header + payload

Allows us to distinguish from a shortened header.Log the result and expected sizes to try and diagnose.
Copy link

@coderabbitaicoderabbitaibot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/privsep-root.c (2)

118-140:Dynamic buffer growth andiov_len capping avoid hangs and handle ENOBUFS cleanly

Therealloc ofpsr_ctx->psr_mdata and the subsequent setup:

  • Preserve the old buffer ifrealloc fails, so the pointer and size remain valid.
  • Useiov[1].iov_base from eitherpsr_mdata or the caller’s buffer as before.
  • Critically, capiov[1].iov_len topsr_error->psr_datalen, so the total requested length is never larger than the actual message size, eliminating the previous MSG_WAITALL hang scenario when the context buffer was larger than the incoming payload.
  • In the “realloc failed” case,psr_mdatalen < psr_error->psr_datalen, which will force a truncated read in the finalrecvmsg, correctly surfacing ENOBUFS while draining the queue.

Only tiny nit: the new comment still says “failed to malloc” even thoughrealloc is used here.

-       /* If we failed to malloc then psr_mdatalen will be smaller+       /* If we failed to realloc then psr_mdatalen will be smaller

142-157:Finalrecvmsg validation is robust; comment could be tightened to match new invariants

The finalrecvmsg(fd, &msg, MSG_WAITALL) plus:

  • Short‑header check (len < sizeof(*psr_error)),
  • Explicit MSG_TRUNC → ENOBUFS mapping,
  • Exact size check againstsizeof(*psr_error) + psr_error->psr_datalen with EBADMSG and logging,

gives a solid guard against partial or malformed records and guarantees the caller sees consistent errors while the message is fully consumed from the queue. With the newiov_len capping above, the “receive buffers are bigger than the actual message” case cannot actually occur anymore, so the comment at Line 143 is now slightly misleading; consider updating it to say explicitly that the total requested length is bounded by the announcedpsr_datalen, which is what prevents stalling with MSG_WAITALL.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between595c305 and36d3dad.

📒 Files selected for processing (1)
  • src/privsep-root.c (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
  • GitHub Check: netbsd (--disable-dhcp6, -DSMALL)
  • GitHub Check: netbsd (--disable-ipv4ll, -DSMALL)
  • GitHub Check: netbsd (--disable-dhcp6)
  • GitHub Check: netbsd (--disable-ipv6, -DSMALL)
  • GitHub Check: netbsd (--disable-ipv4ll)
  • GitHub Check: netbsd (--disable-ipv6)
  • GitHub Check: netbsd (-DSMALL)
  • GitHub Check: netbsd
  • GitHub Check: netbsd (--disable-ipv4, -DSMALL)
  • GitHub Check: netbsd (--disable-arp, -DSMALL)
  • GitHub Check: netbsd (--disable-arp)
  • GitHub Check: netbsd (--disable-ipv4)
  • GitHub Check: freebsd
  • GitHub Check: openbsd
🔇 Additional comments (1)
src/privsep-root.c (1)

89-112:Two-phaserecvmsg with MSG_PEEK looks correct and keeps the queue in sync

The introduction ofstruct msghdr msg and the initialrecvmsg(fd, &msg, MSG_PEEK | MSG_WAITALL) followed by an unconditional non‑peekrecvmsg (viagoto recv when needed) ensures you (a) size buffers based on the header, and (b) always actually consume the message from the SOCK_SEQPACKET queue. This addresses the prior risk of acting only on peeked data without draining the record, without introducing obvious new failure modes.

@perkelix
Copy link
Contributor

Does this one seem ready to merge too?

@rsmarples
Copy link
MemberAuthor

Does this one seem ready to merge too?

I would like some feedback on it first as I still don't know if it fixes people issues as I cannot replicate the problem.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@coderabbitaicoderabbitai[bot]coderabbitai[bot] left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

SIOCSPFXFLUSH_IN6: No buffer space available after updating to 10.3.0 (FreeBSD 14.3)

2 participants

@rsmarples@perkelix

[8]ページ先頭

©2009-2025 Movatter.jp