Blame SOURCES/0208-UPBZ-1430097-multipathd-IPC-changes.patch

d88bf6
[PATCH] Multipath: Remove duplicated memset() for multipathd show command.
d88bf6
[PATCH] multipath-tools: New way to limit the IPC command length.
d88bf6
[PATCH] multipath-tools: Perform socket client uid check on IPC commands.
d88bf6
d88bf6
Signed-off-by: Gris Ge <fge@redhat.com>
d88bf6
---
d88bf6
 libmultipath/print.c  |   10 ----------
d88bf6
 libmultipath/uxsock.c |   38 +++++++++++++++++++++++++++++---------
d88bf6
 libmultipath/uxsock.h |    9 +++++++++
d88bf6
 multipathd/main.c     |   15 +++++++++++++--
d88bf6
 multipathd/uxlsnr.c   |   31 ++++++++++++++++++++++++++-----
d88bf6
 multipathd/uxlsnr.h   |    8 +++++---
d88bf6
 6 files changed, 82 insertions(+), 29 deletions(-)
d88bf6
d88bf6
Index: multipath-tools-130222/libmultipath/print.c
d88bf6
===================================================================
d88bf6
--- multipath-tools-130222.orig/libmultipath/print.c
d88bf6
+++ multipath-tools-130222/libmultipath/print.c
d88bf6
@@ -771,8 +771,6 @@ snprint_multipath_header (char * line, i
d88bf6
 	int fwd;
d88bf6
 	struct multipath_data * data;
d88bf6
 
d88bf6
-	memset(line, 0, len);
d88bf6
-
d88bf6
 	do {
d88bf6
 		if (!TAIL)
d88bf6
 			break;
d88bf6
@@ -806,8 +804,6 @@ snprint_multipath (char * line, int len,
d88bf6
 	struct multipath_data * data;
d88bf6
 	char buff[MAX_FIELD_LEN] = {};
d88bf6
 
d88bf6
-	memset(line, 0, len);
d88bf6
-
d88bf6
 	do {
d88bf6
 		if (!TAIL)
d88bf6
 			break;
d88bf6
@@ -842,8 +838,6 @@ snprint_path_header (char * line, int le
d88bf6
 	int fwd;
d88bf6
 	struct path_data * data;
d88bf6
 
d88bf6
-	memset(line, 0, len);
d88bf6
-
d88bf6
 	do {
d88bf6
 		if (!TAIL)
d88bf6
 			break;
d88bf6
@@ -877,8 +871,6 @@ snprint_path (char * line, int len, char
d88bf6
 	struct path_data * data;
d88bf6
 	char buff[MAX_FIELD_LEN];
d88bf6
 
d88bf6
-	memset(line, 0, len);
d88bf6
-
d88bf6
 	do {
d88bf6
 		if (!TAIL)
d88bf6
 			break;
d88bf6
@@ -914,8 +906,6 @@ snprint_pathgroup (char * line, int len,
d88bf6
 	struct pathgroup_data * data;
d88bf6
 	char buff[MAX_FIELD_LEN];
d88bf6
 
d88bf6
-	memset(line, 0, len);
d88bf6
-
d88bf6
 	do {
d88bf6
 		if (!TAIL)
d88bf6
 			break;
d88bf6
Index: multipath-tools-130222/libmultipath/uxsock.c
d88bf6
===================================================================
d88bf6
--- multipath-tools-130222.orig/libmultipath/uxsock.c
d88bf6
+++ multipath-tools-130222/libmultipath/uxsock.c
d88bf6
@@ -20,6 +20,15 @@
d88bf6
 
d88bf6
 #include "memory.h"
d88bf6
 #include "uxsock.h"
d88bf6
+
d88bf6
+/*
d88bf6
+ * Code is similar with mpath_recv_reply() with data size limitation
d88bf6
+ * and debug-able malloc.
d88bf6
+ * When limit == 0, it means no limit on data size, used for socket client
d88bf6
+ * to receiving data from multipathd.
d88bf6
+ */
d88bf6
+static int _recv_packet(int fd, char **buf, ssize_t limit);
d88bf6
+
d88bf6
 /*
d88bf6
  * create a unix domain socket and start listening on it
d88bf6
  * return a file descriptor open on the socket
d88bf6
@@ -95,27 +104,38 @@ int send_packet(int fd, const char *buf)
d88bf6
 	return ret;
d88bf6
 }
d88bf6
 
d88bf6
-/*
d88bf6
- * receive a packet in length prefix format
d88bf6
- */
d88bf6
-int recv_packet(int fd, char **buf)
d88bf6
+static int _recv_packet(int fd, char **buf, ssize_t limit)
d88bf6
 {
d88bf6
-	int err;
d88bf6
-	ssize_t len;
d88bf6
+	int err = 0;
d88bf6
+	ssize_t len = 0;
d88bf6
 	unsigned int timeout = DEFAULT_REPLY_TIMEOUT;
d88bf6
 
d88bf6
 	*buf = NULL;
d88bf6
 	len = mpath_recv_reply_len(fd, timeout);
d88bf6
 	if (len <= 0)
d88bf6
 		return len;
d88bf6
+	if ((limit > 0) && (len > limit))
d88bf6
+		return -EINVAL;
d88bf6
 	(*buf) = MALLOC(len);
d88bf6
 	if (!*buf)
d88bf6
 		return -ENOMEM;
d88bf6
 	err = mpath_recv_reply_data(fd, *buf, len, timeout);
d88bf6
-	if (err) {
d88bf6
+	if (err != 0) {
d88bf6
 		FREE(*buf);
d88bf6
 		(*buf) = NULL;
d88bf6
-		return err;
d88bf6
 	}
d88bf6
-	return 0;
d88bf6
+	return err;
d88bf6
+}
d88bf6
+
d88bf6
+/*
d88bf6
+ * receive a packet in length prefix format
d88bf6
+ */
d88bf6
+int recv_packet(int fd, char **buf)
d88bf6
+{
d88bf6
+	return _recv_packet(fd, buf, 0 /* no limit */);
d88bf6
+}
d88bf6
+
d88bf6
+int recv_packet_from_client(int fd, char **buf)
d88bf6
+{
d88bf6
+	return _recv_packet(fd, buf, _MAX_CMD_LEN);
d88bf6
 }
d88bf6
Index: multipath-tools-130222/libmultipath/uxsock.h
d88bf6
===================================================================
d88bf6
--- multipath-tools-130222.orig/libmultipath/uxsock.h
d88bf6
+++ multipath-tools-130222/libmultipath/uxsock.h
d88bf6
@@ -3,3 +3,12 @@ int ux_socket_listen(const char *name);
d88bf6
 int send_packet(int fd, const char *buf);
d88bf6
 int recv_packet(int fd, char **buf);
d88bf6
 size_t write_all(int fd, const void *buf, size_t len);
d88bf6
+
d88bf6
+#define _MAX_CMD_LEN           512
d88bf6
+
d88bf6
+/*
d88bf6
+ * Used for receiving socket command from untrusted socket client where data
d88bf6
+ * size is restricted to 512(_MAX_CMD_LEN) at most.
d88bf6
+ * Return -EINVAL if data length requested by client exceeded the _MAX_CMD_LEN.
d88bf6
+ */
d88bf6
+int recv_packet_from_client(int fd, char **buf);
d88bf6
Index: multipath-tools-130222/multipathd/main.c
d88bf6
===================================================================
d88bf6
--- multipath-tools-130222.orig/multipathd/main.c
d88bf6
+++ multipath-tools-130222/multipathd/main.c
d88bf6
@@ -18,6 +18,7 @@
d88bf6
 #include <linux/oom.h>
d88bf6
 #include <libudev.h>
d88bf6
 #include <semaphore.h>
d88bf6
+#include <stdbool.h>
d88bf6
 #include <mpath_persist.h>
d88bf6
 #include "prioritizers/alua_rtpg.h"
d88bf6
 
d88bf6
@@ -859,7 +860,8 @@ map_discovery (struct vectors * vecs)
d88bf6
 }
d88bf6
 
d88bf6
 int
d88bf6
-uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
d88bf6
+uxsock_trigger (char * str, char ** reply, int * len, bool is_root,
d88bf6
+		void * trigger_data)
d88bf6
 {
d88bf6
 	struct vectors * vecs;
d88bf6
 	int r;
d88bf6
@@ -872,6 +874,15 @@ uxsock_trigger (char * str, char ** repl
d88bf6
 	lock(vecs->lock);
d88bf6
 	pthread_testcancel();
d88bf6
 
d88bf6
+	if ((str != NULL) && (is_root == false) &&
d88bf6
+	    (strncmp(str, "list", strlen("list")) != 0) &&
d88bf6
+	    (strncmp(str, "show", strlen("show")) != 0)) {
d88bf6
+		*reply = STRDUP("permission deny: need to be root");
d88bf6
+		*len = strlen(*reply) + 1;
d88bf6
+		r = 1;
d88bf6
+		goto out;
d88bf6
+	}
d88bf6
+
d88bf6
 	r = parse_cmd(str, reply, len, vecs);
d88bf6
 
d88bf6
 	if (r > 0) {
d88bf6
@@ -885,7 +896,7 @@ uxsock_trigger (char * str, char ** repl
d88bf6
 		r = 0;
d88bf6
 	}
d88bf6
 	/* else if (r < 0) leave *reply alone */
d88bf6
-
d88bf6
+out:
d88bf6
 	lock_cleanup_pop(vecs->lock);
d88bf6
 	return r;
d88bf6
 }
d88bf6
Index: multipath-tools-130222/multipathd/uxlsnr.c
d88bf6
===================================================================
d88bf6
--- multipath-tools-130222.orig/multipathd/uxlsnr.c
d88bf6
+++ multipath-tools-130222/multipathd/uxlsnr.c
d88bf6
@@ -21,6 +21,7 @@
d88bf6
 #include <sys/un.h>
d88bf6
 #include <sys/poll.h>
d88bf6
 #include <signal.h>
d88bf6
+#include <stdbool.h>
d88bf6
 #include <checkers.h>
d88bf6
 #include <memory.h>
d88bf6
 #include <debug.h>
d88bf6
@@ -48,6 +49,23 @@ struct pollfd *polls;
d88bf6
 volatile sig_atomic_t reconfig_sig = 0;
d88bf6
 volatile sig_atomic_t log_reset_sig = 0;
d88bf6
 
d88bf6
+static bool _socket_client_is_root(int fd);
d88bf6
+
d88bf6
+static bool _socket_client_is_root(int fd)
d88bf6
+{
d88bf6
+	socklen_t len = 0;
d88bf6
+	struct ucred uc;
d88bf6
+
d88bf6
+	len = sizeof(struct ucred);
d88bf6
+	if ((fd >= 0) &&
d88bf6
+	    (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &uc, &len) == 0) &&
d88bf6
+	    (uc.uid == 0))
d88bf6
+			return true;
d88bf6
+
d88bf6
+	/* Treat error as not root client */
d88bf6
+	return false;
d88bf6
+}
d88bf6
+
d88bf6
 /*
d88bf6
  * handle a new client joining
d88bf6
  */
d88bf6
@@ -105,8 +123,7 @@ void uxsock_cleanup(void *arg)
d88bf6
 /*
d88bf6
  * entry point
d88bf6
  */
d88bf6
-void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *),
d88bf6
-			void * trigger_data)
d88bf6
+void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
d88bf6
 {
d88bf6
 	int ux_sock;
d88bf6
 	int rlen;
d88bf6
@@ -171,12 +188,16 @@ void * uxsock_listen(int (*uxsock_trigge
d88bf6
 			struct client *next = c->next;
d88bf6
 
d88bf6
 			if (polls[i].revents & POLLIN) {
d88bf6
-				if (recv_packet(c->fd, &inbuf) != 0) {
d88bf6
+				if (recv_packet_from_client(c->fd,
d88bf6
+							    &inbuf) != 0) {
d88bf6
 					dead_client(c);
d88bf6
+				} else if (!inbuf) {
d88bf6
+					condlog(4, "recv_packet_from_client "
d88bf6
+						"get null request");
d88bf6
+					continue;
d88bf6
 				} else {
d88bf6
 					condlog(4, "Got request [%s]", inbuf);
d88bf6
-					uxsock_trigger(inbuf, &reply, &rlen,
d88bf6
-						       trigger_data);
d88bf6
+					uxsock_trigger(inbuf, &reply, &rlen, _socket_client_is_root(c->fd), trigger_data);
d88bf6
 					if (reply) {
d88bf6
 						if (send_packet(c->fd,
d88bf6
 								reply) != 0) {
d88bf6
Index: multipath-tools-130222/multipathd/uxlsnr.h
d88bf6
===================================================================
d88bf6
--- multipath-tools-130222.orig/multipathd/uxlsnr.h
d88bf6
+++ multipath-tools-130222/multipathd/uxlsnr.h
d88bf6
@@ -1,9 +1,11 @@
d88bf6
 #ifndef _UXLSNR_H
d88bf6
 #define _UXLSNR_H
d88bf6
 
d88bf6
-void * uxsock_listen(int (*uxsock_trigger)
d88bf6
-			(char *, char **, int *, void *),
d88bf6
-			void * trigger_data);
d88bf6
+#include <stdbool.h>
d88bf6
+
d88bf6
+typedef int (uxsock_trigger_fn)(char *, char **, int *, bool, void *);
d88bf6
+
d88bf6
+void *uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data);
d88bf6
 
d88bf6
 extern volatile sig_atomic_t reconfig_sig;
d88bf6
 extern volatile sig_atomic_t log_reset_sig;