nixpkgs/pkgs/build-support
aszlig 8e6c0c14a4
libredirect: Fix segfault handling null paths
While using libredirect in conjunction with geckodriver, I stumbled on
odd segfaults that happened when running the wrapped statx() call from
libredirect:

  0x00007ffff7ddd541 in __strncmp_avx2 () from .../lib/libc.so.6
  0x00007ffff7f6fe57 in statx () from .../lib/libredirect.so
  0x00005555558d35bd in std::sys::unix::fs::try_statx::h2045d39b0c66d4e8 ()
  0x00005555558d2230 in std::sys::unix::fs::stat::ha063998dfb361520 ()
  0x0000555555714019 in mozversion::firefox_version::hdc3b57eb04947426 ()
  0x00005555556a603c in geckodriver::capabilities::FirefoxCapabilities::version::h58e289917bd3c721 ()
  0x00005555556a77f5 in <geckodriver::capabilities::FirefoxCapabilities as webdriver::capabilities::BrowserCapabilities>::validate_custom::h62d23cf9fd63b719 ()
  0x000055555562a7c8 in webdriver::capabilities::SpecNewSessionParameters::validate::h60da250d33f0989f ()
  0x00005555556d7a13 in <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold::h9427a360a3d0bf8f ()
  0x0000555555669d85 in <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter::hd274d536ea29bb33 ()
  0x00005555555c05ef in core::iter::adapters::try_process::hdf96a01ec1f9b8bd ()
  0x000055555561768d in <webdriver::capabilities::SpecNewSessionParameters as webdriver::capabilities::CapabilitiesMatching>::match_browser::hfbd8c38f6db17e9f ()
  0x00005555555ca6ef in <geckodriver::marionette::MarionetteHandler as webdriver::server::WebDriverHandler<geckodriver::command::GeckoExtensionRoute>>::handle_command::h13b98b9cb87a69d6 ()
  0x00005555555e859e in webdriver::server::Dispatcher<T,U>::run::h746a8bf2f0bc24fd ()
  0x000055555569ff0f in std::sys_common::backtrace::__rust_begin_short_backtrace::h3b920773bd467d2a ()
  0x00005555555dbc99 in core::ops::function::FnOnce::call_once{{vtable.shim}}::h81ba7228877515f7 ()
  0x00005555558d31a3 in std::sys::unix:🧵:Thread:🆕:thread_start::h4514580219a899c5 ()
  0x00007ffff7d0ce24 in start_thread () from .../lib/libc.so.6
  0x00007ffff7d8e9b0 in clone3 () from .../lib/libc.so.6

The reason why I found this odd was because it happens in the following
piece of code (shortened a bit):

   1 static const char * rewrite(const char * path, char * buf)
   2 {
   3   if (path == NULL) return path;
   4   for (int n = 0; n < nrRedirects; ++n) {
   5     int len = strlen(from[n]);
   6     if (strncmp(path, from[n], len) != 0) continue;
   7     if (snprintf(buf, PATH_MAX, "%s%s", to[n], path + len) >= PATH_MAX)
   8       abort();
   9     return buf;
  10   }
  11   return path;
  12 }

When inspecting the assembly, I found that the check for the null
pointer in line 3 was completely missing and the code was directly
entering the loop and then eventually segfault when running strncmp()
with a null pointer as its first argument.

I confirmed that indeed that check was missing by compiling libredirect
with "-O0" and comparing the generated assembly with the optimized one.
The one compiled with "-O0" had that check while the optimized one did
not and indeed when running geckodriver with the unoptimized version it
worked fine.

Digging in the Git history, I found 5677ce2008,
which actually introduced the null pointer check. Going back to that
commit however, the check actually was still in the generated assembly.

So I bisected between that commit and the most recent one and ended up
with commit ca8aa5dc87, which moved
everything to use GCC 7.

I haven't found out why *exactly* GCC was optimizing the check away, but
playing around on Godbolt with various other compilers seems that other
compilers such as Clang are doing it as well. Additionally, given that
passing NULL to stat() is UB, my guess is that compilers tend to assume
that such an argument can't be NULL. My assumption is based on the fact
that GCC warns with "argument 1 null where non-null expected" when
passing NULL to eg. stat().

To address this for now, I marked the path argument of the rewrite()
volatile and also added a test that should cause a segfault in case this
would regress again as it already did.

Signed-off-by: aszlig <aszlig@nix.build>
2023-08-19 00:58:43 +02:00
..
add-opengl-runpath
agda
alternatives lapack: force a rebuild on x86_64-darwin 2023-03-24 08:57:05 +01:00
appimage tree-wide: buildFHSUserEnv -> buildFHSEnv 2023-04-16 10:15:13 +02:00
binary-cache binary-cache: use lib.makeBinPath 2023-06-30 00:42:14 +02:00
bintools-wrapper Merge pull request #239624 from Stunkymonkey/use-optionalString-then 2023-07-22 13:02:47 +02:00
build-bazel-package buildBazelPackage: add support for bazel run targets 2023-08-02 16:05:55 +08:00
build-fhsenv-bubblewrap buildFHSEnv: disable multiArch by default 2023-07-07 21:33:04 +02:00
build-fhsenv-chroot buildFHSEnv: use default values as fallback for XDG_DATA_DIRS 2023-05-08 18:56:51 -03:00
build-graalvm-native-image buildGraalvmNativeImage: allow overwriting {build,install}Phase/nativeBuildInputs 2023-03-17 16:50:52 +00:00
build-setupcfg
buildenv buildenv: Limit exclusion of info/dir 2023-07-09 22:06:26 -04:00
cc-wrapper cc-wrapper: include fortify-headers before libc includes for musl 2023-08-06 17:52:28 +01:00
coq treewide: deprecate isNull 2023-03-06 22:40:04 +01:00
dart buildDartApplication: add sigtool on darwin 2023-06-14 22:29:01 +03:00
deterministic-uname
dhall
docker treewide: use optionalString instead of 'then ""' 2023-06-24 20:19:19 +02:00
dotnet nuget-to-nix: set nullglob 2023-06-24 19:13:16 -03:00
emacs treewide: overrideScope' -> overrideScope 2023-08-14 18:46:47 +03:00
expand-response-params
fake-nss
fetch9front lib: add fetchFrom9Front 2023-07-23 13:16:28 -05:00
fetchbitbucket treewide: Make some fetchers overridable 2023-04-22 22:29:23 +02:00
fetchbower
fetchbzr fetchbzr: set cache directory to tmpdir 2023-03-01 04:20:00 +00:00
fetchcvs treewide: Make some fetchers overridable 2023-04-22 22:29:23 +02:00
fetchdarcs fetchdarcs: clean up md5 references 2023-07-20 21:24:13 -07:00
fetchdocker treewide: don't hardcode /nix/store (no rebuilds changes) 2023-03-24 20:11:33 +02:00
fetchfirefoxaddon fetchfirefoxaddon: fix passing md5 to fetchurl 2023-07-28 16:18:15 -04:00
fetchfossil
fetchgit Merge master into staging-next 2023-07-21 06:01:05 +00:00
fetchgitea treewide: Make some fetchers overridable 2023-04-22 22:29:23 +02:00
fetchgithub treewide: use optionalString instead of 'then ""' 2023-06-24 20:19:19 +02:00
fetchgitiles treewide: Make some fetchers overridable 2023-04-22 22:29:23 +02:00
fetchgitlab treewide: Make some fetchers overridable 2023-04-22 22:29:23 +02:00
fetchgitlocal treewide: Make some fetchers overridable 2023-04-22 22:29:23 +02:00
fetchgx
fetchhg fetchhg: clean up md5 references 2023-07-20 21:24:13 -07:00
fetchipfs
fetchmavenartifact fetchMavenArtifact: deprecate phases & use pname+version 2023-03-26 18:44:55 +02:00
fetchmtn
fetchnextcloudapp
fetchpatch fetchpatch: add decode test 2023-03-11 21:39:29 +00:00
fetchpijul build-support: Add fetchpijul function. 2023-07-17 18:44:06 +02:00
fetchpypi
fetchrepoorcz
fetchrepoproject fetchrepoproject: fix a bug that was there since bef6bef0d2 2023-08-06 12:17:09 +00:00
fetchs3
fetchsavannah treewide: Make some fetchers overridable 2023-04-22 22:29:23 +02:00
fetchsourcehut fetchFromSourcehut: expose gitRepoUrl to consumers 2023-06-11 09:02:34 +01:00
fetchsvn fetchsvn: clean up md5 references 2023-07-20 21:24:07 -07:00
fetchsvnrevision
fetchsvnssh fetchsvnssh: clean up md5 references 2023-07-20 21:24:13 -07:00
fetchurl fetchurl: clean up md5 references 2023-07-20 21:23:04 -07:00
fetchzip
flutter flutter: build-support: allow customizing wrapProgram args 2023-05-14 17:10:49 +02:00
go buildGoModule: set GOPROXY to go default 2023-08-02 01:08:27 +10:00
icon-conv-tools
install-shell-files
java
kernel Merge pull request #239624 from Stunkymonkey/use-optionalString-then 2023-07-22 13:02:47 +02:00
libredirect libredirect: Fix segfault handling null paths 2023-08-19 00:58:43 +02:00
make-darwin-bundle
make-desktopitem treewide: deprecate isNull 2023-03-06 22:40:04 +01:00
make-hardcode-gsettings-patch
make-pkgconfigitem
make-startupitem
mkshell mkShell: set preferLocalBuild by default 2023-02-22 11:53:51 +01:00
mono-dll-fixer
nix-gitignore Merge pull request #239624 from Stunkymonkey/use-optionalString-then 2023-07-22 13:02:47 +02:00
node feat: implement redirect for fetch-yarn-deps 2023-07-26 15:13:37 +08:00
nuke-references
ocaml ocamlPackages: default to version 3 of Dune 2023-05-12 05:50:40 +02:00
oci-tools
pkg-config-wrapper treewide: use optionalAttrs instead of 'else {}' 2023-06-25 11:01:34 -03:00
portable-service
prefer-remote-fetch
references-by-popularity
release treewide: use lib.optionalAttrs 2023-07-24 21:59:17 +02:00
remove-references-to Merge pull request #162447 from thefloweringash/remove-references-to-region 2023-05-17 12:34:28 +03:00
replace-secret
rust Merge #239191: treewide: gobject-introspection from buildInputs to nativeBuildInputs 2023-07-26 20:53:32 +02:00
setup-hooks setup-hooks/strip: Create the log file in '$TMDPIR' 2023-08-07 18:20:56 +03:00
singularity-tools
snap
src-only nixos/tests: Test that Remote SSH can patch Node 2023-06-09 18:21:41 +10:00
substitute
substitute-files
templaterpm
testers testers.hasPkgConfigModules: allow checking multiple pkg-config mods 2023-08-08 19:16:48 +02:00
trivial-builders trivial-builders: add meta.mainProgram to writeShellScriptBin 2023-08-11 00:40:37 -05:00
vm Merge pull request #239624 from Stunkymonkey/use-optionalString-then 2023-07-22 13:02:47 +02:00
wrapper-common wrapBintoolsWith: support LINK.EXE-style args in purity checks 2023-04-29 01:37:00 +00:00
writers Merge branch 'staging-next' into staging 2023-08-05 07:45:29 +02:00
build-maven.nix
build-pecl.nix
closure-info.nix
make-impure-test.nix
plugins.nix
replace-dependency.nix replaceDependency: use runCommandLocal 2023-03-01 14:36:49 +02:00
setup-systemd-units.nix
source-from-head-fun.nix