commit 3c4431b

shrub  ·  2026-02-08 00:45:35 +0000 UTC
parent 86a6462
handle sealed sh pool fd during resize

check the current fd size with fstat before ftruncate-ing. tolerate some
ftrunacate permission issues. this is to remedy issues that openBSD
compat patches were causing on linux.
1 files changed,  +16, -2
+16, -2
 1@@ -34,6 +34,7 @@
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <sys/mman.h>
 5+#include <sys/stat.h>
 6 #include <unistd.h>
 7 #include <wayland-server.h>
 8 #include <wld/pixman.h>
 9@@ -159,12 +160,25 @@ resize(struct wl_client *client, struct wl_resource *resource, int32_t size)
10 {
11 	struct pool *pool = wl_resource_get_user_data(resource);
12 	void *data;
13+	struct stat st;
14 
15-	if (ftruncate(pool->fd, size) != 0) {
16-		wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "ftruncate failed: %s", strerror(errno));
17+	if (fstat(pool->fd, &st) != 0) {
18+		wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "fstat failed: %s", strerror(errno));
19 		return;
20 	}
21+	if (st.st_size < size) {
22+		if (ftruncate(pool->fd, size) != 0) {
23+			int saved = errno;
24+			/* some clients seal memfd  if size is already fine, allo */
25+			if ((saved == EPERM || saved == EACCES) && fstat(pool->fd, &st) == 0 && st.st_size >= size) {
26+				goto remap;
27+			}
28+			wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "ftruncate failed: %s", strerror(saved));
29+			return;
30+		}
31+	}
32 
33+remap:
34 	data = swc_mremap(pool, pool->data, pool->size, size);
35 	if (data == MAP_FAILED) {
36 		wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "mremap failed: %s", strerror(errno));