Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Do not use os.getpid() and tempfile._get_candidate_names().
  • Loading branch information
serhiy-storchaka committed Aug 5, 2025
commit ed050a03a3870f7477fd5e3ff7eeba61a05b4d92
6 changes: 2 additions & 4 deletions Lib/asyncio/windows_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import _winapi
import msvcrt
import os
import random
import subprocess
import tempfile
import warnings


Expand Down Expand Up @@ -48,12 +48,10 @@ def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):
else:
flags_and_attribs = 0

prefix = fr'\\.\pipe\python-pipe-{os.getpid()}-'
names = tempfile._get_candidate_names()
h1 = h2 = None
try:
while True:
address = prefix + next(names)
address = r'\\.\pipe\python-pipe-' + random.randbytes(8).hex()
Copy link
Copy Markdown
Member

@picnixz picnixz Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment:

we could use os.urandom(8).hex() but this would only reduce the number of loop iterations we would do until we find a suitable name. OTOH, this makes normal cases much slower (usually, we are not in the presence of an adversary that is trying to create pipes...), so we would be only doing the loop once.

Now, I'm actually worried that it if we're able to interact with the process that is creating the pipes, then we could actually recover enough samples from the underlying PRNG instance and get the original seed. But this is only if 1) there are no random calls in between and 2) we can get 624 consecutive 32-bit samples from the random source.

Condition 2) already holds because random.randbytes(8) is actually equivalent to two calls of random.randbytes(4) that are concatenated. Since reverting MT-19937 requires only consecutive 624 32-bit words, this is the same as doing 312 pipe creations where we don't have calls to random.* in between (and then inspect the named file). This could be possible in practice, especially if we're talking about the multiprocessing and the asyncio components which could have some interactivness (e.g., a server).

Therefore, I would still suggest using os.urandom(8).hex() even if it slows down the calls.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I did not thought about this.

Sorry for not answering immediately, I missed your comment.

try:
h1 = _winapi.CreateNamedPipe(
address, openmode, _winapi.PIPE_WAIT,
Expand Down
3 changes: 2 additions & 1 deletion Lib/multiprocessing/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import errno
import io
import os
import random
import sys
import socket
import struct
Expand Down Expand Up @@ -75,7 +76,7 @@ def arbitrary_address(family):
elif family == 'AF_UNIX':
return tempfile.mktemp(prefix='sock-', dir=util.get_temp_dir())
elif family == 'AF_PIPE':
return fr'\\.\pipe\pyc-{os.getpid()}-{next(tempfile._get_candidate_names())}'
return r'\\.\pipe\pyc-' + random.randbytes(8).hex()
else:
raise ValueError('unrecognized family')

Expand Down
Loading