-
Notifications
You must be signed in to change notification settings - Fork 36
[Coding Guideline]: Prevent OS Command Injection #360
Copy link
Copy link
Open
Labels
CERT CIssues or coding guidelines directly related to the CERT C Coding GuidelinesIssues or coding guidelines directly related to the CERT C Coding Guidelinescategory: mandatoryA coding guideline with category mandatoryA coding guideline with category mandatorychapter: program-structure-and-compilationcoding guidelineAn issue related to a suggestion for a coding guidelineAn issue related to a suggestion for a coding guidelinedecidability: undecidableA coding guideline which cannot be checked automaticallyA coding guideline which cannot be checked automaticallyscope: moduleA coding guideline that can be determined applied at the module levelA coding guideline that can be determined applied at the module levelsign-off: create prA coding guideline issue that's been reviewed and now requesting to create a Pull Request for itA coding guideline issue that's been reviewed and now requesting to create a Pull Request for itstatus: awaiting reviewer responseReviewer-bot is waiting on reviewer freshness or current-head reviewReviewer-bot is waiting on reviewer freshness or current-head reviewstatus: draft
Metadata
Metadata
Assignees
Labels
CERT CIssues or coding guidelines directly related to the CERT C Coding GuidelinesIssues or coding guidelines directly related to the CERT C Coding Guidelinescategory: mandatoryA coding guideline with category mandatoryA coding guideline with category mandatorychapter: program-structure-and-compilationcoding guidelineAn issue related to a suggestion for a coding guidelineAn issue related to a suggestion for a coding guidelinedecidability: undecidableA coding guideline which cannot be checked automaticallyA coding guideline which cannot be checked automaticallyscope: moduleA coding guideline that can be determined applied at the module levelA coding guideline that can be determined applied at the module levelsign-off: create prA coding guideline issue that's been reviewed and now requesting to create a Pull Request for itA coding guideline issue that's been reviewed and now requesting to create a Pull Request for itstatus: awaiting reviewer responseReviewer-bot is waiting on reviewer freshness or current-head reviewReviewer-bot is waiting on reviewer freshness or current-head reviewstatus: draft
Type
Projects
Status
No status
Chapter
Program Structure and Compilation
Guideline Title
Prevent OS Command Injection
Category
Mandatory
Status
Draft
Release Begin
1.0.0
Release End
latest
FLS Paragraph ID
fls_hdwwrsyunir
Decidability
Undecidable
Scope
Module
Tags
injection,sanitization
Amplification
Commands that are passed to an external OS command interpreter, like
std::process::Command, should not allow untrusted input to be parsed as part of the command syntax.Instead, an untrusted input should be passed as a single argument.
Exception(s)
No response
Rationale
This rule was inspired by CERT-J-IDS07.
When preparing a command to be executed by the operating system, untrusted input should be sanitized to make sure it does not alter the syntax of the command to be executed. For commands that do not tokenize their arguments, such as
sh, the easiest way to do this is to avoid mixing untrusted data with trusted data via concatenation or formatting (a laformat!()). Instead provide the untrusted data as a lone argument. TheCommand::new()constructor makes this easy by accepting the pre-tokenized arguments as a list of strings.Traditionally untrusted data should be one argument (aka command-line token). OS command injection occurs when a malicious data fools the command tokenizer into interpreting it as multiple arguments, or even multiple commands. Complexity in the command tokenizer can exacerbate this problem, leading to vulnerabilities such as CVE-2024-24576. See RUST-WIN-ARG-SPLIT and SEI-BATBADBUT for more information.
Non-Compliant Example 1 - Prose
The following code lists the contents the directory provided in the
dirvariable. However, since this variable is untrusted, adirsuch asdummy | echo BOOwill cause the command to be executed. Thus, the program prints "BOO".Non-Compliant Example 1 - Code
Non-Compliant Example 2 - Prose (Optional)
No response
Non-Compliant Example 2 - Code (Optional)
No response
Non-Compliant Example 3 - Prose (Optional)
No response
Non-Compliant Example 3 - Code (Optional)
No response
Non-Compliant Example 4 - Prose (Optional)
No response
Non-Compliant Example 4 - Code (Optional)
No response
Compliant Example 1 - Prose
An untrusted input should be passed as a single argument. This prevents any spaces or other shell punctuation in the input from being misinterpreted by the OS command interpreter.
Compliant Example 1 - Code
Compliant Example 2 - Prose (Optional)
A better approach is to avoid OS commands and use a specific API (in this case
fs::read_dir()) to achieve the desired result.Compliant Example 2 - Code (Optional)
Compliant Example 3 - Prose (Optional)
No response
Compliant Example 3 - Code (Optional)
No response
Compliant Example 4 - Prose (Optional)
No response
Compliant Example 4 - Code (Optional)
No response
Bibliography