Linux Manual Pages development
 help / color / mirror / Atom feed
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: Wed, 13 Dec 2023 14:54:19 +0100	[thread overview]
Message-ID: <ZXm3kXJF-FGtbMxf@debian> (raw)
In-Reply-To: <ZXl53MYCgFRMSYUG@debian>

[-- Attachment #1: Type: text/plain, Size: 2353 bytes --]

Hi ellie,

On Wed, Dec 13, 2023 at 10:31:02AM +0100, Alejandro Colomar wrote:
> I'm going to guess a little bit, from the following script:
> 
> 	$ cat ppe.sh 
> 	#!/home/alx/tmp/bash
> 
> 	f="$(readlink /proc/$$/exe)";
> 
> 	set -x;
> 
> 	mv $f $f.bak;
> 	cp /usr/bin/bash $f;
> 	readlink /proc/$$/exe;
> 
> 	cp /usr/bin/bash $f.bak;
> 	readlink /proc/$$/exe;
> 
> 	rm $f.bak;
> 	readlink /proc/$$/exe;
> 
> 	echo malicious >$f.bak;
> 	readlink /proc/$$/exe;
> 
> 	head -c4 /proc/$$/exe;

On 12/13/23 10:31 AM, Alejandro Colomar wrote:
> There don't seem to be any
> races in open("/proc/[pid]/exe", ...).

ellie wrote:
| Sorry if I'm just confused, but I don't understand how the given test 
| script reproduces or tests anything relevant to a potential open() race.
| 
| The race would work like this:
| 
| 1. Process A issues open("/proc/self/exe")
| 
| 2. Process A's open() on /proc/self/exe heads into whatever libc or 
| kernel path that resolves where that symlink points to a path, if it's 
| treated as one.
| 
| 3. Process scheduler switches to process B.
| 
| 4. Process B switches out process A's binary, such that a different 
| binary is now at the old path.
| 
| 5. Process A's open() code in libc or kernel space resumes and opens the 
| file pointed to by the given path, which is now a new binary.
| 
| I'm pretty sure you can't test that anyway to rule it out, that could 
| only be answered by looking at all the relevant code and whether 
| /proc/self/exe is ever resolved to a path (like I assume an actual 
| symlink usually is) or directly to an inode (which would likely be safe).

I didn't test exactly that, but I proved that it doesn't work like a
symlink.  If it were a simple symlink, my script wouldn't get the new
path of the file after moving or removing it, and the symlink would
become dangling.

Still, the kernel could do the stupid thing: magically update the
symlink when the file changes, but still use a symlink, so open(2) would
still have a race.  I'm assuming the kernel doesn't do that stupid thing.

It must be implemented using the file description, or something like
that, and thus it would be free of races.

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 --]

  parent reply	other threads:[~2023-12-13 13:54 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
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 [this message]
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=ZXm3kXJF-FGtbMxf@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox