From: Carlos O'Donell <carlos@redhat.com>
To: Moody Liu <mooodyhunter@outlook.com>, libc-alpha@sourceware.org
Cc: Qixing ksyx Xue <qixingxue@outlook.com>
Subject: Re: [PATCH] elf: Fix marking root dir as nonexist in open_path
Date: Mon, 8 May 2023 16:04:32 -0400 [thread overview]
Message-ID: <e9a74ab4-5c4f-1a0a-d2d1-9b684e180c41@redhat.com> (raw)
In-Reply-To: <ME3P282MB39685DC6FC209A91F072BA3CD3719@ME3P282MB3968.AUSP282.PROD.OUTLOOK.COM>
On 5/8/23 11:45, Moody Liu via Libc-alpha wrote:
> 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.
Thanks for working on the patch.
Could you please file a bug in bugzilla for this?
>
> 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 '/'.
Are you able to write a containerized test case for this?
We have tests-container testing for just such "/" scenarios with a distinct
mount namespace for the tests.
> 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.
>
> Suggested-by: Qixing ksyx Xue <qixingxue@outlook.com>
> ---
> elf/dl-load.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index fcb39a78d4..10757dd5a5 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -1865,7 +1865,11 @@ open_path (const char *name, size_t namelen, int mode,
> test whether there is any directory at all. */
> struct __stat64_t64 st;
>
> - buf[buflen - namelen - 1] = '\0';
> + /* We only have absolute paths go into this branch.
> + In the rare case where 'this_dir' is only a '/', we
> + must keep it. */
> + buflen = MAX(buflen - namelen - 1, 1);
> + buf[buflen] = '\0';
>
> if (__stat64_time64 (buf, &st) != 0
> || ! S_ISDIR (st.st_mode))
--
Cheers,
Carlos.
next prev parent reply other threads:[~2023-05-08 20:04 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-08 15:45 Moody Liu
2023-05-08 20:04 ` Carlos O'Donell [this message]
2023-05-09 22:56 ` [PATCH v2 1/2] elf: Fix marking root dir as nonexist in open_path (bug 30435) Qixing ksyx Xue
2023-05-09 22:56 ` [PATCH v2 2/2] elf: Add test for finding libraries in root dir Qixing ksyx Xue
2023-05-25 12:51 ` Siddhesh Poyarekar
2023-05-25 13:29 ` [PATCH v3] elf: Add test for locating libraries in root dir (bug 30435) Qixing ksyx Xue
2023-05-25 14:52 ` Siddhesh Poyarekar
2023-05-25 14:56 ` Qixing ksyx Xue
2023-05-25 15:12 ` Siddhesh Poyarekar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e9a74ab4-5c4f-1a0a-d2d1-9b684e180c41@redhat.com \
--to=carlos@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=mooodyhunter@outlook.com \
--cc=qixingxue@outlook.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).