From mboxrd@z Thu Jan  1 00:00:00 1970
Received: by passt.top (Postfix, from userid 1000)
	id 2D9BA5A0275; Fri, 12 Apr 2024 00:18:00 +0200 (CEST)
From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Subject: [PATCH] pasta, util: Align stack area for clones to maximum natural alignment
Date: Fri, 12 Apr 2024 00:18:00 +0200
Message-ID: <20240411221800.548140-1-sbrivio@redhat.com>
X-Mailer: git-send-email 2.43.0
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Message-ID-Hash: TOIBVLXG4DV3DNORJ5JAQZ5WOIYAZF3O
X-Message-ID-Hash: TOIBVLXG4DV3DNORJ5JAQZ5WOIYAZF3O
X-MailFrom: sbrivio@passt.top
X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header
CC: runsisi <runsisi@hust.edu.cn>
X-Mailman-Version: 3.3.8
Precedence: list
List-Id: Development discussion and patches for passt <passt-dev.passt.top>
Archived-At: <https://archives.passt.top/passt-dev/20240411221800.548140-1-sbrivio@redhat.com/>
Archived-At: <https://passt.top/hyperkitty/list/passt-dev@passt.top/message/TOIBVLXG4DV3DNORJ5JAQZ5WOIYAZF3O/>
List-Archive: <https://archives.passt.top/passt-dev/>
List-Archive: <https://passt.top/hyperkitty/list/passt-dev@passt.top/>
List-Help: <mailto:passt-dev-request@passt.top?subject=help>
List-Owner: <mailto:passt-dev-owner@passt.top>
List-Post: <mailto:passt-dev@passt.top>
List-Subscribe: <mailto:passt-dev-join@passt.top>
List-Unsubscribe: <mailto:passt-dev-leave@passt.top>

Given that we use this stack pointer as a location to store arbitrary
data types from the cloned process, we need to guarantee that its
alignment matches any of those possible data types.

runsisi reports that pasta gets a SIGBUS in pasta_open_ns() on
aarch64, where the alignment requirement for stack pointers is a
16 bytes (same as the size of a long double), and similar requirements
actually apply to most architectures we run on.

Reported-by: runsisi <runsisi@hust.edu.cn>
Link: https://bugs.passt.top/show_bug.cgi?id=85
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 pasta.c | 3 ++-
 util.h  | 4 +++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/pasta.c b/pasta.c
index e73b5af..31e1e00 100644
--- a/pasta.c
+++ b/pasta.c
@@ -211,12 +211,13 @@ static int pasta_spawn_cmd(void *arg)
 void pasta_start_ns(struct ctx *c, uid_t uid, gid_t gid,
 		    int argc, char *argv[])
 {
+	char ns_fn_stack[NS_FN_STACK_SIZE]
+	__attribute__ ((aligned(__alignof__(max_align_t))));
 	struct pasta_spawn_cmd_arg arg = {
 		.exe = argv[0],
 		.argv = argv,
 	};
 	char uidmap[BUFSIZ], gidmap[BUFSIZ];
-	char ns_fn_stack[NS_FN_STACK_SIZE];
 	char *sh_argv[] = { NULL, NULL };
 	char sh_arg0[PATH_MAX + 1];
 	sigset_t set;
diff --git a/util.h b/util.h
index 7c261d7..8c37729 100644
--- a/util.h
+++ b/util.h
@@ -9,6 +9,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <string.h>
 #include <signal.h>
 
@@ -116,7 +117,8 @@ int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
 	     void *arg);
 #define NS_CALL(fn, arg)						\
 	do {								\
-		char ns_fn_stack[NS_FN_STACK_SIZE];			\
+		char ns_fn_stack[NS_FN_STACK_SIZE]			\
+		__attribute__ ((aligned(__alignof__(max_align_t))));	\
 									\
 		do_clone((fn), ns_fn_stack, sizeof(ns_fn_stack),	\
 			 CLONE_VM | CLONE_VFORK | CLONE_FILES | SIGCHLD,\
-- 
2.43.0