public inbox for astral-devel@lists.astral-os.org
 help / color / mirror / Atom feed
* [PATCH 0/5] devfs: add anonymous register/remove
@ 2026-10-05 18:01 Matheus
  2026-10-05 18:01 ` [PATCH 1/5] statvfs: handle vnodes without a vfs Matheus
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Matheus @ 2026-10-05 18:01 UTC (permalink / raw)
  To: astral-devel

From: Matheus <matheusuky@gmail.com>

The current pty code uses a very hacky way of handling devfs
registration:

named register -> get device by name -> remove device

This is generally an unclean solution and temporarily leaves a device
name in the tree which any process could open() and interfere. 

Add anonymous operations which work on nodes that are unnamed and
directly return a pointer to the vnode. This still leaves a window which
could be used to access the vnode but it is much smaller and requires a
device node to already be in the filesystem (which needs root anyways).
Then, move the pty implementation to use these new functions.

Also fix a few related issues along the way:
- These new devfs vnodes are tecnically orphaned, so they don't have a
  valid vfs. Doing statvfs on them could crash the system.
- Fix a UAF on VOP_RELEASE, where the NULL could be set after the
  vfs_inactive happens but the lifetime of the object where NULL
  happened might be attached to the vnode lifetime.
- Fix another UAF on tty code: calling devfs_remove() in tty_unregister()
  might cause the tty to be free'd. This results in a possible UAF.

(this is also a test of the mailing list for multi-patch series and the
new @astral-os.org email)

Matheus (5):
  statvfs: handle vnodes without a vfs
  vfs: fix UAF on VOP_RELEASE
  tty: fix lifetime issue in tty_unregister()
  devfs: add anonymous variants to register/remove
  pty: use new anonymous devfs operations

 kernel-src/fs/devfs.c             | 47 ++++++++++++++++++++++++++++++-
 kernel-src/include/kernel/devfs.h |  7 ++++-
 kernel-src/include/kernel/vfs.h   |  9 +++---
 kernel-src/io/pty.c               | 13 +++------
 kernel-src/io/tty.c               |  3 ++
 kernel-src/sys/syscalls/statvfs.c | 10 +++++++
 6 files changed, 74 insertions(+), 15 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] statvfs: handle vnodes without a vfs
  2026-10-05 18:01 [PATCH 0/5] devfs: add anonymous register/remove Matheus
@ 2026-10-05 18:01 ` Matheus
  2026-10-05 18:01 ` [PATCH 2/5] vfs: fix UAF on VOP_RELEASE Matheus
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Matheus @ 2026-10-05 18:01 UTC (permalink / raw)
  To: astral-devel

From: Matheus <matheusuky@gmail.com>

---
 kernel-src/sys/syscalls/statvfs.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel-src/sys/syscalls/statvfs.c b/kernel-src/sys/syscalls/statvfs.c
index efd25c6a..3f9797fb 100644
--- a/kernel-src/sys/syscalls/statvfs.c
+++ b/kernel-src/sys/syscalls/statvfs.c
@@ -36,6 +36,11 @@ syscallret_t syscall_fstatvfs(context_t *ctx, int fd, statvfs_t *ustatvfs) {
 		return ret;
 	}
 
+	if (file->vnode->vfs == NULL) {
+		ret.errno = ENODEV;
+		goto cleanup;
+	}
+
 	fsattr_t buf;
 	ret.errno = VFS_STATFS(file->vnode->vfs, &buf);
 	if (ret.errno)
@@ -98,6 +103,11 @@ syscallret_t syscall_fstatvfsat(context_t *ctx, int dirfd, char *upath, stat_t *
 	// locked by vfs_lookup
 	VOP_UNLOCK(node);
 
+	if (node->vfs == NULL) {
+		ret.errno = ENODEV;
+		goto cleanup;
+	}
+
 	fsattr_t buf;
 	ret.errno = VFS_STATFS(node->vfs, &buf);
 	if (ret.errno)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/5] vfs: fix UAF on VOP_RELEASE
  2026-10-05 18:01 [PATCH 0/5] devfs: add anonymous register/remove Matheus
  2026-10-05 18:01 ` [PATCH 1/5] statvfs: handle vnodes without a vfs Matheus
@ 2026-10-05 18:01 ` Matheus
  2026-10-05 18:01 ` [PATCH 3/5] tty: fix lifetime issue in tty_unregister() Matheus
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Matheus @ 2026-10-05 18:01 UTC (permalink / raw)
  To: astral-devel

From: Matheus <matheusuky@gmail.com>

---
 kernel-src/include/kernel/vfs.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/kernel-src/include/kernel/vfs.h b/kernel-src/include/kernel/vfs.h
index cbb16098..04661cbd 100644
--- a/kernel-src/include/kernel/vfs.h
+++ b/kernel-src/include/kernel/vfs.h
@@ -242,12 +242,13 @@ typedef struct vops_t {
 #define VOP_SYNC(v) (v)->ops->sync(v)
 #define VOP_ADVLOCK(v, o, a) (v)->ops->advlock(v, o, a)
 #define VOP_HOLD(v) __atomic_add_fetch(&(v)->refcount, 1, __ATOMIC_SEQ_CST)
-#define VOP_RELEASE(v) {\
-		if (__atomic_sub_fetch(&(v)->refcount, 1, __ATOMIC_SEQ_CST) == 0) {\
-			vfs_inactive(v); \
+#define VOP_RELEASE(v) do { \
+		vnode_t *vop_release_node = (v); \
+		if (__atomic_sub_fetch(&vop_release_node->refcount, 1, __ATOMIC_SEQ_CST) == 0) { \
 			(v) = NULL; \
+			vfs_inactive(vop_release_node); \
 		} \
-	}
+	} while (0)
 
 #define VFS_STUB(x) static int x () { return ENODEV; }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/5] tty: fix lifetime issue in tty_unregister()
  2026-10-05 18:01 [PATCH 0/5] devfs: add anonymous register/remove Matheus
  2026-10-05 18:01 ` [PATCH 1/5] statvfs: handle vnodes without a vfs Matheus
  2026-10-05 18:01 ` [PATCH 2/5] vfs: fix UAF on VOP_RELEASE Matheus
@ 2026-10-05 18:01 ` Matheus
  2026-10-05 18:01 ` [PATCH 4/5] devfs: add anonymous variants to register/remove Matheus
  2026-10-05 18:01 ` [PATCH 5/5] pty: use new anonymous devfs operations Matheus
  4 siblings, 0 replies; 6+ messages in thread
From: Matheus @ 2026-10-05 18:01 UTC (permalink / raw)
  To: astral-devel

From: Matheus <matheusuky@gmail.com>

The tty_unregister code removes the tty's vnode from the devfs mappings.
This causes a reference to be dropped, which can then cause the tty to
be freed and the remaining code in tty_unregister does a UAF.

Fix this by holding a reference across the unregister operation.
---
 kernel-src/io/tty.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel-src/io/tty.c b/kernel-src/io/tty.c
index b316bd7d..c21e2ca3 100644
--- a/kernel-src/io/tty.c
+++ b/kernel-src/io/tty.c
@@ -581,6 +581,8 @@ tty_t *tty_create(char *name, ttydevicewritefn_t writefn, ttyinactivefn_t inacti
 }
 
 void tty_unregister(tty_t *tty) {
+	vnode_t *mastervnode = tty->mastervnode;
+	VOP_HOLD(mastervnode);
 	devfs_remove(tty->name, DEV_MAJOR_TTY, tty->minor);
 
 	proc_t *foreground = getforeground(tty);
@@ -588,6 +590,7 @@ void tty_unregister(tty_t *tty) {
 		jobctl_signal(foreground, SIGHUP, NULL);
 		PROC_RELEASE(foreground);
 	}
+	VOP_RELEASE(mastervnode);
 }
 
 void tty_release(tty_t *tty) {
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 4/5] devfs: add anonymous variants to register/remove
  2026-10-05 18:01 [PATCH 0/5] devfs: add anonymous register/remove Matheus
                   ` (2 preceding siblings ...)
  2026-10-05 18:01 ` [PATCH 3/5] tty: fix lifetime issue in tty_unregister() Matheus
@ 2026-10-05 18:01 ` Matheus
  2026-10-05 18:01 ` [PATCH 5/5] pty: use new anonymous devfs operations Matheus
  4 siblings, 0 replies; 6+ messages in thread
From: Matheus @ 2026-10-05 18:01 UTC (permalink / raw)
  To: astral-devel

From: Matheus <matheusuky@gmail.com>

The current devfs code requires registered devices to have a name on
creation. This makes temporary/hidden devices like pty master ends
appear for a split second on the devfs tree.

Add a way of registering/removing devfs nodes which do not have a name
on the tree.
---
 kernel-src/fs/devfs.c             | 47 ++++++++++++++++++++++++++++++-
 kernel-src/include/kernel/devfs.h |  7 ++++-
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/kernel-src/fs/devfs.c b/kernel-src/fs/devfs.c
index e71dd3f3..d172aa10 100644
--- a/kernel-src/fs/devfs.c
+++ b/kernel-src/fs/devfs.c
@@ -527,8 +527,41 @@ int devfs_getbyname(char *name, vnode_t **ret) {
 	cleanup:
 	return error;
 }
+
+int devfs_register_anonymous(devops_t *devops, int type, int major, int minor, mode_t mode,
+			     cred_t *cred, vnode_t **out) {
+	__assert(type == V_TYPE_CHDEV || type == V_TYPE_BLKDEV);
+	devnode_t *master = slab_allocate(nodecache);
+	if (master == NULL)
+		return ENOMEM;
+
+	master->vnode.type = type;
+	master->devops = devops;
+	master->attr.mode = mode;
+	master->attr.rdevmajor = major;
+	master->attr.rdevminor = minor;
+	master->attr.uid = cred == NULL ? 0 : cred->euid;
+	master->attr.gid = cred == NULL ? 0 : cred->egid;
+
+	int key[2] = {major, minor};
+
+	MUTEX_ACQUIRE(&tablelock);
+	int error = hashtable_set(&devtable, master, key, sizeof(key), true);
+	MUTEX_RELEASE(&tablelock);
+	if (error) {
+		destroy_node(master);
+		return error;
+	}
+
+	VOP_HOLD(&master->vnode);
+	*out = &master->vnode;
+
+	return 0;
+}
+
 // register new device 
-int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode, cred_t *cred) {
+int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode,
+		   cred_t *cred) {
 	__assert(type == V_TYPE_CHDEV || type == V_TYPE_BLKDEV);
 	devnode_t *master = slab_allocate(nodecache);
 	if (master == NULL)
@@ -613,6 +646,18 @@ int devfs_getnode(vnode_t *physical, int major, int minor, vnode_t **node) {
 	return 0;
 }
 
+void devfs_remove_anonymous(int major, int minor) {
+	int key[2] = {major, minor};
+	void *master;
+	MUTEX_ACQUIRE(&tablelock);
+	__assert(hashtable_get(&devtable, &master, key, sizeof(key)) == 0);
+	__assert(hashtable_remove(&devtable, key, sizeof(key)) == 0);
+	MUTEX_RELEASE(&tablelock);
+
+	vnode_t *vnode = master;
+	VOP_RELEASE(vnode);
+}
+
 void devfs_remove(char *name, int major, int minor) {
 	int key[2] = {major, minor};
 	// remove devtable reference
diff --git a/kernel-src/include/kernel/devfs.h b/kernel-src/include/kernel/devfs.h
index d83b9dee..cfcb2068 100644
--- a/kernel-src/include/kernel/devfs.h
+++ b/kernel-src/include/kernel/devfs.h
@@ -23,6 +23,7 @@
 #define DEV_MAJOR_PROF 14
 #define DEV_MAJOR_INPUT 15
 #define DEV_MAJOR_AUDIO 16
+#define DEV_MAJOR_KCOV 17
 
 typedef struct {
 	int (*open)(int minor, vnode_t **vnode, int flags);
@@ -49,9 +50,13 @@ typedef struct devnode_t {
 
 void devfs_init();
 int devfs_getnode(vnode_t *physical, int major, int minor, vnode_t **node);
-int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode, cred_t *cred);
+int devfs_register_anonymous(devops_t *devops, int type, int major, int minor, mode_t mode,
+			     cred_t *cred, vnode_t **out);
+int devfs_register(devops_t *devops, char *name, int type, int major, int minor, mode_t mode,
+		   cred_t *cred);
 int devfs_getbyname(char *name, vnode_t **ret);
 int devfs_createdir(char *name);
+void devfs_remove_anonymous(int major, int minor);
 void devfs_remove(char *name, int major, int minor);
 
 #endif
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 5/5] pty: use new anonymous devfs operations
  2026-10-05 18:01 [PATCH 0/5] devfs: add anonymous register/remove Matheus
                   ` (3 preceding siblings ...)
  2026-10-05 18:01 ` [PATCH 4/5] devfs: add anonymous variants to register/remove Matheus
@ 2026-10-05 18:01 ` Matheus
  4 siblings, 0 replies; 6+ messages in thread
From: Matheus @ 2026-10-05 18:01 UTC (permalink / raw)
  To: astral-devel

From: Matheus <matheusuky@gmail.com>

Switch the old hacky way of registering a new pty master into using the
new anonymous api.
---
 kernel-src/io/pty.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/kernel-src/io/pty.c b/kernel-src/io/pty.c
index faa0f260..d9194bd8 100644
--- a/kernel-src/io/pty.c
+++ b/kernel-src/io/pty.c
@@ -261,25 +261,20 @@ static int open(int oldminor, vnode_t **vnode, int flags) {
 
 	pty->minor = newminor;
 
-	char tmpname[20];
-	snprintf(tmpname, 20, ".ptmx%d", newminor);
-
-	// register a master device and immediatelly remove it from the filesystem and tables
-	// while holding the refcount
-	int error = devfs_register(&devops, tmpname, V_TYPE_CHDEV, DEV_MAJOR_PTY, newminor, 0, NULL);
+	int error = devfs_register_anonymous(&devops, V_TYPE_CHDEV, DEV_MAJOR_PTY, newminor,
+					     0, NULL, &pty->mastervnode);
 	if (error) {
 		freeptyminor(newminor);
 		freepty(pty);
 		return error;
 	}
 
-	__assert(devfs_getbyname(tmpname, &pty->mastervnode) == 0);
-
-	devfs_remove(tmpname, DEV_MAJOR_PTY, newminor);
+	devfs_remove_anonymous(DEV_MAJOR_PTY, newminor);
 
 	// we are now the only ones to hold the master vnode and its inaccessible from devfs
 	// create a pairing slave device
 
+	char tmpname[20];
 	snprintf(tmpname, 20, "pts/%d", newminor);
 	pty->tty = tty_create(tmpname, writetopty, ttyinactive, NULL, hup_check, pty);
 	if (pty->tty == NULL) {
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-10-05 18:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-05 18:01 [PATCH 0/5] devfs: add anonymous register/remove Matheus
2026-10-05 18:01 ` [PATCH 1/5] statvfs: handle vnodes without a vfs Matheus
2026-10-05 18:01 ` [PATCH 2/5] vfs: fix UAF on VOP_RELEASE Matheus
2026-10-05 18:01 ` [PATCH 3/5] tty: fix lifetime issue in tty_unregister() Matheus
2026-10-05 18:01 ` [PATCH 4/5] devfs: add anonymous variants to register/remove Matheus
2026-10-05 18:01 ` [PATCH 5/5] pty: use new anonymous devfs operations Matheus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox