[Python-checkins] cpython: #4489: Use dir_fd in rmdir in _rmtree_safe_fd()

hynek.schlawack python-checkins at python.org
Thu Jun 28 15:32:19 CEST 2012


http://hg.python.org/cpython/rev/9134bb4d0578
changeset:   77835:9134bb4d0578
user:        Hynek Schlawack <hs at ox.cx>
date:        Thu Jun 28 15:30:47 2012 +0200
summary:
  #4489: Use dir_fd in rmdir in _rmtree_safe_fd()

Now that rmdir supports dir_fd, we also use it. Attackers can't even delete
empty directories anymore.

files:
  Lib/shutil.py |  12 ++++++++----
  1 files changed, 8 insertions(+), 4 deletions(-)


diff --git a/Lib/shutil.py b/Lib/shutil.py
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -393,6 +393,10 @@
                 try:
                     if os.path.samestat(orig_st, os.fstat(dirfd)):
                         _rmtree_safe_fd(dirfd, fullname, onerror)
+                        try:
+                            os.rmdir(name, dir_fd=topfd)
+                        except os.error:
+                            onerror(os.rmdir, fullname, sys.exc_info())
                 finally:
                     os.close(dirfd)
         else:
@@ -400,10 +404,6 @@
                 os.unlink(name, dir_fd=topfd)
             except os.error:
                 onerror(os.unlink, fullname, sys.exc_info())
-    try:
-        os.rmdir(path)
-    except os.error:
-        onerror(os.rmdir, path, sys.exc_info())
 
 _use_fd_functions = (os.unlink in os.supports_dir_fd and
                      os.open in os.supports_dir_fd)
@@ -445,6 +445,10 @@
             if (stat.S_ISDIR(orig_st.st_mode) and
                 os.path.samestat(orig_st, os.fstat(fd))):
                 _rmtree_safe_fd(fd, path, onerror)
+                try:
+                    os.rmdir(path)
+                except os.error:
+                    onerror(os.rmdir, path, sys.exc_info())
             else:
                 raise NotADirectoryError(20,
                                          "Not a directory: '{}'".format(path))

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list