# New passes in clang-tidy to detect (some) Trojan Source

DevFeed: [New passes in clang-tidy to detect (some) Trojan Source](<https://devfeed.tech/articles/new-passes-in-clang-tidy-to-detect-some-trojan-source-43085.md>)

Original publisher: [Read original article](<https://blog.llvm.org/posts/2022-01-12-trojan-source/>)

Author: Serge Guelton

Published: 2022-01-12T00:00:00Z

Content type: article

Language: en

Sources: [LLVM Project](<https://devfeed.tech/sources/the-llvm-project-blog.md>)

Topics: [clang](<https://devfeed.tech/topics/clang.md>), [Security](<https://devfeed.tech/topics/security.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [ascii](<https://devfeed.tech/tags/ascii.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [clang](<https://devfeed.tech/tags/clang.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [identifier](<https://devfeed.tech/tags/identifier.md>), [security](<https://devfeed.tech/tags/security.md>), [source](<https://devfeed.tech/tags/source.md>), [trojan](<https://devfeed.tech/tags/trojan.md>), [unicode](<https://devfeed.tech/tags/unicode.md>)

## AI overview

This article describes new clang-tidy checks for detecting Trojan Source issues in C++ code. The checks identify misleading bidirectional Unicode characters, identifiers with misleading text direction, and confusable identifiers that can make source code appear different from how it is processed.

## Source excerpt

Trojan Source The original Trojan Source paper encompasses afamily of attacks that rely on Unicode properties to make code look differentfrom how a compiler processes it. For instance the following code taken fromthe paper: #include <stdio.h>#include <stdbool.h>int main() { bool isAdmin = false; /*‮ } ⁦if (isAdmin)⁩ ⁦begin admins only */ printf("You are an admin.\n"); /* end admins only ‮ { ⁦*/ return 0;} looks like there is a guard on isAdmin while the compiler actually reads thefollowing byte stream /* <U+0x202E> } <U+0x2066> if (isAdmin) <U+0x2069> <U+0x2066> begin admins only */ This issue got submitted before the official release to the LLVM Security group,and while we agreed this was more of a display issue than an actualcompiler-related issue, we also agreed having a clang-tidy check for eachflaws described in the paper could not hurt. Using clang-tidy The tool named clang-tidy can run a bunch of extra passes on a codebase,detecting coding convention issues, API misuses, security flaws etc. We havebeen adding three new checkers: Detecting misleading bidirectional characters The new check misc-misleading-bidirectional parses each comment and stringliteral from the codebase and looks for unterminated bidirectional sequence,i.e. sequence that leak past the end of comment or string literal, makingregular code being displayed right-to-left instead of the usual left-to-right.In the case of the example above we get a warning close to: 5:3: warning: comment contains misleading bidirectional Unicode characters [misc-misleading-bidirectional]Detecting misleading identifiers C++ allows for some Unicode codepoints within identifiers, including identifiersthat have a strong right-to-left direction, which can lead to misleadingstatements. For instance in the following, int א = ג; Are we assigining to א or to ג? We are actually doing the latter, andthat is confusing. The pass misc-misleading-identifier detect thatconfiguration and outputs a warning similar to 10:3: warning: