[PATCH] Fix seq_read dead loop and trigger memory allocation failure.

From: Fengwei Yin
Date: Mon Apr 21 2014 - 10:04:54 EST


When dump /proc/xxx/maps, if d_path return error in seq_path, the
buffer will be exhaust and trigger dead loop in seq_read. Till
kmalloc fails with -ENOMEM.

Saving and restoring the m->count to avoid the dead loop in seq_read
if d_path return error.

Signed-off-by: Fengwei Yin <yfw.kernel@xxxxxxxxx>
---
fs/proc/task_mmu.c | 10 +++++++++-
fs/proc/task_nommu.c | 10 +++++++++-
2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 442177b..a080531 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -295,8 +295,16 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
* special [heap] marker for the heap:
*/
if (file) {
+ size_t sz;
seq_pad(m, ' ');
- seq_path(m, &file->f_path, "\n");
+ /* Save current count. Once seq_path return negtive value,
+ * we need to restore saved count. Otherwise, seq_path will
+ * exhaust the buffer and make seq_read dead loop till
+ * m->buff allocation failure.
+ */
+ sz = m->count;
+ if (seq_path(m, &file->f_path, "\n") < 0)
+ m->count = sz;
goto done;
}

diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
index 678455d..0d4d6e0 100644
--- a/fs/proc/task_nommu.c
+++ b/fs/proc/task_nommu.c
@@ -160,8 +160,16 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
MAJOR(dev), MINOR(dev), ino);

if (file) {
+ size_t sz;
seq_pad(m, ' ');
- seq_path(m, &file->f_path, "");
+ /* Save current count. Once seq_path return negtive value,
+ * we need to restore saved count. Otherwise, seq_path will
+ * exhaust the buffer and make seq_read dead loop till
+ * m->buff allocation failure.
+ */
+ sz = m->count;
+ if (seq_path(m, &file->f_path, "\n") < 0)
+ m->count = sz;
} else if (mm) {
pid_t tid = vm_is_stack(priv->task, vma, is_pid);

--
1.8.3.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/