From 30c93547cd14db1ae44ea9e32bf36a2c2b152c07 Mon Sep 17 00:00:00 2001
From: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date: Sun, 28 Aug 2022 03:33:37 +0200
Subject: kmsg: factorize uint32_t into an alignment type

and restore the checks for offset alignment in the message, even if
currently it is trivially always alright.
---
 ipc/ipc_kmsg.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/ipc/ipc_kmsg.c b/ipc/ipc_kmsg.c
index cf7ec77d..8f54f81d 100644
--- a/ipc/ipc_kmsg.c
+++ b/ipc/ipc_kmsg.c
@@ -69,9 +69,11 @@
 #endif
 
 /* msg body is always aligned to 4 bytes */
-#define msg_is_misaligned(x)	( ((vm_offset_t)(x)) & (sizeof(uint32_t)-1) )
+typedef uint32_t msg_align_t;
+
+#define msg_is_misaligned(x)	( ((vm_offset_t)(x)) & (sizeof(msg_align_t)-1) )
 #define msg_align(x)	\
-	( ( ((vm_offset_t)(x)) + (sizeof(uint32_t)-1) ) & ~(sizeof(uint32_t)-1) )
+	( ( ((vm_offset_t)(x)) + (sizeof(msg_align_t)-1) ) & ~(sizeof(msg_align_t)-1) )
 
 ipc_kmsg_t ipc_kmsg_cache[NCPUS];
 
@@ -1377,6 +1379,9 @@ ipc_kmsg_copyin_body(
 		} else {
 			vm_offset_t addr;
 
+			if (sizeof(msg_align_t) > sizeof(mach_msg_type_t))
+				saddr = msg_align(saddr);
+
 			if ((eaddr - saddr) < sizeof(vm_offset_t)) {
 				ipc_kmsg_clean_partial(kmsg, taddr, FALSE, 0);
 				return MACH_SEND_MSG_TOO_SMALL;
@@ -2424,6 +2429,9 @@ ipc_kmsg_copyout_body(
 		} else {
 			vm_offset_t data;
 
+			if (sizeof(msg_align_t) > sizeof(mach_msg_type_t))
+				saddr = msg_align(saddr);
+
 			data = * (vm_offset_t *) saddr;
 
 			/* copyout memory carried in the message */
-- 
cgit v1.2.3