Skip to content

Carriage return without line feed can be used to hide malicious code #151473

@krtab

Description

@krtab

The issue is basically in the title: rustc parses a carriage return ("\r") as a proper whitespace token. However, on some platforms/systems (for example cat in my linux terminal), the carriage return is interpreted as simply returning the cursor to the beginning of the line and overwriting what follows.

Hence the following file:

fn do_something_very_bad() {
     todo!()
}

fn main() {
     do_something_very_bad();\r    // No need to worry here, this looks very innocuous, I think nothing bad will happen
     println!("Oh hi! You should definitely run this very innocuous binary");
}

is displayed as

fn do_something_very_bad() {
     todo!()
}

fn main() {
     // No need to worry here, this looks very innocuous, I think nothing bad will happen
     println!("Oh hi! You should definitely run this very innocuous binary");
}

The security team and I agree that this does not qualify as a security issue.

In the past, we have added lint to safe-guard against CVE-2021-42574 (https://blog.rust-lang.org/2021/11/01/cve-2021-42574/), which is similar and was treated as a security issue. However to quote Pietro: "the main reason we treated [CVE-2021-42574] that way back then was that editors and code review tools didn't handle it at all, while most editors and code review sites seem to handle \r gracefully".

We may be opening a can of worm by trying to properly handle all Unicode messes that may lead to wrong displays of code across all possible systems allowing to read code, but I think it it nonetheless worth it to have the possibility of a discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-parserArea: The lexing & parsing of Rust source code to an ASTC-discussionCategory: Discussion or questions that doesn't represent real issues.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions