Blame SOURCES/bz1836146-ipc_set_ownership.patch

573961
diff -urp libqb-1.0.3.orig/lib/ipc_shm.c libqb-1.0.3/lib/ipc_shm.c
573961
--- libqb-1.0.3.orig/lib/ipc_shm.c	2020-05-20 09:00:31.826899188 +0100
573961
+++ libqb-1.0.3/lib/ipc_shm.c	2020-05-20 09:11:56.607788472 +0100
573961
@@ -282,6 +282,8 @@ qb_ipcs_shm_connect(struct qb_ipcs_servi
573961
 		    struct qb_ipc_connection_response *r)
573961
 {
573961
 	int32_t res;
573961
+	char dirname[PATH_MAX];
573961
+	char *slash;
573961
 
573961
 	qb_util_log(LOG_DEBUG, "connecting to client [%d]", c->pid);
573961
 
573961
@@ -292,6 +294,14 @@ qb_ipcs_shm_connect(struct qb_ipcs_servi
573961
 	snprintf(r->event, NAME_MAX, "%s-event-%s",
573961
 		 c->description, s->name);
573961
 
573961
+	/* Set correct ownership if qb_ipcs_connection_auth_set() has been used */
573961
+	strlcpy(dirname, c->description, sizeof(dirname));
573961
+	slash = strrchr(dirname, '/');
573961
+	if (slash) {
573961
+		*slash = '\0';
573961
+		(void)chown(dirname, c->auth.uid, c->auth.gid);
573961
+	}
573961
+
573961
 	res = qb_ipcs_shm_rb_open(c, &c->request,
573961
 				  r->request);
573961
 	if (res != 0) {
573961
diff -urp libqb-1.0.3.orig/tests/check_ipc.c libqb-1.0.3/tests/check_ipc.c
573961
--- libqb-1.0.3.orig/tests/check_ipc.c	2017-12-21 09:02:11.000000000 +0000
573961
+++ libqb-1.0.3/tests/check_ipc.c	2020-05-20 09:07:55.607104804 +0100
573961
@@ -98,6 +98,8 @@ enum my_msg_ids {
573961
  * 7) service availability
573961
  *
573961
  * 8) multiple services
573961
+ *
573961
+ * 9) setting perms on the sockets
573961
  */
573961
 static qb_loop_t *my_loop;
573961
 static qb_ipcs_service_t* s1;
573961
@@ -109,6 +111,7 @@ static int32_t num_bulk_events = 10;
573961
 static int32_t num_stress_events = 30000;
573961
 static int32_t reference_count_test = QB_FALSE;
573961
 static int32_t multiple_connections = QB_FALSE;
573961
+static int32_t set_perms_on_socket = QB_FALSE;
573961
 
573961
 
573961
 static int32_t
573961
@@ -360,6 +363,16 @@ s1_connection_destroyed(qb_ipcs_connecti
573961
 	qb_leave();
573961
 }
573961
 
573961
+static int32_t
573961
+s1_connection_accept(qb_ipcs_connection_t *c, uid_t uid, gid_t gid)
573961
+{
573961
+	if (set_perms_on_socket) {
573961
+		qb_ipcs_connection_auth_set(c, 555, 741, S_IRWXU|S_IRWXG|S_IROTH|S_IWOTH);
573961
+	}
573961
+	return 0;
573961
+}
573961
+
573961
+
573961
 static void
573961
 s1_connection_created(qb_ipcs_connection_t *c)
573961
 {
573961
@@ -402,7 +415,7 @@ run_ipc_server(void)
573961
 	qb_loop_signal_handle handle;
573961
 
573961
 	struct qb_ipcs_service_handlers sh = {
573961
-		.connection_accept = NULL,
573961
+		.connection_accept = s1_connection_accept,
573961
 		.connection_created = s1_connection_created,
573961
 		.msg_process = s1_msg_process_fn,
573961
 		.connection_destroyed = s1_connection_destroyed,
573961
@@ -517,7 +530,7 @@ verify_graceful_stop(pid_t pid)
573961
 	} else {
573961
 		fail_if(rc == 0);
573961
 	}
573961
-	
573961
+
573961
 	return 0;
573961
 }
573961
 
573961
@@ -1018,7 +1031,7 @@ test_ipc_stress_connections(void)
573961
 			}
573961
 		} while (conn == NULL && c < 5);
573961
 		fail_if(conn == NULL);
573961
-		
573961
+
573961
 		if (((connections+1) % 1000) == 0) {
573961
 			qb_log(LOG_INFO, "%d ipc connections made", connections+1);
573961
 		}
573961
@@ -1448,6 +1461,63 @@ START_TEST(test_ipcc_truncate_when_unlin
573961
 END_TEST
573961
 #endif
573961
 
573961
+// Check perms uses illegal access to libqb internals
573961
+// DO NOT try this at home.
573961
+#include "../lib/ipc_int.h"
573961
+#include "../lib/ringbuffer_int.h"
573961
+START_TEST(test_ipc_server_perms)
573961
+{
573961
+	pid_t pid;
573961
+	struct stat st;
573961
+	int j;
573961
+	uint32_t max_size;
573961
+	int res;
573961
+	int c = 0;
573961
+
573961
+	// Can only test this if we are root
573961
+	if (getuid() != 0) {
573961
+		return;
573961
+	}
573961
+
573961
+	ipc_type = QB_IPC_SHM;
573961
+	set_perms_on_socket = QB_TRUE;
573961
+	max_size = MAX_MSG_SIZE;
573961
+
573961
+	pid = run_function_in_new_process(run_ipc_server);
573961
+	fail_if(pid == -1);
573961
+
573961
+	do {
573961
+		conn = qb_ipcc_connect(ipc_name, max_size);
573961
+		if (conn == NULL) {
573961
+			j = waitpid(pid, NULL, WNOHANG);
573961
+			ck_assert_int_eq(j, 0);
573961
+			poll(NULL, 0, 400);
573961
+			c++;
573961
+		}
573961
+	} while (conn == NULL && c < 5);
573961
+	fail_if(conn == NULL);
573961
+
573961
+	// Check perms - uses illegal access to libqb internals
573961
+	char sockdir[PATH_MAX];
573961
+	strcpy(sockdir, conn->request.u.shm.rb->shared_hdr->hdr_path);
573961
+	*strrchr(sockdir, '/') = 0;
573961
+	res = stat(sockdir, &st);
573961
+
573961
+	ck_assert_int_eq(res, 0);
573961
+	ck_assert(st.st_mode & S_IRWXG);
573961
+	ck_assert_int_eq(st.st_uid, 555);
573961
+	ck_assert_int_eq(st.st_gid, 741);
573961
+
573961
+	res = stat(conn->request.u.shm.rb->shared_hdr->hdr_path, &st);
573961
+	ck_assert_int_eq(res, 0);
573961
+	ck_assert_int_eq(st.st_uid, 555);
573961
+	ck_assert_int_eq(st.st_gid, 741);
573961
+
573961
+	qb_ipcc_disconnect(conn);
573961
+	verify_graceful_stop(pid);
573961
+}
573961
+END_TEST
573961
+
573961
 static void
573961
 test_ipc_service_ref_count(void)
573961
 {
573961
@@ -1502,7 +1572,7 @@ END_TEST
573961
 #if 0
573961
 static void test_max_dgram_size(void)
573961
 {
573961
-	/* most implementations will not let you set a dgram buffer 
573961
+	/* most implementations will not let you set a dgram buffer
573961
 	 * of 1 million bytes. This test verifies that the we can detect
573961
 	 * the max dgram buffersize regardless, and that the value we detect
573961
 	 * is consistent. */
573961
@@ -1562,6 +1632,7 @@ make_shm_suite(void)
573961
 	add_tcase(s, tc, test_ipc_exit_shm, 8);
573961
 	add_tcase(s, tc, test_ipc_event_on_created_shm, 10);
573961
 	add_tcase(s, tc, test_ipc_service_ref_count_shm, 10);
573961
+	add_tcase(s, tc, test_ipc_server_perms, 7);
573961
 	add_tcase(s, tc, test_ipc_stress_connections_shm, 3600);
573961
 
573961
 #ifdef HAVE_FAILURE_INJECTION