Re: [PATCH v3] char: tty3270: fix a missing check on list iterator

Xiaomeng Tong
2022-03-28T10:27:45Z

On Mon, 28 Mar 2022 12:09:59 +0200, Jiri Slaby wrote:
> On 28. 03. 22, 11:35, Xiaomeng Tong wrote:
> > The bug is here:
> > if (s->len != flen) {
> >
> > The list iterator 's' will point to a bogus position containing
> > HEAD if the list is empty or no element is found.
>
> Could you also explain how that can happen?
>

When list_for_each_entry_* do not early exits (if the list is empty
or no break/goto/return hit inside the loop), it will set pos ('s' here)
with a bogus pointer that point to a invalid struct computed based
on &HEAD using container_of.

#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
!list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))

> > This case must
> > be checked before any use of the iterator, otherwise it may bpass
> > the 'if (s->len != flen) {' in theory iif s->len's value is flen,
>
> bpass + iif -- others already commented on that and you ignored them.
>

Thank you, i will correct it.

> > or/and lead to an invalid memory access.
> >
> > To fix this bug, use a new variable 'iter' as the list iterator,
> > while using the origin variable 's' as a dedicated pointer to
> > point to the found element. And if the list is empty or no element
> > is found, WARN_ON and return.
> >
> > Cc: stable@vger.kernel.org
> > Fixes: ^1da177e4c3f4 ("Linux-2.6.12-rc2")
>
> That's barely the commit introducing the behavior.
>

So just remove the Fixes tag? or something else? I find this commitID with
git blame.

--
Xiaomeng Tong