[New-bugs-announce] [issue28353] os.fwalk() unhandled exception when error occurs accessing symbolic link target

Samson Lee report at bugs.python.org
Tue Oct 4 03:06:56 EDT 2016


New submission from Samson Lee:

The bug is os.fwalk() crashes with unhandled exception when there is an error
accessing symbolic link targets.

To reproduce the bug, create a symbolic link that targets a file that you do not
have permission to access:

    $ touch handsoff
    $ sudo chown root:root handsoff
    $ sudo chmod 700 handsoff
    $ ln -s handsoff blah

Now, os.fwalk() fails:

    >>> for root, dirs, files, fd in os.fwalk('.'):
    ...     print(root, dirs, files)
    ...
    Traceback (most recent call last):
      File "test_fwalk_permission_error.py", line 3, in <module>
        for root, dirs, files, fd in os.fwalk('.'):
      File "/usr/lib64/python3.5/os.py", line 520, in fwalk
        yield from _fwalk(topfd, top, topdown, onerror, follow_symlinks)
      File "/usr/lib64/python3.5/os.py", line 537, in _fwalk
        if st.S_ISDIR(stat(name, dir_fd=topfd).st_mode):
    PermissionError: [Errno 13] Permission denied: 'blah'


The cause of the problem is in this part of os.py:

for name in names:
    try:
        # Here, we don't use AT_SYMLINK_NOFOLLOW to be consistent with
        # walk() which reports symlinks to directories as directories.
        # We do however check for symlinks before recursing into
        # a subdirectory.
        if st.S_ISDIR(stat(name, dir_fd=topfd).st_mode):
            dirs.append(name)
        else:
            nondirs.append(name)
    except FileNotFoundError:
        try:
            # Add dangling symlinks, ignore disappeared files
            if st.S_ISLNK(stat(name, dir_fd=topfd, follow_symlinks=False)
                        .st_mode):
                nondirs.append(name)
        except FileNotFoundError:
            continue

To fix it, simply replace FileNotFoundError with more general OSError.

Cheers

----------
components: Library (Lib)
messages: 278016
nosy: Samson Lee
priority: normal
severity: normal
status: open
title: os.fwalk() unhandled exception when error occurs accessing symbolic link target
versions: Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28353>
_______________________________________


More information about the New-bugs-announce mailing list