tty_io: Fix a missing-check bug in drivers/tty/tty_io.c
PATCH 1/1 tty_io: Fix a missing-check bug in drivers/tty/tty_io.c
In alloc_tty_struct(), tty->dev is assigned by tty_get_device(). And it calls class_find_device(). And class_find_device() may return NULL. And tty->dev is dereferenced in the following codes. When tty_get_device() returns NULL, dereferencing this tty->dev null pointer may cause the kernel go wrong. Thus we should check tty->dev. Further, if tty_get_device() returns NULL, we should free tty and return NULL.
---
drivers/tty/tty_io.c +4 -0
@@ -3008,6 +3008,10 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) tty->index = idx;tty_line_name (driver, idx, tty->name); tty->dev =tty_get_device (tty); +if (!tty->dev) { +kfree (tty); +return NULL ; + }return tty; }
Quoted 16 lines · Gen Zhang
In
alloc_tty_struct(),tty->devis assigned bytty_get_device(). And it callsclass_find_device(). Andclass_find_device()may return NULL. Andtty->devis dereferenced in the following codes. Whentty_get_device()returns NULL, dereferencing thistty->devnull pointer may cause the kernel go wrong. Thus we should checktty->dev. Further, iftty_get_device()returns NULL, we should free tty and return NULL.Signed-off-by Gen Zhang <[email protected]>---
drivers/tty/tty_io.c +0
-0
drivers/tty/tty_io.c struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) · line 3014
Quoted 7 lines
drivers/tty/tty_io.c +4
-0@@ -3008,6 +3008,10 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) tty->index = idx;tty_line_name (driver, idx, tty->name); tty->dev =tty_get_device (tty); +if (!tty->dev) { +kfree (tty); +return NULL ; + }
This is incorrect, you introduced an ldisc reference leak.
And can this happen at all?
thanks,
Signature · 2 lines
js suse labs
Quoted 11 lines · Jiri Slaby
Quoted 10 lines · Gen Zhang
In
alloc_tty_struct(),tty->devis assigned bytty_get_device(). And it callsclass_find_device(). Andclass_find_device()may return NULL. Andtty->devis dereferenced in the following codes. Whentty_get_device()returns NULL, dereferencing thistty->devnull pointer may cause the kernel go wrong. Thus we should checktty->dev. Further, iftty_get_device()returns NULL, we should free tty and return NULL.Signed-off-by Gen Zhang <[email protected]>
Quoted 15 lines
drivers/tty/tty_io.c struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) · line 3014
Quoted 13 lines
---
drivers/tty/tty_io.c +4
-0@@ -3008,6 +3008,10 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) tty->index = idx;tty_line_name (driver, idx, tty->name); tty->dev =tty_get_device (tty); +if (!tty->dev) { +kfree (tty); +return NULL ; + }This is incorrect, you introduced an ldisc reference leak.
Thanks for your reply, Jiri!
And what do you mean by an ldisc reference leak? I did't get the reason of introducing it.
Quoted 2 lines
And can this happen at all?
I think tty_get_device() may happen to return NULL. Because it calls class_find_device() and there's a chance class_find_device() returns NULL.
Thanks
Gen
Quoted 5 lines
thanks,
Signature · 2 lines
js suse labs
Quoted 27 lines · Gen Zhang
Quoted 11 lines · Jiri Slaby
Quoted 10 lines · Gen Zhang
In
alloc_tty_struct(),tty->devis assigned bytty_get_device(). And it callsclass_find_device(). Andclass_find_device()may return NULL. Andtty->devis dereferenced in the following codes. Whentty_get_device()returns NULL, dereferencing thistty->devnull pointer may cause the kernel go wrong. Thus we should checktty->dev. Further, iftty_get_device()returns NULL, we should free tty and return NULL.Signed-off-by Gen Zhang <[email protected]>Quoted 15 lines
drivers/tty/tty_io.c struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) · line 3014
Quoted 13 lines
---
drivers/tty/tty_io.c +4
-0@@ -3008,6 +3008,10 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) tty->index = idx;tty_line_name (driver, idx, tty->name); tty->dev =tty_get_device (tty); +if (!tty->dev) { +kfree (tty); +return NULL ; + }This is incorrect, you introduced an ldisc reference leak.
Quoted 3 lines
Thanks for your reply, Jiri!
And what do you mean by an ldisc reference leak? I did't get the reason of introducing it.
Look at the top of alloc_tty_struct: there is tty_ldisc_init. If tty_get_device fails here, you have to call tty_ldisc_deinit. Better, you should add a failure-handling tail to this function and "goto" there.
Quoted 4 lines
Quoted 1 line
And can this happen at all?
I think
tty_get_device()may happen to return NULL. Because it callsclass_find_device()and there's a chanceclass_find_device()returns NULL.
Sure, but can class_find_device return NULL in this tty case here?
thanks,
Signature · 2 lines
js suse labs
Quoted 8 lines · Jiri Slaby
Quoted 7 lines · Gen Zhang
Quoted 6 lines · Jiri Slaby
Quoted 5 lines · Gen Zhang
In
alloc_tty_struct(),tty->devis assigned bytty_get_device(). And it callsclass_find_device(). Andclass_find_device()may return NULL. Andtty->devis dereferenced in the following codes. Whentty_get_device()returns NULL, dereferencing thistty->devnull pointer may cause the kernel go wrong. Thus we should checktty->dev.
Where do you see that the kernel is dereferencing tty->dev without checking for NULL first? If you can find that, then that would indeed be a bug that needs fixing.
Quoted 28 lines
Quoted 20 lines
Quoted 5 lines
Quoted 5 lines
Further, if
tty_get_device()returns NULL, we should free tty and return NULL.Signed-off-by Gen Zhang <[email protected]>Quoted 15 lines
drivers/tty/tty_io.c struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) · line 3014
Quoted 13 lines
---
drivers/tty/tty_io.c +4
-0@@ -3008,6 +3008,10 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) tty->index = idx;tty_line_name (driver, idx, tty->name); tty->dev =tty_get_device (tty); +if (!tty->dev) { +kfree (tty); +return NULL ; + }This is incorrect, you introduced an ldisc reference leak.
Quoted 3 lines
Thanks for your reply, Jiri!
And what do you mean by an ldisc reference leak? I did't get the reason of introducing it.Look at the top of
alloc_tty_struct: there istty_ldisc_init. Iftty_get_devicefails here, you have to calltty_ldisc_deinit. Better, you should add a failure-handling tail to this function and "goto" there.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
And can this happen at all?
I think
tty_get_device()may happen to return NULL. Because it callsclass_find_device()and there's a chanceclass_find_device()returns NULL.Sure, but can
class_find_devicereturn NULL in this tty case here?
Yes, it can and will and that's fine, not all ttys have a struct device (e.g. ptys).
This patch is broken and should be dropped.
Johan
Quoted 4 lines · Johan Hovold
Quoted 1 line
Sure, but can
class_find_devicereturn NULL in this tty case here?Yes, it can and will and that's fine, not all ttys have a struct device (e.g. ptys).
IOW, the code needs a comment, if anything.
thanks,
Signature · 2 lines
js suse labs
Quoted 3 lines · Johan Hovold
Where do you see that the kernel is dereferencing
tty->devwithout checking for NULL first? If you can find that, then that would indeed be a bug that needs fixing.
Thanks for your reply, Johan!
I examined the code but failed to find this situation. Anyway, checking return value of tty_get_device() is theoritically right. But tty->dev is never dereferenced, so checking is not needed. However, what if in later kernels tty->dev is dereferenced by some codes? Is it better to apply this check for this reason? Thanks
Gen
Quoted 6 lines · Gen Zhang
Quoted 3 lines · Johan Hovold
Where do you see that the kernel is dereferencing
tty->devwithout checking for NULL first? If you can find that, then that would indeed be a bug that needs fixing.Thanks for your reply, Johan!
I examined the code but failed to find this situation.
Ok, so your claim in the commit message was incorrect:
And tty->dev is dereferenced in the following codes.
Quoted 2 lines
Anyway, checking return value of
tty_get_device()is theoritically right. Buttty->devis never dereferenced, so checking is not needed.
No, sorry, it's not even theoretically correct. Our current code depends on tty->dev sometimes being NULL. Your patch would specifically break pseudo terminals.
Quoted 2 lines
However, what if in later kernels
tty->devis dereferenced by some codes? Is it better to apply this check for this reason?
So for the above reason, no.
Johan
Quoted 10 lines · Johan Hovold
Quoted 6 lines · Gen Zhang
Quoted 3 lines · Johan Hovold
Where do you see that the kernel is dereferencing
tty->devwithout checking for NULL first? If you can find that, then that would indeed be a bug that needs fixing.Thanks for your reply, Johan!
I examined the code but failed to find this situation.Ok, so your claim in the commit message was incorrect:
Quoted 8 lines
And tty->dev is dereferenced in the following codes.Quoted 2 lines
Anyway, checking return value of
tty_get_device()is theoritically right. Buttty->devis never dereferenced, so checking is not needed.No, sorry, it's not even theoretically correct. Our current code depends on
tty->devsometimes being NULL. Your patch would specifically break pseudo terminals.
Thanks for your comments. I have to be very proficient in this module to know this. Of course I am not.
Anyway, appreciate your replies, Johan.
Thanks
Gen
Quoted 7 lines
Quoted 3 lines
However, what if in later kernels
tty->devis dereferenced by some codes? Is it better to apply this check for this reason?So for the above reason, no.
Johan
Quoted 3 lines · Jiri Slaby
Look at the top of
alloc_tty_struct: there istty_ldisc_init. Iftty_get_devicefails here, you have to calltty_ldisc_deinit. Better, you should add a failure-handling tail to this function and "goto" there.
Thanks for your explaination, Jiri.
I will work on it.
Thanks
Gen