elf: Add test for locating libraries in root dir (bug 30435)

When dlopen is being called, efforts have been made to improve
future lookup performance. This includes marking a search path
as non-existent using `stat`. However, if the root directory
is given as a search path, there exists a bug which erroneously
marks it as non-existing.

The bug is reproduced under the following sequence:

  1. dlopen is called to open a shared library, with at least:
     1) a dependency 'A.so' not directly under the '/' directory
        (e.g. /lib/A.so), and
     2) another dependency 'B.so' resides in '/'.
  2. for this bug to reproduce, 'A.so' should be searched *before* 'B.so'.
  3. it first tries to find 'A.so' in /, (e.g. /A.so):
     - this will (obviously) fail,
     - since it's the first time we have seen the '/' directory,
       its 'status' is 'unknown'.
  4. `buf[buflen - namelen - 1] = '\0'` is executed:
     - it intends to remove the leaf and its final slash,
     - because of the speciality of '/', its buflen == namelen + 1,
     - it erroneously clears the entire buffer.
  6. it then calls 'stat' with the empty buffer:
     - which will result in an error.
  7. so it marks '/' as 'nonexisting', future lookups will not consider
     this path.
  8. while /B.so *does* exist, failure to look it up in the '/'
     directory leads to a 'cannot open shared object file' error.

This patch fixes the bug by preventing 'buflen', an index to put '\0',
from being set to 0, so that the root '/' is always kept.
Relative search paths are always considered as 'existing' so this
wont be affected.

Writeup by Moody Liu <mooodyhunter@outlook.com>

Suggested-by: Carlos O'Donell <carlos@redhat.com>
Signed-off-by: Qixing ksyx Xue <qixingxue@outlook.com>
Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
This commit is contained in:
Qixing ksyx Xue 2023-05-25 11:10:54 -04:00 committed by Siddhesh Poyarekar
parent a118dc3129
commit dbfc83bdca
5 changed files with 64 additions and 0 deletions

View File

@ -325,6 +325,7 @@ static-dlopen-environment = \
LD_LIBRARY_PATH=$(ld-library-path):$(common-objpfx)dlfcn LD_LIBRARY_PATH=$(ld-library-path):$(common-objpfx)dlfcn
tst-tls9-static-ENV = $(static-dlopen-environment) tst-tls9-static-ENV = $(static-dlopen-environment)
tst-single_threaded-static-dlopen-ENV = $(static-dlopen-environment) tst-single_threaded-static-dlopen-ENV = $(static-dlopen-environment)
tst-rootdir-ENV = LD_LIBRARY_PATH=/
tests += \ tests += \
argv0test \ argv0test \
@ -506,6 +507,7 @@ tests-container += \
tst-dlopen-tlsmodid-container \ tst-dlopen-tlsmodid-container \
tst-pldd \ tst-pldd \
tst-preload-pthread-libc \ tst-preload-pthread-libc \
tst-rootdir \
# tests-container # tests-container
test-srcs = \ test-srcs = \
@ -855,6 +857,7 @@ modules-names += \
tst-relsort1mod1 \ tst-relsort1mod1 \
tst-relsort1mod2 \ tst-relsort1mod2 \
tst-ro-dynamic-mod \ tst-ro-dynamic-mod \
tst-rootdir-lib \
tst-single_threaded-mod1 \ tst-single_threaded-mod1 \
tst-single_threaded-mod2 \ tst-single_threaded-mod2 \
tst-single_threaded-mod3 \ tst-single_threaded-mod3 \

23
elf/tst-rootdir-lib.c Normal file
View File

@ -0,0 +1,23 @@
/* Simple library for testing locating library in root directories.
Copyright The GNU Toolchain Authors.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
const char *
test_func (void)
{
return "Success";
}

37
elf/tst-rootdir.c Normal file
View File

@ -0,0 +1,37 @@
/* Test code for locating libraries in root directories.
Copyright The GNU Toolchain Authors.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include <support/test-driver.h>
#include <support/check.h>
#include <dlfcn.h>
#include <assert.h>
static int
do_test (void)
{
void *handle = dlopen ("libtest.so", RTLD_LAZY);
TEST_VERIFY_EXIT (handle != NULL);
typedef const char *(test_func_t) (void);
test_func_t *func = dlsym (handle, "test_func");
assert (func != NULL);
TEST_COMPARE_STRING (func (), "Success");
dlclose (handle);
return 0;
}
#include <support/test-driver.c>

View File

1
elf/tst-rootdir.script Normal file
View File

@ -0,0 +1 @@
cp $B/elf/tst-rootdir-lib.so /libtest.so