Skip to content

glibc-2.35: spurious "buffer overrun" for strncpy#1215

Merged
aayushi363 merged 1 commit intodmtcp:mainfrom
aayushi363:dev/glibc-buffer-overrun
Aug 22, 2025
Merged

glibc-2.35: spurious "buffer overrun" for strncpy#1215
aayushi363 merged 1 commit intodmtcp:mainfrom
aayushi363:dev/glibc-buffer-overrun

Conversation

@aayushi363
Copy link
Copy Markdown
Collaborator

@aayushi363 aayushi363 commented Aug 2, 2025

This occurs when port=="0"

Summary by CodeRabbit

  • Bug Fixes
    • Improved robustness of coordinator port handling to prevent boundary-related issues that could cause truncated or malformed port suffixes.
    • Ensures consistent formatting and proper termination of short command labels, improving reliability during startup and when displaying/processing coordinator command lines.
    • No functional changes expected; reduces risk of crashes or undefined behavior with edge-case port input lengths.

@aayushi363 aayushi363 requested review from gc00 and karya0 August 2, 2025 02:43
@aayushi363 aayushi363 added the bug label Aug 2, 2025
@karya0 karya0 requested a review from Copilot August 2, 2025 08:18
Copy link
Copy Markdown
Contributor

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

This PR fixes a buffer overrun warning in glibc-2.35 by correcting the length calculation in a strncpy call. The issue occurs specifically when the port parameter is "0".

  • Corrects the buffer size calculation in strncpy to prevent potential buffer overrun
  • Changes from sizeof(port_str)+1-strlen(port) to sizeof(port_str)-strlen(port)

char buf[16] = " ";
char port_str[16] =":";
strncpy(port_str+1, port, sizeof(port_str)+1-strlen(port));
strncpy(port_str+1, port, sizeof(port_str)-strlen(port));
Copy link

Copilot AI Aug 2, 2025

Choose a reason for hiding this comment

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

The length calculation is incorrect. It should be sizeof(port_str)-1-1 (or sizeof(port_str)-2) to account for both the ':' character at position 0 and the null terminator. The current calculation uses strlen(port) which is the length of the source string, not the available space in the destination buffer.

Suggested change
strncpy(port_str+1, port, sizeof(port_str)-strlen(port));
strncpy(port_str+1, port, sizeof(port_str)-2);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@aayushi363 , I have a question. How were you compiling this? From my reading, we should not have seen this, unless our g++ compilation was explicitly using -D_FORTIFY_SOURCE=XXX was being explicitly used by us. If so, where did this flag come from in compiling DMTCP?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Neither we are using this flag explicitly while compiling nor it is mentioned in any of the make files.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think I like the comment of Copilot. Have you tried that change? If that works with port == 0, then let's use that one. As it indicates, strlen(port) could be just 1, and the correct constant to subtract here really should be 2.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree, tying it to strlen(port) isn’t reliable, and using sizeof(port_str) - 2 is safer. I tested it, and the change works.

@aayushi363 aayushi363 force-pushed the dev/glibc-buffer-overrun branch from a21d80c to b1a395f Compare August 18, 2025 18:37
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Aug 18, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Replaced dynamic strncpy length computation for port suffixing with a fixed bound (sizeof(port_str) - 2) when copying into port_str+1. This standardizes the number of copied characters, reduces boundary overrun risk, and preserves existing control flow and usage of the resulting short command line.

Changes

Cohort / File(s) Summary
Coordinator port suffix handling
src/dmtcp_coordinator.cpp
Adjust strncpy length from a runtime-calculated value to a fixed bound (sizeof(port_str) - 2) when appending the port after a leading colon into port_str+1; no other logic changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitch my whiskers at colons and ports,
Snipping the string to safer shorts.
A careful hop—two chars to spare—
Nulls in place, no overflow scare.
With tidy bounds, I thump in cheer,
“Ship it!” says this codey hare. 🐇💻

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot 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/dmtcp_coordinator.cpp (2)

1598-1602: Prefer snprintf for clarity and explicit NUL-termination (minor).

strncpy here relies on the earlier zero-initialization of port_str for NUL-termination. Using snprintf makes the intent obvious and guarantees termination without depending on initialization details.

Apply this diff:

-  strncpy(port_str+1, port, sizeof(port_str)-2);
+  // Ensure ':' + port fits within port_str (16 bytes incl. NUL)
+  // Truncate port to at most 14 chars if longer.
+  snprintf(port_str, sizeof(port_str), ":%.*s",
+           (int)sizeof(port_str) - 2, port);

1601-1605: Optionally check short_name() return to avoid setting a blank comm name (very unlikely here).

short_name() can return NULL when the suffix is too long for the 16-byte comm buffer. Not an issue for real port strings, but a defensive check avoids PR_SET_NAME being set to spaces if the suffix logic changes later.

Apply this diff:

-  short_name(buf, argv0, sizeof buf, port_str);
+  if (!short_name(buf, argv0, sizeof buf, port_str)) {
+    // Fallback: leave buf as-is or set a minimal identifier
+    // e.g., strncpy(buf, "dmtcp", sizeof(buf)-1);
+  }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between b7fab64 and b1a395f.

📒 Files selected for processing (1)
  • src/dmtcp_coordinator.cpp (1 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). (1)
  • GitHub Check: build
🔇 Additional comments (1)
src/dmtcp_coordinator.cpp (1)

1600-1601: Correct bound for strncpy; fixes the fortify overrun and handles port == "0".

Using sizeof(port_str) - 2 is the right bound when writing at port_str + 1: it leaves room for the leading ':' and the trailing NUL. Given the zero-initialization from the ":" initializer, the buffer remains NUL-terminated even when truncation occurs. This should eliminate the glibc-2.35 _FORTIFY_SOURCE warning.

Copy link
Copy Markdown
Contributor

@gc00 gc00 left a comment

Choose a reason for hiding this comment

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

KGTM.

@aayushi363 aayushi363 merged commit d3d2a0d into dmtcp:main Aug 22, 2025
2 checks passed
@aayushi363 aayushi363 deleted the dev/glibc-buffer-overrun branch August 22, 2025 18:46
@aayushi363 aayushi363 restored the dev/glibc-buffer-overrun branch August 30, 2025 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants