Blame SOURCES/bz1836146-ipc_set_ownership.patch

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