diff mbox series

[rc] RDMA/nl: Do not permit empty devices names during RDMA_NLDEV_CMD_NEWLINK/SET

Message ID 20200309191648.GA30852@ziepe.ca (mailing list archive)
State Accepted
Delegated to: Jason Gunthorpe
Headers show
Series [rc] RDMA/nl: Do not permit empty devices names during RDMA_NLDEV_CMD_NEWLINK/SET | expand

Commit Message

Jason Gunthorpe March 9, 2020, 7:16 p.m. UTC
Empty device names cannot be added to sysfs and crash with:

  kobject: (00000000f9de3792): attempted to be registered with empty name!
  WARNING: CPU: 1 PID: 10856 at lib/kobject.c:234 kobject_add_internal+0x7ac/0x9a0 lib/kobject.c:234
  Kernel panic - not syncing: panic_on_warn set ...
  CPU: 1 PID: 10856 Comm: syz-executor459 Not tainted 5.6.0-rc3-syzkaller #0
  Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
  Call Trace:
   __dump_stack lib/dump_stack.c:77 [inline]
   dump_stack+0x197/0x210 lib/dump_stack.c:118
   panic+0x2e3/0x75c kernel/panic.c:221
   __warn.cold+0x2f/0x3e kernel/panic.c:582
   report_bug+0x289/0x300 lib/bug.c:195
   fixup_bug arch/x86/kernel/traps.c:174 [inline]
   fixup_bug arch/x86/kernel/traps.c:169 [inline]
   do_error_trap+0x11b/0x200 arch/x86/kernel/traps.c:267
   do_invalid_op+0x37/0x50 arch/x86/kernel/traps.c:286
   invalid_op+0x23/0x30 arch/x86/entry/entry_64.S:1027
  RIP: 0010:kobject_add_internal+0x7ac/0x9a0 lib/kobject.c:234
  Code: 7a ca ca f9 e9 f0 f8 ff ff 4c 89 f7 e8 cd ca ca f9 e9 95 f9 ff ff e8 13 25 8c f9 4c 89 e6 48 c7 c7 a0 08 1a 89 e8 a3 76 5c f9 <0f> 0b 41 bd ea ff ff ff e9 52 ff ff ff e8 f2 24 8c f9 0f 0b e8 eb
  RSP: 0018:ffffc90002006eb0 EFLAGS: 00010286
  RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000
  RDX: 0000000000000000 RSI: ffffffff815eae46 RDI: fffff52000400dc8
  RBP: ffffc90002006f08 R08: ffff8880972ac500 R09: ffffed1015d26659
  R10: ffffed1015d26658 R11: ffff8880ae9332c7 R12: ffff888093034668
  R13: 0000000000000000 R14: ffff8880a69d7600 R15: 0000000000000001
   kobject_add_varg lib/kobject.c:390 [inline]
   kobject_add+0x150/0x1c0 lib/kobject.c:442
   device_add+0x3be/0x1d00 drivers/base/core.c:2412
   ib_register_device drivers/infiniband/core/device.c:1371 [inline]
   ib_register_device+0x93e/0xe40 drivers/infiniband/core/device.c:1343
   rxe_register_device+0x52e/0x655 drivers/infiniband/sw/rxe/rxe_verbs.c:1231
   rxe_add+0x122b/0x1661 drivers/infiniband/sw/rxe/rxe.c:302
   rxe_net_add+0x91/0xf0 drivers/infiniband/sw/rxe/rxe_net.c:539
   rxe_newlink+0x39/0x90 drivers/infiniband/sw/rxe/rxe.c:318
   nldev_newlink+0x28a/0x430 drivers/infiniband/core/nldev.c:1538
   rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:195 [inline]
   rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
   rdma_nl_rcv+0x5d9/0x980 drivers/infiniband/core/netlink.c:259
   netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
   netlink_unicast+0x59e/0x7e0 net/netlink/af_netlink.c:1329
   netlink_sendmsg+0x91c/0xea0 net/netlink/af_netlink.c:1918
   sock_sendmsg_nosec net/socket.c:652 [inline]
   sock_sendmsg+0xd7/0x130 net/socket.c:672
   ____sys_sendmsg+0x753/0x880 net/socket.c:2343
   ___sys_sendmsg+0x100/0x170 net/socket.c:2397
   __sys_sendmsg+0x105/0x1d0 net/socket.c:2430
   __do_sys_sendmsg net/socket.c:2439 [inline]
   __se_sys_sendmsg net/socket.c:2437 [inline]
   __x64_sys_sendmsg+0x78/0xb0 net/socket.c:2437
   do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
   entry_SYSCALL_64_after_hwframe+0x49/0xbe

Prevent empty names when checking the name provided from userspace during
newlink and rename.

Cc: stable@kernel.org
Fixes: 3856ec4b93c9 ("RDMA/core: Add RDMA_NLDEV_CMD_NEWLINK/DELLINK support")
Fixes: 05d940d3a3ec ("RDMA/nldev: Allow IB device rename through RDMA netlink")
Reported-by: syzbot+da615ac67d4dbea32cbc@syzkaller.appspotmail.com
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
---
 drivers/infiniband/core/nldev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git for-next

Comments

Jason Gunthorpe March 9, 2020, 7:20 p.m. UTC | #1
On Mon, Mar 09, 2020 at 04:16:48PM -0300, Jason Gunthorpe wrote:
> Empty device names cannot be added to sysfs and crash with:
> 
>   kobject: (00000000f9de3792): attempted to be registered with empty name!
>   WARNING: CPU: 1 PID: 10856 at lib/kobject.c:234 kobject_add_internal+0x7ac/0x9a0 lib/kobject.c:234
>   Kernel panic - not syncing: panic_on_warn set ...
>   CPU: 1 PID: 10856 Comm: syz-executor459 Not tainted 5.6.0-rc3-syzkaller #0
>   Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
>   Call Trace:
>    __dump_stack lib/dump_stack.c:77 [inline]
>    dump_stack+0x197/0x210 lib/dump_stack.c:118
>    panic+0x2e3/0x75c kernel/panic.c:221
>    __warn.cold+0x2f/0x3e kernel/panic.c:582
>    report_bug+0x289/0x300 lib/bug.c:195
>    fixup_bug arch/x86/kernel/traps.c:174 [inline]
>    fixup_bug arch/x86/kernel/traps.c:169 [inline]
>    do_error_trap+0x11b/0x200 arch/x86/kernel/traps.c:267
>    do_invalid_op+0x37/0x50 arch/x86/kernel/traps.c:286
>    invalid_op+0x23/0x30 arch/x86/entry/entry_64.S:1027
>   RIP: 0010:kobject_add_internal+0x7ac/0x9a0 lib/kobject.c:234
>   Code: 7a ca ca f9 e9 f0 f8 ff ff 4c 89 f7 e8 cd ca ca f9 e9 95 f9 ff ff e8 13 25 8c f9 4c 89 e6 48 c7 c7 a0 08 1a 89 e8 a3 76 5c f9 <0f> 0b 41 bd ea ff ff ff e9 52 ff ff ff e8 f2 24 8c f9 0f 0b e8 eb
>   RSP: 0018:ffffc90002006eb0 EFLAGS: 00010286
>   RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000
>   RDX: 0000000000000000 RSI: ffffffff815eae46 RDI: fffff52000400dc8
>   RBP: ffffc90002006f08 R08: ffff8880972ac500 R09: ffffed1015d26659
>   R10: ffffed1015d26658 R11: ffff8880ae9332c7 R12: ffff888093034668
>   R13: 0000000000000000 R14: ffff8880a69d7600 R15: 0000000000000001
>    kobject_add_varg lib/kobject.c:390 [inline]
>    kobject_add+0x150/0x1c0 lib/kobject.c:442
>    device_add+0x3be/0x1d00 drivers/base/core.c:2412
>    ib_register_device drivers/infiniband/core/device.c:1371 [inline]
>    ib_register_device+0x93e/0xe40 drivers/infiniband/core/device.c:1343
>    rxe_register_device+0x52e/0x655 drivers/infiniband/sw/rxe/rxe_verbs.c:1231
>    rxe_add+0x122b/0x1661 drivers/infiniband/sw/rxe/rxe.c:302
>    rxe_net_add+0x91/0xf0 drivers/infiniband/sw/rxe/rxe_net.c:539
>    rxe_newlink+0x39/0x90 drivers/infiniband/sw/rxe/rxe.c:318
>    nldev_newlink+0x28a/0x430 drivers/infiniband/core/nldev.c:1538
>    rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:195 [inline]
>    rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
>    rdma_nl_rcv+0x5d9/0x980 drivers/infiniband/core/netlink.c:259
>    netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
>    netlink_unicast+0x59e/0x7e0 net/netlink/af_netlink.c:1329
>    netlink_sendmsg+0x91c/0xea0 net/netlink/af_netlink.c:1918
>    sock_sendmsg_nosec net/socket.c:652 [inline]
>    sock_sendmsg+0xd7/0x130 net/socket.c:672
>    ____sys_sendmsg+0x753/0x880 net/socket.c:2343
>    ___sys_sendmsg+0x100/0x170 net/socket.c:2397
>    __sys_sendmsg+0x105/0x1d0 net/socket.c:2430
>    __do_sys_sendmsg net/socket.c:2439 [inline]
>    __se_sys_sendmsg net/socket.c:2437 [inline]
>    __x64_sys_sendmsg+0x78/0xb0 net/socket.c:2437
>    do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
>    entry_SYSCALL_64_after_hwframe+0x49/0xbe
> 
> Prevent empty names when checking the name provided from userspace during
> newlink and rename.
> 
> Cc: stable@kernel.org
> Fixes: 3856ec4b93c9 ("RDMA/core: Add RDMA_NLDEV_CMD_NEWLINK/DELLINK support")
> Fixes: 05d940d3a3ec ("RDMA/nldev: Allow IB device rename through RDMA netlink")
> Reported-by: syzbot+da615ac67d4dbea32cbc@syzkaller.appspotmail.com
> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
>  drivers/infiniband/core/nldev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> #syz test: git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git for-next
> 
> diff --git a/drivers/infiniband/core/nldev.c b/drivers/infiniband/core/nldev.c
> index 37b433aa730610..7147a4e49167db 100644
> +++ b/drivers/infiniband/core/nldev.c
> @@ -1514,7 +1514,7 @@ static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
>  
>  	nla_strlcpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
>  		    sizeof(ibdev_name));
> -	if (strchr(ibdev_name, '%'))
> +	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
>  		return -EINVAL;
>  
>  	nla_strlcpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));

Gah, a hunk got lost here:

--- a/drivers/infiniband/core/nldev.c
+++ b/drivers/infiniband/core/nldev.c
@@ -918,6 +918,10 @@ static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
 
                nla_strlcpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
                            IB_DEVICE_NAME_MAX);
+               if (strlen(name) == 0) {
+                       err = -EINVAL;
+                       goto done;
+               }
                err = ib_device_rename(device, name);
                goto done;
        }

Jason
syzbot March 9, 2020, 8:38 p.m. UTC | #2
Hello,

syzbot has tested the proposed patch and the reproducer did not trigger crash:

Reported-and-tested-by: syzbot+da615ac67d4dbea32cbc@syzkaller.appspotmail.com

Tested on:

commit:         0aeb3622 RDMA/hns: fix spelling mistake "attatch" -> "atta..
git tree:       git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git for-next
kernel config:  https://syzkaller.appspot.com/x/.config?x=b58f96e9824c82cb
dashboard link: https://syzkaller.appspot.com/bug?extid=da615ac67d4dbea32cbc
compiler:       gcc (GCC) 9.0.0 20181231 (experimental)
patch:          https://syzkaller.appspot.com/x/patch.diff?x=12ca10a9e00000

Note: testing is done by a robot and is best-effort only.
Jason Gunthorpe March 13, 2020, 1:31 p.m. UTC | #3
On Mon, Mar 09, 2020 at 04:16:48PM -0300, Jason Gunthorpe wrote:
> Empty device names cannot be added to sysfs and crash with:
> 
>   kobject: (00000000f9de3792): attempted to be registered with empty name!
>   WARNING: CPU: 1 PID: 10856 at lib/kobject.c:234 kobject_add_internal+0x7ac/0x9a0 lib/kobject.c:234
>   Kernel panic - not syncing: panic_on_warn set ...
>   CPU: 1 PID: 10856 Comm: syz-executor459 Not tainted 5.6.0-rc3-syzkaller #0
>   Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
>   Call Trace:
>    __dump_stack lib/dump_stack.c:77 [inline]
>    dump_stack+0x197/0x210 lib/dump_stack.c:118
>    panic+0x2e3/0x75c kernel/panic.c:221
>    __warn.cold+0x2f/0x3e kernel/panic.c:582
>    report_bug+0x289/0x300 lib/bug.c:195
>    fixup_bug arch/x86/kernel/traps.c:174 [inline]
>    fixup_bug arch/x86/kernel/traps.c:169 [inline]
>    do_error_trap+0x11b/0x200 arch/x86/kernel/traps.c:267
>    do_invalid_op+0x37/0x50 arch/x86/kernel/traps.c:286
>    invalid_op+0x23/0x30 arch/x86/entry/entry_64.S:1027
>   RIP: 0010:kobject_add_internal+0x7ac/0x9a0 lib/kobject.c:234
>   Code: 7a ca ca f9 e9 f0 f8 ff ff 4c 89 f7 e8 cd ca ca f9 e9 95 f9 ff ff e8 13 25 8c f9 4c 89 e6 48 c7 c7 a0 08 1a 89 e8 a3 76 5c f9 <0f> 0b 41 bd ea ff ff ff e9 52 ff ff ff e8 f2 24 8c f9 0f 0b e8 eb
>   RSP: 0018:ffffc90002006eb0 EFLAGS: 00010286
>   RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000
>   RDX: 0000000000000000 RSI: ffffffff815eae46 RDI: fffff52000400dc8
>   RBP: ffffc90002006f08 R08: ffff8880972ac500 R09: ffffed1015d26659
>   R10: ffffed1015d26658 R11: ffff8880ae9332c7 R12: ffff888093034668
>   R13: 0000000000000000 R14: ffff8880a69d7600 R15: 0000000000000001
>    kobject_add_varg lib/kobject.c:390 [inline]
>    kobject_add+0x150/0x1c0 lib/kobject.c:442
>    device_add+0x3be/0x1d00 drivers/base/core.c:2412
>    ib_register_device drivers/infiniband/core/device.c:1371 [inline]
>    ib_register_device+0x93e/0xe40 drivers/infiniband/core/device.c:1343
>    rxe_register_device+0x52e/0x655 drivers/infiniband/sw/rxe/rxe_verbs.c:1231
>    rxe_add+0x122b/0x1661 drivers/infiniband/sw/rxe/rxe.c:302
>    rxe_net_add+0x91/0xf0 drivers/infiniband/sw/rxe/rxe_net.c:539
>    rxe_newlink+0x39/0x90 drivers/infiniband/sw/rxe/rxe.c:318
>    nldev_newlink+0x28a/0x430 drivers/infiniband/core/nldev.c:1538
>    rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:195 [inline]
>    rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
>    rdma_nl_rcv+0x5d9/0x980 drivers/infiniband/core/netlink.c:259
>    netlink_unicast_kernel net/netlink/af_netlink.c:1303 [inline]
>    netlink_unicast+0x59e/0x7e0 net/netlink/af_netlink.c:1329
>    netlink_sendmsg+0x91c/0xea0 net/netlink/af_netlink.c:1918
>    sock_sendmsg_nosec net/socket.c:652 [inline]
>    sock_sendmsg+0xd7/0x130 net/socket.c:672
>    ____sys_sendmsg+0x753/0x880 net/socket.c:2343
>    ___sys_sendmsg+0x100/0x170 net/socket.c:2397
>    __sys_sendmsg+0x105/0x1d0 net/socket.c:2430
>    __do_sys_sendmsg net/socket.c:2439 [inline]
>    __se_sys_sendmsg net/socket.c:2437 [inline]
>    __x64_sys_sendmsg+0x78/0xb0 net/socket.c:2437
>    do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
>    entry_SYSCALL_64_after_hwframe+0x49/0xbe
> 
> Prevent empty names when checking the name provided from userspace during
> newlink and rename.
> 
> Cc: stable@kernel.org
> Fixes: 3856ec4b93c9 ("RDMA/core: Add RDMA_NLDEV_CMD_NEWLINK/DELLINK support")
> Fixes: 05d940d3a3ec ("RDMA/nldev: Allow IB device rename through RDMA netlink")
> Reported-by: syzbot+da615ac67d4dbea32cbc@syzkaller.appspotmail.com
> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
> ---
>  drivers/infiniband/core/nldev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Applied to for-rc

Jason
diff mbox series

Patch

diff --git a/drivers/infiniband/core/nldev.c b/drivers/infiniband/core/nldev.c
index 37b433aa730610..7147a4e49167db 100644
--- a/drivers/infiniband/core/nldev.c
+++ b/drivers/infiniband/core/nldev.c
@@ -1514,7 +1514,7 @@  static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nla_strlcpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
 		    sizeof(ibdev_name));
-	if (strchr(ibdev_name, '%'))
+	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
 		return -EINVAL;
 
 	nla_strlcpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));