Skip to contentSkip to conversation

p9_client_write doesn't check for negative "written" from server

DISCUSSION [email protected] 2 participants 2 messages Dec 17, 2024 – Dec 18, 2024 9P file system

lore
Share & tools
lore
#0 lore raw

If the kernel's 9p client sends a write request, and the server returns a successful Rwrite reply, but with a negative "written" value, then this line towards the end of p9_client_write():

                iov_iter_revert(from, count - written - iov_iter_count(from));

can pass an unroll argument to iov_iter_revert() that's larger than the number of bytes in the iov. This causes iov_iter_folioq_revert() to try to back up beyond the beginning of the iov data, and this code

                if (slot == 0) { 
                        folioq = folioq->prev;
                        slot = folioq_nr_slots(folioq);

tries to dereference a NULL folioq, since folioq->prev is NULL.

I've attached a demo.

# cc 9p2a.c
# ./a.out
...
BUG: kernel NULL pointer dereference, address: 000000000000011e
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
PGD 0 P4D 0
Oops: Oops: 0000 [#1] SMP DEBUG_PAGEALLOC PTI
CPU: 1 UID: 0 PID: 1613 Comm: sh Not tainted 6.13.0-rc3-00017-gf44d154d6e3d #12
Hardware name: FreeBSD BHYVE/BHYVE, BIOS 14.0 10/17/2021
RIP: 0010:iov_iter_revert+0xc9/0x150
Code: cc cc cc 48 29 f1 48 89 4f 08 c3 cc cc cc cc 48 c7 47 08 00 00 00 00 0f b6
 57 20 bf 00 10 00 00 eb 1f 83 ea 01 89 d1 49 89 f9 <41> 0f b6 8c 08 00 01 00 00
 49 d3 e1 4c 89 c9 49 39 f1 73 15 4c 29
RSP: 0018:ffffc900020dfae8 EFLAGS: 00010246
Call Trace:
 <TASK>
 ? __die+0x1e/0x60
 ? page_fault_oops+0x157/0x450
 ? check_object+0xc2/0x430
 ? exc_page_fault+0x66/0x140
 ? asm_exc_page_fault+0x26/0x30
 ? entry_SYSCALL_64_after_hwframe+0x76/0x7e
 ? iov_iter_revert+0xc9/0x150
 p9_client_write+0x115/0x250
 v9fs_issue_write+0x38/0x70
 netfs_advance_write+0xc6/0x2a0
 netfs_write_folio+0x2e7/0x9a0
 netfs_writepages+0xd7/0x3e0
 ? filemap_dirty_folio+0x5b/0x70
 do_writepages+0xc6/0x240
 __filemap_fdatawrite_range+0x9a/0xb0
 v9fs_dir_release+0x153/0x180
 ? task_work_add+0x71/0xe0
 __fput+0xe8/0x290
 task_work_run+0x57/0xa0
 syscall_exit_to_user_mode+0x105/0x110
 do_syscall_64+0x4b/0xd0
 entry_SYSCALL_64_after_hwframe+0x76/0x7e
RIP: 0033:0x7fdfc55fe96b

Robert Morris
[email protected]

#1 lore raw

Hi,

[email protected] wrote on Tue, Dec 17, 2024 at 10:17:11AM -0500:

Quoted 8 lines

If the kernel's 9p client sends a write request, and the server returns a successful Rwrite reply, but with a negative "written" value, then this line towards the end of p9_client_write():

                iov_iter_revert(from, count - written - iov_iter_count(from));

can pass an unroll argument to iov_iter_revert() that's larger than the number of bytes in the iov

Thanks for the report, I'm surprised this didn't come up sooner through syzbot...

That is correct, written (and simlarily received in p9_client_read_once) should be made unsigned.
They're actually compared with 'rsize' which is an int but is set from fid->iounit which is a u32, so just making these three variables u32 will sort the issue.

The patch is trivial so would you like to send it?
If you don't want to (or there is no reply in a while) then I'll send a patch this weekend crediting you as reported-by.

Signature · 1 line
Dominique