Skip to content
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

os.stat fails when access is denied #72262

Closed
ramson mannequin opened this issue Sep 11, 2016 · 6 comments
Closed

os.stat fails when access is denied #72262

ramson mannequin opened this issue Sep 11, 2016 · 6 comments
Labels
3.7 (EOL) end of life OS-windows type-bug An unexpected behavior, bug, or error

Comments

@ramson
Copy link
Mannequin

ramson mannequin commented Sep 11, 2016

BPO 28075
Nosy @pfmoore, @tjguk, @berkerpeksag, @zware, @eryksun, @zooba
PRs
  • [Do Not Merge] Convert Misc/NEWS so that it is managed by towncrier #552
  • Files
  • issue_28075_01.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2016-09-17.12:51:48.248>
    created_at = <Date 2016-09-11.09:04:40.384>
    labels = ['type-bug', '3.7', 'OS-windows']
    title = 'os.stat fails when access is denied'
    updated_at = <Date 2017-03-31.16:36:34.089>
    user = 'https://bugs.python.org/ramson'

    bugs.python.org fields:

    activity = <Date 2017-03-31.16:36:34.089>
    actor = 'dstufft'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-09-17.12:51:48.248>
    closer = 'berker.peksag'
    components = ['Windows']
    creation = <Date 2016-09-11.09:04:40.384>
    creator = 'ramson'
    dependencies = []
    files = ['44554']
    hgrepos = []
    issue_num = 28075
    keywords = ['patch']
    message_count = 6.0
    messages = ['275750', '275777', '275795', '276791', '276792', '276877']
    nosy_count = 8.0
    nosy_names = ['paul.moore', 'tim.golden', 'ramson', 'python-dev', 'berker.peksag', 'zach.ware', 'eryksun', 'steve.dower']
    pr_nums = ['552']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28075'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    @ramson
    Copy link
    Mannequin Author

    ramson mannequin commented Sep 11, 2016

    doing os.stat on the directory c:\config.msi gives different results on my Windows 10 machine, when done with py27 and py35
    ------------------------------------------------------

    Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.stat('c:\\config.msi')
    nt.stat_result(st_mode=16895, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=2891776L, st_atime=1473584558L, st_mtime=1473584558L, st_ctime=1449228297L)
    
    ------------------------------------------------------
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.stat('c:\\config.msi')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    PermissionError: [WinError 5] Zugriff verweigert: 'c:\\config.msi'

    -> Access denied

    @ramson ramson mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Sep 11, 2016
    @eryksun
    Copy link
    Contributor

    eryksun commented Sep 11, 2016

    Python 3's os.stat tries to open a handle for the file or directory in order to call GetFileInformationByHandle. Opening a file handle via CreateFile requests at least FILE_READ_ATTRIBUTES and SYNCHRONIZE access when it calls NtCreateFile. If access is denied, os.stat is supposed to fall back on the basic WIN32_FIND_DATA information from FindFirstFile. However, it's not working as intended. For example:

        >>> import os
        >>> os.mkdir('test')
        >>> os.system('icacls test /deny Users:(S,RA)')
        processed file: test
        Successfully processed 1 files; Failed processing 0 files
        0
    
        >>> os.stat('test')
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        PermissionError: [WinError 5] Access is denied: 'test'

    The problem is that it's mistakenly checking for ERROR_SHARING_VIOLATION instead of ERROR_ACCESS_DENIED. Technically, getting a sharing violation should be impossible here. That only applies to requesting execute (traverse), read (list), write (add file), append (add subdirectory), or delete access for the contents of the file or directory, not its metadata.

    After modifying the code to instead check for ERROR_ACCESS_DENIED, os.stat correctly falls back on using the file attributes from the WIN32_FIND_DATA:

        >>> os.stat('test')
        os.stat_result(st_mode=16895, st_ino=0, st_dev=0, st_nlink=0, 
        st_uid=0, st_gid=0, st_size=0, st_atime=1473589600, 
        st_mtime=1473589600, st_ctime=1473589600)

    @eryksun eryksun changed the title py35 os.stat behavoir different than python 2.7 os.stat fails when access is denied Sep 11, 2016
    @eryksun
    Copy link
    Contributor

    eryksun commented Sep 11, 2016

    I overlooked attempting to open a paging-file for any access, which is hard coded as a sharing violation. The attached patch checks for both cases.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 17, 2016

    New changeset eabb86463462 by Berker Peksag in branch '3.5':
    Issue bpo-28075: Check for ERROR_ACCESS_DENIED in Windows implementation of os.stat()
    https://hg.python.org/cpython/rev/eabb86463462

    New changeset 4071a7cf6437 by Berker Peksag in branch '3.6':
    Issue bpo-28075: Merge from 3.5
    https://hg.python.org/cpython/rev/4071a7cf6437

    New changeset b04d5864e59a by Berker Peksag in branch 'default':
    Issue bpo-28075: Merge from 3.6
    https://hg.python.org/cpython/rev/b04d5864e59a

    @berkerpeksag
    Copy link
    Member

    Thanks, Eryk.

    @berkerpeksag berkerpeksag added the 3.7 (EOL) end of life label Sep 17, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 18, 2016

    New changeset 20c4ad866620 by Berker Peksag in branch '3.5':
    Issue bpo-28075: Fix test_access_denied in Python 3.5
    https://hg.python.org/cpython/rev/20c4ad866620

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life OS-windows type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants