Skip to content

Commit f30d87b

Browse files
yonghua zhenggregkh
authored andcommitted
fs/proc/task_mmu.c: fix buffer overflow in add_page_map()
commit 8c82962 upstream. Recently we met quite a lot of random kernel panic issues after enabling CONFIG_PROC_PAGE_MONITOR. After debuggind we found this has something to do with following bug in pagemap: In struct pagemapread: struct pagemapread { int pos, len; pagemap_entry_t *buffer; bool v2; }; pos is number of PM_ENTRY_BYTES in buffer, but len is the size of buffer, it is a mistake to compare pos and len in add_page_map() for checking buffer is full or not, and this can lead to buffer overflow and random kernel panic issue. Correct len to be total number of PM_ENTRY_BYTES in buffer. [akpm@linux-foundation.org: document pagemapread.pos and .len units, fix PM_ENTRY_BYTES definition] Signed-off-by: Yonghua Zheng <younghua.zheng@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f6c19e2 commit f30d87b

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎fs/proc/task_mmu.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,14 +792,14 @@ typedef struct {
792792
} pagemap_entry_t;
793793

794794
struct pagemapread {
795-
int pos, len;
795+
int pos, len; /* units: PM_ENTRY_BYTES, not bytes */
796796
pagemap_entry_t *buffer;
797797
};
798798

799799
#define PAGEMAP_WALK_SIZE (PMD_SIZE)
800800
#define PAGEMAP_WALK_MASK (PMD_MASK)
801801

802-
#define PM_ENTRY_BYTES sizeof(u64)
802+
#define PM_ENTRY_BYTES sizeof(pagemap_entry_t)
803803
#define PM_STATUS_BITS 3
804804
#define PM_STATUS_OFFSET (64 - PM_STATUS_BITS)
805805
#define PM_STATUS_MASK (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
@@ -1038,8 +1038,8 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
10381038
if (!count)
10391039
goto out_task;
10401040

1041-
pm.len = PM_ENTRY_BYTES * (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1042-
pm.buffer = kmalloc(pm.len, GFP_TEMPORARY);
1041+
pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
1042+
pm.buffer = kmalloc(pm.len * PM_ENTRY_BYTES, GFP_TEMPORARY);
10431043
ret = -ENOMEM;
10441044
if (!pm.buffer)
10451045
goto out_task;

0 commit comments

Comments
 (0)