From patchwork Wed Feb 5 23:06:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Luczaj X-Patchwork-Id: 13962005 X-Patchwork-Delegate: kuba@kernel.org Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65297215F42 for ; Wed, 5 Feb 2025 23:07:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738796851; cv=none; b=DvX/6rQrsCZzPJvXz0UL1LiAOZYmqVCzfLpMN1NmsPYScRknuJPVZiZ8CaRJam8TOAN0AIKIUM5f2+Y7mTwjWZ1o9sbY7t1otmQ/fiducYWRnNEYXstRXSGaOZnQ2Od8PppwTRSf82sHKJjLk5frpxNgcZFHfocf9EbkmmcVaJ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738796851; c=relaxed/simple; bh=HFT37ec+sRmG1cHS4L1k0j/4DXXVwyXDumHH3SQs6zI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FVwpZEYdh1dEUjuFPdExsGvbeeWaASPBtQ2v59HDDr61x1w6KrdDcfuNMMZl+9mhOKWvJwDjWTbJOMkQhT+pRId4lyUC6xO0l1kThJea/Rc7wQMoa033QLVE4PXbFLqULmu/Sq8qP+cxioEhgVC9ntDq11+9QjaA5RqZinGeaBc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co; spf=pass smtp.mailfrom=rbox.co; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b=Jmbwh/8X; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rbox.co Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b="Jmbwh/8X" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1tfoUR-008kqD-FD; Thu, 06 Feb 2025 00:07:27 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector1; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From; bh=CApnBZY8aW04rYiLWasp59iJjgpPrNdh8OevlYFQObE=; b=Jmbwh/8XKyhIYxJcRBvdWvirRF kkRQs6yyO8HTWKFapFuIvOFFYcxg89w8wCG+LqqSp+DfV6FDxfA2hBPBQpLMhyzTl9LOEnminTytg CUifG5Qulv36b0GIm76bcaUy8vH+1y9Hvbubl5XSWgMUpYIXH7z6pAdHZlgpTBfJXHgoveTuO+itz +bsqLAP+9I9JEfv5le+FFQBptcYr3psm3oKO9fhjoRrJBtlBcZnegTYPS8TGLaIXhXyxQZ0xdwLtw s4AhuNgmf8XeH2cyP68ZwciErAHcPCqQA+itunakdoK8Cr6xibJhFX7XWhuyHUb+bmS2vl8sfmSMi 0WChT9UA==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1tfoUQ-0006Hq-RU; Thu, 06 Feb 2025 00:07:27 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1tfoU5-009hRv-Mr; Thu, 06 Feb 2025 00:07:05 +0100 From: Michal Luczaj Date: Thu, 06 Feb 2025 00:06:47 +0100 Subject: [PATCH net v2 1/2] vsock: Orphan socket after transport release Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250206-vsock-linger-nullderef-v2-1-f8a1f19146f8@rbox.co> References: <20250206-vsock-linger-nullderef-v2-0-f8a1f19146f8@rbox.co> In-Reply-To: <20250206-vsock-linger-nullderef-v2-0-f8a1f19146f8@rbox.co> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: netdev@vger.kernel.org, Michal Luczaj , syzbot+9d55b199192a4be7d02c@syzkaller.appspotmail.com, Luigi Leonardi X-Mailer: b4 0.14.2 X-Patchwork-Delegate: kuba@kernel.org During socket release, sock_orphan() is called without considering that it sets sk->sk_wq to NULL. Later, if SO_LINGER is enabled, this leads to a null pointer dereferenced in virtio_transport_wait_close(). Orphan the socket only after transport release. While there, reflow the other comment. Partially reverts the 'Fixes:' commit. KASAN: null-ptr-deref in range [0x0000000000000018-0x000000000000001f] lock_acquire+0x19e/0x500 _raw_spin_lock_irqsave+0x47/0x70 add_wait_queue+0x46/0x230 virtio_transport_release+0x4e7/0x7f0 __vsock_release+0xfd/0x490 vsock_release+0x90/0x120 __sock_release+0xa3/0x250 sock_close+0x14/0x20 __fput+0x35e/0xa90 __x64_sys_close+0x78/0xd0 do_syscall_64+0x93/0x1b0 entry_SYSCALL_64_after_hwframe+0x76/0x7e Reported-by: syzbot+9d55b199192a4be7d02c@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=9d55b199192a4be7d02c Fixes: fcdd2242c023 ("vsock: Keep the binding until socket destruction") Tested-by: Luigi Leonardi Signed-off-by: Michal Luczaj --- net/vmw_vsock/af_vsock.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 075695173648d3a4ecbd04e908130efdbb393b41..85d20891b771a25b8172a163983054a2557f98c1 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -817,20 +817,25 @@ static void __vsock_release(struct sock *sk, int level) vsk = vsock_sk(sk); pending = NULL; /* Compiler warning. */ - /* 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). + /* 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); - sock_orphan(sk); + /* Indicate to vsock_remove_sock() that the socket is being released and + * can be removed from the bound_table. Unlike transport reassignment + * case, where the socket must remain bound despite vsock_remove_sock() + * being called from the transport release() callback. + */ + sock_set_flag(sk, SOCK_DEAD); if (vsk->transport) vsk->transport->release(vsk); else if (sock_type_connectible(sk->sk_type)) vsock_remove_sock(vsk); + sock_orphan(sk); sk->sk_shutdown = SHUTDOWN_MASK; skb_queue_purge(&sk->sk_receive_queue); From patchwork Wed Feb 5 23:06:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Luczaj X-Patchwork-Id: 13962003 X-Patchwork-Delegate: kuba@kernel.org Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 983E1215F42 for ; Wed, 5 Feb 2025 23:07:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738796836; cv=none; b=YTTSal5s6HuaC2Ubm/kDKFCOBPd5bnWEFkB8zEVqp4J9UAOkSu92nhcOKhmViI3ar54d3CHvRPga2YV4tB7W3wblMxHirY0BO+sRf9qSxLItRAPn5q5wF5nvFLooLS4WhlimtTJfV4+JhrIZp4CszP6/T3IGCFf1T+k+ctjycUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738796836; c=relaxed/simple; bh=oiQL0rGNDZ46rJCsYVvAQMrGpmw5q5aq+uZvd7k0acE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=im7rjP9JZo2m+M0q1jTedf6NtPd6o1AbTzjpuBLYsgPPv47BhTqagqy3St+JHeg4WZxap8RJelSDUgXR2db0fSkbUf9iRfOIZvOFQRtk1TpgUPT5j/NMByMhi+ZkKNqXt5f6pDs6WsMA7HAjwdoIeEvACSZT+7MWFDAhdfCM1CY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co; spf=pass smtp.mailfrom=rbox.co; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b=d48ZKKpl; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rbox.co Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b="d48ZKKpl" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1tfoU8-008fAz-V9; Thu, 06 Feb 2025 00:07:08 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector1; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From; bh=UOUxlpZcOVasweLp4wnSsb70UvGKPP18Kr2dC+XZKoc=; b=d48ZKKplNHHIwIOA8NqDrepnOL 00/f2Z0UxkaUQoOn116xQNHU6+KDrHUiLpSCEPQ6Fi/7nZaGuTxNcUtaO2Mm/Jg43HcAZP6TX5w7F FRDajqf8+RTJcHAHYJ93xV+WxzU1GiYZG7nkA1LB6Jppqy5sYTnI2sYE53vtO4ajunqBv5awERijH 98wHPeLlEdeKqXQqWE3P1gyrqd9ET4r5FTUP/whv+LksDIvcNxsKWyzCk0pEan7Bim6TDaJoCImFz mbhJsq7CsEYF+f2Qmav5gP3GAg2PkRJMxNiBq9J+TtlCTUQg6d9sheLWMGqrp8RYp6fafIoITT4/E fVd+AV5Q==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1tfoU8-0006G5-IZ; Thu, 06 Feb 2025 00:07:08 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1tfoU6-009hRv-5s; Thu, 06 Feb 2025 00:07:06 +0100 From: Michal Luczaj Date: Thu, 06 Feb 2025 00:06:48 +0100 Subject: [PATCH net v2 2/2] vsock/test: Add test for SO_LINGER null ptr deref Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250206-vsock-linger-nullderef-v2-2-f8a1f19146f8@rbox.co> References: <20250206-vsock-linger-nullderef-v2-0-f8a1f19146f8@rbox.co> In-Reply-To: <20250206-vsock-linger-nullderef-v2-0-f8a1f19146f8@rbox.co> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: netdev@vger.kernel.org, Michal Luczaj , Luigi Leonardi X-Mailer: b4 0.14.2 X-Patchwork-Delegate: kuba@kernel.org Explicitly close() a TCP_ESTABLISHED (connectible) socket with SO_LINGER enabled. As for now, test does not verify if close() actually lingers. On an unpatched machine, may trigger a null pointer dereference. Tested-by: Luigi Leonardi Signed-off-by: Michal Luczaj --- tools/testing/vsock/vsock_test.c | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index dfff8b288265f96b602cb1bfa0e6dce02f114222..d0f6d253ac72d08a957cb81a3c38fcc72bec5a53 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -1788,6 +1788,42 @@ static void test_stream_connect_retry_server(const struct test_opts *opts) close(fd); } +static void test_stream_linger_client(const struct test_opts *opts) +{ + struct linger optval = { + .l_onoff = 1, + .l_linger = 1 + }; + int fd; + + fd = vsock_stream_connect(opts->peer_cid, opts->peer_port); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) { + perror("setsockopt(SO_LINGER)"); + exit(EXIT_FAILURE); + } + + close(fd); +} + +static void test_stream_linger_server(const struct test_opts *opts) +{ + int fd; + + fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); + if (fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + vsock_wait_remote_close(fd); + close(fd); +} + static struct test_case test_cases[] = { { .name = "SOCK_STREAM connection reset", @@ -1943,6 +1979,11 @@ static struct test_case test_cases[] = { .run_client = test_stream_connect_retry_client, .run_server = test_stream_connect_retry_server, }, + { + .name = "SOCK_STREAM SO_LINGER null-ptr-deref", + .run_client = test_stream_linger_client, + .run_server = test_stream_linger_server, + }, {}, };