Skip to content

Restrict access to new files created by shutil.copy2() - #61

Draft
zaniebot wants to merge 1 commit into
mainfrom
fix/shutil-listxattr-permissions
Draft

zaniebot wants to merge 1 commit into
mainfrom
fix/shutil-listxattr-permissions

Conversation

@zaniebot

@zaniebot zaniebot commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

On POSIX, shutil.copy2() can leave a copy of a 0700 file readable by other users. With a 0022 umask, the destination typically starts at 0644 and stays that way if os.listxattr() raises before shutil.copystat() applies the source mode. RunSecure encountered this denial when setuptools copied package metadata during pip install . (see AndEnd-Collective/RunSecure#12). Other users can also read the destination while the contents are being copied (see python#59305 (comment)).

Create new destinations with the source’s owner permission bits plus owner write. This gives shutil.copystat() a chance to set extended attributes before it applies a read-only source mode. If listing the source attributes is denied, shutil.copy2() still raises PermissionError (see python#61278 (comment)).

Create new POSIX destinations with owner-only permissions before copying data. A denied source attribute listing still raises, and the new file cannot be left group- or world-readable because of the creation umask.
@zaniebot
zaniebot force-pushed the fix/shutil-listxattr-permissions branch from e211141 to 58b1acb Compare October 24, 2026 01:04
@zaniebot zaniebot changed the title Let shutil.copy2() continue when listing extended attributes is denied Restrict access to new files created by shutil.copy2() Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant