Skip to contentSkip to conversation

n_tty: add null check for tty->link in packet mode

PATCH In review Osama Abdelkader 2 participants 3 messages Mar 14 – Mar 23 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 n_tty: add null check for tty->link in packet mode

Add null check for tty->link before dereferencing in n_tty_read and n_tty_poll. When the pty master closes, tty->link can be NULL while the slave is still reading, causing a null pointer dereference.

---
 drivers/tty/n_tty.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/n_tty.c +2 -2
index e6a0f5b40d0a..dc04b87364f6 100644
@@ -2232,7 +2232,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
 	add_wait_queue(&tty->read_wait, &wait);
 	while (nr) {
 		/* First test for status change. */
-		if (packet && tty->link->ctrl.pktstatus) {
+		if (packet && tty->link && tty->link->ctrl.pktstatus) {
 			u8 cs;
 			if (kb != kbuf)
 				break;
@@ -2444,7 +2444,7 @@ static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
 		if (input_available_p(tty, 1))
 			mask |= EPOLLIN | EPOLLRDNORM;
 	}
-	if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
+	if (tty->ctrl.packet && tty->link && tty->link->ctrl.pktstatus)
 		mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
 	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
 		mask |= EPOLLHUP;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 3 lines · Osama Abdelkader

Add null check for tty->link before dereferencing in n_tty_read and n_tty_poll. When the pty master closes, tty->link can be NULL while the slave is still reading, causing a null pointer dereference.

How can that happen?

Quoted 19 lines
---
 drivers/tty/n_tty.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/n_tty.c +1 -1
index e6a0f5b40d0a..dc04b87364f6 100644
@@ -2232,7 +2232,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
 	add_wait_queue(&tty->read_wait, &wait);
 	while (nr) {
 		/* First test for status change. */
-		if (packet && tty->link->ctrl.pktstatus) {
+		if (packet && tty->link && tty->link->ctrl.pktstatus) {
 			u8 cs;
 			if (kb != kbuf)
 				break;

drivers/tty/n_tty.c static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file, · line 2447

Quoted 5 lines
drivers/tty/n_tty.c +1 -1
@@ -2444,7 +2444,7 @@ static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
 		if (input_available_p(tty, 1))
 			mask |= EPOLLIN | EPOLLRDNORM;
 	}
-	if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
+	if (tty->ctrl.packet && tty->link && tty->link->ctrl.pktstatus)

What happens if link changes right after you test it? Where is the lock?

And what changed to cause this to show up now?

thanks,

greg k-h

#2 lore raw
Quoted 35 lines · Greg Kroah-Hartman
Quoted 3 lines · Osama Abdelkader

Add null check for tty->link before dereferencing in n_tty_read and n_tty_poll. When the pty master closes, tty->link can be NULL while the slave is still reading, causing a null pointer dereference.

How can that happen?

Quoted 19 lines
---
 drivers/tty/n_tty.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/n_tty.c +1 -1
index e6a0f5b40d0a..dc04b87364f6 100644
@@ -2232,7 +2232,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
 	add_wait_queue(&tty->read_wait, &wait);
 	while (nr) {
 		/* First test for status change. */
-		if (packet && tty->link->ctrl.pktstatus) {
+		if (packet && tty->link && tty->link->ctrl.pktstatus) {
 			u8 cs;
 			if (kb != kbuf)
 				break;

drivers/tty/n_tty.c static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file, · line 2447

Quoted 5 lines
drivers/tty/n_tty.c +1 -1
@@ -2444,7 +2444,7 @@ static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
 		if (input_available_p(tty, 1))
 			mask |= EPOLLIN | EPOLLRDNORM;
 	}
-	if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
+	if (tty->ctrl.packet && tty->link && tty->link->ctrl.pktstatus)

What happens if link changes right after you test it? Where is the lock?

Quoted 5 lines

And what changed to cause this to show up now?

thanks,

greg k-h

Hi Greg,

I was just thinking about null dereferencing possiblity in tty->link->ctrl.pktstatus. But, you are right It’s reasonable to drop this patch and reopen it only if I get a solid reproducer or bug report.

BR,
Osama