Skip to content

Commit f5beeb1

Browse files
olsajiritorvalds
authored andcommitted
fs/proc/kcore.c: Make bounce buffer global for read
Next patch adds bounce buffer for ktext area, so it's convenient to have single bounce buffer for both vmalloc/module and ktext cases. Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Acked-by: Kees Cook <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d2ffb01 commit f5beeb1

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

fs/proc/kcore.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
430430
static ssize_t
431431
read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
432432
{
433+
char *buf = file->private_data;
433434
ssize_t acc = 0;
434435
size_t size, tsz;
435436
size_t elf_buflen;
@@ -500,18 +501,10 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
500501
if (clear_user(buffer, tsz))
501502
return -EFAULT;
502503
} else if (is_vmalloc_or_module_addr((void *)start)) {
503-
char * elf_buf;
504-
505-
elf_buf = kzalloc(tsz, GFP_KERNEL);
506-
if (!elf_buf)
507-
return -ENOMEM;
508-
vread(elf_buf, (char *)start, tsz);
504+
vread(buf, (char *)start, tsz);
509505
/* we have to zero-fill user buffer even if no read */
510-
if (copy_to_user(buffer, elf_buf, tsz)) {
511-
kfree(elf_buf);
506+
if (copy_to_user(buffer, buf, tsz))
512507
return -EFAULT;
513-
}
514-
kfree(elf_buf);
515508
} else {
516509
if (kern_addr_valid(start)) {
517510
unsigned long n;
@@ -549,6 +542,11 @@ static int open_kcore(struct inode *inode, struct file *filp)
549542
{
550543
if (!capable(CAP_SYS_RAWIO))
551544
return -EPERM;
545+
546+
filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
547+
if (!filp->private_data)
548+
return -ENOMEM;
549+
552550
if (kcore_need_update)
553551
kcore_update_ram();
554552
if (i_size_read(inode) != proc_root_kcore->size) {
@@ -559,10 +557,16 @@ static int open_kcore(struct inode *inode, struct file *filp)
559557
return 0;
560558
}
561559

560+
static int release_kcore(struct inode *inode, struct file *file)
561+
{
562+
kfree(file->private_data);
563+
return 0;
564+
}
562565

563566
static const struct file_operations proc_kcore_operations = {
564567
.read = read_kcore,
565568
.open = open_kcore,
569+
.release = release_kcore,
566570
.llseek = default_llseek,
567571
};
568572

0 commit comments

Comments
 (0)