Blame SOURCES/bz1654301-fix-improper-pathname-validation.patch

9cfb40
diff --git a/keepalived/core/pidfile.c b/keepalived/core/pidfile.c
9cfb40
index f3f3a2c8..6cfbfe8d 100644
9cfb40
--- a/keepalived/core/pidfile.c
9cfb40
+++ b/keepalived/core/pidfile.c
9cfb40
@@ -54,7 +54,7 @@ int
9cfb40
 pidfile_write(const char *pid_file, int pid)
9cfb40
 {
9cfb40
 	FILE *pidfile = NULL;
9cfb40
-	int pidfd = creat(pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
9cfb40
+	int pidfd = open(pid_file, O_NOFOLLOW | O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
9cfb40
 	if (pidfd != -1) pidfile = fdopen(pidfd, "w");
9cfb40
 
9cfb40
 	if (!pidfile) {
9cfb40
diff --git a/keepalived/vrrp/vrrp_dbus.c b/keepalived/vrrp/vrrp_dbus.c
9cfb40
index f29a974f..1320a6c2 100644
9cfb40
--- a/keepalived/vrrp/vrrp_dbus.c
9cfb40
+++ b/keepalived/vrrp/vrrp_dbus.c
9cfb40
@@ -551,7 +551,7 @@ read_file(gchar* filepath)
9cfb40
 	size_t length;
9cfb40
 	gchar *ret = NULL;
9cfb40
 
9cfb40
-	f = fopen(filepath, "rb");
9cfb40
+	f = fopen(filepath, "r");
9cfb40
 	if (f) {
9cfb40
 		fseek(f, 0, SEEK_END);
9cfb40
 		length = (size_t)ftell(f);
9cfb40
diff --git a/keepalived/vrrp/vrrp_print.c b/keepalived/vrrp/vrrp_print.c
9cfb40
index 7adb701d..03840adf 100644
9cfb40
--- a/keepalived/vrrp/vrrp_print.c
9cfb40
+++ b/keepalived/vrrp/vrrp_print.c
9cfb40
@@ -27,6 +27,7 @@
9cfb40
 #include "vrrp.h"
9cfb40
 #include "vrrp_data.h"
9cfb40
 #include "vrrp_print.h"
9cfb40
+#include "utils.h"
9cfb40
 #ifdef _HAVE_FIB_ROUTING_
9cfb40
 #include "vrrp_iproute.h"
9cfb40
 #include "vrrp_iprule.h"
9cfb40
@@ -350,7 +351,7 @@ void
9cfb40
 vrrp_print_data(void)
9cfb40
 {
9cfb40
 	FILE *file;
9cfb40
-	file = fopen ("/tmp/keepalived.data","w");
9cfb40
+	file = fopen_safe("/tmp/keepalived.data","w");
9cfb40
 
9cfb40
 	if (!file) {
9cfb40
 		log_message(LOG_INFO, "Can't open /tmp/keepalived.data (%d: %s)",
9cfb40
@@ -374,7 +375,7 @@ void
9cfb40
 vrrp_print_stats(void)
9cfb40
 {
9cfb40
 	FILE *file;
9cfb40
-	file = fopen ("/tmp/keepalived.stats","w");
9cfb40
+	file = fopen_safe("/tmp/keepalived.stats","w");
9cfb40
 
9cfb40
 	if (!file) {
9cfb40
 		log_message(LOG_INFO, "Can't open /tmp/keepalived.stats (%d: %s)",
9cfb40
@@ -393,8 +394,7 @@ vrrp_print_stats(void)
9cfb40
 		fprintf(file, "    Received: %" PRIu64 "\n", vrrp->stats->advert_rcvd);
9cfb40
 		fprintf(file, "    Sent: %d\n", vrrp->stats->advert_sent);
9cfb40
 		fprintf(file, "  Became master: %d\n", vrrp->stats->become_master);
9cfb40
-		fprintf(file, "  Released master: %d\n",
9cfb40
-			vrrp->stats->release_master);
9cfb40
+		fprintf(file, "  Released master: %d\n", vrrp->stats->release_master);
9cfb40
 		fprintf(file, "  Packet Errors:\n");
9cfb40
 		fprintf(file, "    Length: %" PRIu64 "\n", vrrp->stats->packet_len_err);
9cfb40
 		fprintf(file, "    TTL: %" PRIu64 "\n", vrrp->stats->ip_ttl_err);
9cfb40
diff --git a/lib/memory.c b/lib/memory.c
9cfb40
index 96a4e7b9..04b3d6ee 100644
9cfb40
--- a/lib/memory.c
9cfb40
+++ b/lib/memory.c
9cfb40
@@ -441,7 +441,7 @@ mem_log_init(const char* prog_name, const char *banner)
9cfb40
 	}
9cfb40
 
9cfb40
 	snprintf(log_name, log_name_len, "/tmp/%s_mem.%d.log", prog_name, getpid());
9cfb40
-	log_op = fopen(log_name, "a");
9cfb40
+	log_op = fopen_safe(log_name, "a");
9cfb40
 	if (log_op == NULL) {
9cfb40
 		log_message(LOG_INFO, "Unable to open %s for appending", log_name);
9cfb40
 		log_op = stderr;
9cfb40
diff --git a/lib/parser.c b/lib/parser.c
9cfb40
index 2f7959e8..a5c3465b 100644
9cfb40
--- a/lib/parser.c
9cfb40
+++ b/lib/parser.c
9cfb40
@@ -153,12 +153,12 @@ dump_keywords(vector_t *keydump, int level, FILE *fp)
9cfb40
 {
9cfb40
 	unsigned int i;
9cfb40
 	keyword_t *keyword_vec;
9cfb40
-	char file_name[21];
9cfb40
+	char file_name[22];
9cfb40
 
9cfb40
 	if (!level) {
9cfb40
 		sprintf(file_name, "/tmp/keywords.%d", getpid());
9cfb40
 		snprintf(file_name, sizeof(file_name), "/tmp/keywords.%d", getpid());
9cfb40
-		fp = fopen(file_name, "w");
9cfb40
+		fp = fopen_safe(file_name, "w");
9cfb40
 		if (!fp)
9cfb40
 			return;
9cfb40
 	}
9cfb40
diff --git a/lib/utils.c b/lib/utils.c
9cfb40
index 88465609..f317937e 100644
9cfb40
--- a/lib/utils.c
9cfb40
+++ b/lib/utils.c
9cfb40
@@ -89,7 +89,7 @@ dump_buffer(char *buff, size_t count, FILE* fp)
9cfb40
 void
9cfb40
 write_stacktrace(const char *file_name)
9cfb40
 {
9cfb40
-	int fd = open(file_name, O_WRONLY | O_APPEND | O_CREAT, 0644);
9cfb40
+	int fd = open(file_name, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
9cfb40
 	void *buffer[100];
9cfb40
 	int nptrs;
9cfb40
 
9cfb40
@@ -518,6 +518,47 @@ string_equal(const char *str1, const char *str2)
9cfb40
 	return (*str1 == 0 && *str2 == 0);
9cfb40
 }
9cfb40
 
9cfb40
+/* We need to use O_NOFOLLOW if opening a file for write, so that a non privileged user can't
9cfb40
+ * create a symbolic link from the path to a system file and cause a system file to be overwritten. */
9cfb40
+FILE *fopen_safe(const char *path, const char *mode)
9cfb40
+{
9cfb40
+	int fd;
9cfb40
+	FILE *file;
9cfb40
+	int flags = O_NOFOLLOW | O_CREAT;
9cfb40
+
9cfb40
+	if (mode[0] == 'r')
9cfb40
+		return fopen(path, mode);
9cfb40
+
9cfb40
+	if (mode[0] != 'a' && mode[0] != 'w')
9cfb40
+		return NULL;
9cfb40
+
9cfb40
+	if (mode[1] &&
9cfb40
+	    (mode[1] != '+' || mode[2]))
9cfb40
+		return NULL;
9cfb40
+
9cfb40
+	if (mode[0] == 'w')
9cfb40
+		flags |= O_TRUNC;
9cfb40
+	else
9cfb40
+		flags |= O_APPEND;
9cfb40
+
9cfb40
+	if (mode[1])
9cfb40
+		flags |= O_RDWR;
9cfb40
+	else
9cfb40
+		flags |= O_WRONLY;
9cfb40
+
9cfb40
+	fd = open(path, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
9cfb40
+	if (fd == -1)
9cfb40
+		return NULL;
9cfb40
+
9cfb40
+	file = fdopen (fd, "w");
9cfb40
+	if (!file) {
9cfb40
+		close(fd);
9cfb40
+		return NULL;
9cfb40
+	}
9cfb40
+
9cfb40
+	return file;
9cfb40
+}
9cfb40
+
9cfb40
 void
9cfb40
 set_std_fd(int force)
9cfb40
 {
9cfb40
diff --git a/lib/utils.h b/lib/utils.h
9cfb40
index ec717125..1e440795 100644
9cfb40
--- a/lib/utils.h
9cfb40
+++ b/lib/utils.h
9cfb40
@@ -70,6 +70,7 @@ extern int inet_inaddrcmp(int, void *, void *);
9cfb40
 extern int inet_sockaddrcmp(struct sockaddr_storage *, struct sockaddr_storage *);
9cfb40
 extern char *get_local_name(void);
9cfb40
 extern int string_equal(const char *, const char *);
9cfb40
+extern FILE *fopen_safe(const char *, const char *);
9cfb40
 extern void set_std_fd(int);
9cfb40
 #if !defined _HAVE_LIBIPTC_ || defined _LIBIPTC_DYNAMIC_
9cfb40
 extern int fork_exec(char **argv);