Skip to contentSkip to conversation

tty/vt: fix a memory leak in con_insert_unipair

PATCH In review James Bond 3 participants 4 messages Aug 9, 2020 – Aug 10, 2020 TTY layer and serial drivers

lore
Share & tools
lore

James Bond

author
#0 lore raw

PATCH 1/1 tty/vt: fix a memory leak in con_insert_unipair

Syzkaller find a memory leak in con_insert_unipair:

    BUG: memory leak
    unreferenced object 0xffff88804893d100 (size 256):
    comm "syz-executor.3", pid 16154, jiffies 4295043307 (age 2392.340s)
    hex dump (first 32 bytes):
    80 af 88 4e 80 88 ff ff 00 a8 88 4e 80 88 ff ff  ...N.......N....
    80 ad 88 4e 80 88 ff ff 00 aa 88 4e 80 88 ff ff  ...N.......N....
    backtrace:
    [<00000000f76ff1de>] kmalloc include/linux/slab.h:555 [inline]
    [<00000000f76ff1de>] kmalloc_array include/linux/slab.h:596 [inline]
    [<00000000f76ff1de>] con_insert_unipair+0x9e/0x1a0 drivers/tty/vt/consolemap.c:482
    [<000000002f1ad7da>] con_set_unimap+0x244/0x2a0 drivers/tty/vt/consolemap.c:595
    [<0000000046ccb106>] do_unimap_ioctl drivers/tty/vt/vt_ioctl.c:297 [inline]
    [<0000000046ccb106>] vt_ioctl+0x863/0x12f0 drivers/tty/vt/vt_ioctl.c:1018
    [<00000000db1577ff>] tty_ioctl+0x4cd/0xa30 drivers/tty/tty_io.c:2656
    [<00000000e5cdf5ed>] vfs_ioctl fs/ioctl.c:48 [inline]
    [<00000000e5cdf5ed>] ksys_ioctl+0xa6/0xd0 fs/ioctl.c:753
    [<00000000fb4aa12c>] __do_sys_ioctl fs/ioctl.c:762 [inline]
    [<00000000fb4aa12c>] __se_sys_ioctl fs/ioctl.c:760 [inline]
    [<00000000fb4aa12c>] __x64_sys_ioctl+0x1a/0x20 fs/ioctl.c:760
    [<00000000f561f260>] do_syscall_64+0x4c/0xe0 arch/x86/entry/common.c:384
    [<0000000056206928>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
    BUG: leak checking failed

To fix this issue, we need to release the pointer p1 when the call of the function kmalloc_array fail.

---
 drivers/tty/vt/consolemap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/vt/consolemap.c +4 -1
index 5947b54d92be..1e8d06c32ca1 100644
@@ -489,7 +489,10 @@ con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos)
 	p2 = p1[n = (unicode >> 6) & 0x1f];
 	if (!p2) {
 		p2 = p1[n] = kmalloc_array(64, sizeof(u16), GFP_KERNEL);
-		if (!p2) return -ENOMEM;
+		if (!p2) {
+			kfree(p1);
+			return -ENOMEM;
+		}
 		memset(p2, 0xff, 64*sizeof(u16)); /* No glyphs for the characters (yet) */
 	}
 
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 24 lines · James Bond

Syzkaller find a memory leak in con_insert_unipair:

    BUG: memory leak
    unreferenced object 0xffff88804893d100 (size 256):
    comm "syz-executor.3", pid 16154, jiffies 4295043307 (age 2392.340s)
    hex dump (first 32 bytes):
    80 af 88 4e 80 88 ff ff 00 a8 88 4e 80 88 ff ff  ...N.......N....
    80 ad 88 4e 80 88 ff ff 00 aa 88 4e 80 88 ff ff  ...N.......N....
    backtrace:
    [<00000000f76ff1de>] kmalloc include/linux/slab.h:555 [inline]
    [<00000000f76ff1de>] kmalloc_array include/linux/slab.h:596 [inline]
    [<00000000f76ff1de>] con_insert_unipair+0x9e/0x1a0 drivers/tty/vt/consolemap.c:482
    [<000000002f1ad7da>] con_set_unimap+0x244/0x2a0 drivers/tty/vt/consolemap.c:595
    [<0000000046ccb106>] do_unimap_ioctl drivers/tty/vt/vt_ioctl.c:297 [inline]
    [<0000000046ccb106>] vt_ioctl+0x863/0x12f0 drivers/tty/vt/vt_ioctl.c:1018
    [<00000000db1577ff>] tty_ioctl+0x4cd/0xa30 drivers/tty/tty_io.c:2656
    [<00000000e5cdf5ed>] vfs_ioctl fs/ioctl.c:48 [inline]
    [<00000000e5cdf5ed>] ksys_ioctl+0xa6/0xd0 fs/ioctl.c:753
    [<00000000fb4aa12c>] __do_sys_ioctl fs/ioctl.c:762 [inline]
    [<00000000fb4aa12c>] __se_sys_ioctl fs/ioctl.c:760 [inline]
    [<00000000fb4aa12c>] __x64_sys_ioctl+0x1a/0x20 fs/ioctl.c:760
    [<00000000f561f260>] do_syscall_64+0x4c/0xe0 arch/x86/entry/common.c:384
    [<0000000056206928>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
    BUG: leak checking failed
Quoted 2 lines

To fix this issue, we need to release the pointer p1 when the call of the function kmalloc_array fail.

This reminds me of:
commit 15b3cd8ef46ad1b100e0d3c7e38774f330726820

Author: Ben Hutchings <[email protected]>
Date:   Tue Jun 4 19:00:39 2019 +0100

Revert "consolemap: Fix a memory leaking bug in
drivers/tty/vt/consolemap.c"

    This reverts commit 84ecc2f6eb1cb12e6d44818f94fa49b50f06e6ac.

So NACK.

Do we have some annotations for this instead?

Quoted 10 lines
---
 drivers/tty/vt/consolemap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/tty/vt/consolemap.c +0 -0
index 5947b54d92be..1e8d06c32ca1 100644

drivers/tty/vt/consolemap.c con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos) · line 497

Quoted 10 lines
drivers/tty/vt/consolemap.c +4 -1
@@ -489,7 +489,10 @@ con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos)
 	p2 = p1[n = (unicode >> 6) & 0x1f];
 	if (!p2) {
 		p2 = p1[n] = kmalloc_array(64, sizeof(u16), GFP_KERNEL);
-		if (!p2) return -ENOMEM;
+		if (!p2) {
+			kfree(p1);
+			return -ENOMEM;
+		}
 		memset(p2, 0xff, 64*sizeof(u16)); /* No glyphs for the characters (yet) */
 	}

thanks,

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 33 lines · Jiri Slaby
Quoted 24 lines · James Bond

Syzkaller find a memory leak in con_insert_unipair:

    BUG: memory leak
    unreferenced object 0xffff88804893d100 (size 256):
    comm "syz-executor.3", pid 16154, jiffies 4295043307 (age 2392.340s)
    hex dump (first 32 bytes):
    80 af 88 4e 80 88 ff ff 00 a8 88 4e 80 88 ff ff  ...N.......N....
    80 ad 88 4e 80 88 ff ff 00 aa 88 4e 80 88 ff ff  ...N.......N....
    backtrace:
    [<00000000f76ff1de>] kmalloc include/linux/slab.h:555 [inline]
    [<00000000f76ff1de>] kmalloc_array include/linux/slab.h:596 [inline]
    [<00000000f76ff1de>] con_insert_unipair+0x9e/0x1a0 drivers/tty/vt/consolemap.c:482
    [<000000002f1ad7da>] con_set_unimap+0x244/0x2a0 drivers/tty/vt/consolemap.c:595
    [<0000000046ccb106>] do_unimap_ioctl drivers/tty/vt/vt_ioctl.c:297 [inline]
    [<0000000046ccb106>] vt_ioctl+0x863/0x12f0 drivers/tty/vt/vt_ioctl.c:1018
    [<00000000db1577ff>] tty_ioctl+0x4cd/0xa30 drivers/tty/tty_io.c:2656
    [<00000000e5cdf5ed>] vfs_ioctl fs/ioctl.c:48 [inline]
    [<00000000e5cdf5ed>] ksys_ioctl+0xa6/0xd0 fs/ioctl.c:753
    [<00000000fb4aa12c>] __do_sys_ioctl fs/ioctl.c:762 [inline]
    [<00000000fb4aa12c>] __se_sys_ioctl fs/ioctl.c:760 [inline]
    [<00000000fb4aa12c>] __x64_sys_ioctl+0x1a/0x20 fs/ioctl.c:760
    [<00000000f561f260>] do_syscall_64+0x4c/0xe0 arch/x86/entry/common.c:384
    [<0000000056206928>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
    BUG: leak checking failed
Quoted 2 lines

To fix this issue, we need to release the pointer p1 when the call of the function kmalloc_array fail.

This reminds me of:
commit 15b3cd8ef46ad1b100e0d3c7e38774f330726820

Author: Ben Hutchings <[email protected]>
Date:   Tue Jun 4 19:00:39 2019 +0100
Quoted 8 lines

Revert "consolemap: Fix a memory leaking bug in
drivers/tty/vt/consolemap.c"

    This reverts commit 84ecc2f6eb1cb12e6d44818f94fa49b50f06e6ac.

So NACK.

I have a whole talk just about the "fun" involved with that change:

	https://kernel-recipes.org/en/2019/talks/cves-are-dead-long-live-the-cve/
Quoted 1 line

Do we have some annotations for this instead?

We need something there, a comment saying "this is fine, don't touch it!" or something like that? We need that in a few other places in the vt code as well.

Quoted 1 line
Quoted 1 line

Nice name...

James, can you mark this syzbot report as "invalid" or something like that please so that this does not come up again and again.

thanks,

greg k-h

maintainer
#3 lore raw
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Jiri Slaby
Quoted 26 lines · James Bond

Syzkaller find a memory leak in con_insert_unipair:

    BUG: memory leak
    unreferenced object 0xffff88804893d100 (size 256):
    comm "syz-executor.3", pid 16154, jiffies 4295043307 (age 2392.340s)
    hex dump (first 32 bytes):
    80 af 88 4e 80 88 ff ff 00 a8 88 4e 80 88 ff ff  ...N.......N....
    80 ad 88 4e 80 88 ff ff 00 aa 88 4e 80 88 ff ff  ...N.......N....
    backtrace:
    [<00000000f76ff1de>] kmalloc include/linux/slab.h:555 [inline]
    [<00000000f76ff1de>] kmalloc_array include/linux/slab.h:596 [inline]
    [<00000000f76ff1de>] con_insert_unipair+0x9e/0x1a0 drivers/tty/vt/consolemap.c:482
    [<000000002f1ad7da>] con_set_unimap+0x244/0x2a0 drivers/tty/vt/consolemap.c:595
    [<0000000046ccb106>] do_unimap_ioctl drivers/tty/vt/vt_ioctl.c:297 [inline]
    [<0000000046ccb106>] vt_ioctl+0x863/0x12f0 drivers/tty/vt/vt_ioctl.c:1018
    [<00000000db1577ff>] tty_ioctl+0x4cd/0xa30 drivers/tty/tty_io.c:2656
    [<00000000e5cdf5ed>] vfs_ioctl fs/ioctl.c:48 [inline]
    [<00000000e5cdf5ed>] ksys_ioctl+0xa6/0xd0 fs/ioctl.c:753
    [<00000000fb4aa12c>] __do_sys_ioctl fs/ioctl.c:762 [inline]
    [<00000000fb4aa12c>] __se_sys_ioctl fs/ioctl.c:760 [inline]
    [<00000000fb4aa12c>] __x64_sys_ioctl+0x1a/0x20 fs/ioctl.c:760
    [<00000000f561f260>] do_syscall_64+0x4c/0xe0 arch/x86/entry/common.c:384
    [<0000000056206928>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
    BUG: leak checking failed

To fix this issue, we need to release the pointer p1 when the call of the function kmalloc_array fail.

...

Quoted 5 lines
Quoted 1 line

Do we have some annotations for this instead?

We need something there, a comment saying "this is fine, don't touch it!" or something like that? We need that in a few other places in the vt code as well.

Sure, comment as the last resort (to silence patch writers). But I had some kmemleak annotation (to silence the warning) in mind.

Or better fix/tune kmemleak: why it dares to think it's a mem leak in the first place?

thanks,

Signature · 1 line
js