Skip to content

Commit 91bfbf4

Browse files
committed
Merge branch 'rs/ban-mktemp'
Rewrite the only use of "mktemp()" that is subject to TOCTOU race and Stop using the insecure "mktemp()" function. * rs/ban-mktemp: compat: remove gitmkdtemp() banned.h: ban mktemp(3) compat: remove mingw_mktemp() compat: use git_mkdtemp() wrapper: add git_mkdtemp()
2 parents 72154ce + 10bba53 commit 91bfbf4

File tree

10 files changed

+26
-33
lines changed

10 files changed

+26
-33
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1919,7 +1919,6 @@ ifdef NO_SETENV
19191919
endif
19201920
ifdef NO_MKDTEMP
19211921
COMPAT_CFLAGS += -DNO_MKDTEMP
1922-
COMPAT_OBJS += compat/mkdtemp.o
19231922
endif
19241923
ifdef MKDIR_WO_TRAILING_SLASH
19251924
COMPAT_CFLAGS += -DMKDIR_WO_TRAILING_SLASH

banned.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@
4141
#undef asctime_r
4242
#define asctime_r(t, buf) BANNED(asctime_r)
4343

44+
#undef mktemp
45+
#define mktemp(x) BANNED(mktemp)
46+
4447
#endif /* BANNED_H */

compat/mingw-posix.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,6 @@ int mingw_chdir(const char *dirname);
241241
int mingw_chmod(const char *filename, int mode);
242242
#define chmod mingw_chmod
243243

244-
char *mingw_mktemp(char *template);
245-
#define mktemp mingw_mktemp
246-
247244
char *mingw_getcwd(char *pointer, int len);
248245
#define getcwd mingw_getcwd
249246

compat/mingw.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,18 +1164,6 @@ unsigned int sleep (unsigned int seconds)
11641164
return 0;
11651165
}
11661166

1167-
char *mingw_mktemp(char *template)
1168-
{
1169-
wchar_t wtemplate[MAX_PATH];
1170-
if (xutftowcs_path(wtemplate, template) < 0)
1171-
return NULL;
1172-
if (!_wmktemp(wtemplate))
1173-
return NULL;
1174-
if (xwcstoutf(template, wtemplate, strlen(template) + 1) < 0)
1175-
return NULL;
1176-
return template;
1177-
}
1178-
11791167
int mkstemp(char *template)
11801168
{
11811169
return git_mkstemp_mode(template, 0600);

compat/mkdtemp.c

Lines changed: 0 additions & 8 deletions
This file was deleted.

compat/posix.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,7 @@ int gitsetenv(const char *, const char *, int);
329329
#endif
330330

331331
#ifdef NO_MKDTEMP
332-
#define mkdtemp gitmkdtemp
333-
char *gitmkdtemp(char *);
332+
#define mkdtemp git_mkdtemp
334333
#endif
335334

336335
#ifdef NO_UNSETENV

contrib/buildsystems/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,6 @@ if(NOT HAVE_SETENV)
411411
list(APPEND compat_SOURCES compat/setenv.c)
412412
endif()
413413

414-
if(NOT HAVE_MKDTEMP)
415-
list(APPEND compat_SOURCES compat/mkdtemp.c)
416-
endif()
417-
418414
if(NOT HAVE_PREAD)
419415
list(APPEND compat_SOURCES compat/pread.c)
420416
endif()

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ checkfuncs = {
14011401
'strlcpy' : ['strlcpy.c'],
14021402
'strtoull' : [],
14031403
'setenv' : ['setenv.c'],
1404-
'mkdtemp' : ['mkdtemp.c'],
1404+
'mkdtemp' : [],
14051405
'initgroups' : [],
14061406
'strtoumax' : ['strtoumax.c', 'strtoimax.c'],
14071407
'pread' : ['pread.c'],

wrapper.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,11 @@ int xmkstemp(char *filename_template)
429429
#undef TMP_MAX
430430
#define TMP_MAX 16384
431431

432-
int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
432+
/*
433+
* Returns -1 on error, 0 if it created a directory, or an open file
434+
* descriptor to the created regular file.
435+
*/
436+
static int git_mkdstemps_mode(char *pattern, int suffix_len, int mode, bool dir)
433437
{
434438
static const char letters[] =
435439
"abcdefghijklmnopqrstuvwxyz"
@@ -471,7 +475,10 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
471475
v /= num_letters;
472476
}
473477

474-
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
478+
if (dir)
479+
fd = mkdir(pattern, mode);
480+
else
481+
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
475482
if (fd >= 0)
476483
return fd;
477484
/*
@@ -486,6 +493,16 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
486493
return -1;
487494
}
488495

496+
char *git_mkdtemp(char *pattern)
497+
{
498+
return git_mkdstemps_mode(pattern, 0, 0700, true) ? NULL : pattern;
499+
}
500+
501+
int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
502+
{
503+
return git_mkdstemps_mode(pattern, suffix_len, mode, false);
504+
}
505+
489506
int git_mkstemp_mode(char *pattern, int mode)
490507
{
491508
/* mkstemp is just mkstemps with no suffix */

wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...);
3737

3838
int xgethostname(char *buf, size_t len);
3939

40+
char *git_mkdtemp(char *pattern);
41+
4042
/* set default permissions by passing mode arguments to open(2) */
4143
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
4244
int git_mkstemp_mode(char *pattern, int mode);

0 commit comments

Comments
 (0)