Skip to content

Commit 1745778

Browse files
lorenzo-stoakesakpm00
authored andcommitted
fs/proc/kcore: reinstate bounce buffer for KCORE_TEXT regions
Some architectures do not populate the entire range categorised by KCORE_TEXT, so we must ensure that the kernel address we read from is valid. Unfortunately there is no solution currently available to do so with a purely iterator solution so reinstate the bounce buffer in this instance so we can use copy_from_kernel_nofault() in order to avoid page faults when regions are unmapped. This change partly reverts commit 2e1c017 ("fs/proc/kcore: avoid bounce buffer for ktext data"), reinstating the bounce buffer, but adapts the code to continue to use an iterator. [lstoakes@gmail.com: correct comment to be strictly correct about reasoning] Link: https://lkml.kernel.org/r/525a3f14-74fa-4c22-9fca-9dab4de8a0c3@lucifer.local Link: https://lkml.kernel.org/r/20230731215021.70911-1-lstoakes@gmail.com Fixes: 2e1c017 ("fs/proc/kcore: avoid bounce buffer for ktext data") Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com> Reported-by: Jiri Olsa <olsajiri@gmail.com> Closes: https://lore.kernel.org/all/ZHc2fm+9daF6cgCE@krava Tested-by: Jiri Olsa <jolsa@kernel.org> Tested-by: Will Deacon <will@kernel.org> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Baoquan He <bhe@redhat.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: David Hildenbrand <david@redhat.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: Kefeng Wang <wangkefeng.wang@huawei.com> Cc: Liu Shixin <liushixin2@huawei.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Thorsten Leemhuis <regressions@leemhuis.info> Cc: Uladzislau Rezki (Sony) <urezki@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent d1ef9db commit 1745778

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

fs/proc/kcore.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ static void append_kcore_note(char *notes, size_t *i, const char *name,
309309

310310
static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter)
311311
{
312+
struct file *file = iocb->ki_filp;
313+
char *buf = file->private_data;
312314
loff_t *fpos = &iocb->ki_pos;
313315
size_t phdrs_offset, notes_offset, data_offset;
314316
size_t page_offline_frozen = 1;
@@ -555,10 +557,21 @@ static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter)
555557
case KCORE_VMEMMAP:
556558
case KCORE_TEXT:
557559
/*
558-
* We use _copy_to_iter() to bypass usermode hardening
559-
* which would otherwise prevent this operation.
560+
* Sadly we must use a bounce buffer here to be able to
561+
* make use of copy_from_kernel_nofault(), as these
562+
* memory regions might not always be mapped on all
563+
* architectures.
560564
*/
561-
if (_copy_to_iter((char *)start, tsz, iter) != tsz) {
565+
if (copy_from_kernel_nofault(buf, (void *)start, tsz)) {
566+
if (iov_iter_zero(tsz, iter) != tsz) {
567+
ret = -EFAULT;
568+
goto out;
569+
}
570+
/*
571+
* We know the bounce buffer is safe to copy from, so
572+
* use _copy_to_iter() directly.
573+
*/
574+
} else if (_copy_to_iter(buf, tsz, iter) != tsz) {
562575
ret = -EFAULT;
563576
goto out;
564577
}
@@ -595,6 +608,10 @@ static int open_kcore(struct inode *inode, struct file *filp)
595608
if (ret)
596609
return ret;
597610

611+
filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
612+
if (!filp->private_data)
613+
return -ENOMEM;
614+
598615
if (kcore_need_update)
599616
kcore_update_ram();
600617
if (i_size_read(inode) != proc_root_kcore->size) {
@@ -605,9 +622,16 @@ static int open_kcore(struct inode *inode, struct file *filp)
605622
return 0;
606623
}
607624

625+
static int release_kcore(struct inode *inode, struct file *file)
626+
{
627+
kfree(file->private_data);
628+
return 0;
629+
}
630+
608631
static const struct proc_ops kcore_proc_ops = {
609632
.proc_read_iter = read_kcore_iter,
610633
.proc_open = open_kcore,
634+
.proc_release = release_kcore,
611635
.proc_lseek = default_llseek,
612636
};
613637

0 commit comments

Comments
 (0)