Skip to contentSkip to conversation

fs/namespace: reject bufsize < sizeof(struct statmount) in prepare_kstatmount()

PATCH In review Hui Peng 2 participants 2 messages Sep 19 – Oct 21 VFS

lore
Share & tools
lore

Hui Peng

author
#0 lore raw

PATCH 1/1 fs/namespace: reject bufsize < sizeof(struct statmount) in prepare_kstatmount()

In prepare_kstatmount(), kstatmount.buf_size is initialized from the caller's bufsize argument without rejecting buffers smaller than sizeof(struct statmount), allowing string offset calculations relative to sizeof(struct statmount) to underflow or write past the user buffer. Reject bufsize < sizeof(struct statmount) in prepare_kstatmount().

Fixes 68385d77c05b ("statmount: simplify string option retrieval")
Assisted-by LLM

---

fs/namespace.c +1 -1
index ae5dc64f8b45..7f7905286fa6 100644
@@ -5882,7 +5882,7 @@ static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
 	ks->bufsize = bufsize;
 
 	if (ks->mask & STATMOUNT_STRING_REQ) {
-		if (bufsize == sizeof(ks->sm))
+		if (bufsize <= sizeof(ks->sm))
 			return -EOVERFLOW;
 
 		ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT);
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Hui Peng

In prepare_kstatmount(), kstatmount.buf_size is initialized from the caller's bufsize argument without rejecting buffers smaller than sizeof(struct statmount), allowing string offset calculations relative to sizeof(struct statmount) to underflow or write past the user buffer. Reject bufsize < sizeof(struct statmount) in prepare_kstatmount().

Fixes 68385d77c05b ("statmount: simplify string option retrieval")
Assisted-by LLM

Yeah, indeed it looks like we could overrun the user buffer. Feel free to add:

Honza

Quoted 14 lines

---

fs/namespace.c +1 -1
index ae5dc64f8b45..7f7905286fa6 100644
@@ -5882,7 +5882,7 @@ static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
 	ks->bufsize = bufsize;
 
 	if (ks->mask & STATMOUNT_STRING_REQ) {
-		if (bufsize == sizeof(ks->sm))
+		if (bufsize <= sizeof(ks->sm))
 			return -EOVERFLOW;
 
 		ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT);
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR