Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4777682ybv; Wed, 26 Feb 2020 02:59:51 -0800 (PST) X-Google-Smtp-Source: APXvYqzXzLQC3KWu+IbuHARgNjzCb9MY0ofr7xQ9hQ3HUWHh1NXQKwb4SPqbmRSn3g+7OP0hhuRB X-Received: by 2002:aca:fd83:: with SMTP id b125mr2592761oii.1.1582714791306; Wed, 26 Feb 2020 02:59:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582714791; cv=none; d=google.com; s=arc-20160816; b=G2Yn9yIM+ALPH0IRdPMhAqq4SHENFwuu7nc5WwtE0f4jYSQhaOuImf+PvKNoGcSNBR ufIXUrcLIBkUNYc5JvTmjBp7QOiFJyr7rDp3Z8OkQe09GbPoqsU+h10r6g81nXTkxkxb o39uf8GbbOviPUM3z5TDPH8gdNb/OK6edh+Kdu9AvV4ZYbiejHCdmKHS+mgx1qPnoOd9 8c8IoqMO2irDNohwZg+EKoEhjOdy0UR4QUbU+n9RtzlPZ6w6dJ4fdrxyulosM2sBIPpm qPfgEehyuZAp4gwF1yVZ+q5EoFUin4PWnzOLd+KOCxtlKVJAe/yWUbteSX+2CdJnk8qV Sn0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Ao4shkARYBOlBvwWDPYByOMa60o0c7ck0bB41Z0If+w=; b=AH2swxEYzxePUhASVJFzVbSSK5dovsbEFoEl05lvstwIEelD0FbEDcALSUKmIy+aWU naT1cXGVNLFB6tXGAzCSId4uaFvjWPg5Y5aNYR76Z+XEIusvE+Qm8CnreiNqN6nlm1Mj qTFHq66HLKxDefnp7LaOIsPh9WG490cP0Gfe//SySVX0qs3GlTqy3KgW1KLJitGZIqpL 9CIF5Vbp5h1ElI8c2weeZR1WC3HRgEfFz5gSOnKYC+pY6BFRsE1gpN9YOaAsIELCdPM/ J9c7ttdchiIjPRe24sqx2tQd60hYGLvwaIws169k5tB7K4Vir3AFbbvQpuJaHVcq8Oqv B7nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ebcBzj7J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m24si1075897otn.67.2020.02.26.02.59.39; Wed, 26 Feb 2020 02:59:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ebcBzj7J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728080AbgBZK62 (ORCPT + 99 others); Wed, 26 Feb 2020 05:58:28 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:38708 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727686AbgBZK60 (ORCPT ); Wed, 26 Feb 2020 05:58:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582714705; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Ao4shkARYBOlBvwWDPYByOMa60o0c7ck0bB41Z0If+w=; b=ebcBzj7JZRtl4yJG2ucuqBkycELIg2oCp/NYRejkF+wI73YIWTrxeakNKlV50lG0f+76xl lGBrzIKcLS6fYhxxQbn1gwuZQFpuihIjXTgm2vnp/aVSlueg6gF1rfhK1RtJFXyAVu5UU6 tdd0vZad3u/dOAV2baj/sy/Y1ODQbvo= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-308-oZBINokqOfOh_LcYTj2SOg-1; Wed, 26 Feb 2020 05:58:23 -0500 X-MC-Unique: oZBINokqOfOh_LcYTj2SOg-1 Received: by mail-wm1-f70.google.com with SMTP id w3so560833wmg.4 for ; Wed, 26 Feb 2020 02:58:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Ao4shkARYBOlBvwWDPYByOMa60o0c7ck0bB41Z0If+w=; b=h4vzf7BVP64XR07Ex0yNYEHi7wm5O8Z+pGzXflJ5YHRa9L+rutu9blCEWpNJMERxc2 Tu/BDuGqmV6/LmGoAmbbK/j1S4390SseAcEsQ0wgJu8a5Wfs/5CGUKd6Gjp2aW6/9t+c UQtN6KL4/ttGAqVcsij5aO9uApSqW1xzfGT92tPjKTHqtABG9NdWADnxvFJrRDWKoZcJ ABBjArAxcvR/Ia+gqE5rdayPvKRqQwvxiXDgDDRV86oLYSwFvD1YJRe4GX3mHdUN4T03 phl+Apn3S6sC7IYb62nkTHrd6whPehUzqe4RFg/ONE4sWJ8VMQNf+1RuVFBvfux/Ls+4 xr+g== X-Gm-Message-State: APjAAAXp0UztwmTPJuyhQ+0HELmn3rh9SVfSbyL7FGAreVwTFP9mWACj ioQNLlT5GhiuD4TVqGnpQWmCSecsIgBH6dpO5m9WUSq643S37XdC5+D0cwyzy1gb9k5Juz08RvE l77rexh1ZceF3ss+qt5dONZ/M X-Received: by 2002:a5d:526c:: with SMTP id l12mr5137363wrc.117.1582714700513; Wed, 26 Feb 2020 02:58:20 -0800 (PST) X-Received: by 2002:a5d:526c:: with SMTP id l12mr5137321wrc.117.1582714700113; Wed, 26 Feb 2020 02:58:20 -0800 (PST) Received: from steredhat.redhat.com (host209-4-dynamic.27-79-r.retail.telecomitalia.it. [79.27.4.209]) by smtp.gmail.com with ESMTPSA id l132sm2619123wmf.16.2020.02.26.02.58.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Feb 2020 02:58:19 -0800 (PST) From: Stefano Garzarella To: davem@davemloft.net Cc: Dexuan Cui , Hillf Danton , virtualization@lists.linux-foundation.org, "K. Y. Srinivasan" , kvm@vger.kernel.org, Stephen Hemminger , syzbot+731710996d79d0d58fbc@syzkaller.appspotmail.com, netdev@vger.kernel.org, Sasha Levin , Sunil Muthuswamy , Stefano Garzarella , linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, Jakub Kicinski , Haiyang Zhang , Stefan Hajnoczi , Jorgen Hansen Subject: [PATCH net] vsock: fix potential deadlock in transport->release() Date: Wed, 26 Feb 2020 11:58:18 +0100 Message-Id: <20200226105818.36055-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some transports (hyperv, virtio) acquire the sock lock during the .release() callback. In the vsock_stream_connect() we call vsock_assign_transport(); if the socket was previously assigned to another transport, the vsk->transport->release() is called, but the sock lock is already held in the vsock_stream_connect(), causing a deadlock reported by syzbot: INFO: task syz-executor280:9768 blocked for more than 143 seconds. Not tainted 5.6.0-rc1-syzkaller #0 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. syz-executor280 D27912 9768 9766 0x00000000 Call Trace: context_switch kernel/sched/core.c:3386 [inline] __schedule+0x934/0x1f90 kernel/sched/core.c:4082 schedule+0xdc/0x2b0 kernel/sched/core.c:4156 __lock_sock+0x165/0x290 net/core/sock.c:2413 lock_sock_nested+0xfe/0x120 net/core/sock.c:2938 virtio_transport_release+0xc4/0xd60 net/vmw_vsock/virtio_transport_common.c:832 vsock_assign_transport+0xf3/0x3b0 net/vmw_vsock/af_vsock.c:454 vsock_stream_connect+0x2b3/0xc70 net/vmw_vsock/af_vsock.c:1288 __sys_connect_file+0x161/0x1c0 net/socket.c:1857 __sys_connect+0x174/0x1b0 net/socket.c:1874 __do_sys_connect net/socket.c:1885 [inline] __se_sys_connect net/socket.c:1882 [inline] __x64_sys_connect+0x73/0xb0 net/socket.c:1882 do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294 entry_SYSCALL_64_after_hwframe+0x49/0xbe To avoid this issue, this patch remove the lock acquiring in the .release() callback of hyperv and virtio transports, and it holds the lock when we call vsk->transport->release() in the vsock core. Reported-by: syzbot+731710996d79d0d58fbc@syzkaller.appspotmail.com Fixes: 408624af4c89 ("vsock: use local transport when it is loaded") Signed-off-by: Stefano Garzarella --- net/vmw_vsock/af_vsock.c | 20 ++++++++++++-------- net/vmw_vsock/hyperv_transport.c | 3 --- net/vmw_vsock/virtio_transport_common.c | 2 -- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 9c5b2a91baad..a5f28708e0e7 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -451,6 +451,12 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) if (vsk->transport == new_transport) return 0; + /* transport->release() must be called with sock lock acquired. + * This path can only be taken during vsock_stream_connect(), + * where we have already held the sock lock. + * In the other cases, this function is called on a new socket + * which is not assigned to any transport. + */ vsk->transport->release(vsk); vsock_deassign_transport(vsk); } @@ -753,20 +759,18 @@ static void __vsock_release(struct sock *sk, int level) vsk = vsock_sk(sk); pending = NULL; /* Compiler warning. */ - /* The release call is supposed to use lock_sock_nested() - * rather than lock_sock(), if a sock lock should be acquired. - */ - if (vsk->transport) - vsk->transport->release(vsk); - else if (sk->sk_type == SOCK_STREAM) - vsock_remove_sock(vsk); - /* When "level" is SINGLE_DEPTH_NESTING, use the nested * version to avoid the warning "possible recursive locking * detected". When "level" is 0, lock_sock_nested(sk, level) * is the same as lock_sock(sk). */ lock_sock_nested(sk, level); + + if (vsk->transport) + vsk->transport->release(vsk); + else if (sk->sk_type == SOCK_STREAM) + vsock_remove_sock(vsk); + sock_orphan(sk); sk->sk_shutdown = SHUTDOWN_MASK; diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c index 3492c021925f..630b851f8150 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -526,12 +526,9 @@ static bool hvs_close_lock_held(struct vsock_sock *vsk) static void hvs_release(struct vsock_sock *vsk) { - struct sock *sk = sk_vsock(vsk); bool remove_sock; - lock_sock_nested(sk, SINGLE_DEPTH_NESTING); remove_sock = hvs_close_lock_held(vsk); - release_sock(sk); if (remove_sock) vsock_remove_sock(vsk); } diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index d9f0c9c5425a..f3c4bab2f737 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -829,7 +829,6 @@ void virtio_transport_release(struct vsock_sock *vsk) struct sock *sk = &vsk->sk; bool remove_sock = true; - lock_sock_nested(sk, SINGLE_DEPTH_NESTING); if (sk->sk_type == SOCK_STREAM) remove_sock = virtio_transport_close(vsk); @@ -837,7 +836,6 @@ void virtio_transport_release(struct vsock_sock *vsk) list_del(&pkt->list); virtio_transport_free_pkt(pkt); } - release_sock(sk); if (remove_sock) vsock_remove_sock(vsk); -- 2.24.1