char: tty3270: fix a missing check on list iterator
Subsystems (2)
PATCH v3 1/1 char: tty3270: fix a missing check on list iterator
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. 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, 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.
---
changes since v2:
- WARN_ON and return (Sven Schnelle)
changes since v1:
- reallocate s when s == NULL (Sven Schnelle)
v1:https://lore.kernel.org/lkml/[email protected]/ v2:https://lore.kernel.org/lkml/[email protected]/
--- drivers/s390/char/tty3270.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/s390/char/tty3270.c +8 -3
@@ -1109,9 +1109,9 @@ static void tty3270_put_character(struct tty3270 *tp, char ch)static void tty3270_convert_line (struct tty3270 *tp,int line_nr) { +struct string *s =NULL , *n, *iter;struct tty3270_line *line;struct tty3270_cell *cell; -struct string *s, *n;unsigned char highlight;unsigned char f_color;char *cp;
@@ -1142,9 +1142,14 @@ tty3270_convert_line(struct tty3270 *tp, int line_nr)/* Find the line in the list. */ i = tp->view.rows -2 - line_nr; -list_for_each_entry_reverse (s, &tp->lines, list) -if (--i <=0 ) +list_for_each_entry_reverse (iter, &tp->lines, list) +if (--i <=0 ) { + s = iter;break ; + } + +if (WARN_ON (!s)) +return ;/* * Check if the line needs to get reallocated. */
Signature · 1 line
2.17.1
Quoted 5 lines · Xiaomeng Tong
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?
Quoted 3 lines
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.
Quoted 9 lines
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_ONand return.Fixes ^1da177e4c3f4 ("Linux-2.6.12-rc2")
That's barely the commit introducing the behavior.
Quoted 37 lines
Signed-off-by Xiaomeng Tong <[email protected]>---
changes since v2:
-WARN_ONand return (Sven Schnelle)changes since v1:
- reallocate s when s == NULL (Sven Schnelle)v1:https://lore.kernel.org/lkml/[email protected]/ v2:https://lore.kernel.org/lkml/[email protected]/
--- drivers/s390/char/tty3270.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/s390/char/tty3270.c +2
-3@@ -1109,9 +1109,9 @@ static void tty3270_put_character(struct tty3270 *tp, char ch)static void tty3270_convert_line (struct tty3270 *tp,int line_nr) { +struct string *s =NULL , *n, *iter;struct tty3270_line *line;struct tty3270_cell *cell; -struct string *s, *n;unsigned char highlight;unsigned char f_color;char *cp;@@ -1142,9 +1142,14 @@ tty3270_convert_line(struct tty3270 *tp, int line_nr)/* Find the line in the list. */ i = tp->view.rows -2 - line_nr; -list_for_each_entry_reverse (s, &tp->lines, list) -if (--i <=0 ) +list_for_each_entry_reverse (iter, &tp->lines, list)
drivers/s390/char/tty3270.c tty3270_convert_line(struct tty3270 *tp, int line_nr) · line 1155
Quoted 10 lines
drivers/s390/char/tty3270.c +6
-0+if (--i <=0 ) { + s = iter;break ; + } + +if (WARN_ON (!s)) +return ;/* * Check if the line needs to get reallocated. */
thanks,
Signature · 2 lines
js suse labs
Sven Schnelle
reviewerQuoted 5 lines · Jiri Slaby
Quoted 2 lines
Fixes ^1da177e4c3f4 ("Linux-2.6.12-rc2")That's barely the commit introducing the behavior.
Well, that code was introduced way before linux switch to git - not sure whether it makes sense to provide a Fixes: header in that case.
Quoted 9 lines · Jiri Slaby
Quoted 5 lines · Xiaomeng Tong
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))
Quoted 6 lines
Quoted 3 lines
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.
Quoted 7 lines
Quoted 7 lines
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_ONand return.
Quoted 5 lines
Quoted 2 lines
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
Quoted 16 lines · Xiaomeng Tong
Quoted 9 lines · Jiri Slaby
Quoted 5 lines · Xiaomeng Tong
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.
Quoted 4 lines
#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))No, I didn't mean what happens on that site on the code level. I think everyone understands that. Instead, I meant: what circumstances lead to this _situation_ in reality?
thanks,
Signature · 2 lines
js suse labs