From: Alejandro Colomar <alx@kernel.org>
To: ellie <el@horse64.org>
Cc: linux-man@vger.kernel.org, Petr Gajdos <pgajdos@suse.cz>
Subject: Re: Suggestion for clarifications on "man 5 proc" page regarding /proc/[pid]/self race conditions
Date: Tue, 12 Dec 2023 18:39:13 +0100 [thread overview]
Message-ID: <ZXiaxx3mjXhLDayT@debian> (raw)
In-Reply-To: <c78fe5eb-65cd-4705-8628-d07240906bff@horse64.org>
[-- Attachment #1: Type: text/plain, Size: 2457 bytes --]
Hi ellie,
On Tue, Dec 12, 2023 at 05:55:13PM +0100, ellie wrote:
> Thanks so much for the response!
>
> For what it's worth, I checked my code again for the correct path, what I
> meant was /proc/self/exe which links to the binary of the currently running
> process, as far as I understand it.
>
> I'm not sure it's easily possible to write a test program, because the
> open() wrapper by the libc on the /proc/self/exe symlink would need to be
> intercepted at just the right time in case /proc/self/exe is actually
> vulnerable. The breakpoint wouldn't be in the regular user code, might even
> be kernel code I guess, depending on where the race condition is located if
> it exists. (For FreeBSD a developer told me it supposedly exists for
> /proc/curproc/file which is apparently the equivalent, although that was
> about two years ago so I don't know if that has changed since.)
Do you suggest that open("/proc/self/exe", ...) could have a race within
the kernel? It might be useful to CC <linux-kernel@vger.kernel.org> or
some other mailing list to be sure.
>
> The wrong approach via readlink() on /proc/self/exe and then libc open() on
> the resulting path should be easy to intercept and break, but that doesn't
> really say much about the question at hand. I guess that this readlink
> approach isn't a good idea, even if commonly used, should be relatively
> obvious.
Now I'm not sure if the question at hand is that readlink(2)+open(2) has
a race (which of course it has), or if open("/proc/self/exe", ...) is
the race you're suggesting. A patch for the former is welcome, and you
could add something like this to the commit message (or you could skip
it if you feel lazy, but these things help).
int fd, r;
char path[PATH_MAX];
char text[BUFSIZ];
if (readlink("/proc/self/exe", path, NITEMS(path)) == -1)
err(EXIT_FAILURE, "readlink");
sleep(100); // Give some time for the race
// $ readlink /proc/$pid/exe \
// | while read f; do
// mv $f $f.bak;
// echo malicious >$f;
// done;
fd = open(path, 0_RDONLY);
if (fd == -1)
err(EXIT_FAILURE, "open");
r = read(fd, text, NITEMS(text));
if (r == -1)
err(EXIT_FAILURE, "read");
if (write(STDOUT_FILENO, text, r) == -1)
err(EXIT_FAILURE, "write");
Have a lovely day,
Alex
--
<https://www.alejandro-colomar.es/>
Looking for a remote C programming job at the moment.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2023-12-12 17:39 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-12 8:47 Suggestion for clarifications on "man 5 proc" page regarding /proc/[pid]/self race conditions ellie
2023-12-12 14:17 ` Alejandro Colomar
2023-12-12 16:55 ` ellie
2023-12-12 17:39 ` Alejandro Colomar [this message]
2023-12-12 17:55 ` ellie
2023-12-13 9:31 ` Alejandro Colomar
2023-12-13 9:41 ` ellie
2023-12-13 13:54 ` Alejandro Colomar
2023-12-13 13:57 ` ellie
2024-06-07 22:23 ` ellie
2024-06-09 17:08 ` Alejandro Colomar
2024-06-09 17:28 ` Alejandro Colomar
2024-06-09 17:29 ` Alejandro Colomar
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=ZXiaxx3mjXhLDayT@debian \
--to=alx@kernel.org \
--cc=el@horse64.org \
--cc=linux-man@vger.kernel.org \
--cc=pgajdos@suse.cz \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.