Author Samson Lee
Recipients Samson Lee
Date 2016-10-04.07:06:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1475564816.03.0.238756399525.issue28353@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2016-10-04 07:06:56Samson Leesetrecipients: + Samson Lee
2016-10-04 07:06:56Samson Leesetmessageid: <1475564816.03.0.238756399525.issue28353@psf.upfronthosting.co.za>
2016-10-04 07:06:55Samson Leelinkissue28353 messages
2016-10-04 07:06:55Samson Leecreate