From 6856d7533b4cec8d35639578b02578f28f3699bc Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 5 Jul 2025 00:27:38 +0000 Subject: kernel: tmpfs: Prevent read() on empty files If we have never written to a tmpfs file, the buffer for it will have never been allocated and that would lead to NULL pointer issues such as system-wide crashes. Return zero bytes read if nothing was ever written. Signed-off-by: Ian Moffett --- sys/fs/tmpfs.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'sys') diff --git a/sys/fs/tmpfs.c b/sys/fs/tmpfs.c index 21dd7b5..6ae1c38 100644 --- a/sys/fs/tmpfs.c +++ b/sys/fs/tmpfs.c @@ -318,6 +318,15 @@ tmpfs_read(struct vnode *vp, struct sio_txn *sio) return -EIO; } + /* + * The node data is only allocated during writes, if + * we read this file before a write was ever done to it, + * np->data will be NULL. We must handle this. + */ + if (np->data == NULL) { + return 0; + } + /* Is this even a regular file? */ if (np->type != VREG) { return -EISDIR; -- cgit v1.2.3