Re: [PATCH v2] ci: disallow directional formatting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Nov 03 2021, Johannes Schindelin via GitGitGadget wrote:

> From: Johannes Schindelin <johannes.schindelin@xxxxxx>
>
> As described in https://trojansource.codes/trojan-source.pdf, it is
> possible to abuse directional formatting (a feature of Unicode) to
> deceive human readers into interpreting code differently from compilers.
>
> For example, an "if ()" expression could be enclosed in a comment, but
> rendered as if it was outside of that comment. In effect, this could
> fool a reviewer into misinterpreting the code flow as benign when it is
> not.
>
> It is highly unlikely that Git's source code wants to contain such
> directional formatting in the first place, so let's just disallow it.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@xxxxxx>
> ---
>     ci: disallow directional formatting
>     
>     I just stumbled over
>     https://siliconangle.com/2021/11/01/trojan-source-technique-can-inject-malware-source-code-without-detection/,
>     which details an interesting social-engineering attack: it uses
>     directional formatting in source code to pretend to human readers that
>     the code does something different than it actually does.
>     
>     It is highly unlikely that Git's source code wants to contain such
>     directional formatting in the first place, so let's disallow it.
>     
>     Technically, this is not exactly -rc material, but the paper was just
>     published, and I want us to be safe.
>     
>     Changes since v1:
>     
>      * The code was moved into a script in ci/.
>      * We use git ls-files now to exclude files marked as binary in the Git
>        attributes.
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1071%2Fdscho%2Fcheck-for-utf-8-directional-formatting-v2
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1071/dscho/check-for-utf-8-directional-formatting-v2
> Pull-Request: https://github.com/gitgitgadget/git/pull/1071
>
> Range-diff vs v1:
>
>  1:  6a1661fd887 ! 1:  bbf963695ba ci: disallow directional formatting
>      @@ Commit message
>           possible to abuse directional formatting (a feature of Unicode) to
>           deceive human readers into interpreting code differently from compilers.
>       
>      +    For example, an "if ()" expression could be enclosed in a comment, but
>      +    rendered as if it was outside of that comment. In effect, this could
>      +    fool a reviewer into misinterpreting the code flow as benign when it is
>      +    not.
>      +
>           It is highly unlikely that Git's source code wants to contain such
>      -    directional formatting in the first place, so let's disallow it.
>      +    directional formatting in the first place, so let's just disallow it.
>       
>           Signed-off-by: Johannes Schindelin <johannes.schindelin@xxxxxx>
>       
>      @@ .github/workflows/main.yml: jobs:
>            - uses: actions/checkout@v2
>            - run: ci/install-dependencies.sh
>            - run: ci/run-static-analysis.sh
>      -+    - name: disallow Unicode directional formatting
>      -+      run: |
>      -+        # Use UTF-8-aware `printf` to feed a byte pattern to non-UTF-8-aware `git grep`
>      -+        # (Ubuntu's `git grep` is compiled without support for libpcre, otherwise we
>      -+        # could use `git grep -P` with the `\u` syntax).
>      -+        ! LANG=C git grep -Il "$(LANG=C.UTF-8 printf \
>      -+          '\\(\u202a\\|\u202b\\|\u202c\\|\u202d\\|\u202e\\|\u2066\\|\u2067\\|\u2068\\|\u2069\\)')"
>      ++    - run: ci/check-directional-formatting.sh
>          sparse:
>            needs: ci-config
>            if: needs.ci-config.outputs.enabled == 'yes'
>      +
>      + ## ci/check-directional-formatting.sh (new) ##
>      +@@
>      ++#!/bin/bash
>      ++
>      ++# This script verifies that the non-binary files tracked in the Git index do
>      ++# not contain any Unicode directional formatting: such formatting could be used
>      ++# to deceive reviewers into interpreting code differently from the compiler.
>      ++# This is intended to run on an Ubuntu agent in a GitHub workflow.
>      ++#
>      ++# `git grep` as well as GNU grep do not handle `\u` as a way to specify UTF-8.
>      ++# A PCRE-enabled `git grep` would handle `\u` as desired, but Ubuntu does
>      ++# not build its `git` packages with PCRE support.
>      ++#
>      ++# To work around that, we use `printf` to produce the pattern as a byte
>      ++# sequence, and then feed that to `git grep` as a byte sequence (setting
>      ++# `LC_CTYPE` to make sure that the arguments are interpreted as intended).
>      ++#
>      ++# Note: we need to use Bash here because its `printf` interprets `\uNNNN` as
>      ++# UTF-8 code points, as desired. Running this script through Ubuntu's `dash`,
>      ++# for example, would use a `printf` that does not understand that syntax.
>      ++
>      ++# U+202a..U+2a2e: LRE, RLE, PDF, LRO and RLO
>      ++# U+2066..U+2069: LRI, RLI, FSI and PDI
>      ++regex='(\u202a|\u202b|\u202c|\u202d|\u202e|\u2066|\u2067|\u2068|\u2069)'
>      ++
>      ++! git ls-files -z ':(attr:!binary)' |
>      ++LC_CTYPE=C xargs -0r git grep -Ele "$(LC_CTYPE=C.UTF-8 printf "$regex")" --
>
>
>  .github/workflows/main.yml         |  1 +
>  ci/check-directional-formatting.sh | 25 +++++++++++++++++++++++++
>  2 files changed, 26 insertions(+)
>  create mode 100755 ci/check-directional-formatting.sh
>
> diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
> index 6ed6a9e8076..36b7a0bee38 100644
> --- a/.github/workflows/main.yml
> +++ b/.github/workflows/main.yml
> @@ -289,6 +289,7 @@ jobs:
>      - uses: actions/checkout@v2
>      - run: ci/install-dependencies.sh
>      - run: ci/run-static-analysis.sh
> +    - run: ci/check-directional-formatting.sh
>    sparse:
>      needs: ci-config
>      if: needs.ci-config.outputs.enabled == 'yes'
> diff --git a/ci/check-directional-formatting.sh b/ci/check-directional-formatting.sh
> new file mode 100755
> index 00000000000..ab894715cf1
> --- /dev/null
> +++ b/ci/check-directional-formatting.sh
> @@ -0,0 +1,25 @@
> +#!/bin/bash
> +
> +# This script verifies that the non-binary files tracked in the Git index do
> +# not contain any Unicode directional formatting: such formatting could be used
> +# to deceive reviewers into interpreting code differently from the compiler.
> +# This is intended to run on an Ubuntu agent in a GitHub workflow.
> +#
> +# `git grep` as well as GNU grep do not handle `\u` as a way to specify UTF-8.
> +# A PCRE-enabled `git grep` would handle `\u` as desired, but Ubuntu does
> +# not build its `git` packages with PCRE support.

I just took you at your word before, but, no. It does have PCRE. It's
this Ubuntu version:
https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-README.md
i.e. "Ubuntu 20.04.3 LTS".

Debian's "git" package builds with libpcre, and Ubuntu's page about it
suggests it does the same: https://packages.ubuntu.com/focal/git

And if you try to push e.g. a grep for "git grep -P 'foo(?=bar)' you'll
find that it works.

What we haven't done in "git grep" is to compile the pattern with
PCRE2_ALT_BSUX, which is probably what you ran into. I.e. it doesn't
grok \u by default, we should probably support that, but you don't need
it.

> +#
> +# To work around that, we use `printf` to produce the pattern as a byte
> +# sequence, and then feed that to `git grep` as a byte sequence (setting
> +# `LC_CTYPE` to make sure that the arguments are interpreted as intended).
> +#
> +# Note: we need to use Bash here because its `printf` interprets `\uNNNN` as
> +# UTF-8 code points, as desired. Running this script through Ubuntu's `dash`,
> +# for example, would use a `printf` that does not understand that syntax.
> +
> +# U+202a..U+2a2e: LRE, RLE, PDF, LRO and RLO
> +# U+2066..U+2069: LRI, RLI, FSI and PDI
> +regex='(\u202a|\u202b|\u202c|\u202d|\u202e|\u2066|\u2067|\u2068|\u2069)'
> +
> +! git ls-files -z ':(attr:!binary)' |
> +LC_CTYPE=C xargs -0r git grep -Ele "$(LC_CTYPE=C.UTF-8 printf "$regex")" --

FWIW with that ls-files suggestion of mine I meant that would make sense
as a way to pipe it into Perl since doing this with a Unicode-aware
regex engine is less painful. I.e. you'd be able to just name the
Unicode range.

But since you didn't go for that surely this whole ls-files -> xargs ->
git grep pipeline isn't needed, and you'd just want:

    ! git -P grep -nE "$(LC_CTYPE=C.UTF-8 printf "$regex")" ':(attr:!binary)'

I added the -n for readability of the output, a matter of taste, and -P
to "git" for ease of ad-hoc testing. A test of that on the researcher's
trojan-source.git repo:
    
    $ ! git -P grep -nE "$(LC_CTYPE=C.UTF-8 printf "$regex")" ':(attr:!binary)'; echo $?
    C#/commenting-out.csx:4:/*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
    C#/commenting-out.csx:6:/* end admins only ‮ { ⁦*/
    C#/stretched-string.csx:4:if (access_level != "user‮ ⁦// Check if admin⁩ ⁦") {
    C++/commenting-out.cpp:5:    /*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
    C++/commenting-out.cpp:7:    /* end admins only ‮ { ⁦*/
    C++/stretched-string.cpp:6:    if (access_level.compare("user‮ ⁦// Check if admin⁩ ⁦")) {
    C/commenting-out.c:6:    /*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
    C/commenting-out.c:8:    /* end admins only ‮ { ⁦*/
    C/early-return.c:4:    /* Say hello; newline⁧ /*/ return 0 ;
    C/stretched-string.c:6:    if (strcmp(access_level, "user‮ ⁦// Check if admin⁩ ⁦")) {
    Go/commenting-out.go:9:    /*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
    Go/commenting-out.go:11:    /* end admins only ‮ { ⁦*/
    Go/stretched-string.go:7:       if accessLevel != "user‮ ⁦// Check if admin⁩ ⁦" {
    Java/CommentingOut.java:5:        /*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
    Java/CommentingOut.java:7:        /* end admins only ‮ { ⁦*/
    Java/StretchedString.java:5:        if (accessLevel != "user‮ ⁦// Check if admin⁩ ⁦") {
    JavaScript/commenting-out.js:4:/*‮ } ⁦if (isAdmin)⁩ ⁦ begin admins only */
    JavaScript/commenting-out.js:6:/* end admins only ‮ { ⁦*/
    JavaScript/stretched-string.js:4:if (accessLevel != "user‮ ⁦// Check if admin⁩ ⁦") {
    Python/commenting-out.py:4:if access_level != 'none‮⁦': # Check if admin ⁩⁦' and access_level != 'user
    Python/early-return.py:5:    ''' Subtract funds from bank account then ⁧''' ;return
    Rust/commenting-out.rs:3:    /*‮ } ⁦if is_admin⁩ ⁦ begin admins only */
    Rust/commenting-out.rs:5:    /* end admins only ‮ { ⁦*/
    Rust/stretched-string.rs:3:    if access_level != "user‮ ⁦// Check if admin⁩ ⁦" {
    Binary file website/public/trojan-source.pdf matches
    Binary file website/src/assets/img/faces/erik-lucatero-2.jpg matches
    1

That "Binary file..." being because they don't maintain an up-to-date
.gitattributes file.

The -e you've got is redundant since you have only one pattern. A relic
of trying to feed a list of these with "-e" to "git grep"?

But anyway, as noted you're not correct that we don't have PCRE with the
"git" in the CI, we just don't have \uxxxx, but you don't need that
optional syntax. Just use \N{U+xxxx}:

    ! git -P grep -nP '[\N{U+202a}-\N{U+202e}\N{U+2066}-\N{U+2069}]' ':(attr:!binary)'

All that being said I still don't see much of a point in doing this, but
if we are let's not make it needlessly complex, but actually, why not
some variant of:
    
    $ ~/g/git/git grep -Pn '[\N{U+2000}-\N{U+206F}]' -- ':!(attr:binary)' ':!/po/' ':!*/po/*' ':!t/t0200/' ':!t/t0204*' ':!t/t5150-request-pull.sh'
    builtin/gc.c:1543: *   * if the input value *cmd isn’t the key of any of the comma-separated list
    builtin/gc.c:1555: *   GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
    builtin/submodule--helper.c:3262:                          N_("sets the submodule’s name to the given string "
    contrib/credential/netrc/git-credential-netrc.perl:122:   The credential’s username, if we already have one. (username=X)
    t/helper/test-mergesort.c:364: * Function" by Bentley and McIlroy, Software—Practice and Experience,
    t/helper/test-mergesort.c:365: * Volume 23, Issue 11, 1249–1265 (November 1993).
    Binary file t/t0013/shattered-1.pdf matches

I.e. that notices that the "binary" is missing for that *.pdf, and that
we have some Unicode quoting we probably should get rid of anyway, it
also nicely excludes po/* in the pathspec, so any legitimate use of this
for RTL languages won't be prevented by this check.




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux