Commitcc9ea811Recorded17 Nov 2026Repositorysigil-ui

Bound DMA-BUF imports by each buffer's real size

Message

Replace the fixed 64 MiB plane span with checks against the size the kernel reports for each DMA-BUF, matching the compositor's own import checks. A 5120 x 3456 ARGB page now imports. A plane that runs past its buffer, or whose span does not fit in 32 bits, fails locally with EINVAL instead of ending the connection with an out_of_bounds error.

Prepare 0.2.7.

Changed
 NEWS.md                           | 13 +++++++++++++
 README.md                         |  9 ++++++---
 native/wayland-bridge.h           |  6 ++++--
 native/wayland-dmabuf-import.c    | 34 +++++++++++++++++++++++++++-------
 package.sgl                       |  2 +-
 scripts/check-dmabuf-import.sgl   |  4 ++--
 test/wayland/dmabuf-import-peer.c | 39 ++++++++++++++++++++++++++++++++++++---
 7 files changed, 89 insertions(+), 18 deletions(-)
Diff
NEWS.mdmodified
@@ -1,3 +1,16 @@
+1
# sigil-ui 0.2.7
+2
+3
A Wayland DMA-BUF import fix. Requirements are unchanged from 0.2.6.
+4
+5
- `sui_wayland_dmabuf_import` bounds each plane by its DMA-BUF's real size,
+6
as the kernel reports it, instead of a fixed 64 MiB span. A 5120 x 3456
+7
ARGB page (70,778,880 bytes) now imports. A plane that runs past its
+8
buffer is refused locally with EINVAL, not sent to the compositor, whose
+9
own check would end the connection with an `out_of_bounds` error.
+10
A plane whose span exceeds 32 bits is refused the same way, even when
+11
its descriptor cannot report a size.
+12
- The size query leaves the descriptor's file position unchanged.
+13
14
# sigil-ui 0.2.6
15
16
An SDK-free AppKit drawing adapter release. Requires Sigil 0.24.8 and
README.mdmodified
@@ -28,7 +28,7 @@ Requires Sigil 0.24.8. Add the registry dependency to your package's
28
(from-pkg name: "sigil/sigil-ui" version: "^0.2")
29
```
30
−31
This release is 0.2.6; see [NEWS](NEWS.md) for changes.
+31
This release is 0.2.7; see [NEWS](NEWS.md) for changes.
32
33
## Minimal example
34
@@ -234,8 +234,11 @@ attaching buffers. The caller supplies negotiated format/modifier/plane
234
layout, retains its original descriptors and observes pending/ready/failed/
235
canceled status through an opaque reference-counted handle. Metadata checks
236
do not establish that a GPU or compositor supports a layout. The connection
−237
allows at most 64 live handles and a 64-MiB offset/stride/height span per
−238
plane; this metadata bound does not measure driver allocation size.
+237
allows at most 64 live handles. Each plane's span must fit in 32 bits and
+238
lie within its DMA-BUF's real size, which the kernel reports through
+239
`lseek(SEEK_END)`; the first plane's offset + stride * height must fit too.
+240
These are the compositor's own import checks, so a refused layout fails
+241
locally instead of ending the connection with a protocol error.
242
Canceling or dropping a pending handle keeps
243
a bounded private listener until its terminal reply or connection close,
244
then destroys any late-created buffer. Parent close cancels surviving
native/wayland-bridge.hmodified
@@ -53,8 +53,10 @@ int sui_wayland_dmabuf_feedback_format(const SuiWaylandDmabufFeedback *feedback,
53
* Cancellation retains a bounded private listener until created/failed or
54
* connection close, so a late created buffer can be destroyed correctly.
55
* Calls belong to the connection's owning thread. At most 64 handles may
−56
* coexist on one connection; each plane's offset + stride * height is
−57
* bounded to 64 MiB. This bounds metadata, not driver allocation size.
+56
* coexist on one connection. Each plane's span must fit in 32 bits and
+57
* lie within its DMA-BUF's real size, as the kernel reports it; the first
+58
* plane's offset + stride * height must fit too. These are the compositor's own import
+59
* checks, so a refused plane never reaches it as a fatal protocol error.
60
* Creation returns NULL with errno on refusal. */
61
typedef struct SuiWaylandDmabufImport SuiWaylandDmabufImport;
62
typedef struct { int fd; uint32_t offset, stride; } SuiWaylandDmabufPlane;
native/wayland-dmabuf-import.cmodified
@@ -1,6 +1,5 @@
1
/* Included by wayland.c. Imports are connection resources, not presentations. */
2
#define DMABUF_IMPORT_LIMIT 64u
−3
#define DMABUF_PLANE_SPAN (64u * 1024 * 1024)
3
4
struct SuiWaylandDmabufImport {
5
unsigned references;
@@ -217,6 +216,32 @@ static void dmabuf_imports_disconnected(SuiWaylandDmabufImport *list)
216
}
217
}
218
+219
/* Bound a plane with the same checks a compositor makes on import: a plane
+220
* that fails them ends the whole connection with an out_of_bounds protocol
+221
* error. Planes after the first may be subsampled, so only the first is
+222
* checked against the full height. A span beyond 32 bits is refused before
+223
* any size query, as compositors do; the rest is checked against the
+224
* DMA-BUF's real size. A descriptor that cannot report its size (a pipe or
+225
* a socket) leaves that part to the compositor, which skips it too.
+226
* dma_buf_llseek never moves the file position, so the restore below only
+227
* matters for other files, such as a memfd. */
+228
static int dmabuf_plane_fits(const SuiWaylandDmabufSpec *spec, uint32_t plane)
+229
{
+230
uint64_t offset = spec->planes[plane].offset, stride = spec->planes[plane].stride;
+231
int fd = spec->planes[plane].fd;
+232
/* offset and stride are 32-bit and height is at most 16384, so none of
+233
* these sums or products can overflow 64 bits. */
+234
if (offset + stride > UINT32_MAX ||
+235
(plane == 0 && offset + stride * (uint64_t)spec->height > UINT32_MAX)) return 0;
+236
off_t position = lseek(fd, 0, SEEK_CUR);
+237
off_t end = lseek(fd, 0, SEEK_END);
+238
if (end < 0) return 1;
+239
lseek(fd, position < 0 ? 0 : position, SEEK_SET);
+240
uint64_t size = (uint64_t)end;
+241
if (offset > size || offset + stride > size) return 0;
+242
return plane > 0 || offset + stride * (uint64_t)spec->height <= size;
+243
}
+244
245
SuiWaylandDmabufImport *sui_wayland_dmabuf_import(SuiWaylandSurface *surface,
246
const SuiWaylandDmabufSpec *spec)
247
{
@@ -225,15 +250,10 @@ SuiWaylandDmabufImport *sui_wayland_dmabuf_import(SuiWaylandSurface *surface,
250
!spec->n_planes || spec->n_planes > 4 || !spec->fourcc) { errno = EINVAL; return NULL; }
251
for (uint32_t i = 0; i < spec->n_planes; i++) {
252
if (spec->planes[i].fd < 0 || !spec->planes[i].stride) { errno = EINVAL; return NULL; }
−228
if (spec->planes[i].offset > DMABUF_PLANE_SPAN ||
−229
spec->planes[i].stride > DMABUF_PLANE_SPAN ||
−230
(uint32_t)spec->height > (DMABUF_PLANE_SPAN - spec->planes[i].offset) / spec->planes[i].stride) {
−231
errno = EINVAL;
−232
return NULL;
−233
}
253
/* Validate all borrowed descriptors before duplicates can reuse a
254
* closed descriptor number appearing later in the input array. */
255
if (fcntl(spec->planes[i].fd, F_GETFD) < 0) return NULL;
+256
if (!dmabuf_plane_fits(spec, i)) { errno = EINVAL; return NULL; }
257
}
258
Wl *owner = surface;
259
while (owner->parent) owner = owner->parent;
package.sglmodified
@@ -29,7 +29,7 @@
29
30
(package
31
name: "sigil-ui"
−32
version: "0.2.6"
+32
version: "0.2.7"
33
sigil: "0.24.8"
34
description: "Retained-mode UI framework with a software renderer"
35
url: "https://git.usesigil.org/sigil/sigil-ui"
scripts/check-dmabuf-import.sglmodified
@@ -11,7 +11,7 @@
11
(define cases '("created" "failed" "delayed" "cancel-pending" "cancel-ready"
12
"unref-pending" "unref-ready" "close-pending" "close-ready" "child-close"
13
"factory-pending" "factory-ready" "disconnect" "four-planes" "invalid"
−14
"limit" "late-created" "missing" "legacy" "close-unflushed"))
+14
"limit" "late-created" "missing" "legacy" "close-unflushed" "large-span"))
15
(each variant '("plain" "sanitized")
16
(define executable (path evidence (str "import-" variant)))
17
(define flags
@@ -34,4 +34,4 @@
34
(display (read-text log))
35
(unless (and (ok? result) (contains? (read-text log) (str "PASS: import wire " case ",")))
36
(error "Import wire check failed" variant case (format-outcome result)))))
−37
(println "PASS: 20 import wire cases in plain and ASan/UBSan builds")
+37
(println "PASS: 21 import wire cases in plain and ASan/UBSan builds")
test/wayland/dmabuf-import-peer.cmodified
@@ -12,6 +12,8 @@
12
#include SUI_TEST_WAYLAND_SOURCE
13
#include "owned-peer.h"
14
+15
#define LARGE_SPAN_SIZE 70778880
+16
17
enum ImportPeerObject { IP_DISPLAY = 1, IP_REGISTRY, IP_COMPOSITOR, IP_DMABUF,
18
IP_SUBCOMPOSITOR, IP_SURFACE, IP_PARAMS, IP_FRAME };
19
typedef struct {
@@ -158,16 +160,19 @@ static int import_serve(int fd, const char *mode, ImportStats *stats)
160
struct stat info;
161
uint32_t marker = 0;
162
unsigned index = params[id].planes++;
−161
assert(fstat(plane_fd, &info) == 0 && info.st_size == 4096);
+163
int large = !strcmp(mode, "large-span");
+164
assert(fstat(plane_fd, &info) == 0 && info.st_size == (large ? LARGE_SPAN_SIZE : 4096));
165
assert(fcntl(plane_fd, F_GETFD) & FD_CLOEXEC);
166
assert(pread(plane_fd, &marker, sizeof marker, 0) == 4 && marker == 0x22334455);
167
close(plane_fd);
168
assert(message[2] == index && message[3] == index * 1024 &&
−166
message[4] == (!strcmp(mode, "p-scale2") ? 128u : 64u));
+169
message[4] == (large ? 20480u : !strcmp(mode, "p-scale2") ? 128u : 64u));
170
assert(message[5] == 0x01000000 && message[6] == 1);
171
stats->adds++;
172
} else if (objects[id] == IP_PARAMS && opcode == 2) {
−170
assert(count == 6 && (message[2] == 16 || message[2] == 32) && message[3] == message[2] &&
+173
assert(count == 6 && (!strcmp(mode, "large-span") ?
+174
message[2] == 5120 && message[3] == 3456 :
+175
(message[2] == 16 || message[2] == 32) && message[3] == message[2]) &&
176
message[4] == 0x34325241 && message[5] == 0);
177
assert(params[id].planes == (!strcmp(mode, "four-planes") ? 4u : 1u));
178
stats->creates++;
@@ -250,6 +255,14 @@ int main(int argc, char **argv)
255
SuiWaylandDmabufSpec spec = {.width = 16, .height = 16, .fourcc = 0x34325241,
256
.n_planes = !strcmp(mode, "four-planes") ? 4 : 1, .modifier = 0x0100000000000001};
257
for (unsigned i = 0; i < 4; i++) spec.planes[i] = (SuiWaylandDmabufPlane){plane, i * 1024, 64};
+258
if (!strcmp(mode, "large-span")) {
+259
/* 5120 x 3456 x 4 bytes is above the former fixed 64 MiB plane
+260
* bound and within this sparse allocation, so it must import. */
+261
assert(ftruncate(plane, LARGE_SPAN_SIZE) == 0);
+262
spec.width = 5120;
+263
spec.height = 3456;
+264
spec.planes[0].stride = 20480;
+265
}
266
SuiWaylandDmabufImport *buffer = NULL;
267
if (!strcmp(mode, "invalid")) {
268
SuiWaylandDmabufSpec bad = spec;
@@ -265,6 +278,26 @@ int main(int argc, char **argv)
278
assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
279
bad = spec; bad.planes[0].stride = UINT32_MAX;
280
assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
+281
/* Real-size bounds: 64 x 128 rows exceed the 4096-byte allocation,
+282
* as does a later plane whose first row runs past its end. The
+283
* descriptor's file position survives the size query. */
+284
assert(lseek(plane, 100, SEEK_SET) == 100);
+285
bad = spec; bad.height = 128;
+286
assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
+287
bad = spec; bad.n_planes = 2; bad.planes[1].offset = 4096 - 32;
+288
assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
+289
assert(lseek(plane, 0, SEEK_CUR) == 100);
+290
/* A pipe cannot report its size, but a span beyond 32 bits is still
+291
* refused locally: compositors end the connection over it. */
+292
int pipe_fds[2];
+293
assert(pipe2(pipe_fds, O_CLOEXEC) == 0);
+294
bad = spec; bad.height = 16384;
+295
for (unsigned i = 0; i < 4; i++) bad.planes[i].fd = pipe_fds[0];
+296
bad.planes[0].stride = 0x80000000u;
+297
assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
+298
bad.planes[0].stride = 64; bad.planes[0].offset = UINT32_MAX - 32;
+299
assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
+300
assert(close(pipe_fds[0]) == 0 && close(pipe_fds[1]) == 0);
301
int stale = dup(plane);
302
assert(stale >= 0 && close(stale) == 0);
303
bad = spec; bad.n_planes = 2; bad.planes[1].fd = stale;