From 162b394726bbf3198034aa9aa5361f1256641e3e Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 21 Jun 2015 21:54:24 +0200 Subject: [PATCH] Replace insecure tmpfile() with mkstemp() based local tempfile() Signed-off-by: Joachim Nilsson --- exec.c | 2 +- helpers.c | 21 +++++++++++++++++++++ helpers.h | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/exec.c b/exec.c index 6bfb8138..d6d74a1e 100644 --- a/exec.c +++ b/exec.c @@ -179,7 +179,7 @@ int run_interactive(char *cmd, char *fmt, ...) } /* Redirect output from cmd to a tempfile */ - fp = tmpfile(); + fp = tempfile(); if (fp && !debug) { oldout = dup(STDOUT_FILENO); olderr = dup(STDERR_FILENO); diff --git a/helpers.c b/helpers.c index 7757eab8..16525f63 100644 --- a/helpers.c +++ b/helpers.c @@ -126,6 +126,27 @@ int atonum(char *str) 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) { if (!lvl) return 0; diff --git a/helpers.h b/helpers.h index 122b9c92..9d374d90 100644 --- a/helpers.h +++ b/helpers.h @@ -69,6 +69,8 @@ int makepath (char *path); void ifconfig (char *ifname, char *addr, char *mask, int up); int atonum (char *str); +FILE *tempfile (void); + void runlevel_set (int pre, int now); int runlevel_get (void); char *runlevel_string (int levels);