mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
Merge branch 'rs/ban-mktemp' into jch
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()
This commit is contained in:
1
Makefile
1
Makefile
@@ -1919,7 +1919,6 @@ ifdef NO_SETENV
|
||||
endif
|
||||
ifdef NO_MKDTEMP
|
||||
COMPAT_CFLAGS += -DNO_MKDTEMP
|
||||
COMPAT_OBJS += compat/mkdtemp.o
|
||||
endif
|
||||
ifdef MKDIR_WO_TRAILING_SLASH
|
||||
COMPAT_CFLAGS += -DMKDIR_WO_TRAILING_SLASH
|
||||
|
||||
3
banned.h
3
banned.h
@@ -41,4 +41,7 @@
|
||||
#undef asctime_r
|
||||
#define asctime_r(t, buf) BANNED(asctime_r)
|
||||
|
||||
#undef mktemp
|
||||
#define mktemp(x) BANNED(mktemp)
|
||||
|
||||
#endif /* BANNED_H */
|
||||
|
||||
@@ -241,9 +241,6 @@ int mingw_chdir(const char *dirname);
|
||||
int mingw_chmod(const char *filename, int mode);
|
||||
#define chmod mingw_chmod
|
||||
|
||||
char *mingw_mktemp(char *template);
|
||||
#define mktemp mingw_mktemp
|
||||
|
||||
char *mingw_getcwd(char *pointer, int len);
|
||||
#define getcwd mingw_getcwd
|
||||
|
||||
|
||||
@@ -1164,18 +1164,6 @@ unsigned int sleep (unsigned int seconds)
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *mingw_mktemp(char *template)
|
||||
{
|
||||
wchar_t wtemplate[MAX_PATH];
|
||||
if (xutftowcs_path(wtemplate, template) < 0)
|
||||
return NULL;
|
||||
if (!_wmktemp(wtemplate))
|
||||
return NULL;
|
||||
if (xwcstoutf(template, wtemplate, strlen(template) + 1) < 0)
|
||||
return NULL;
|
||||
return template;
|
||||
}
|
||||
|
||||
int mkstemp(char *template)
|
||||
{
|
||||
return git_mkstemp_mode(template, 0600);
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#include "../git-compat-util.h"
|
||||
|
||||
char *gitmkdtemp(char *template)
|
||||
{
|
||||
if (!*mktemp(template) || mkdir(template, 0700))
|
||||
return NULL;
|
||||
return template;
|
||||
}
|
||||
@@ -329,8 +329,7 @@ int gitsetenv(const char *, const char *, int);
|
||||
#endif
|
||||
|
||||
#ifdef NO_MKDTEMP
|
||||
#define mkdtemp gitmkdtemp
|
||||
char *gitmkdtemp(char *);
|
||||
#define mkdtemp git_mkdtemp
|
||||
#endif
|
||||
|
||||
#ifdef NO_UNSETENV
|
||||
|
||||
@@ -411,10 +411,6 @@ if(NOT HAVE_SETENV)
|
||||
list(APPEND compat_SOURCES compat/setenv.c)
|
||||
endif()
|
||||
|
||||
if(NOT HAVE_MKDTEMP)
|
||||
list(APPEND compat_SOURCES compat/mkdtemp.c)
|
||||
endif()
|
||||
|
||||
if(NOT HAVE_PREAD)
|
||||
list(APPEND compat_SOURCES compat/pread.c)
|
||||
endif()
|
||||
|
||||
@@ -1401,7 +1401,7 @@ checkfuncs = {
|
||||
'strlcpy' : ['strlcpy.c'],
|
||||
'strtoull' : [],
|
||||
'setenv' : ['setenv.c'],
|
||||
'mkdtemp' : ['mkdtemp.c'],
|
||||
'mkdtemp' : [],
|
||||
'initgroups' : [],
|
||||
'strtoumax' : ['strtoumax.c', 'strtoimax.c'],
|
||||
'pread' : ['pread.c'],
|
||||
|
||||
21
wrapper.c
21
wrapper.c
@@ -429,7 +429,11 @@ int xmkstemp(char *filename_template)
|
||||
#undef TMP_MAX
|
||||
#define TMP_MAX 16384
|
||||
|
||||
int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
|
||||
/*
|
||||
* Returns -1 on error, 0 if it created a directory, or an open file
|
||||
* descriptor to the created regular file.
|
||||
*/
|
||||
static int git_mkdstemps_mode(char *pattern, int suffix_len, int mode, bool dir)
|
||||
{
|
||||
static const char letters[] =
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
@@ -471,7 +475,10 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
|
||||
v /= num_letters;
|
||||
}
|
||||
|
||||
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
|
||||
if (dir)
|
||||
fd = mkdir(pattern, mode);
|
||||
else
|
||||
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
|
||||
if (fd >= 0)
|
||||
return fd;
|
||||
/*
|
||||
@@ -486,6 +493,16 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *git_mkdtemp(char *pattern)
|
||||
{
|
||||
return git_mkdstemps_mode(pattern, 0, 0700, true) ? NULL : pattern;
|
||||
}
|
||||
|
||||
int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
|
||||
{
|
||||
return git_mkdstemps_mode(pattern, suffix_len, mode, false);
|
||||
}
|
||||
|
||||
int git_mkstemp_mode(char *pattern, int mode)
|
||||
{
|
||||
/* mkstemp is just mkstemps with no suffix */
|
||||
|
||||
@@ -37,6 +37,8 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...);
|
||||
|
||||
int xgethostname(char *buf, size_t len);
|
||||
|
||||
char *git_mkdtemp(char *pattern);
|
||||
|
||||
/* set default permissions by passing mode arguments to open(2) */
|
||||
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
|
||||
int git_mkstemp_mode(char *pattern, int mode);
|
||||
|
||||
Reference in New Issue
Block a user