[New-bugs-announce] [issue30400] Race condition in shutil.copyfile()

Preston Moore report at bugs.python.org
Thu May 18 16:22:00 EDT 2017


New submission from Preston Moore:

A race condition exists in shutil.copyfile() that allows the file being copied to be replaced between the time it was initially checked with stat() in this function and when it is actually open()'d and copied.  This issue can be triggered from shutil.move() (and likely other places) when attempting to move a file from one location to another where the source and destination are on different devices.  This behavior can be replicated by setting a catchpoint in gdb on calls to stat() and, after the initial call to stat in shutil.copyfile(), replacing the source file.

The attached pull request addresses this issue by storing the inode number of the source file when it is initially stat()'d and comparing this value to an inode value taken from a call to fstat() after the file is open. If these two values differ, the file has been replaced.  This is the pattern employed by coreutil's mv utility in an effort to address this issue.

This bug was found as part of an ongoing research effort into detecting and addressing bugs caused by differences in the environments in which an application may be run (the environmental issue in this place being the difficulties around correctly copying a file from one disk to another).

----------
components: Library (Lib)
messages: 293938
nosy: Preston Moore
priority: normal
severity: normal
status: open
title: Race condition in shutil.copyfile()
type: security
versions: Python 2.7, Python 3.7

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


More information about the New-bugs-announce mailing list