diff mbox series

[rc] RDMA/cma: Do not change route.addr.src_addr outside state checks

Message ID 0-v1-83dba2d1b721+1c3-syz_cma_srcaddr_jgg@nvidia.com (mailing list archive)
State Superseded
Headers show
Series [rc] RDMA/cma: Do not change route.addr.src_addr outside state checks | expand

Commit Message

Jason Gunthorpe Feb. 8, 2022, 8:05 p.m. UTC
If the state is not idle then resolve_prepare_src() will immediately fail
and no change to global state should happen.

For instance if the state is already RDMA_CM_LISTEN then this will corrupt
the src_addr and would cause the test in cma_cancel_operation():

           if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)

This would manifest as this trace from syzkaller:

  BUG: KASAN: use-after-free in __list_add_valid+0x93/0xa0 lib/list_debug.c:26
  Read of size 8 at addr ffff8881546491e0 by task syz-executor.1/32204

  CPU: 1 PID: 32204 Comm: syz-executor.1 Not tainted 5.12.0-rc8-syzkaller #0
  Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
  Call Trace:
   __dump_stack lib/dump_stack.c:79 [inline]
   dump_stack+0x141/0x1d7 lib/dump_stack.c:120
   print_address_description.constprop.0.cold+0x5b/0x2f8 mm/kasan/report.c:232
   __kasan_report mm/kasan/report.c:399 [inline]
   kasan_report.cold+0x7c/0xd8 mm/kasan/report.c:416
   __list_add_valid+0x93/0xa0 lib/list_debug.c:26
   __list_add include/linux/list.h:67 [inline]
   list_add_tail include/linux/list.h:100 [inline]
   cma_listen_on_all drivers/infiniband/core/cma.c:2557 [inline]
   rdma_listen+0x787/0xe00 drivers/infiniband/core/cma.c:3751
   ucma_listen+0x16a/0x210 drivers/infiniband/core/ucma.c:1102
   ucma_write+0x259/0x350 drivers/infiniband/core/ucma.c:1732
   vfs_write+0x28e/0xa30 fs/read_write.c:603
   ksys_write+0x1ee/0x250 fs/read_write.c:658
   do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46
   entry_SYSCALL_64_after_hwframe+0x44/0xae

Which is indicating that an rdma_id_private was destroyed without doing
cma_cancel_listens().

Instead of trying to re-use the src_addr memory to indirectly create an
any address derived from the dst build one explicitly on the stack and
bind to that as any other normal flow would do. rdma_bind_addr() will copy
it over the src_addr once it knows the state is valid.

Also, src_addr is never NULL in cma_bind_addr().

This is similar to commit bc0bdc5afaa7 ("RDMA/cma: Do not change
route.addr.src_addr.ss_family")

Cc: stable@vger.kernel.org
Fixes: 732d41c545bb ("RDMA/cma: Make the locking for automatic state transition more clear")
Reported-by: syzbot+c94a3675a626f6333d74@syzkaller.appspotmail.com
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/infiniband/core/cma.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)


base-commit: 2f1b2820b546c1eef07d15ed73db4177c0cf6d46

Comments

Leon Romanovsky Feb. 9, 2022, 8:43 a.m. UTC | #1
On Tue, Feb 08, 2022 at 04:05:31PM -0400, Jason Gunthorpe wrote:
> If the state is not idle then resolve_prepare_src() will immediately fail
> and no change to global state should happen.
> 
> For instance if the state is already RDMA_CM_LISTEN then this will corrupt
> the src_addr and would cause the test in cma_cancel_operation():
> 
>            if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
> 
> This would manifest as this trace from syzkaller:
> 
>   BUG: KASAN: use-after-free in __list_add_valid+0x93/0xa0 lib/list_debug.c:26
>   Read of size 8 at addr ffff8881546491e0 by task syz-executor.1/32204
> 
>   CPU: 1 PID: 32204 Comm: syz-executor.1 Not tainted 5.12.0-rc8-syzkaller #0
>   Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
>   Call Trace:
>    __dump_stack lib/dump_stack.c:79 [inline]
>    dump_stack+0x141/0x1d7 lib/dump_stack.c:120
>    print_address_description.constprop.0.cold+0x5b/0x2f8 mm/kasan/report.c:232
>    __kasan_report mm/kasan/report.c:399 [inline]
>    kasan_report.cold+0x7c/0xd8 mm/kasan/report.c:416
>    __list_add_valid+0x93/0xa0 lib/list_debug.c:26
>    __list_add include/linux/list.h:67 [inline]
>    list_add_tail include/linux/list.h:100 [inline]
>    cma_listen_on_all drivers/infiniband/core/cma.c:2557 [inline]
>    rdma_listen+0x787/0xe00 drivers/infiniband/core/cma.c:3751
>    ucma_listen+0x16a/0x210 drivers/infiniband/core/ucma.c:1102
>    ucma_write+0x259/0x350 drivers/infiniband/core/ucma.c:1732
>    vfs_write+0x28e/0xa30 fs/read_write.c:603
>    ksys_write+0x1ee/0x250 fs/read_write.c:658
>    do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46
>    entry_SYSCALL_64_after_hwframe+0x44/0xae
> 
> Which is indicating that an rdma_id_private was destroyed without doing
> cma_cancel_listens().
> 
> Instead of trying to re-use the src_addr memory to indirectly create an
> any address derived from the dst build one explicitly on the stack and
> bind to that as any other normal flow would do. rdma_bind_addr() will copy
> it over the src_addr once it knows the state is valid.
> 
> Also, src_addr is never NULL in cma_bind_addr().
> 
> This is similar to commit bc0bdc5afaa7 ("RDMA/cma: Do not change
> route.addr.src_addr.ss_family")
> 
> Cc: stable@vger.kernel.org
> Fixes: 732d41c545bb ("RDMA/cma: Make the locking for automatic state transition more clear")
> Reported-by: syzbot+c94a3675a626f6333d74@syzkaller.appspotmail.com
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/infiniband/core/cma.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
> index 27a00ce2e10120..f9b7b6f0703f58 100644
> --- a/drivers/infiniband/core/cma.c
> +++ b/drivers/infiniband/core/cma.c
> @@ -3368,20 +3368,24 @@ static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
>  static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
>  			 const struct sockaddr *dst_addr)
>  {
> -	if (!src_addr || !src_addr->sa_family) {
> -		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
> -		src_addr->sa_family = dst_addr->sa_family;
> +	struct sockaddr_storage zero_sock = {};
> +
> +	if (!src_addr->sa_family) {

You removed !src_addr check and it will cause to crash for SRP flow,
as it passes NULL in srp_new_rdma_cm_id() function.

   334 static int srp_new_rdma_cm_id(struct srp_rdma_ch *ch)

....
   349         ret = rdma_resolve_addr(new_cm_id, target->rdma_cm.src_specified ?
   350                                 &target->rdma_cm.src.sa : NULL,
   351                                 &target->rdma_cm.dst.sa,
   352                                 SRP_PATH_REC_TIMEOUT_MS);
....
  3421 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
  3422                       const struct sockaddr *dst_addr, unsigned long timeout_ms)
  3423 {
....
  3428         ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
....
  3389 static int resolve_prepare_src(struct rdma_id_private *id_priv,
  3390                                struct sockaddr *src_addr,
  3391                                const struct sockaddr *dst_addr)
  3392 {
....
  3362 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
  3363                          const struct sockaddr *dst_addr)

Maybe it is not possible to do not have src_addr in this stage, but it
is not clear from the code.

Thanks

> +		zero_sock.ss_family = dst_addr->sa_family;
>  		if (IS_ENABLED(CONFIG_IPV6) &&
>  		    dst_addr->sa_family == AF_INET6) {
> -			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
> +			struct sockaddr_in6 *src_addr6 =
> +				(struct sockaddr_in6 *)&zero_sock;
>  			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
> +
>  			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
>  			if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
>  				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
>  		} else if (dst_addr->sa_family == AF_IB) {
> -			((struct sockaddr_ib *) src_addr)->sib_pkey =
> -				((struct sockaddr_ib *) dst_addr)->sib_pkey;
> +			((struct sockaddr_ib *)&zero_sock)->sib_pkey =
> +				((struct sockaddr_ib *)dst_addr)->sib_pkey;
>  		}
> +		src_addr = (struct sockaddr *)&zero_sock;
>  	}
>  	return rdma_bind_addr(id, src_addr);
>  }
> 
> base-commit: 2f1b2820b546c1eef07d15ed73db4177c0cf6d46
> -- 
> 2.35.1
>
diff mbox series

Patch

diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
index 27a00ce2e10120..f9b7b6f0703f58 100644
--- a/drivers/infiniband/core/cma.c
+++ b/drivers/infiniband/core/cma.c
@@ -3368,20 +3368,24 @@  static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
 			 const struct sockaddr *dst_addr)
 {
-	if (!src_addr || !src_addr->sa_family) {
-		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
-		src_addr->sa_family = dst_addr->sa_family;
+	struct sockaddr_storage zero_sock = {};
+
+	if (!src_addr->sa_family) {
+		zero_sock.ss_family = dst_addr->sa_family;
 		if (IS_ENABLED(CONFIG_IPV6) &&
 		    dst_addr->sa_family == AF_INET6) {
-			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
+			struct sockaddr_in6 *src_addr6 =
+				(struct sockaddr_in6 *)&zero_sock;
 			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
+
 			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
 			if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
 				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
 		} else if (dst_addr->sa_family == AF_IB) {
-			((struct sockaddr_ib *) src_addr)->sib_pkey =
-				((struct sockaddr_ib *) dst_addr)->sib_pkey;
+			((struct sockaddr_ib *)&zero_sock)->sib_pkey =
+				((struct sockaddr_ib *)dst_addr)->sib_pkey;
 		}
+		src_addr = (struct sockaddr *)&zero_sock;
 	}
 	return rdma_bind_addr(id, src_addr);
 }