Replace insecure tmpfile() with mkstemp() based local tempfile()

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2015-06-21 21:54:24 +02:00
1 parent c6a9fa3b98
commit 162b394726
3 files changed
+24 -1

No files matched your search

+1 -1
View File
@@ -179,7 +179,7 @@ int run_interactive(char *cmd, char *fmt, ...)
} }
/* Redirect output from cmd to a tempfile */ /* Redirect output from cmd to a tempfile */
fp = tmpfile(); fp = tempfile();
if (fp && !debug) { if (fp && !debug) {
oldout = dup(STDOUT_FILENO); oldout = dup(STDOUT_FILENO);
olderr = dup(STDERR_FILENO); olderr = dup(STDERR_FILENO);
+21
View File
@@ -126,6 +126,27 @@ int atonum(char *str)
return val; return val;
} }
/**
* tempfile - A secure tmpfile() replacement
*
* This is the secure replacement for tmpfile() that does not exist in GLIBC.
* The function uses mkstemp() for security.
*
* Returns:
* An open %FILE pointer, or %NULL on error.
*/
FILE *tempfile(void)
{
int fd;
char *template = "/tmp/finit.XXXXXX";
fd = mkstemp(template);
if (-1 == fd)
return NULL;
return fdopen(fd, "rw");
}
static int encode(int lvl) static int encode(int lvl)
{ {
if (!lvl) return 0; if (!lvl) return 0;
+2
View File
@@ -69,6 +69,8 @@ int makepath (char *path);
void ifconfig (char *ifname, char *addr, char *mask, int up); void ifconfig (char *ifname, char *addr, char *mask, int up);
int atonum (char *str); int atonum (char *str);
FILE *tempfile (void);
void runlevel_set (int pre, int now); void runlevel_set (int pre, int now);
int runlevel_get (void); int runlevel_get (void);
char *runlevel_string (int levels); char *runlevel_string (int levels);