Fix undefined behavior in dnscache.c resolveAddr#177
Conversation
When processing a domain name that contains characters with the high bit set (e.g., non-ASCII characters or malicious input), passing a signed `char` to `tolower()` results in Undefined Behavior if the value is negative. This patch fixes the issue by casting the argument to `unsigned char`, ensuring the value is within the valid range (0-255) expected by `tolower()`. This addresses a potential security vulnerability (CERT STR37-C). Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Summary of ChangesHello @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 addresses a critical issue in the Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a potential undefined behavior in runtime/dnscache.c. By casting the char argument of tolower() to unsigned char, it ensures the function behaves correctly even on platforms where char is signed and for character values that would be negative. This is a crucial fix for portability and correctness. The change is minimal, targeted, and well-explained in the pull request description. I have no further suggestions for improvement on this change.
There was a problem hiding this comment.
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:313">
P2: Rule violated: **Code Quality Guard**
**SLOP_SCORE: High** — This patch fixes `tolower()` UB only in `dnscache.c` but leaves the identical bug unfixed in 4 other `runtime/` files (`net.c:1171`, `tcpsrv.c:752`, `srutils.c:597`, `msg.c:4097`). The `(int)` casts used in some of those files are also insufficient to prevent UB with negative `char` values — only `(unsigned char)` is correct. An AI-generated patch that claims to fix a class of UB but addresses a single instance is incomplete and sloppy.
| Metric | Score |
|---|---|
| AI_PROBABILITY | 95% |
| POLICY_COMPLIANCE | 60% |
| SLOP_SCORE | 70% |</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| fqdnLen = strlen(fqdnBuf); | ||
| prop.CreateStringProp(&etry->fqdn, (uchar *)fqdnBuf, fqdnLen); | ||
| for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower(fqdnBuf[i]); | ||
| for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower((unsigned char)fqdnBuf[i]); |
There was a problem hiding this comment.
P2: Rule violated: Code Quality Guard
SLOP_SCORE: High — This patch fixes tolower() UB only in dnscache.c but leaves the identical bug unfixed in 4 other runtime/ files (net.c:1171, tcpsrv.c:752, srutils.c:597, msg.c:4097). The (int) casts used in some of those files are also insufficient to prevent UB with negative char values — only (unsigned char) is correct. An AI-generated patch that claims to fix a class of UB but addresses a single instance is incomplete and sloppy.
| Metric | Score |
|---|---|
| AI_PROBABILITY | 95% |
| POLICY_COMPLIANCE | 60% |
| SLOP_SCORE | 70% |
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At runtime/dnscache.c, line 313:
<comment>**SLOP_SCORE: High** — This patch fixes `tolower()` UB only in `dnscache.c` but leaves the identical bug unfixed in 4 other `runtime/` files (`net.c:1171`, `tcpsrv.c:752`, `srutils.c:597`, `msg.c:4097`). The `(int)` casts used in some of those files are also insufficient to prevent UB with negative `char` values — only `(unsigned char)` is correct. An AI-generated patch that claims to fix a class of UB but addresses a single instance is incomplete and sloppy.
| Metric | Score |
|---|---|
| AI_PROBABILITY | 95% |
| POLICY_COMPLIANCE | 60% |
| SLOP_SCORE | 70% |</comment>
<file context>
@@ -310,7 +310,7 @@ static rsRetVal ATTR_NONNULL() resolveAddr(struct sockaddr_storage *addr, dnscac
fqdnLen = strlen(fqdnBuf);
prop.CreateStringProp(&etry->fqdn, (uchar *)fqdnBuf, fqdnLen);
- for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower(fqdnBuf[i]);
+ for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower((unsigned char)fqdnBuf[i]);
prop.CreateStringProp(&etry->fqdnLowerCase, (uchar *)fqdnBuf, fqdnLen);
}
</file context>
This patch fixes potential Undefined Behavior when passing signed `char` values to `isdigit()` and `isspace()`. In `plugins/imptcp/imptcp.c`, signed chars from the input buffer were passed to `isdigit()` without casting, which can lead to UB if the input contains bytes with the high bit set (negative values). In `tools/omfile.c`, signed chars from the filename string were passed to `isspace()` without casting, which is similarly unsafe for non-ASCII filenames. Both instances are now fixed by casting the argument to `(unsigned char)` before passing it to the `ctype` functions, as required by the C standard (CERT STR37-C). Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
This patch makes the dependency downloading in `tests/diag.sh` more robust by adding a `download_and_verify_tgz` helper function. The function verifies the integrity of downloaded `.tar.gz` files using `gzip -t` before using them. If a file is corrupted (which was causing CI failures with "gzip: stdin: unexpected end of file" and "tar: Error is not recoverable"), it is automatically removed and re-downloaded. This fix is applied to: - Kafka and Zookeeper downloads (`download_kafka`) - Elasticsearch downloads (`download_elasticsearch`) - OTEL Collector downloads (`download_otel_collector`) This should resolve the flaky CI failures where corrupted partial downloads were being cached and reused in subsequent test runs. Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
There was a problem hiding this comment.
2 issues 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="tests/diag.sh">
<violation number="1" location="tests/diag.sh:2501">
P2: `((i++))` returns exit status 1 when `i` is 0 (post-increment evaluates to 0). This will cause premature script termination if `set -e` is ever enabled. Use `i=$((i + 1))` instead for safe arithmetic increment.</violation>
<violation number="2" location="tests/diag.sh:2502">
P2: `$TESTTOOL_DIR/msleep 2000` is used without checking if `TESTTOOL_DIR` is set or `msleep` exists. Other code in this diff defensively falls back to `sleep`. Add a similar guard here to avoid breaking the retry loop.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| printf '%s: wget failed, retrying...\n' "$name" | ||
| fi | ||
| rm -f "$dest" | ||
| ((i++)) |
There was a problem hiding this comment.
P2: ((i++)) returns exit status 1 when i is 0 (post-increment evaluates to 0). This will cause premature script termination if set -e is ever enabled. Use i=$((i + 1)) instead for safe arithmetic increment.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/diag.sh, line 2501:
<comment>`((i++))` returns exit status 1 when `i` is 0 (post-increment evaluates to 0). This will cause premature script termination if `set -e` is ever enabled. Use `i=$((i + 1))` instead for safe arithmetic increment.</comment>
<file context>
@@ -2463,51 +2463,78 @@ otel_exit_handling() {
+ printf '%s: wget failed, retrying...\n' "$name"
+ fi
+ rm -f "$dest"
+ ((i++))
+ $TESTTOOL_DIR/msleep 2000
+ done
</file context>
| ((i++)) | |
| i=$((i + 1)) |
| fi | ||
| rm -f "$dest" | ||
| ((i++)) | ||
| $TESTTOOL_DIR/msleep 2000 |
There was a problem hiding this comment.
P2: $TESTTOOL_DIR/msleep 2000 is used without checking if TESTTOOL_DIR is set or msleep exists. Other code in this diff defensively falls back to sleep. Add a similar guard here to avoid breaking the retry loop.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/diag.sh, line 2502:
<comment>`$TESTTOOL_DIR/msleep 2000` is used without checking if `TESTTOOL_DIR` is set or `msleep` exists. Other code in this diff defensively falls back to `sleep`. Add a similar guard here to avoid breaking the retry loop.</comment>
<file context>
@@ -2463,51 +2463,78 @@ otel_exit_handling() {
+ fi
+ rm -f "$dest"
+ ((i++))
+ $TESTTOOL_DIR/msleep 2000
+ done
+
</file context>
Fixed undefined behavior in
runtime/dnscache.cwheretolower()was called with a signedchar, potentially leading to UB with negative values. Cast the argument tounsigned char.PR created automatically by Jules for task 16300544792167519701 started by @rgerhards