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 b0bb09f97c77162a8fc531582018e7cf22662a1a
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 06:35:21 2026 -0600
tymuxd: hand clients read-only memfds
A client got a read-write duplicate of every page memfd and of the
screen memfd. Mapping the head writable, it could put the last link id
and the wrapped bit in the newest entry, and a RESIZE then made the
daemon count references on that id far past its link table: ASan
reports a SEGV in term_link_refcount_inc(). It could also truncate the
screen memfd under the daemon's mapping.
Clients now get an fd reopened read-only through /proc/self/fd: a
duplicate shares the access of the original. That alone is not
enough, as a client can reopen its read-only fd read-write the same
way, so every memfd is also sealed with F_SEAL_FUTURE_WRITE once the
daemon has mapped it: no new writable mapping or write() can be made,
while the daemon's own mappings keep writing. The screen memfd is
sealed against resizing like the pages. The daemon never needs more
after sealing: the screen memfd has its largest size from the start,
and the pages it writes, raw, rebuilt or made the head again by a pop,
are written through the mapping made before the seal.
F_SEAL_FUTURE_WRITE needs Linux 5.1 and headers that define it. On an
older kernel the seal fails with EINVAL and is skipped: tymuxd still
runs, the read-only fds still stop a client that does not reopen them,
and it logs a warning for each session whose screen memfd is not sealed
against writes.
The store tests that played the client writing to a header now write
through the store's own mapping, as the store still never trusts what
it reads there.
---
src/bin/backlog_store.c | 63 +++++++++++----------
src/bin/termd_tymux.c | 36 +++++++++---
src/bin/termsrv.c | 9 +++
src/bin/termsrv.h | 32 ++++++++++-
src/bin/tytest.c | 1 +
src/bin/tytest_tymux.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++
src/bin/tytest_tymux.h | 1 +
tests/meson.build | 3 +-
8 files changed, 252 insertions(+), 38 deletions(-)
diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index 38423868..037e5b1e 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -353,8 +353,7 @@ _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.
- * The memfd is sealed against resizing, as a peer that truncates it makes
- * whoever touches the mapping raise SIGBUS. */
+ * The memfd is sealed: clients can only read it. */
static void *
_memfd_map(const char *memfd_name, size_t file_size, size_t size,
int *fd_out)
@@ -374,12 +373,6 @@ _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)
{
@@ -387,6 +380,13 @@ _memfd_map(const char *memfd_name, size_t file_size, size_t size,
close(fd);
return NULL;
}
+ if (termsrv_memfd_seal(fd) < 0)
+ {
+ ERR("F_ADD_SEALS: %s", strerror(errno));
+ munmap(map, size);
+ close(fd);
+ return NULL;
+ }
*fd_out = fd;
return map;
}
@@ -2164,15 +2164,12 @@ _test_pages(BacklogStoreBacking backing)
assert(page_cells[_test_off(page)[last] + i].codepoint == 'a' + i);
_test_page_unmap(bs, page);
- /* the memfd is writable by the clients: what they put in the head must
- * not steer the daemon */
+ /* what the header of the head says does not steer the store, which
+ * only ever stores to it */
if (backing == BACKLOG_STORE_MEMFD)
{
- TermSrvBacklogPage *rw = mmap(NULL, bs->geo.page_size,
- PROT_READ | PROT_WRITE, MAP_SHARED,
- backlog_store_page_fd(bs, 2), 0);
+ TermSrvBacklogPage *rw = _page_find(bs, 2)->header;
- assert(rw != MAP_FAILED);
atomic_store(&rw->num_entries, 0x7fffffff);
atomic_store(&_test_w(rw)[last], 0xffffffff);
_test_row_fill(row, 10, 'a');
@@ -2180,7 +2177,6 @@ _test_pages(BacklogStoreBacking backing)
assert(atomic_load(&_test_w(rw)[last]) == 40);
assert(backlog_store_append(bs, row, 10, EINA_FALSE, EINA_TRUE, NULL));
assert(atomic_load(&rw->num_entries) == last + 2);
- munmap(rw, bs->geo.page_size);
}
assert(backlog_store_page_fd(bs, 3) < 0);
@@ -2968,9 +2964,9 @@ tytest_backlog_store_held_evict(void)
return 0;
}
-/* The widths a client wrote into the memfd of the head do not steer pop,
- * nor do the page ids it wrote into a page being compressed, or into a held
- * page once evicted. */
+/* The widths in the header of the head do not steer pop, nor do the page
+ * ids in the header of a page being compressed, or of a held page once
+ * evicted: the store never reads back what it stores there. */
static void
_test_pop_untrusted(void)
{
@@ -2980,9 +2976,7 @@ _test_pop_untrusted(void)
assert(bs);
_test_pages_fill(bs, 0, 3);
- rw = mmap(NULL, bs->geo.page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
- backlog_store_page_fd(bs, 0), 0);
- assert(rw != MAP_FAILED);
+ rw = _page_find(bs, 0)->header;
atomic_store(&rw->num_entries, 77);
atomic_store(&_test_w(rw)[1], 0xffffffff);
atomic_store(&_test_w(rw)[2], 1);
@@ -2994,16 +2988,13 @@ _test_pop_untrusted(void)
assert(atomic_load(&rw->num_entries) == 2);
assert(_test_off(rw)[1] == TEST_ROW_W);
assert(atomic_load(&_test_w(rw)[1]) == TEST_ROW_W);
- munmap(rw, bs->geo.page_size);
backlog_store_free(bs);
bs = _test_store_new(BACKLOG_STORE_MEMFD, 300);
assert(bs);
_test_pages_fill(bs, 0, 2 * _TEST_ENTRIES + 1);
backlog_store_hold(bs, 0);
- rw = mmap(NULL, bs->geo.page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
- backlog_store_page_fd(bs, 1), 0);
- assert(rw != MAP_FAILED);
+ rw = _page_find(bs, 1)->header;
backlog_store_maintain(bs, ecore_time_get() + 6.0);
assert(_page_find(bs, 1)->state == PAGE_COMPRESSING);
rw->page_id = 999;
@@ -3017,7 +3008,6 @@ _test_pop_untrusted(void)
assert(atomic_load(&rw->num_entries) == _TEST_ENTRIES);
assert(_test_off(rw)[_TEST_ENTRIES - 1] ==
(_TEST_ENTRIES - 1) * TEST_ROW_W);
- munmap(rw, bs->geo.page_size);
_test_pages_fill(bs, 2 * _TEST_ENTRIES, 300);
assert(backlog_store_first_id(bs) > 0);
@@ -4318,17 +4308,32 @@ static void
_test_fd_sealed_check(int fd, size_t page_size)
{
const int wanted = F_SEAL_SHRINK | F_SEAL_GROW;
+ void *map;
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);
+# if defined(F_SEAL_FUTURE_WRITE)
+ if (!(fcntl(fd, F_GET_SEALS) & F_SEAL_FUTURE_WRITE))
+ return;
+ map = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ assert(map == MAP_FAILED && errno == EPERM);
+ assert(pwrite(fd, "x", 1, 0) < 0 && errno == EPERM);
+ map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
+ assert(map != MAP_FAILED);
+ assert(mprotect(map, page_size, PROT_READ | PROT_WRITE) < 0);
+ munmap(map, page_size);
+# else
+ (void)map;
+# endif
}
#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. */
+/* Test: the memfds of a MEMFD store cannot be shrunk, grown or written to
+ * 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, and the store writes the head while a peer reads it. */
int
tytest_backlog_store_sealed(void)
{
diff --git a/src/bin/termd_tymux.c b/src/bin/termd_tymux.c
index f13f68d1..be57633a 100644
--- a/src/bin/termd_tymux.c
+++ b/src/bin/termd_tymux.c
@@ -489,8 +489,8 @@ _client_page_queue_log(size_t bytes_ahead)
close(fd);
}
-/* Queue the reply in the output, ahead of what comes later, with a duplicate
- * of the page memfd that _client_flush() passes along with it, so that a
+/* Queue the reply in the output, ahead of what comes later, with a read-only
+ * fd of the page memfd that _client_flush() passes along with it, so that a
* full socket only delays the page. Returns EINA_FALSE if it was not
* sent. */
static Eina_Bool
@@ -519,7 +519,7 @@ _client_page_fd_send(Client *client, const TermSrvMsgBacklogPageId *reply,
_client_page_refuse(client, reply);
return EINA_FALSE;
}
- queued->fd = fcntl(page_fd, F_DUPFD_CLOEXEC, 0);
+ queued->fd = termsrv_fd_read_only(page_fd);
if (queued->fd < 0)
{
free(queued);
@@ -893,6 +893,7 @@ termd_tymux_new(const char *name, const char *cmd)
{
TermDTymux *td;
char memfd_name[128];
+ int rw_fd = -1;
td = calloc(1, sizeof(*td));
if (!td)
@@ -905,25 +906,44 @@ termd_tymux_new(const char *name, const char *cmd)
td->config->term_type = TERM_TYPE_XTERM_256COLOR;
snprintf(memfd_name, sizeof(memfd_name), "tymux-%s", name);
- td->shm_fd = memfd_create(memfd_name, MFD_CLOEXEC);
- if (td->shm_fd < 0)
+ rw_fd = memfd_create(memfd_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
+ if (rw_fd < 0)
{
ERR("memfd_create: %s", strerror(errno));
goto err;
}
- if (ftruncate(td->shm_fd, TERMSRV_SHM_TOTAL_SIZE) < 0)
+ if (ftruncate(rw_fd, TERMSRV_SHM_TOTAL_SIZE) < 0)
{
ERR("ftruncate: %s", strerror(errno));
goto err;
}
td->shm = mmap(NULL, TERMSRV_SHM_TOTAL_SIZE, PROT_READ | PROT_WRITE,
- MAP_SHARED, td->shm_fd, 0);
+ MAP_SHARED, rw_fd, 0);
if (td->shm == MAP_FAILED)
{
ERR("mmap: %s", strerror(errno));
td->shm = NULL;
goto err;
}
+ if (termsrv_memfd_seal(rw_fd) < 0)
+ {
+ ERR("sealing the screen memfd: %s", strerror(errno));
+ goto err;
+ }
+#if defined(F_SEAL_FUTURE_WRITE)
+ if (!(fcntl(rw_fd, F_GET_SEALS) & F_SEAL_FUTURE_WRITE))
+#endif
+ WRN("no F_SEAL_FUTURE_WRITE (Linux < 5.1): a client that reopens its "
+ "memfds can write to them");
+ /* what clients get: they only read the screen */
+ td->shm_fd = termsrv_fd_read_only(rw_fd);
+ if (td->shm_fd < 0)
+ {
+ ERR("read-only screen memfd: %s", strerror(errno));
+ goto err;
+ }
+ close(rw_fd);
+ rw_fd = -1;
td->shm->magic = TERMSRV_SHM_MAGIC;
td->shm->version = TERMSRV_SHM_VERSION;
@@ -955,6 +975,8 @@ termd_tymux_new(const char *name, const char *cmd)
return td;
err:
+ if (rw_fd >= 0)
+ close(rw_fd);
termd_tymux_free(td);
return NULL;
}
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 471987b8..e956ef43 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -447,6 +447,15 @@ termsrv_msg_send_fd(int fd, TermSrvMsgType type,
return _sendmsg_all(fd, &msg);
}
+int
+termsrv_fd_read_only(int fd)
+{
+ char path[64];
+
+ snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+ return open(path, O_RDONLY | O_CLOEXEC);
+}
+
int
termsrv_msg_send(int fd, TermSrvMsgType type,
const void *payload, uint32_t size)
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index 03ee116a..9315f282 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -4,7 +4,7 @@
/* Wire protocol between tymuxd and its clients.
*
* A client connects to the daemon's unix socket, sends ATTACH and receives
- * ATTACHED along with a memfd (SCM_RIGHTS) holding the screen: a
+ * ATTACHED along with a read-only memfd (SCM_RIGHTS) holding the screen: a
* TermSrvShmHeader followed by rows * cols TermSrvCell, row 0 at the top.
* The daemon then sends NOTIFY whenever the screen was updated. */
@@ -380,6 +380,36 @@ int termsrv_msg_send(int fd, TermSrvMsgType type,
int termsrv_msg_send_fd(int fd, TermSrvMsgType type,
const void *payload, uint32_t size, int pass_fd);
+#if defined(__linux__)
+# include <errno.h>
+# include <fcntl.h>
+
+/* Seals memfd fd, which the caller has already mapped for writing, against
+ * resizing and against any new write: a peer that truncates it would make
+ * whoever touches a mapping raise SIGBUS, and a peer that can write to it
+ * could change what the daemon reads back. Whoever is handed fd, read-only
+ * or not, or reopens it, can then only map it read-only. The mappings made
+ * before keep their access. Linux before 5.1 has no F_SEAL_FUTURE_WRITE:
+ * there only the read-only fds peers are handed protect the memfd.
+ * Returns 0 on success, -1 on error. */
+static inline int
+termsrv_memfd_seal(int fd)
+{
+ if (fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW) < 0)
+ return -1;
+# if defined(F_SEAL_FUTURE_WRITE)
+ if ((fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) < 0) && (errno != EINVAL))
+ return -1;
+# endif
+ return 0;
+}
+#endif
+
+/* A read-only fd of the file that fd opens, for a peer: a reopen, as a
+ * duplicate would share the read-write access of fd. Returns -1 on
+ * error. */
+int termsrv_fd_read_only(int fd);
+
/* Gathers the bytes of a connection until they make whole messages.
* Zero it before use. */
typedef struct
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index a27cd92f..0a78c467 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -277,6 +277,7 @@ static const struct {
{ "tymux_live_fd_starved", tytest_tymux_live_fd_starved},
{ "tymux_live_page_async", tytest_tymux_live_page_async},
{ "tymux_live_paste", tytest_tymux_live_paste},
+ { "tymux_live_pages_read_only", tytest_tymux_live_pages_read_only},
#endif
{ NULL, NULL},
};
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index 90ebeb9a..0ef54a58 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -2117,3 +2117,148 @@ tytest_tymux_live_paste(void)
{
return tytest_tymux_live_run(_live_paste_body);
}
+
+#define READ_ONLY_COLS 60
+
+/* What a client that turned hostile gets out of fd: a shared mapping of
+ * size bytes it can write to, either through fd or through a reopen of it,
+ * or NULL. Every other way of changing the memfd must fail too. */
+static void *
+_fd_writable_map(int fd, size_t size)
+{
+ char path[64];
+ void *map;
+ int rw;
+
+ map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (map != MAP_FAILED)
+ return map;
+ assert(errno == EACCES);
+ assert(pwrite(fd, "x", 1, 0) < 0);
+ assert(ftruncate(fd, 0) < 0);
+ snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+ rw = open(path, O_RDWR | O_CLOEXEC);
+ if (rw < 0)
+ return NULL;
+ map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, rw, 0);
+ if (map == MAP_FAILED)
+ {
+ assert(errno == EPERM);
+ assert(pwrite(rw, "x", 1, 0) < 0);
+ assert(ftruncate(rw, 0) < 0);
+#if defined(FALLOC_FL_PUNCH_HOLE)
+ assert(fallocate(rw, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+ 0, size) < 0);
+#endif
+ map = NULL;
+ }
+ close(rw);
+ return map;
+}
+
+/* Turns the newest entry of the head page mapped writable at page into one
+ * that references the last link id, and continues on the screen: a resize
+ * then makes the daemon take references on that id. */
+static void
+_page_newest_entry_poison(TermSrvBacklogPage *page)
+{
+ uint32_t epp = page->entries_per_page, n, off, w, k;
+ Termcell *cells;
+
+ n = atomic_load(&page->num_entries);
+ assert(n > 0 && n <= epp);
+ off = termsrv_backlog_entry_off(page)[n - 1];
+ w = atomic_load(&termsrv_backlog_entry_w(page, epp)[n - 1]);
+ assert(w > 0);
+ cells = (Termcell *)((char *)page + page->header_size) + off;
+ for (k = 0; k < w; k++)
+ cells[k].att.link_id = UINT16_MAX;
+ cells[w - 1].att.autowrapped = 1;
+}
+
+static Eina_Bool
+_seq_scrolled_done(void *data)
+{
+ return _backlog_find(data, "25") != 0;
+}
+
+static Eina_Bool
+_alive_done(void *data)
+{
+ return _logical_line_done(data, "alive");
+}
+
+/* The screen and the backlog pages that the daemon hands out are read-only
+ * to its clients: a client that could write the cells of the head page
+ * could have the daemon take references on a link id past its table. */
+static int
+_live_pages_read_only_body(void)
+{
+ TermTymux *ts;
+ Termpty *ty;
+ TermSrvReader reader = { 0 };
+ TermSrvMsgAttached attached;
+ TermSrvMsgBacklogPageId req;
+ Screen_Cols_Wait wait;
+ void *payload, *screen, *writable;
+ uint32_t size;
+ uint8_t mode = TSRV_ATTACH_READONLY;
+ int fd, shm_fd, page_fd = -1, type;
+ struct stat st;
+
+ assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+ termtymux_resize(ts, 80, SCREEN_ROWS);
+ assert(tytest_tymux_wait(_screen_rows_done, ty, 5.0));
+ assert(tytest_tymux_wait(_prompt_done, ty, 5.0));
+ /* a link gives the daemon a link table */
+ assert(tytest_tymux_type(ts, ty, "printf '\\033]8;;http://x/\\033\\\\l"
+ "\\033]8;;\\033\\\\\\n'; seq 1 40\n"));
+ assert(tytest_tymux_wait(_prompt_done, ty, 5.0));
+ assert(tytest_tymux_wait(_seq_scrolled_done, ty, 5.0));
+
+ fd = termsrv_connect(SESSION);
+ assert(fd >= 0);
+ assert(termsrv_msg_send(fd, TSRV_MSG_ATTACH, &mode, sizeof(mode)) == 0);
+ assert(termsrv_recv_attached(fd, &shm_fd, &attached) == 0);
+ assert(attached.page_count > 0);
+ screen = _fd_writable_map(shm_fd, TERMSRV_SHM_TOTAL_SIZE);
+ if (screen)
+ munmap(screen, TERMSRV_SHM_TOTAL_SIZE);
+ close(shm_fd);
+
+ req.page_id = attached.first_page_id + attached.page_count - 1;
+ assert(termsrv_msg_send(fd, TSRV_MSG_BACKLOG_PAGE_REQ, &req,
+ sizeof(req)) == 0);
+ do
+ {
+ type = _pages_recv(&reader, fd, 5000, &payload, &size, &page_fd);
+ assert(type > 0);
+ free(payload);
+ }
+ while (type != TSRV_MSG_BACKLOG_PAGE);
+ assert(page_fd >= 0);
+ assert(fstat(page_fd, &st) == 0);
+ writable = _fd_writable_map(page_fd, st.st_size);
+ if (writable)
+ _page_newest_entry_poison(writable);
+ close(page_fd);
+
+ wait.ty = ty;
+ wait.cols = READ_ONLY_COLS;
+ termtymux_resize(ts, READ_ONLY_COLS, SCREEN_ROWS);
+ assert(tytest_tymux_wait(_screen_cols_done, &wait, 5.0));
+ assert(tytest_tymux_type(ts, ty, "echo al''ive\n"));
+ assert(tytest_tymux_wait(_alive_done, ty, 5.0));
+ assert(!screen && !writable);
+
+ termsrv_reader_clear(&reader);
+ close(fd);
+ tytest_tymux_stop(ts);
+ return 0;
+}
+
+int
+tytest_tymux_live_pages_read_only(void)
+{
+ return tytest_tymux_live_run(_live_pages_read_only_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
index 7353a1b3..6dd6ddca 100644
--- a/src/bin/tytest_tymux.h
+++ b/src/bin/tytest_tymux.h
@@ -57,5 +57,6 @@ int tytest_tymux_live_fd_budget(void);
int tytest_tymux_live_fd_starved(void);
int tytest_tymux_live_page_async(void);
int tytest_tymux_live_paste(void);
+int tytest_tymux_live_pages_read_only(void);
#endif
diff --git a/tests/meson.build b/tests/meson.build
index 1532efb1..fb6b33b2 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -106,7 +106,8 @@ if tests
'tymux_live_clear', 'tymux_live_scrollback_config',
'tymux_live_scrollback_zero', 'tymux_live_fd_budget',
'tymux_live_fd_starved',
- 'tymux_live_page_async', 'tymux_live_paste'],
+ 'tymux_live_page_async', 'tymux_live_paste',
+ 'tymux_live_pages_read_only'],
depends: [tymuxd],
timeout: 120)
endif
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.