Skip to contentSkip to conversation

tools/pygrub: Fix TOCTOU race introduced by c/s 63dcc68

PATCH In review Andrew Cooper 3 participants 6 messages Oct 29, 2014 – Nov 4, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/pygrub: Fix TOCTOU race introduced by c/s 63dcc68

In addition, use os.makedirs() which will also create intermediate directories if they don't exist.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
CC Konrad Rzeszutek Wilk <[email protected]>
CC Olaf Hering <[email protected]>
---
 tools/pygrub/src/pygrub |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/pygrub/src/pygrub +9 -3
index 1ae34a2..37dde32 100644
@@ -13,7 +13,7 @@
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 #
 
-import os, sys, string, struct, tempfile, re, traceback, stat
+import os, sys, string, struct, tempfile, re, traceback, stat, errno
 import copy
 import logging
 import platform
@@ -846,8 +846,14 @@ if __name__ == "__main__":
     if debug:
 	logging.basicConfig(level=logging.DEBUG)
 
-    if not os.path.isdir(output_directory):
-        os.mkdir(output_directory, 0700)
+
+    try:
+        os.makedirs(output_directory, 0700)
+    except OSError,e:
+        if (e.errno == errno.EEXIST) and os.path.isdir(output_directory):
+            pass
+        else:
+            raise
 
     if output is None or output == "-":
         fd = sys.stdout.fileno()
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 2 lines · Andrew Cooper

In addition, use os.makedirs() which will also create intermediate directories if they don't exist.

Can this happen in practice, given that /var/run/xen is created by the runlevel scripts already? The mkdir is now really just @XEN_RUN_DIR@/pygrub.

Olaf

#2 lore raw
Quoted 8 lines · Olaf Hering
Quoted 2 lines · Andrew Cooper

In addition, use os.makedirs() which will also create intermediate directories if they don't exist.

Can this happen in practice, given that /var/run/xen is created by the runlevel scripts already? The mkdir is now really just @XEN_RUN_DIR@/pygrub.

Olaf

Yes, although I presume not given the runlevel scripts.

XenServer, and Xapi in particular, is very different from an upstream xl based system. It has traditionally had its own initscripts. We are in the process of disentangling these, but it is not a trivial amount of work.

The result is that I have positively proved that XenServer hit this race during automatic testing. I agree that it shouldn't happen in reality for a system configured as expected, but that fact alone doesn't invalidate the fix itself.

~Andrew

#3 lore raw
Quoted 4 lines · Andrew Cooper

The result is that I have positively proved that XenServer hit this race during automatic testing. I agree that it shouldn't happen in reality for a system configured as expected, but that fact alone doesn't invalidate the fix itself.

I think it can happen even with upstream if two guests start at the same time and both execute the code at the same time. Both will find the dir does not exist yet and both will attempt to create the dir. I think one of both will get an exception.

Olaf

#4 lore raw
In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

In addition, use os.makedirs() which will also create intermediate directories if they don't exist.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
CC Konrad Rzeszutek Wilk <[email protected]>
CC Olaf Hering <[email protected]>

Acked and applied as a bug fix.

In the future when you include a "for-4.5" tag on a patch please *always* state your reasoning (usually after the "---"). Don't make the maintainer / release manager / committer guess why you think it should be in 4.5. If you consider it "obviously a bug fix" then say so and then say something about the impact of the bug and the risk associated with the fix.

I'm a bit grumpy about the number of patches which don't do this, such patches are mostly going to the back of my queue, alongside the 4.6 patches, since without any rationale that is effectively what I consider them to be despite the use of the for-4.5 tag.

Ian.

#5 lore raw

Please *justify* your use of the for-4.5 tag when posting patches

On Tue, 2014-11-04 at 10:48 +0000, Ian Campbell wrote:

Reposting with a more eyecatching subject, since this was by far not the only patch with this issue.

Quoted 11 lines

In the future when you include a "for-4.5" tag on a patch please *always* state your reasoning (usually after the "---"). Don't make the maintainer / release manager / committer guess why you think it should be in 4.5. If you consider it "obviously a bug fix" then say so and then say something about the impact of the bug and the risk associated with the fix.

I'm a bit grumpy about the number of patches which don't do this, such patches are mostly going to the back of my queue, alongside the 4.6 patches, since without any rationale that is effectively what I consider them to be despite the use of the for-4.5 tag.