Skip to content

Commit 04d168c

Browse files
Jakob-Koschelakpm00
authored andcommitted
fs/proc/kcore.c: remove check of list iterator against head past the loop body
When list_for_each_entry() completes the iteration over the whole list without breaking the loop, the iterator value will be a bogus pointer computed based on the head element. While it is safe to use the pointer to determine if it was computed based on the head element, either with list_entry_is_head() or &pos->member == head, using the iterator variable after the loop should be avoided. In preparation to limit the scope of a list iterator to the list traversal loop, use a dedicated pointer to point to the found element [1]. [akpm@linux-foundation.org: reduce scope of `iter'] Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1] Link: https://lkml.kernel.org/r/20220331223700.902556-1-jakobkoschel@gmail.com Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: David Hildenbrand <david@redhat.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: "Brian Johannesmeyer" <bjohannesmeyer@gmail.com> Cc: Cristiano Giuffrida <c.giuffrida@vu.nl> Cc: "Bos, H.J." <h.j.bos@vu.nl> Cc: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent f1e75d1 commit 04d168c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

fs/proc/kcore.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,15 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
479479
* the previous entry, search for a matching entry.
480480
*/
481481
if (!m || start < m->addr || start >= m->addr + m->size) {
482-
list_for_each_entry(m, &kclist_head, list) {
483-
if (start >= m->addr &&
484-
start < m->addr + m->size)
482+
struct kcore_list *iter;
483+
484+
m = NULL;
485+
list_for_each_entry(iter, &kclist_head, list) {
486+
if (start >= iter->addr &&
487+
start < iter->addr + iter->size) {
488+
m = iter;
485489
break;
490+
}
486491
}
487492
}
488493

@@ -492,12 +497,11 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
492497
page_offline_freeze();
493498
}
494499

495-
if (&m->list == &kclist_head) {
500+
if (!m) {
496501
if (clear_user(buffer, tsz)) {
497502
ret = -EFAULT;
498503
goto out;
499504
}
500-
m = NULL; /* skip the list anchor */
501505
goto skip;
502506
}
503507

0 commit comments

Comments
 (0)