From 74fbbb3c789fa170eb25d621acd0f549e9272e94 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 20 Aug 2014 01:13:56 +0200 Subject: [PATCH] Fix mkstemp(3) not rejecting bad templates. Found by musl's libc-test. --- libc/stdlib/mkstemp.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libc/stdlib/mkstemp.cpp b/libc/stdlib/mkstemp.cpp index 9cf0824f..68978825 100644 --- a/libc/stdlib/mkstemp.cpp +++ b/libc/stdlib/mkstemp.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2014. This file is part of the Sortix C Library. @@ -29,13 +29,22 @@ extern "C" int mkstemp(char* templ) { + size_t templ_length = strlen(templ); + for ( size_t i = 0; i < 6; i++ ) + { + if ( templ_length - i == 0 ) + return errno = EINVAL, -1; + if ( templ[templ_length - i - 1] != 'X' ) + return errno = EINVAL, -1; + } + int fd; do { - size_t templ_len = strlen(templ); - for ( size_t i = templ_len - 6UL; i < templ_len; i++ ) + for ( size_t i = templ_length - 6; i < templ_length; i++ ) templ[i] = '0' + rand() % 10; } while ( (fd = open(templ, O_RDWR | O_EXCL | O_CREAT, 0666)) < 0 && (errno == EEXIST) ); + return fd; }