-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-118441: Limit posixpath.realpath(..., strict=True)
symlinks
#119172
base: main
Are you sure you want to change the base?
gh-118441: Limit posixpath.realpath(..., strict=True)
symlinks
#119172
Conversation
@barneygale, I decided to exclude cached symlinks from the count as they don't matter. But |
Hah, we seem to be working on almost the same thing :-) I've opened another PR (#119178) that adds internal support for limiting the number of symlink traversals, but doesn't (yet) enable or expose it in |
I think they probably do matter, otherwise, when given a symlink that references a parent directory, |
On my Linux system,
Note that it doesn't say anything about the number of unique symbolic links followed, and I doubt Linux has something resembling our |
Fine by me.
Yeah, I'm very aware of that, that's the reason I created my bug report. But like I already outlined previously, we can't properly track the number of symlinks traversed. Take this symlink chain: I don't think there's much cost to looking up the already resolved symlinks compared to reading and resolving them.
Which tests hang from doing dictionary lookups? We have mostly the same cost from calling |
We can also re-use |
You're equating a "symlink traversal" with a
They hang from attempting to recursively walk an infinite-depth virtual filesystem, which is the denial-of-service issue mentioned in the Linux docs. |
Which doesn't help you, because you need to guard against repeatedly traversing the same symlink. |
I never said that. On macOS the symlink limit is set to 32. If I call
Sadly, the implementation is going to get a lot uglier if we want to make it an implementation detail as we need to store how many symlinks would be traversed without caching. Unless you want to disable caching in strict mode? Please don't. |
Ah I see, because each |
Exactly, so we can't use caching in strict mode (with |
a6f1869
to
4c60431
Compare
Sorry for the force push, I accidentally had my linter on during the merge. |
@eryksun, how do we figure out the limit set by the OS? I don't like a hardcoded constant. |
Does this count as a security issue? And do you think we should do a benchmark? |
It might. See if you can find a simple-ish setup where |
@barneygale, precisely using the ln -nsfw . tmp/b
main/python.exe -m timeit -s "import os; path = os.getcwd() + '/tmp' + '/b' * 200_000" "try: os.path.realpath(path, strict=True)" "except: pass"
limit-posixpath.realpath-symlinks/python.exe -m timeit -s "import os; path = os.getcwd() + '/tmp' + '/b' * 200_000" "try: os.path.realpath(path, strict=True)" "except: pass"
This can be massively improved for non-strict mode: #120145, but that doesn't solve it entirely: ln -nsfw . tmp/b
speedup-symlink-lookup/python.exe -m timeit -s "import os; path = os.getcwd() + '/tmp' + '/b' * 4_000_000" "try: os.path.realpath(path, strict=True)" "except: pass"
limit-posixpath.realpath-symlinks/python.exe -m timeit -s "import os; path = os.getcwd() + '/tmp' + '/b' * 4_000_000" "try: os.path.realpath(path, strict=True)" "except: pass"
|
On Linux, I think it's going to end up being a hard-coded constant equal to 40, which is what glibc returns from its internal #ifndef MIN_ELOOP_THRESHOLD
# define MIN_ELOOP_THRESHOLD 40
#endif
/* Return the maximum number of symlink traversals to permit
before diagnosing ELOOP. */
static inline unsigned int __attribute__ ((const))
__eloop_threshold (void)
{
#ifdef SYMLOOP_MAX
const int symloop_max = SYMLOOP_MAX;
#else
/* The function is marked 'const' even though we use memory and
call a function, because sysconf is required to return the
same value in every call and so it must always be safe to
call __eloop_threshold exactly once and reuse the value. */
static long int sysconf_symloop_max;
if (sysconf_symloop_max == 0)
sysconf_symloop_max = __sysconf (_SC_SYMLOOP_MAX);
const unsigned int symloop_max = (sysconf_symloop_max <= 0
? _POSIX_SYMLOOP_MAX
: sysconf_symloop_max);
#endif
return MAX (symloop_max, MIN_ELOOP_THRESHOLD);
} For the glibc build on Linux, Note that Python hasn't updated its support for |
I also found a setup using 96 symlinks and a directory that >489 levels deep: Show script# limit-posixpath.realpath-symlinks.sh
mkdir -p "tmp/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/\
a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a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/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a\
/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a\
/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a\
/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a\
/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a" tmp/${i};
done
echo 96 symlinks created
main/python.exe -m timeit -s "import os; path = os.getcwd() + '/tmp/' + '/'.join(map(str, range(96)))" "try: os.path.realpath(path, strict=True)" "except: pass"
speedup-symlink-lookup/python.exe -m timeit -s "import os; path = os.getcwd() + '/tmp/' + '/'.join(map(str, range(96)))" "try: os.path.realpath(path, strict=True)" "except: pass"
limit-posixpath.realpath-symlinks/python.exe -m timeit -s "import os; path = os.getcwd() + '/tmp/' + '/'.join(map(str, range(96)))" "try: os.path.realpath(path, strict=True)" "except: pass"
|
Benchmark
Show script
posixpath.realpath(..., strict=True)
doesn't limit symlinks #118441