You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
os.path.realpath(symlink to DOS devices path that starts with '\\?\Some without ":"\') returns without prefix. #102440
os.path.realpath(symblic link to DOS Device Paths) returns a path without \\?\ prefix even if patch that fix.
For example, there is below symlinks on C:\test.
C:\test>dir
03/05/2023 06:16 AM <SYMLINKD> media [\\?\ContainerMappedDirectories\CE94A662-0837-4F45-B403-55B3E57CE848]
03/05/2023 06:19 AM <SYMLINKD> to_c [\\?\C:\]
Then exexute python and call os.path.realpath and call some functions to inspect.
As the log indicates, os.path.realpath returns without a prefix.
I think it is wrong that _getfinalpathname_nonstrict removes DOS devices paths prefix.
So to fix the problem, I think we need to fix _getfinalpathname function on "Modules/posixmodule.c".
changed the title [-]os.path.realpath(symlink to DOS devices path that starts with '\\?\') returns without prefix.[/-][+]os.path.realpath(symlink to DOS devices path that starts with '\\?\Some without ":"\') returns without prefix.[/+]on Mar 5, 2023
I don't see how ntpath._getfinalpathname_nonstrict() is responsible for removing the "\\?\" prefix, unless maybe there's something wrong with the target path of the "media" symlink.
A symbolic link reparse point contains a substitute path that the system uses, plus an optional print path for display1. What you see in the shell is the print path, but os.readlink() returns the substitute path.
What is the result of os.readlink(media_fn)?
As the log indicates, os.path.realpath returns without a prefix.
It's a known issue (#89760) that ntpath.realpath() mistakenly strips the prefix from some device paths for which the prefix must be retained. But that's unrelated to the given example in which the prefix gets stripped by ntpath._getfinalpathname_nonstrict().
Footnotes
The protocol specifies what the print path "SHOULD" be. Per RFC 2119, "SHOULD" means that an item is strongly recommended but optional. ↩
I read a code of ntpath._getfinalpathname_nonstrict(), perhaps the bug is caused by os.readlink behaviors.
The code of ntpath.realpath() processes L702-L706(Python3.11.0 amd64) because _getfinalpathname() causes FileNotFoundError (The specified path is symlink to DOS devices path that starts with '\?\Some without ":"').
That calls _getfinalpathname_nonstrict() and _getfinalpathname_nonstrict() calls os.readlink().
It's a known issue (#89760) that ntpath.realpath() mistakenly strips the prefix from some device paths for which the prefix must be retained. But that's unrelated to the given example in which the prefix gets stripped by ntpath._getfinalpathname_nonstrict().
This appears to be a shortcoming in os.readlink(), but a problem that's uncommon in practice. Normal symlinks created by WinAPI CreateSymbolicLinkW() are restricted to target a volume device name in NT's "\??\" directory, which contains local and global aliases for native NT device names, such as "\??\C:" -> "\Device\HarddiskVolume2". However, when manually creating a symlink via FSCTL_SET_REPARSE_POINT, it's possible to target an arbitrary NT path instead of a "\??\" prefixed path.
If the reparse point is a symlink with SYMLINK_FLAG_RELATIVE set in Flags, and the substitute path is a rooted path such as "\spam", then readlink() has to return the rooted path "\spam". This path is relative to the volume device of the opened path of the symlink. We currently implement this, except we don't check for SYMLINK_FLAG_RELATIVE.
If the reparse point is a mountpoint or if it's a symlink withoutSYMLINK_FLAG_RELATIVE set in Flags, and the substitute path is a rooted path such as "\spam", then readlink() has to prepend the prefix "\\?\GLOBALROOT" to the substitute path. Currently this is not implemented.
Assuming the target of "media" really exists, then the following stat() call should work:
I'm trying to write a fix code to implement prepending "\?GLOBALROOT" to the result of os.readlink().
Modules/posixmodule.c -> os_readlink_impl
wchar_t *name = NULL;
Py_ssize_t nameLen = 0;
if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
{
name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
}
elseif (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
{
name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
rdb->MountPointReparseBuffer.SubstituteNameOffset);
nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
}
else
{
PyErr_SetString(PyExc_ValueError, "not a symbolic link");
}
if (name == NULL) {
returnNULL;
}
if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
/* Our buffer is mutable, so this is okay */
name[1] = L'\\';
}
/* If the substitute name is a rooted path then *//* the reparse point is MountPoint || ( Symlink && Flags == 0 ) then *//* should prepend \\?\GLOBALROOT prefix. */wchar_t prefix[] = L"\\\\?\\GLOBALROOT";
if (!(nameLen > 4 && wcsncmp(name, L"\\\\?\\", 4) == 0) && (nameLen > 1 && wcsncmp(name, L"\\", 1) == 0)) {
bool shouldPrependPrefix =
(rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) ||
(rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK && rdb->SymbolicLinkReparseBuffer.Flags == 0);
if (shouldPrependPrefix) {
wchar_t *oldName = name;
nameLen = wcslen(prefix) + wcslen(oldName);
name = (wchar_t *) PyMem_Malloc(sizeof(wchar_t) * (nameLen + 1));
wcscpy_s(name, nameLen + 1, prefix);
wcscat_s(name, nameLen + 1, oldName);
PyMem_Free(oldName);
}
}
result = PyUnicode_FromWideChar(name, nameLen);
if (result && path->narrow) {
Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
}
return result;
If without PyMem_Free(oldName), the compiled python runs expectedly(but memory leaking).
If with PyMem_Free(oldName), the compiled python crashes when os.readlink('media').
I have no idea why this happens.
This problem happens on volume mount of windows container(docker), so that is not uncommon in practice...
Using containers on Windows is still a niche usage that's relatively uncommon compared to the thousands of Python installations on Windows that will never see such a symlink that cannot be created by the normal CreateSymbolicLinkW() API function. That doesn't mean that it's not worth enhancing os.readlink() to support symlinks and mountpoints that target native paths other than "\??\" device paths.
Note that "\\?\ContainerMappedDirectories\3284C586-8F0D-4325-99EB-76DFF9D5D419" is the "print path" in the reparse point data buffer. We can't rely on the print path because the specification only says what it "SHOULD" be.
If without PyMem_Free(oldName)
You can't call PyMem_Free() on a stack allocated buffer. Anyway, oldName isn't even the base address. After creating the str result, however, you do have to free the newly allocated buffer. Also, you can't call wcslen(oldName) because it isn't a null-terminated string; it's a counted string.
Here's what I wrote to address this problem before I saw your code. The SYMLINK_FLAG_RELATIVE macro should be defined in "Modules/winreparse.h" instead of here, but this was just for testing.
Thankyou your advice!
I'll fix mycode based on your code and advice.
We can't rely on the print path because the specification only says what it "SHOULD" be.
I agree your notion. We must use substitute name. But if os.readlink use substitute name without prepending suffix, we cannot resolve these symbolic paths on Python(except read and analyze reparse point on Python directly but it may be bad).
I think we should enhance os.readlink or make new function.
Enhance os.readlink without optional args to prepend suffix if absolute
Enhance os.readlink with optional args to prepend suffix if absolute
Make new function to read SYMLINK_FLAG_RELATIVE Flag is true.
Activity
[-]os.path.realpath(symlink to DOS devices path that starts with '\\?\') returns without prefix.[/-][+]os.path.realpath(symlink to DOS devices path that starts with '\\?\Some without ":"\') returns without prefix.[/+]eryksun commentedon Mar 5, 2023
I don't see how
ntpath._getfinalpathname_nonstrict()is responsible for removing the "\\?\" prefix, unless maybe there's something wrong with the target path of the "media" symlink.A symbolic link reparse point contains a substitute path that the system uses, plus an optional print path for display1. What you see in the shell is the print path, but
os.readlink()returns the substitute path.What is the result of
os.readlink(media_fn)?It's a known issue (#89760) that
ntpath.realpath()mistakenly strips the prefix from some device paths for which the prefix must be retained. But that's unrelated to the given example in which the prefix gets stripped byntpath._getfinalpathname_nonstrict().Footnotes
The protocol specifies what the print path "SHOULD" be. Per RFC 2119, "SHOULD" means that an item is strongly recommended but optional. ↩
wintermaples commentedon Mar 6, 2023
I read a code of ntpath._getfinalpathname_nonstrict(), perhaps the bug is caused by os.readlink behaviors.
The code of ntpath.realpath() processes L702-L706(Python3.11.0 amd64) because _getfinalpathname() causes FileNotFoundError (The specified path is symlink to DOS devices path that starts with '\?\Some without ":"').
That calls _getfinalpathname_nonstrict() and _getfinalpathname_nonstrict() calls os.readlink().
I know and read #89760 but the bug is remained even if patch #89760 (comment).
eryksun commentedon Mar 6, 2023
This appears to be a shortcoming in
os.readlink(), but a problem that's uncommon in practice. Normal symlinks created by WinAPICreateSymbolicLinkW()are restricted to target a volume device name in NT's "\??\" directory, which contains local and global aliases for native NT device names, such as "\??\C:" -> "\Device\HarddiskVolume2". However, when manually creating a symlink viaFSCTL_SET_REPARSE_POINT, it's possible to target an arbitrary NT path instead of a "\??\" prefixed path.If the reparse point is a symlink with
SYMLINK_FLAG_RELATIVEset inFlags, and the substitute path is a rooted path such as "\spam", thenreadlink()has to return the rooted path "\spam". This path is relative to the volume device of the opened path of the symlink. We currently implement this, except we don't check forSYMLINK_FLAG_RELATIVE.If the reparse point is a mountpoint or if it's a symlink without
SYMLINK_FLAG_RELATIVEset inFlags, and the substitute path is a rooted path such as "\spam", thenreadlink()has to prepend the prefix "\\?\GLOBALROOT" to the substitute path. Currently this is not implemented.Assuming the target of "media" really exists, then the following
stat()call should work:wintermaples commentedon Mar 6, 2023
This problem happens on volume mount of windows container(docker), so that is not uncommon in practice...
I show reparsepoint of media symbolic link below.
(Flags is 0x00000000)
wintermaples commentedon Mar 6, 2023
I'm trying to write a fix code to implement prepending "\?GLOBALROOT" to the result of os.readlink().
Modules/posixmodule.c -> os_readlink_impl
If without PyMem_Free(oldName), the compiled python runs expectedly(but memory leaking).
If with PyMem_Free(oldName), the compiled python crashes when os.readlink('media').
I have no idea why this happens.
Could you give me some advice and review it?
eryksun commentedon Mar 6, 2023
Using containers on Windows is still a niche usage that's relatively uncommon compared to the thousands of Python installations on Windows that will never see such a symlink that cannot be created by the normal
CreateSymbolicLinkW()API function. That doesn't mean that it's not worth enhancingos.readlink()to support symlinks and mountpoints that target native paths other than "\??\" device paths.Note that "\\?\ContainerMappedDirectories\3284C586-8F0D-4325-99EB-76DFF9D5D419" is the "print path" in the reparse point data buffer. We can't rely on the print path because the specification only says what it "SHOULD" be.
You can't call
PyMem_Free()on a stack allocated buffer. Anyway,oldNameisn't even the base address. After creating thestrresult, however, you do have to free the newly allocated buffer. Also, you can't callwcslen(oldName)because it isn't a null-terminated string; it's a counted string.Here's what I wrote to address this problem before I saw your code. The
SYMLINK_FLAG_RELATIVEmacro should be defined in "Modules/winreparse.h" instead of here, but this was just for testing.wintermaples commentedon Mar 8, 2023
Thankyou your advice!
I'll fix mycode based on your code and advice.
I agree your notion. We must use substitute name. But if os.readlink use substitute name without prepending suffix, we cannot resolve these symbolic paths on Python(except read and analyze reparse point on Python directly but it may be bad).
I think we should enhance os.readlink or make new function.