CVE-2018-19045: Exposure of Sensitive Information to an Unauthorized Actor (CWE-200)
value: keepalived 2.0.8 used mode 0666 when creating new temporary files upon a call to PrintData or PrintStats, potentially leaking sensitive information.
CWE description: The product exposes sensitive information to an actor that is not explicitly authorized to have access to that information.
CVSS v3 base score: 7.5
Published: 2018-11-08T20:29Z
Repository: https://github.com/acassen/keepalived
Language: C
Files changed: doc/keepalived.conf.SYNOPSIS, doc/man/man5/keepalived.conf.5, doc/man/man8/keepalived.8, keepalived/core/global_data.c, keepalived/core/global_parser.c, keepalived/core/main.c, keepalived/include/global_data.h, keepalived/include/main.h
Commit date: 2018-10-31 17:08:51 +0000
Commit message: Add command line and configuration option to set umask

Issue #1048 identified that files created by keepalived are created
with mode 0666. This commit changes the default to 0644, and also
allows the umask to be specified in the configuration or as a command
line option.

Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>

Diff: diff --git a/doc/keepalived.conf.SYNOPSIS b/doc/keepalived.conf.SYNOPSIS
index a33ed4ec..807adf0b 100644
--- a/doc/keepalived.conf.SYNOPSIS
+++ b/doc/keepalived.conf.SYNOPSIS
@@ -527,10 +527,14 @@ global_defs {                                 # Block identification
     vrrp_rx_bufs_multiplier NUMBER            # default 3
 
     rs_init_notifies			      # Send notifies at startup for real servers that are starting up
     no_checker_emails                         # Don't send an email every time a real server checker changes state;
                                               #   only send email when a real server is added or removed
+    umask [NUMBER|BITS]                       # The umask to use for creating files. The number can be specified in hex, octal
+                                              #   or decimal. BITS are I{R|W|X}{USR|GRP|OTH}, e.g. IRGRP, separated by '|'s.
+                                              #   The default umask is IWGRP | IWOTH. This option cannot override the
+                                              #   command-line option.
 }
 
 net_namespace NAME                            # Set the network namespace to run in
                                               # The directory /var/run/keepalived will be created as an unshared mount point,
                                               #   for example for pid files.
diff --git a/doc/man/man5/keepalived.conf.5 b/doc/man/man5/keepalived.conf.5
index 6ed6c396..412a23f3 100644
--- a/doc/man/man5/keepalived.conf.5
+++ b/doc/man/man5/keepalived.conf.5
@@ -497,10 +497,16 @@ and
     \fBrs_init_notifies\fR
 
     # Don't send an email every time a real server checker changes state;
     # only send email when a real server is added or removed
     \fBno_checker_emails\fR
+
+    # The umask to use for creating files. The number can be specified in hex, octal
+    #   or decimal. BITS are I{R|W|X}{USR|GRP|OTH}, e.g. IRGRP, separated by '|'s.
+    #   The default umask is IWGRP | IWOTH. This option cannot override the
+    #   command-line option.
+    \fBumask \fR[NUMBER|BITS]
 }
 .fi
 .SH Static track groups
 .PP
 Static track groups are used to allow vrrp instances to track static addresses,
diff --git a/doc/man/man8/keepalived.8 b/doc/man/man8/keepalived.8
index e4941f90..12cd1325 100644
--- a/doc/man/man8/keepalived.8
+++ b/doc/man/man8/keepalived.8
@@ -32,10 +32,11 @@ keepalived \- load\-balancing and high\-availability service
 [\fB\-b\fP|\fB\-\-bfd_pid\fP=FILE]
 [\fB\-s\fP|\fB\-\-namespace\fP=NAME]
 [\fB\-i\fP|\fB\-\-config-id\fP id]
 [\fB\-x\fP|\fB\-\-snmp\fP]
 [\fB\-A\fP|\fB\-\-snmp-agent-socket\fP=FILE]
+[\fB\-u\fP|\fB\-\-umask\fP=NUMBER]
 [\fB\-m\fP|\fB\-\-core\-dump\fP]
 [\fB\-M\fP|\fB\-\-core\-dump\-pattern\fP[=PATTERN]]
 [\fB\-\-signum\fP=SIGFUNC]
 [\fB\-t\fP|\fB\-\-config\-test\fP[=FILE]]
 [\fB\-\-perf\fP[={all|run|end}]]
@@ -156,10 +157,13 @@ hostname without the domain name).
 Enable the SNMP subsystem.
 .TP
 \fB -A, --snmp-agent-socket\fP=FILE
 Use the specified socket for connection to SNMP master agent.
 .TP
+\fB -u, --umask\fP=NUMBER
+The umask specified in the usual numeric way - see man umask(2)
+.TP
 \fB -m, --core-dump\fP
 Override the RLIMIT_CORE hard and soft limits to enable keepalived to
 produce a coredump in the event of a segfault or other failure.
 This is most useful if keepalived has been built with 'make debug'.
 Core dumps will be created in /, unless keepalived is run with the
diff --git a/keepalived/core/global_data.c b/keepalived/core/global_data.c
index b0365e7a..6e872dcf 100644
--- a/keepalived/core/global_data.c
+++ b/keepalived/core/global_data.c
@@ -569,7 +569,8 @@ dump_global_data(FILE *fp, data_t * data)
 	else if (global_data->vrrp_rx_bufs_policy & RX_BUFS_SIZE)
 		sprintf(buf, " rx_bufs_size = %lu", global_data->vrrp_rx_bufs_size);
 	if (buf[0])
 		conf_write(fp, "%s", buf);
 	conf_write(fp, " rx_bufs_multiples = %u", global_data->vrrp_rx_bufs_multiples);
+	conf_write(fp, " umask = 0%o", global_data->umask);
 #endif
 }
diff --git a/keepalived/core/global_parser.c b/keepalived/core/global_parser.c
index cf91a93a..0be01685 100644
--- a/keepalived/core/global_parser.c
+++ b/keepalived/core/global_parser.c
@@ -33,10 +33,12 @@
 #include <ctype.h>
 #ifdef _HAVE_SCHED_RT_
 #include <sched.h>
 #endif
 #include <strings.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #ifdef _WITH_SNMP_
 #include "snmp.h"
 #endif
 
@@ -1339,10 +1341,80 @@ no_checker_emails_handler(vector_t *strvec)
 
 	global_data->no_checker_emails = res;
 }
 #endif
 
+static void
+umask_handler(vector_t *strvec)
+{
+	long umask_long;
+	mode_t umask_val = 0;
+	char *mask = strvec_slot(strvec, 1);
+	char *endptr;
+	unsigned i;
+	char *p;
+
+	if (umask_cmdline) {
+		log_message(LOG_INFO, "umask command line option specified, ignoring config option");
+		return;
+	}
+
+	if (isdigit(mask[0])) {
+		if (vector_size(strvec) != 2) {
+			report_config_error(CONFIG_GENERAL_ERROR, "%s parameter(s) to umask option", vector_size(strvec) == 1 ? "Missing" : "Extra");
+			return;
+		}
+		umask_long = strtol(mask, &endptr, 0);
+		if (*endptr || umask_long < 0 || umask_long & ~0777L) {
+			report_config_error(CONFIG_GENERAL_ERROR, "invalid umask value %s", mask);
+			return;
+		}
+		umask_val = umask_long & 0777;
+	}
+	else {
+		bool need_or = false;
+		for (i = 1; i < vector_size(strvec); i++) {
+			for (p = strvec_slot(strvec, i); *p; ) {
+				if (need_or) {
+					if (*p == '|') {
+						need_or = false;
+						p++;
+						continue;
+					}
+
+					report_config_error(CONFIG_GENERAL_ERROR, "Invalid umask syntax %s", FMT_STR_VSLOT(strvec, i));
+					return;
+				}
+
+				if      (!strncmp(p, "IRUSR", 5)) umask_val |= S_IRUSR;
+				else if (!strncmp(p, "IWUSR", 5)) umask_val |= S_IWUSR;
+				else if (!strncmp(p, "IXUSR", 5)) umask_val |= S_IXUSR;
+				else if (!strncmp(p, "IRGRP", 5)) umask_val |= S_IRGRP;
+				else if (!strncmp(p, "IWGRP", 5)) umask_val |= S_IWGRP;
+				else if (!strncmp(p, "IXGRP", 5)) umask_val |= S_IXGRP;
+				else if (!strncmp(p, "IROTH", 5)) umask_val |= S_IROTH;
+				else if (!strncmp(p, "IWOTH", 5)) umask_val |= S_IWOTH;
+				else if (!strncmp(p, "IXOTH", 5)) umask_val |= S_IXOTH;
+				else {
+					report_config_error(CONFIG_GENERAL_ERROR, "Unknown umask bit %s", p);
+					return;
+				}
+
+				p += 5;
+				need_or = true;
+			}
+		}
+		if (!need_or) {
+			report_config_error(CONFIG_GENERAL_ERROR, "umask missing bit value");
+			return;
+		}
+	}
+
+	global_data->umask = umask_val;
+	umask(umask_val);
+}
+
 void
 init_global_keywords(bool global_active)
 {
 	/* global definitions mapping */
 	install_keyword_root("linkbeat_use_polling", use_polling_handler, global_active);
@@ -1481,6 +1553,7 @@ init_global_keywords(bool global_active)
 #endif
 #ifdef _WITH_VRRP_
 	install_keyword("vrrp_rx_bufs_policy", &vrrp_rx_bufs_policy_handler);
 	install_keyword("vrrp_rx_bufs_multiplier", &vrrp_rx_bufs_multiplier_handler);
 #endif
+	install_keyword("umask", &umask_handler);
 }
diff --git a/keepalived/core/main.c b/keepalived/core/main.c
index 2d524f0a..6670ada2 100644
--- a/keepalived/core/main.c
+++ b/keepalived/core/main.c
@@ -31,10 +31,11 @@
 #endif
 #include <errno.h>
 #include <signal.h>
 #include <fcntl.h>
 #include <sys/wait.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <linux/version.h>
 #include <ctype.h>
@@ -153,10 +154,14 @@ static struct {
 	{LOG_LOCAL0}, {LOG_LOCAL1}, {LOG_LOCAL2}, {LOG_LOCAL3},
 	{LOG_LOCAL4}, {LOG_LOCAL5}, {LOG_LOCAL6}, {LOG_LOCAL7}
 };
 #define	LOG_FACILITY_MAX	((sizeof(LOG_FACILITY) / sizeof(LOG_FACILITY[0])) - 1)
 
+/* umask settings */
+bool umask_cmdline;
+static mode_t umask_val = S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH;
+
 /* Control producing core dumps */
 static bool set_core_dump_pattern = false;
 static bool create_core_dump = false;
 static const char *core_dump_pattern = "core";
 static char *orig_core_dump_pattern = NULL;
@@ -864,10 +869,32 @@ core_dump_init(void)
 		else
 			set_child_rlimit(RLIMIT_CORE, &orig_rlim);
 	}
 }
 
+static mode_t
+set_umask(const char *optarg)
+{
+	long umask_long;
+	mode_t umask_val;
+	char *endptr;
+
+	umask_long = strtoll(optarg, &endptr, 0);
+
+	if (*endptr || umask_long < 0 || umask_long & ~0777L) {
+		fprintf(stderr, "Invalid --umask option %s", optarg);
+		return;
+	}
+
+	umask_val = umask_long & 0777;
+	umask(umask_val);
+
+	umask_cmdline = true;
+
+	return umask_val;
+}
+
 void
 initialise_debug_options(void)
 {
 #if defined WITH_DEBUG_OPTIONS && !defined _DEBUG_
 	char mask = 0;
@@ -1088,10 +1115,11 @@ usage(const char *prog)
 	fprintf(stderr, "  -D, --log-detail             Detailed log messages\n");
 	fprintf(stderr, "  -S, --log-facility=[0-7]     Set syslog facility to LOG_LOCAL[0-7]\n");
 	fprintf(stderr, "  -g, --log-file=FILE          Also log to FILE (default /tmp/keepalived.log)\n");
 	fprintf(stderr, "      --flush-log-file         Flush log file on write\n");
 	fprintf(stderr, "  -G, --no-syslog              Don't log via syslog\n");
+	fprintf(stderr, "  -u, --umask=MASK             umask for file creation (in numeric form)\n");
 #ifdef _WITH_VRRP_
 	fprintf(stderr, "  -X, --release-vips           Drop VIP on transition from signal.\n");
 	fprintf(stderr, "  -V, --dont-release-vrrp      Don't remove VRRP VIPs and VROUTEs on daemon stop\n");
 #endif
 #ifdef _WITH_LVS_
@@ -1181,10 +1209,11 @@ parse_cmdline(int argc, char **argv)
 	struct utsname uname_buf;
 	int longindex;
 	int curind;
 	bool bad_option = false;
 	unsigned facility;
+	mode_t new_umask_val;
 
 	struct option long_options[] = {
 		{"use-file",		required_argument,	NULL, 'f'},
 #if defined _WITH_VRRP_ && defined _WITH_LVS_
 		{"vrrp",		no_argument,		NULL, 'P'},
@@ -1198,10 +1227,11 @@ parse_cmdline(int argc, char **argv)
 		{"log-detail",		no_argument,		NULL, 'D'},
 		{"log-facility",	required_argument,	NULL, 'S'},
 		{"log-file",		optional_argument,	NULL, 'g'},
 		{"flush-log-file",	no_argument,		NULL,  2 },
 		{"no-syslog",		no_argument,		NULL, 'G'},
+		{"umask",		required_argument,	NULL, 'u'},
 #ifdef _WITH_VRRP_
 		{"release-vips",	no_argument,		NULL, 'X'},
 		{"dont-release-vrrp",	no_argument,		NULL, 'V'},
 #endif
 #ifdef _WITH_LVS_
@@ -1250,11 +1280,11 @@ parse_cmdline(int argc, char **argv)
 
 	/* Unfortunately, if a short option is used, getopt_long() doesn't change the value
 	 * of longindex, so we need to ensure that before calling getopt_long(), longindex
 	 * is set to a known invalid value */
 	curind = optind;
-	while (longindex = -1, (c = getopt_long(argc, argv, ":vhlndDRS:f:p:i:mM::g::Gt::"
+	while (longindex = -1, (c = getopt_long(argc, argv, ":vhlndu:DRS:f:p:i:mM::g::Gt::"
 #if defined _WITH_VRRP_ && defined _WITH_LVS_
 					    "PC"
 #endif
 #ifdef _WITH_VRRP_
 					    "r:VX"
@@ -1356,10 +1386,15 @@ parse_cmdline(int argc, char **argv)
 			break;
 		case 'G':
 			__set_bit(NO_SYSLOG_BIT, &debug);
 			reopen_log = true;
 			break;
+		case 'u':
+			new_umask_val = set_umask(optarg);
+			if (umask_cmdline)
+				umask_val = new_umask_val;
+			break;
 		case 't':
 			__set_bit(CONFIG_TEST_BIT, &debug);
 			__set_bit(DONT_RESPAWN_BIT, &debug);
 			__set_bit(DONT_FORK_BIT, &debug);
 			__set_bit(NO_SYSLOG_BIT, &debug);
@@ -1580,10 +1615,13 @@ keepalived_main(int argc, char **argv)
 #endif
 #ifdef _WITH_BFD_
 	__set_bit(DAEMON_BFD, &daemon_mode);
 #endif
 
+	/* Set default file creation mask */
+	umask(022);
+
 	/* Open log with default settings so we can log initially */
 	openlog(PACKAGE_NAME, LOG_PID, log_facility);
 
 #ifdef _MEM_CHECK_
 	mem_log_init(PACKAGE_NAME, "Parent process");
@@ -1676,10 +1714,11 @@ keepalived_main(int argc, char **argv)
 
 		goto end;
 	}
 
 	global_data = alloc_global_data();
+	global_data->umask = umask_val;
 
 	read_config_file();
 
 	init_global_data(global_data, NULL);
 
@@ -1807,13 +1846,10 @@ keepalived_main(int argc, char **argv)
 		FREE_PTR(orig_core_dump_pattern);
 		close_std_fd();
 		exit(0);
 	}
 
-	/* Set file creation mask */
-	umask(0);
-
 #ifdef _MEM_CHECK_
 	enable_mem_log_termination();
 #endif
 
 	if (__test_bit(CONFIG_TEST_BIT, &debug)) {
diff --git a/keepalived/include/global_data.h b/keepalived/include/global_data.h
index fe533bf4..fd885816 100644
--- a/keepalived/include/global_data.h
+++ b/keepalived/include/global_data.h
@@ -211,10 +211,11 @@ typedef struct _data {
 #ifdef _WITH_VRRP_
 	int				vrrp_rx_bufs_policy;
 	size_t				vrrp_rx_bufs_size;
 	int				vrrp_rx_bufs_multiples;
 #endif
+	mode_t				umask;			/* mask for file creation */
 } data_t;
 
 /* Global vars exported */
 extern data_t *global_data;	/* Global configuration data */
 extern data_t *old_global_data;	/* Old global configuration data - used during reload */
diff --git a/keepalived/include/main.h b/keepalived/include/main.h
index 35b2b073..06255d29 100644
--- a/keepalived/include/main.h
+++ b/keepalived/include/main.h
@@ -94,7 +94,8 @@ extern bool running_bfd(void);
 extern void stop_keepalived(void);
 extern void initialise_debug_options(void);
 extern int keepalived_main(int, char**); /* The "real" main function */
 
 extern unsigned child_wait_time;
+extern bool umask_cmdline;
 
 #endif

Vulnerable code: 	while (longindex = -1, (c = getopt_long(argc, argv, ":vhlndDRS:f:p:i:mM::g::Gt::"
	/* Set file creation mask */
	umask(0);

Fixed code:     umask [NUMBER|BITS]                       # The umask to use for creating files. The number can be specified in hex, octal
                                              #   or decimal. BITS are I{R|W|X}{USR|GRP|OTH}, e.g. IRGRP, separated by '|'s.
                                              #   The default umask is IWGRP | IWOTH. This option cannot override the
                                              #   command-line option.

    # The umask to use for creating files. The number can be specified in hex, octal
    #   or decimal. BITS are I{R|W|X}{USR|GRP|OTH}, e.g. IRGRP, separated by '|'s.
    #   The default umask is IWGRP | IWOTH. This option cannot override the
    #   command-line option.
    \fBumask \fR[NUMBER|BITS]
[\fB\-u\fP|\fB\-\-umask\fP=NUMBER]
\fB -u, --umask\fP=NUMBER
The umask specified in the usual numeric way - see man umask(2)
.TP
	conf_write(fp, " umask = 0%o", global_data->umask);
#include <sys/types.h>
#include <sys/stat.h>
static void
umask_handler(vector_t *strvec)
{
	long umask_long;
	mode_t umask_val = 0;
	char *mask = strvec_slot(strvec, 1);
	char *endptr;
	unsigned i;
	char *p;

	if (umask_cmdline) {
		log_message(LOG_INFO, "umask command line option specified, ignoring config option");
		return;
	}

	if (isdigit(mask[0])) {
		if (vector_size(strvec) != 2) {
			report_config_error(CONFIG_GENERAL_ERROR, "%s parameter(s) to umask option", vector_size(strvec) == 1 ? "Missing" : "Extra");
			return;
		}
		umask_long = strtol(mask, &endptr, 0);
		if (*endptr || umask_long < 0 || umask_long & ~0777L) {
			report_config_error(CONFIG_GENERAL_ERROR, "invalid umask value %s", mask);
			return;
		}
		umask_val = umask_long & 0777;
	}
	else {
		bool need_or = false;
		for (i = 1; i < vector_size(strvec); i++) {
			for (p = strvec_slot(strvec, i); *p; ) {
				if (need_or) {
					if (*p == '|') {
						need_or = false;
						p++;
						continue;
					}

					report_config_error(CONFIG_GENERAL_ERROR, "Invalid umask syntax %s", FMT_STR_VSLOT(strvec, i));
					return;
				}

				if      (!strncmp(p, "IRUSR", 5)) umask_val |= S_IRUSR;
				else if (!strncmp(p, "IWUSR", 5)) umask_val |= S_IWUSR;
				else if (!strncmp(p, "IXUSR", 5)) umask_val |= S_IXUSR;
				else if (!strncmp(p, "IRGRP", 5)) umask_val |= S_IRGRP;
				else if (!strncmp(p, "IWGRP", 5)) umask_val |= S_IWGRP;
				else if (!strncmp(p, "IXGRP", 5)) umask_val |= S_IXGRP;
				else if (!strncmp(p, "IROTH", 5)) umask_val |= S_IROTH;
				else if (!strncmp(p, "IWOTH", 5)) umask_val |= S_IWOTH;
				else if (!strncmp(p, "IXOTH", 5)) umask_val |= S_IXOTH;
				else {
					report_config_error(CONFIG_GENERAL_ERROR, "Unknown umask bit %s", p);
					return;
				}

				p += 5;
				need_or = true;
			}
		}
		if (!need_or) {
			report_config_error(CONFIG_GENERAL_ERROR, "umask missing bit value");
			return;
		}
	}

	global_data->umask = umask_val;
	umask(umask_val);
}

	install_keyword("umask", &umask_handler);
#include <sys/types.h>
/* umask settings */
bool umask_cmdline;
static mode_t umask_val = S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH;

static mode_t
set_umask(const char *optarg)
{
	long umask_long;
	mode_t umask_val;
	char *endptr;

	umask_long = strtoll(optarg, &endptr, 0);

	if (*endptr || umask_long < 0 || umask_long & ~0777L) {
		fprintf(stderr, "Invalid --umask option %s", optarg);
		return;
	}

	umask_val = umask_long & 0777;
	umask(umask_val);

	umask_cmdline = true;

	return umask_val;
}

	fprintf(stderr, "  -u, --umask=MASK             umask for file creation (in numeric form)\n");
	mode_t new_umask_val;
		{"umask",		required_argument,	NULL, 'u'},
	while (longindex = -1, (c = getopt_long(argc, argv, ":vhlndu:DRS:f:p:i:mM::g::Gt::"
		case 'u':
			new_umask_val = set_umask(optarg);
			if (umask_cmdline)
				umask_val = new_umask_val;
			break;
	/* Set default file creation mask */
	umask(022);

	global_data->umask = umask_val;
	mode_t				umask;			/* mask for file creation */
extern bool umask_cmdline;