Skip to content

src/libexpr/search-path.cc: avoid out-of-bounds read on string_view - #8825

Merged
edolstra merged 1 commit into
NixOS:masterfrom
trofi:search-path-prefix
Aug 16, 2023
Merged

edolstra merged 1 commit into
NixOS:masterfrom
trofi:search-path-prefix

Conversation

@trofi

@trofi trofi commented Aug 14, 2023

Copy link
Copy Markdown
Contributor

Without the change build with -D_GLIBCXX_ASSERTIONS exposes testsuite assertion:

$ gdb src/libexpr/tests/libnixexpr-tests
Reading symbols from src/libexpr/tests/libnixexpr-tests...
(gdb) break __glibcxx_assert_fail
(gdb) run
(gdb) bt
in std::__glibcxx_assert_fail(char const*, int, char const*, char const*)@plt () from /mnt/archive/big/git/nix/src/libexpr/libnixexpr.so
in std::basic_string_view<char, std::char_traits<char> >::operator[] (this=0x7fffffff56c0, __pos=4)
    at /nix/store/r74fw2j8rx5idb0w8s1s6ynwwgs0qmh9-gcc-14.0.0/include/c++/14.0.0/string_view:258
in nix::SearchPath::Prefix::suffixIfPotentialMatch (this=0x7fffffff5780, path=...) at src/libexpr/search-path.cc:15
in nix::SearchPathElem_suffixIfPotentialMatch_partialPrefix_Test::TestBody (this=0x555555a17540) at src/libexpr/tests/search-path.cc:62

As string sizes are usigned types (a - b) > 0 effectively means a != b. While the intention should be a > b.

The change fixes test suite pass.

Motivation

Context

Checklist for maintainers

Maintainers: tick if completed or explain if not relevant

  • agreed on idea
  • agreed on implementation strategy
  • tests, as appropriate
    • functional tests - tests/**.sh
    • unit tests - src/*/tests
    • integration tests - tests/nixos/*
  • documentation in the manual
  • documentation in the internal API docs
  • code and comments are self-explanatory
  • commit message explains why the change was made
  • new feature or incompatible change: updated release notes

Priorities

Add 👍 to pull requests you find important.

Without the change build with `-D_GLIBCXX_ASSERTIONS` exposes testsuite
assertion:

    $ gdb src/libexpr/tests/libnixexpr-tests
    Reading symbols from src/libexpr/tests/libnixexpr-tests...
    (gdb) break __glibcxx_assert_fail
    (gdb) run
    (gdb) bt
    in std::__glibcxx_assert_fail(char const*, int, char const*, char const*)@plt () from /mnt/archive/big/git/nix/src/libexpr/libnixexpr.so
    in std::basic_string_view<char, std::char_traits<char> >::operator[] (this=0x7fffffff56c0, __pos=4)
        at /nix/store/r74fw2j8rx5idb0w8s1s6ynwwgs0qmh9-gcc-14.0.0/include/c++/14.0.0/string_view:258
    in nix::SearchPath::Prefix::suffixIfPotentialMatch (this=0x7fffffff5780, path=...) at src/libexpr/search-path.cc:15
    in nix::SearchPathElem_suffixIfPotentialMatch_partialPrefix_Test::TestBody (this=0x555555a17540) at src/libexpr/tests/search-path.cc:62

As string sizes are usigned types `(a - b) > 0` effectively means
`a != b`. While the intention should be `a > b`.

The change fixes test suite pass.
@trofi
trofi requested a review from edolstra as a code owner August 14, 2023 21:11
trofi added a commit to trofi/nix-guix-gentoo that referenced this pull request Aug 14, 2023
Initially reported by ThomasP at

    https://discourse.nixos.org/t/error-while-installing-home-manager/31720

There `nix-build` triggered `libstdc++` assertion when `gcc` is built
with `-D_GLIBCXX_ASSERTIONS` enabled by default.

Apply a simple patch proposed upstream to avoid out-of-bounds access to
the `string_view`:

    NixOS/nix#8825
@iFreilicht

Copy link
Copy Markdown
Contributor

Is this the only assertion that fails when -D_GLIBCXX_ASSERTIONS is set? If so, might it make sense to enable this while running the test suites to prevent regressions?

@trofi

trofi commented Aug 15, 2023

Copy link
Copy Markdown
Contributor Author

Certainly that failure was the only one on the nix test suite. And it was enough for the original reported of the failure at https://discourse.nixos.org/t/error-while-installing-home-manager/31720/13 to get working nix in Gentoo environment.

There is a tiny chance there are more bugs lurking, but so far it looks functioning.

As for -D_GLIBCXX_ASSERTIONS enabled by default it sounds reasonable to have at least as one of the flavours of the test. I'm not very familiar with nix's flake.nix to extend it though.

I hoped nix also uses -fsanitize=address as part of it's testsuite which should have caught the failures as well. But maybe boehm-gc does not make it as simple.

@edolstra
edolstra merged commit 7f8c99c into NixOS:master Aug 16, 2023
@github-actions

Copy link
Copy Markdown

Backport failed for 2.15-maintenance, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git fetch origin 2.15-maintenance
git worktree add -d .worktree/backport-8825-to-2.15-maintenance origin/2.15-maintenance
cd .worktree/backport-8825-to-2.15-maintenance
git checkout -b backport-8825-to-2.15-maintenance
ancref=$(git merge-base 5542c1f87ee3325bce8140f4087b12647b4107ef b74962c92b2d8d9b957934e0aefcf4983169ae1e)
git cherry-pick -x $ancref..b74962c92b2d8d9b957934e0aefcf4983169ae1e

@trofi
trofi deleted the search-path-prefix branch August 16, 2023 15:12
@Ericson2314

Copy link
Copy Markdown
Member

Thanks for fixing this! (I probably wrote the original)

@trofi

trofi commented Aug 16, 2023

Copy link
Copy Markdown
Contributor Author

I think be518e7 changed (path.size() > s && path[s] != '/') to n > 0 && (path.size() - n) > 0. It's only a part of 2.17.0. Should be no need to backport anywhere.

@github-actions

github-actions Bot commented Sep 6, 2023

Copy link
Copy Markdown

Backport failed for 2.15-maintenance, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git fetch origin 2.15-maintenance
git worktree add -d .worktree/backport-8825-to-2.15-maintenance origin/2.15-maintenance
cd .worktree/backport-8825-to-2.15-maintenance
git checkout -b backport-8825-to-2.15-maintenance
ancref=$(git merge-base 5542c1f87ee3325bce8140f4087b12647b4107ef b74962c92b2d8d9b957934e0aefcf4983169ae1e)
git cherry-pick -x $ancref..b74962c92b2d8d9b957934e0aefcf4983169ae1e

@github-actions

github-actions Bot commented Sep 6, 2023

Copy link
Copy Markdown

Backport failed for 2.16-maintenance, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git fetch origin 2.16-maintenance
git worktree add -d .worktree/backport-8825-to-2.16-maintenance origin/2.16-maintenance
cd .worktree/backport-8825-to-2.16-maintenance
git checkout -b backport-8825-to-2.16-maintenance
ancref=$(git merge-base 5542c1f87ee3325bce8140f4087b12647b4107ef b74962c92b2d8d9b957934e0aefcf4983169ae1e)
git cherry-pick -x $ancref..b74962c92b2d8d9b957934e0aefcf4983169ae1e

@github-actions

github-actions Bot commented Sep 6, 2023

Copy link
Copy Markdown

Backport failed for 2.15-maintenance, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git fetch origin 2.15-maintenance
git worktree add -d .worktree/backport-8825-to-2.15-maintenance origin/2.15-maintenance
cd .worktree/backport-8825-to-2.15-maintenance
git checkout -b backport-8825-to-2.15-maintenance
ancref=$(git merge-base 5542c1f87ee3325bce8140f4087b12647b4107ef b74962c92b2d8d9b957934e0aefcf4983169ae1e)
git cherry-pick -x $ancref..b74962c92b2d8d9b957934e0aefcf4983169ae1e

@github-actions

github-actions Bot commented Sep 6, 2023

Copy link
Copy Markdown

Backport failed for 2.16-maintenance, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git fetch origin 2.16-maintenance
git worktree add -d .worktree/backport-8825-to-2.16-maintenance origin/2.16-maintenance
cd .worktree/backport-8825-to-2.16-maintenance
git checkout -b backport-8825-to-2.16-maintenance
ancref=$(git merge-base 5542c1f87ee3325bce8140f4087b12647b4107ef b74962c92b2d8d9b957934e0aefcf4983169ae1e)
git cherry-pick -x $ancref..b74962c92b2d8d9b957934e0aefcf4983169ae1e

@github-actions

github-actions Bot commented Sep 6, 2023

Copy link
Copy Markdown

Successfully created backport PR for 2.17-maintenance:

@github-actions

github-actions Bot commented Sep 6, 2023

Copy link
Copy Markdown

Git push to origin failed for 2.17-maintenance with exitcode 1

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.

4 participants