Skip to contentSkip to conversation

char: tty3270: fix a missing check on list iterator

PATCH Superseded Xiaomeng Tong 2 participants 3 messages Mar 28, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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, reallocate s.

Fixes ^1da177e4c3f4 ("Linux-2.6.12-rc2")

---

changes since v1:
- reallocate s when s == NULL (Sven Schnelle)

v1:https://lore.kernel.org/lkml/[email protected]/

---
 drivers/s390/char/tty3270.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/s390/char/tty3270.c +11 -4
index 5c83f71c1d0e..719e04dff63e 100644
@@ -1111,7 +1111,7 @@ tty3270_convert_line(struct tty3270 *tp, int line_nr)
 {
 	struct tty3270_line *line;
 	struct tty3270_cell *cell;
-	struct string *s, *n;
+	struct string *s = NULL, *n, *iter;
 	unsigned char highlight;
 	unsigned char f_color;
 	char *cp;
@@ -1142,13 +1142,20 @@ 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;
+		}
 	/*
 	 * Check if the line needs to get reallocated.
 	 */
-	if (s->len != flen) {
+	if (!s) {
+		/* Reallocate string. */
+		n = tty3270_alloc_string(tp, flen);
+		list_add(&n->list, &tp->lines);
+		s = n;
+	} else if (s->len != flen) {
 		/* Reallocate string. */
 		n = tty3270_alloc_string(tp, flen);
 		list_add(&n->list, &s->list);
Signature · 1 line
2.17.1
reviewer
#1 lore raw

drivers/s390/char/tty3270.c tty3270_convert_line(struct tty3270 *tp, int line_nr) · line 1114

Quoted 8 lines · Xiaomeng Tong
drivers/s390/char/tty3270.c +1 -1
@@ -1111,7 +1111,7 @@ tty3270_convert_line(struct tty3270 *tp, int line_nr)
 {
 	struct tty3270_line *line;
 	struct tty3270_cell *cell;
-	struct string *s, *n;
+	struct string *s = NULL, *n, *iter;

Please keep reverse XMAS-tree layout.

Quoted 17 lines
drivers/s390/char/tty3270.c +4 -2
 	unsigned char highlight;
 	unsigned char f_color;
 	char *cp;
@@ -1142,13 +1142,20 @@ 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;
+		}
 	/*
 	 * Check if the line needs to get reallocated.
 	 */

drivers/s390/char/tty3270.c tty3270_convert_line(struct tty3270 *tp, int line_nr) · line 1161

Quoted 10 lines
drivers/s390/char/tty3270.c +6 -1
-	if (s->len != flen) {
+	if (!s) {
+		/* Reallocate string. */
+		n = tty3270_alloc_string(tp, flen);
+		list_add(&n->list, &tp->lines);
+		s = n;
+	} else if (s->len != flen) {
 		/* Reallocate string. */
 		n = tty3270_alloc_string(tp, flen);
 		list_add(&n->list, &s->list);

I should have written that in my first reply, but s == NULL means the given line number couldn't be found in the list of lines. This is a serious error and should be warned about. So maybe something like:

if (WARN_ON(!s))

	return;

But allocating a new empty line in that case is certainly wrong.

Thanks
Sven

#2 lore raw
Quoted 8 lines

I should have written that in my first reply, but s == NULL means the given line number couldn't be found in the list of lines. This is a serious error and should be warned about. So maybe something like:

if (WARN_ON(!s))

	return;

But allocating a new empty line in that case is certainly wrong.

Thank you, i have resend a v3 patch as you suggested.

--
Xiaomeng Tong