Skip to content

🔒 Fix Command Injection in bs2system - #45

Merged
orpheus497 merged 2 commits into
mainfrom
fix-bs2system-command-injection-10081930905187937297
Jun 5, 2026
Merged

🔒 Fix Command Injection in bs2system#45
orpheus497 merged 2 commits into
mainfrom
fix-bs2system-command-injection-10081930905187937297

Conversation

@orpheus497

Copy link
Copy Markdown
Owner

🎯 What:
Fixes a command injection vulnerability in tc.os.c where an unsanitized string was passed directly to the OS-native execution function bs2system().

⚠️ Risk:
If an attacker can control or influence the string parsed by bs2cmdlist, they could embed shell metacharacters to break out of the intended command context and execute arbitrary malicious commands on the system, leading to system compromise.

🛡️ Solution:
Added a check right before the bs2system call. The code now uses strpbrk to inspect str_beg for dangerous shell execution characters (&, |, <, >, $, ``, \n, \r). If any of these characters are found, tcsh's stderror function is called to raise an error and safely abort execution, preventing the command injection.


PR created automatically by Jules for task 10081930905187937297 started by @orpheus497

The `bs2cmdlist` function in `tc.os.c` iterates over user input and passes parts of the string directly to `bs2system()`, an OS-specific command execution function similar to `system()`. This allows a potential attacker to execute arbitrary commands by embedding shell metacharacters in the input.

This patch adds a sanitization check using `strpbrk` to detect and reject dangerous shell metacharacters (`&`, `|`, `<`, `>`, `$`, \`\`, `\n`, `\r`) in the string `str_beg` right before `bs2system` is called. If any are detected, it aborts the execution via `stderror(ERR_NAME | ERR_STRING, ...)`.

Co-authored-by: orpheus497 <230802898+orpheus497@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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.

@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@orpheus497, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 28 minutes and 3 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4ba5c8a6-4af1-4e51-90f4-421c761d0495

📥 Commits

Reviewing files that changed from the base of the PR and between 571b0fe and ff84153.

📒 Files selected for processing (1)
  • tc.os.c
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-bs2system-command-injection-10081930905187937297

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

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

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

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.

Code Review

This pull request introduces a security check in tc.os.c to prevent command injection by rejecting unsafe characters in commands before they are executed. The review feedback points out a correctness issue where valid commands containing these characters inside quotes would be incorrectly rejected, and provides a robust code suggestion to track quote contexts during validation.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread tc.os.c Outdated
Comment on lines +743 to +746
if (strpbrk(str_beg, "&|<>$`\n\r") != NULL) {
stderror(ERR_NAME | ERR_STRING, "unsafe character in command");
return -1;
}

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.

security-high high

Using strpbrk directly on str_beg introduces a correctness issue where valid commands containing any of the blocked characters (such as & or $) inside single or double quotes (e.g., PRINT "Hello & Welcome") will be incorrectly rejected as unsafe.

Since bs2cmdlist already parses quotes to identify command boundaries, we should respect the quote context when validating characters. We can use an inline loop that tracks single and double quotes to only reject unsafe characters when they appear outside of quotes.

            const char *p;
            int sq = 0, dq = 0;

            for (p = str_beg; *p != '\0'; p++)
            {
                if (*p == '\'' && !dq)
                    sq = !sq;
                else if (*p == '"' && !sq)
                    dq = !dq;
                else if (!sq && !dq && strchr("&|<>$\140\n\r", *p) != NULL)
                {
                    stderror(ERR_NAME | ERR_STRING, "unsafe character in command");
                    return -1;
                }
            }

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Jules security-high high

Using strpbrk directly on str_beg introduces a correctness issue where valid commands containing any of the blocked characters (such as & or $) inside single or double quotes (e.g., PRINT "Hello & Welcome") will be incorrectly rejected as unsafe.

Since bs2cmdlist already parses quotes to identify command boundaries, we should respect the quote context when validating characters. We can use an inline loop that tracks single and double quotes to only reject unsafe characters when they appear outside of quotes.

            const char *p;
            int sq = 0, dq = 0;

            for (p = str_beg; *p != '\0'; p++)
            {
                if (*p == '\'' && !dq)
                    sq = !sq;
                else if (*p == '"' && !sq)
                    dq = !dq;
                else if (!sq && !dq && strchr("&|<>$\140\n\r", *p) != NULL)
                {
                    stderror(ERR_NAME | ERR_STRING, "unsafe character in command");
                    return -1;
                }
            }

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.

You make a great point about quoted characters, implemented!

The `bs2cmdlist` function in `tc.os.c` iterates over user input and passes parts of the string directly to `bs2system()`, an OS-specific command execution function similar to `system()`. This allows a potential attacker to execute arbitrary commands by embedding shell metacharacters in the input.

This patch adds a sanitization check to detect and reject dangerous shell metacharacters (`&`, `|`, `<`, `>`, `$`, \`\`, `\n`, `\r`) in the string `str_beg` right before `bs2system` is called. It correctly handles quoted sections, ensuring that valid commands containing characters like `&` inside quotes are not falsely rejected. If an unsafe character is found outside of quotes, it aborts the execution via `stderror(ERR_NAME | ERR_STRING, ...)`.

Co-authored-by: orpheus497 <230802898+orpheus497@users.noreply.github.com>
@orpheus497
orpheus497 marked this pull request as ready for review June 5, 2026 02:40
@orpheus497
orpheus497 merged commit f2b35ff into main Jun 5, 2026
1 of 2 checks passed
@orpheus497
orpheus497 deleted the fix-bs2system-command-injection-10081930905187937297 branch June 5, 2026 02: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