* [PATCH] fs/proc/kcore.c: remove check of list iterator against head past the loop body
@ 2022-03-31 22:37 Jakob Koschel
2022-03-31 23:48 ` Andrew Morton
0 siblings, 1 reply; 5+ messages in thread
From: Jakob Koschel @ 2022-03-31 22:37 UTC (permalink / raw)
To: Mike Rapoport
Cc: David Hildenbrand, Andrew Morton, Oscar Salvador, linux-kernel,
linux-fsdevel, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.,
Jakob Koschel
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].
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
---
fs/proc/kcore.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
index 982e694aae77..344edcb2addd 100644
--- a/fs/proc/kcore.c
+++ b/fs/proc/kcore.c
@@ -316,6 +316,7 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
size_t page_offline_frozen = 1;
size_t phdrs_len, notes_len;
struct kcore_list *m;
+ struct kcore_list *iter;
size_t tsz;
int nphdr;
unsigned long start;
@@ -479,10 +480,13 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
* the previous entry, search for a matching entry.
*/
if (!m || start < m->addr || start >= m->addr + m->size) {
- list_for_each_entry(m, &kclist_head, list) {
- if (start >= m->addr &&
- start < m->addr + m->size)
+ m = NULL;
+ list_for_each_entry(iter, &kclist_head, list) {
+ if (start >= iter->addr &&
+ start < iter->addr + iter->size) {
+ m = iter;
break;
+ }
}
}
@@ -492,12 +496,11 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
page_offline_freeze();
}
- if (&m->list == &kclist_head) {
+ if (!m) {
if (clear_user(buffer, tsz)) {
ret = -EFAULT;
goto out;
}
- m = NULL; /* skip the list anchor */
goto skip;
}
base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] fs/proc/kcore.c: remove check of list iterator against head past the loop body
2022-03-31 22:37 [PATCH] fs/proc/kcore.c: remove check of list iterator against head past the loop body Jakob Koschel
@ 2022-03-31 23:48 ` Andrew Morton
2022-04-01 7:19 ` Jakob Koschel
0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2022-03-31 23:48 UTC (permalink / raw)
To: Jakob Koschel
Cc: Mike Rapoport, David Hildenbrand, Oscar Salvador, linux-kernel,
linux-fsdevel, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.
On Fri, 1 Apr 2022 00:37:00 +0200 Jakob Koschel <jakobkoschel@gmail.com> wrote:
> 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].
>
> ...
>
Speaking of limiting scope...
--- a/fs/proc/kcore.c~fs-proc-kcorec-remove-check-of-list-iterator-against-head-past-the-loop-body-fix
+++ a/fs/proc/kcore.c
@@ -316,7 +316,6 @@ read_kcore(struct file *file, char __use
size_t page_offline_frozen = 1;
size_t phdrs_len, notes_len;
struct kcore_list *m;
- struct kcore_list *iter;
size_t tsz;
int nphdr;
unsigned long start;
@@ -480,6 +479,8 @@ read_kcore(struct file *file, char __use
* the previous entry, search for a matching entry.
*/
if (!m || start < m->addr || start >= m->addr + m->size) {
+ struct kcore_list *iter;
+
m = NULL;
list_for_each_entry(iter, &kclist_head, list) {
if (start >= iter->addr &&
_
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] fs/proc/kcore.c: remove check of list iterator against head past the loop body
2022-03-31 23:48 ` Andrew Morton
@ 2022-04-01 7:19 ` Jakob Koschel
2022-04-04 22:55 ` Andrew Morton
0 siblings, 1 reply; 5+ messages in thread
From: Jakob Koschel @ 2022-04-01 7:19 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, David Hildenbrand, Oscar Salvador, linux-kernel,
linux-fsdevel, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.
> On 1. Apr 2022, at 01:48, Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Fri, 1 Apr 2022 00:37:00 +0200 Jakob Koschel <jakobkoschel@gmail.com> wrote:
>
>> 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].
>>
>> ...
>>
>
> Speaking of limiting scope...
Fair point :-)
I see you have applied this already to the -mm tree. Shall I still move the iterator?
The hope is to remove the 'iter' variable altogether when there are no uses after
the loop anymore.
>
> --- a/fs/proc/kcore.c~fs-proc-kcorec-remove-check-of-list-iterator-against-head-past-the-loop-body-fix
> +++ a/fs/proc/kcore.c
> @@ -316,7 +316,6 @@ read_kcore(struct file *file, char __use
> size_t page_offline_frozen = 1;
> size_t phdrs_len, notes_len;
> struct kcore_list *m;
> - struct kcore_list *iter;
> size_t tsz;
> int nphdr;
> unsigned long start;
> @@ -480,6 +479,8 @@ read_kcore(struct file *file, char __use
> * the previous entry, search for a matching entry.
> */
> if (!m || start < m->addr || start >= m->addr + m->size) {
> + struct kcore_list *iter;
> +
> m = NULL;
> list_for_each_entry(iter, &kclist_head, list) {
> if (start >= iter->addr &&
> _
>
Jakob
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] fs/proc/kcore.c: remove check of list iterator against head past the loop body
2022-04-01 7:19 ` Jakob Koschel
@ 2022-04-04 22:55 ` Andrew Morton
2022-04-05 8:48 ` Jakob Koschel
0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2022-04-04 22:55 UTC (permalink / raw)
To: Jakob Koschel
Cc: Mike Rapoport, David Hildenbrand, Oscar Salvador, linux-kernel,
linux-fsdevel, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.
On Fri, 1 Apr 2022 09:19:57 +0200 Jakob Koschel <jakobkoschel@gmail.com> wrote:
> > Speaking of limiting scope...
>
> Fair point :-)
>
> I see you have applied this already to the -mm tree. Shall I still move the iterator?
> The hope is to remove the 'iter' variable altogether when there are no uses after
> the loop anymore.
I don't really understand the question.
My plan is to merge your patch with my fixlet immediately prior to
sending upstream.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] fs/proc/kcore.c: remove check of list iterator against head past the loop body
2022-04-04 22:55 ` Andrew Morton
@ 2022-04-05 8:48 ` Jakob Koschel
0 siblings, 0 replies; 5+ messages in thread
From: Jakob Koschel @ 2022-04-05 8:48 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, David Hildenbrand, Oscar Salvador, LKML,
linux-fsdevel, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
H.J.
> On 5. Apr 2022, at 00:55, Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Fri, 1 Apr 2022 09:19:57 +0200 Jakob Koschel <jakobkoschel@gmail.com> wrote:
>
>>> Speaking of limiting scope...
>>
>> Fair point :-)
>>
>> I see you have applied this already to the -mm tree. Shall I still move the iterator?
>> The hope is to remove the 'iter' variable altogether when there are no uses after
>> the loop anymore.
>
> I don't really understand the question.
Basically I was asking if I should send a v2 with the change you suggested.
>
> My plan is to merge your patch with my fixlet immediately prior to
> sending upstream.
ok, great. Even better, so ignore my question.
Thanks,
Jakob
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-04-05 12:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31 22:37 [PATCH] fs/proc/kcore.c: remove check of list iterator against head past the loop body Jakob Koschel
2022-03-31 23:48 ` Andrew Morton
2022-04-01 7:19 ` Jakob Koschel
2022-04-04 22:55 ` Andrew Morton
2022-04-05 8:48 ` Jakob Koschel
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.