This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-10-quiet-head
in repository terminology.
View the commit online.
commit 68b6be7039459196dfe377c128745fcc25bc3827
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 02:46:08 2026 -0600
tymuxd: seal backlog page memfds against shrinking
A peer that truncates a shared memfd makes whoever touches the mapping
raise SIGBUS. Seal every page memfd the store makes (raw pages and the
export rebuild, which share one helper) with F_SEAL_SHRINK, and
F_SEAL_GROW as pages are sized once and never grow. The tymux client
refuses a page whose memfd is not sealed against shrinking.
---
src/bin/backlog_store.c | 53 ++++++++++++++++++++++++-
src/bin/termtymux.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++--
src/bin/tytest.c | 3 ++
src/bin/unit_tests.h | 2 +
4 files changed, 153 insertions(+), 5 deletions(-)
diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index 1553fd11..a66266fe 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -1,5 +1,6 @@
#include "private.h"
#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -350,7 +351,9 @@ _page_memfd_name(const BacklogStore *bs, const char *kind, uint32_t page_id,
}
#if defined(__linux__)
-/* Safe to call from any thread: the memfd is not counted in _memfds. */
+/* Safe to call from any thread: the memfd is not counted in _memfds.
+ * The memfd is sealed against resizing, as a peer that truncates it makes
+ * whoever touches the mapping raise SIGBUS. */
static void *
_memfd_map(const char *memfd_name, size_t file_size, size_t size,
int *fd_out)
@@ -358,7 +361,7 @@ _memfd_map(const char *memfd_name, size_t file_size, size_t size,
void *map;
int fd;
- fd = memfd_create(memfd_name, MFD_CLOEXEC);
+ fd = memfd_create(memfd_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (fd < 0)
{
ERR("memfd_create: %s", strerror(errno));
@@ -370,6 +373,12 @@ _memfd_map(const char *memfd_name, size_t file_size, size_t size,
close(fd);
return NULL;
}
+ if (fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW) < 0)
+ {
+ ERR("F_ADD_SEALS: %s", strerror(errno));
+ close(fd);
+ return NULL;
+ }
map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
{
@@ -4263,6 +4272,46 @@ tytest_backlog_store_page_fd_async(void)
return 0;
}
+#if defined(__linux__)
+static void
+_test_fd_sealed_check(int fd, size_t page_size)
+{
+ const int wanted = F_SEAL_SHRINK | F_SEAL_GROW;
+
+ assert(fd >= 0);
+ assert((fcntl(fd, F_GET_SEALS) & wanted) == wanted);
+ assert(ftruncate(fd, page_size - 4096) < 0 && errno == EPERM);
+ assert(ftruncate(fd, page_size + 4096) < 0 && errno == EPERM);
+}
+#endif
+
+/* Test: the memfds of a MEMFD store cannot be shrunk or grown by a peer
+ * holding them, whether they are raw pages, exports, or pages rebuilt on
+ * a thread, as a truncated memfd makes the mapping of the others SIGBUS. */
+int
+tytest_backlog_store_sealed(void)
+{
+#if defined(__linux__)
+ Test_Fd_Async got = { 0, 0, -1 };
+ BacklogStore *bs;
+ size_t page_size;
+
+ ecore_init();
+ bs = _test_fd_async_store_new(BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ page_size = bs->geo.page_size;
+ _test_fd_sealed_check(backlog_store_page_fd(bs, 2), page_size);
+ _test_fd_sealed_check(backlog_store_page_fd(bs, 0), page_size);
+ assert(backlog_store_page_fd_async(bs, 1, _test_fd_async_done, &got));
+ _test_rebuilds_wait();
+ assert(got.calls == 1);
+ _test_fd_sealed_check(got.fd, page_size);
+ close(got.fd);
+ backlog_store_free(bs);
+ ecore_shutdown();
+#endif
+ return 0;
+}
+
#if defined(__linux__)
/* Whether the mapping at addr is left out of the children fork() makes. */
static Eina_Bool
diff --git a/src/bin/termtymux.c b/src/bin/termtymux.c
index 6a42da0c..158a1259 100644
--- a/src/bin/termtymux.c
+++ b/src/bin/termtymux.c
@@ -274,8 +274,18 @@ _page_header_valid(const TermSrvBacklogPage *hdr, uint32_t id,
return file_size == termsrv_backlog_page_size(hdr->header_size);
}
-/* Maps the memfd of page id, NULL if it is not a backlog page. The
- * geometry of the page is returned in *header_size and *entries_per_page. */
+static Eina_Bool
+_fd_shrink_sealed(int fd)
+{
+ int seals = fcntl(fd, F_GET_SEALS);
+
+ return (seals >= 0) && (seals & F_SEAL_SHRINK);
+}
+
+/* Maps the memfd of page id, NULL if it is not a backlog page or if its
+ * size is not sealed, as a peer could then truncate it under the mapping
+ * and make us raise SIGBUS. The geometry of the page is returned in
+ * *header_size and *entries_per_page. */
static TermSrvBacklogPage *
_page_map(uint32_t id, int fd, uint32_t *header_size,
uint32_t *entries_per_page)
@@ -284,6 +294,7 @@ _page_map(uint32_t id, int fd, uint32_t *header_size,
struct stat st;
if (fstat(fd, &st) < 0 ||
+ !_fd_shrink_sealed(fd) ||
pread(fd, &hdr, sizeof(hdr), 0) != (ssize_t)sizeof(hdr) ||
!_page_header_valid(&hdr, id, st.st_size))
{
@@ -1363,7 +1374,7 @@ _tt_page_memfd_new(uint32_t id, uint32_t header_size, uint32_t entries,
size_t file_size)
{
TermSrvBacklogPage hdr = { 0 };
- int fd = memfd_create("tt-page", MFD_CLOEXEC);
+ int fd = memfd_create("tt-page", MFD_CLOEXEC | MFD_ALLOW_SEALING);
assert(fd >= 0);
assert(ftruncate(fd, file_size) == 0);
@@ -1373,6 +1384,7 @@ _tt_page_memfd_new(uint32_t id, uint32_t header_size, uint32_t entries,
hdr.header_size = header_size;
hdr.entries_per_page = entries;
assert(pwrite(fd, &hdr, sizeof(hdr), 0) == (ssize_t)sizeof(hdr));
+ assert(fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW) == 0);
return fd;
}
@@ -1481,6 +1493,88 @@ tytest_termtymux_backlog_header_checks(void)
return 0;
}
+/* Test: a page memfd that a peer can still shrink or grow is refused and
+ * marked failed, as touching its mapping could then raise SIGBUS. */
+int
+tytest_termtymux_backlog_unsealed_refused(void)
+{
+ TermSrvBacklogPage hdr = { 0 };
+ TermSrvMsgBacklogPageId reply = { 0 };
+ const struct
+ {
+ unsigned int flags, seals;
+ Eina_Bool accepted;
+ } cases[] = {
+ { 0, 0, EINA_FALSE },
+ { MFD_ALLOW_SEALING, 0, EINA_FALSE },
+ { MFD_ALLOW_SEALING, F_SEAL_GROW, EINA_FALSE },
+ { MFD_ALLOW_SEALING, F_SEAL_SHRINK, EINA_TRUE },
+ { MFD_ALLOW_SEALING, F_SEAL_SHRINK | F_SEAL_GROW, EINA_TRUE },
+ };
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ BacklogViewPage *page;
+ TermTymux ts;
+ unsigned int i;
+ int peer;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ memset(&ts, 0, sizeof(ts));
+ ts.shadow_pty = ty;
+ ts.view = backlog_view_new(ty, &_backlog_source, &ts);
+ assert(ts.view);
+ _tt_tymux_connect(&ts, &peer);
+ hdr.magic = TERMSRV_BACKLOG_MAGIC;
+ hdr.version = TERMSRV_BACKLOG_VERSION;
+ hdr.page_id = 0;
+ hdr.header_size = 4096;
+ hdr.entries_per_page = 128;
+ /* a plain file, which no peer sealed: on tmpfs it reports F_SEAL_SEAL,
+ * elsewhere it cannot be sealed at all */
+ {
+ char path[] = "/tmp/tt-page-XXXXXX";
+ int fd = mkstemp(path), seals;
+
+ assert(fd >= 0);
+ unlink(path);
+ assert(ftruncate(fd, termsrv_backlog_page_size(4096)) == 0);
+ assert(pwrite(fd, &hdr, sizeof(hdr), 0) == (ssize_t)sizeof(hdr));
+ seals = fcntl(fd, F_GET_SEALS);
+ assert(seals < 0 || !(seals & F_SEAL_SHRINK));
+ _backlog_range_set(&ts, 0, 1);
+ page = backlog_view_page_find(ts.view, 0);
+ page->failed = EINA_FALSE;
+ _page_received(&ts, &reply, sizeof(reply), fd);
+ assert(page->failed && !page->mapped);
+ _tt_sent(peer, TSRV_MSG_BACKLOG_PAGE_RELEASE, NULL, 0);
+ }
+
+ for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++)
+ {
+ int fd = memfd_create("tt-page", MFD_CLOEXEC | cases[i].flags);
+
+ assert(fd >= 0);
+ assert(ftruncate(fd, termsrv_backlog_page_size(4096)) == 0);
+ assert(pwrite(fd, &hdr, sizeof(hdr), 0) == (ssize_t)sizeof(hdr));
+ if (cases[i].seals)
+ assert(fcntl(fd, F_ADD_SEALS, cases[i].seals) == 0);
+ _backlog_range_set(&ts, 0, 1);
+ page = backlog_view_page_find(ts.view, 0);
+ page->failed = EINA_FALSE;
+ _page_received(&ts, &reply, sizeof(reply), fd);
+ assert(page->failed == !cases[i].accepted);
+ assert((page->mapped != NULL) == cases[i].accepted);
+ if (!cases[i].accepted)
+ _tt_sent(peer, TSRV_MSG_BACKLOG_PAGE_RELEASE, NULL, 0);
+ }
+
+ backlog_view_free(ts.view);
+ close(ts.sock_fd);
+ close(peer);
+ termpty_free(ty);
+ return 0;
+}
+
/* Test: the ring of the shadow pty holds the scrollback and a page of the
* largest size the daemon can make, as the size of its pages is only known
* once one arrives. */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 7eb58237..fde8186f 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -182,6 +182,7 @@ static struct {
{ "backlog_store_blocks", tytest_backlog_store_blocks},
{ "backlog_store_fd_budget", tytest_backlog_store_fd_budget},
{ "backlog_store_page_fd_async", tytest_backlog_store_page_fd_async},
+ { "backlog_store_sealed", tytest_backlog_store_sealed},
{ "backlog_view_sync", tytest_backlog_view_sync},
{ "backlog_view_ring_full", tytest_backlog_view_ring_full},
{ "backlog_view_map_again", tytest_backlog_view_map_again},
@@ -232,6 +233,8 @@ static struct {
{ "termtymux_backlog_external_only", tytest_termtymux_backlog_external_only},
{ "termtymux_backlog_header_checks",
tytest_termtymux_backlog_header_checks},
+ { "termtymux_backlog_unsealed_refused",
+ tytest_termtymux_backlog_unsealed_refused},
{ "termtymux_backlog_ring_size", tytest_termtymux_backlog_ring_size},
{ "termtymux_input_action_get", tytest_termtymux_input_action_get},
{ "termtymux_input_filter_long", tytest_termtymux_input_filter_long},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index ed432460..fc88f9b2 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -137,6 +137,7 @@ int tytest_backlog_store_links(void);
int tytest_backlog_store_blocks(void);
int tytest_backlog_store_fd_budget(void);
int tytest_backlog_store_page_fd_async(void);
+int tytest_backlog_store_sealed(void);
int tytest_backlog_view_sync(void);
int tytest_backlog_view_ring_full(void);
int tytest_backlog_view_map_again(void);
@@ -186,6 +187,7 @@ int tytest_termtymux_input_chunks_send(void);
int tytest_termtymux_attach_escape_parse(void);
int tytest_termtymux_backlog_external_only(void);
int tytest_termtymux_backlog_header_checks(void);
+int tytest_termtymux_backlog_unsealed_refused(void);
int tytest_termtymux_backlog_ring_size(void);
int tytest_termtymux_input_action_get(void);
int tytest_termtymux_input_filter_long(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.