From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Cooper Subject: [PATCH for-4.5] tools/pygrub: Fix TOCTOU race introduced by c/s 63dcc68 Date: Wed, 29 Oct 2014 14:09:41 +0000 Message-ID: <1414591781-19376-1-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Xen-devel Cc: Olaf Hering , Wei Liu , Ian Campbell , Andrew Cooper , Ian Jackson List-Id: xen-devel@lists.xenproject.org In addition, use os.makedirs() which will also create intermediate directories if they don't exist. Signed-off-by: Andrew Cooper CC: Ian Campbell CC: Ian Jackson CC: Wei Liu CC: Konrad Rzeszutek Wilk CC: Olaf Hering --- tools/pygrub/src/pygrub | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/pygrub/src/pygrub b/tools/pygrub/src/pygrub index 1ae34a2..37dde32 100644 --- a/tools/pygrub/src/pygrub +++ b/tools/pygrub/src/pygrub @@ -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() -- 1.7.10.4