Lasse Kantola <lasse.kant...@iki.fi> added the comment:

I have encountered the same problem with vboxsf mounted folders when using 
Debian guest on VirtualBox 6.1 hosted by Windows 10.

The general VirtualBox issue of vboxsf not supporting rmdir when there are open 
file descriptors is difficult to solve, and out of scope for Python.

However, it looks like the shutil.rmtree implementation can be easily modified 
to change the order of operations so that the rmdir is performed after the 
directory file descriptor is closed, which makes shutil.rmtree work under 
vboxsf shared folders.

Example of the change against Python 3.7 is inlined below. The changed order 
matches the behavior of "rm -r" and to my understanding should not change the 
security implications of the code, but as the code is related to the prevention 
of a symlink attack https://bugs.python.org/issue4489 careful review would be 
needed.


--- /usr/lib/python3.7/shutil.py--orig  2021-01-23 05:04:44.000000000 +0900
+++ /usr/lib/python3.7/shutil.py        2021-12-09 20:42:36.795338346 +0900
@@ -427,10 +427,6 @@
                 try:
                     if os.path.samestat(orig_st, os.fstat(dirfd)):
                         _rmtree_safe_fd(dirfd, fullname, onerror)
-                        try:
-                            os.rmdir(entry.name, dir_fd=topfd)
-                        except OSError:
-                            onerror(os.rmdir, fullname, sys.exc_info())
                     else:
                         try:
                             # This can only happen if someone replaces
@@ -442,6 +438,10 @@
                             onerror(os.path.islink, fullname, sys.exc_info())
                 finally:
                     os.close(dirfd)
+                try:
+                    os.rmdir(entry.name, dir_fd=topfd)
+                except OSError:
+                    onerror(os.rmdir, fullname, sys.exc_info())
         else:
             try:
                 os.unlink(entry.name, dir_fd=topfd)
@@ -489,10 +489,6 @@
         try:
             if os.path.samestat(orig_st, os.fstat(fd)):
                 _rmtree_safe_fd(fd, path, onerror)
-                try:
-                    os.rmdir(path)
-                except OSError:
-                    onerror(os.rmdir, path, sys.exc_info())
             else:
                 try:
                     # symlinks to directories are forbidden, see bug #1669
@@ -501,6 +497,10 @@
                     onerror(os.path.islink, path, sys.exc_info())
         finally:
             os.close(fd)
+        try:
+            os.rmdir(path)
+        except OSError:
+            onerror(os.rmdir, path, sys.exc_info())
     else:
         try:
             if os.path.islink(path):

----------
nosy: +lkantola

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39327>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to