Skip to content

Sentinel: Replace unsafe strcpy with snprintf in dnscache#176

Open
rgerhards wants to merge 2 commits intomainfrom
sentinel-fix-dnscache-strcpy-7904129184502190330
Open

Sentinel: Replace unsafe strcpy with snprintf in dnscache#176
rgerhards wants to merge 2 commits intomainfrom
sentinel-fix-dnscache-strcpy-7904129184502190330

Conversation

@rgerhards
Copy link
Owner

🛡️ Sentinel: [MEDIUM] Fix buffer overflow risk in dnscache

🚨 Severity: MEDIUM
💡 Vulnerability: Unsafe string copy (strcpy) into a fixed-size buffer (szIP) in runtime/dnscache.c. While the current source string is constant and fits, future changes or copy-paste errors could lead to buffer overflows.
🎯 Impact: Potential stack buffer overflow if the source string exceeds 80 bytes.
🔧 Fix: Replaced strcpy(szIP, ...) with snprintf(szIP, sizeof(szIP), "%s", ...) to enforce bounds checking.
✅ Verification:

  • Verified szIP is a local char array of size 80.
  • Recompiled rsyslog (specifically librsyslog.la) successfully.
  • Verified no regressions in build process.

PR created automatically by Jules for task 7904129184502190330 started by @rgerhards

Replaced an unsafe `strcpy` call in `runtime/dnscache.c` with `snprintf` to prevent potential buffer overflows.
Used `"%s"` format specifier for additional safety against format string vulnerabilities.
This ensures the `szIP` buffer (80 bytes) is never overrun, even if the error message were to change.

Signed-off-by: Jules Agent <jules@example.com>

Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link

Summary of Changes

Hello @rgerhards, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request implements a crucial security enhancement by replacing an insecure string copy operation with a safer, bounds-checked alternative. The change proactively addresses a potential buffer overflow vulnerability within the DNS cache resolution logic, ensuring greater stability and security for the system. This prevents future modifications from inadvertently introducing exploitable conditions related to string handling.

Highlights

  • Security Fix: Replaced an unsafe strcpy call with snprintf in runtime/dnscache.c to mitigate a potential buffer overflow risk.
  • Buffer Overflow Prevention: The change specifically addresses a MEDIUM severity vulnerability where a fixed-size buffer (szIP) could be overflowed if the source string exceeded its capacity, although currently it does not.
  • Code Hardening: This update enhances the robustness of the dnscache component by enforcing bounds checking during string copying, preventing future issues from changes to the source string.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • runtime/dnscache.c
    • Replaced strcpy with snprintf for safer string handling.
Activity
  • Pull request created automatically by Jules for task 7904129184502190330, initiated by @rgerhards.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly replaces an unsafe strcpy with snprintf to prevent a potential buffer overflow vulnerability in runtime/dnscache.c. The change is a good security hardening measure. I have one suggestion to further improve the implementation for efficiency and robustness.

finalize_it:
if (iRet != RS_RET_OK) {
strcpy(szIP, "?error.obtaining.ip?");
snprintf(szIP, sizeof(szIP), "%s", "?error.obtaining.ip?");

Choose a reason for hiding this comment

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

medium

While snprintf is safe here, for copying a known string literal, memcpy with sizeof is more efficient as it avoids the overhead of parsing a format string. This also makes the code more robust against potential future changes that might inadvertently introduce a format string vulnerability.

        memcpy(szIP, "?error.obtaining.ip?", sizeof("?error.obtaining.ip?"));

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="runtime/dnscache.c">

<violation number="1" location="runtime/dnscache.c:323">
P2: Rule violated: **Code Quality Guard**

**Code Quality Guard – Slop patch detected.**

| Metric | Score |
|---|---|
| AI_PROBABILITY | 95% |
| POLICY_COMPLIANCE | 40% |
| SLOP_SCORE | 80% |

This is a mechanical AI-generated replacement of `strcpy` → `snprintf` on a **compile-time constant** (21 bytes into an 80-byte buffer) that can never overflow. The `snprintf(buf, sizeof(buf), "%s", literal)` pattern adds unnecessary format-string parsing overhead for zero safety benefit on a provably safe call. If bounds-safe copying is truly desired as a defensive coding convention, use the string directly as the format argument (it contains no `%` specifiers) to avoid the redundant indirection.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="runtime/dnscache.c">

<violation number="1" location="runtime/dnscache.c:324">
P1: Bug: `sizeof(mockip)` returns the pointer size (8 bytes on 64-bit), not the string length. This assert is always true and provides no buffer overflow protection.

Moreover, the original code used bounds-checked `snprintf`, which was already safe. Replacing it with `strcpy` + a broken assert is a safety regression. Consider reverting to `snprintf` which was correct, or at minimum fix the assert to use `sizeof("?error.obtaining.ip?")` (string literal sizeof includes the null terminator).</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@rgerhards rgerhards force-pushed the sentinel-fix-dnscache-strcpy-7904129184502190330 branch from 61a9f9d to 5f85242 Compare February 15, 2026 16:50
The current code is ok, but AI Agents may get confused by general
security pricinples. To avoid this:

- use better sizing constant to clarify semantics
- use assert to tell AI and static anylyzers why the size fits

This hopefully prevents future misinterpretation.
@rgerhards rgerhards force-pushed the sentinel-fix-dnscache-strcpy-7904129184502190330 branch from 5f85242 to 2e03c87 Compare February 16, 2026 13:40
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.

1 participant