From patchwork Wed Jan 8 18:06:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 13931419 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 4F759201271 for ; Wed, 8 Jan 2025 18:06:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736359592; cv=none; b=OBLxhCj0OzieSFFv7gYtee/0h2JugX75CAiDYXfZSXosp/DRqMBqhNZHGQQFSON+Qjm7l1eubq0CaVw7TI58ff5XDErS9G2t00Mo1boAFRXl3FObzyBtqHxpY42JLNrBTv1f2BUq4tcOVRFjKNsz2eo9yFeYl1S6uXBKHc9mYeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736359592; c=relaxed/simple; bh=XhT2B/XMcI2q+vQQF0C3xrDtTRflQwJMsW4aCd7eL48=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FhWnEsJZEWP0+tFXSA5+cPcMFRCjLj0RbwQWA07B2iWPaDQoijQml6V8+n37nG5C+oeRa/9p0kIcslUS2ohlkVMYRP4NHG1hjbD+BS6Z2yLnOuzCcmFYHgKnF61Lt/hVLcsPqLCTU6LUjEMxDGl0pehL5O8njE6ZQs5hCYMw2nU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LxuBkGJG; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LxuBkGJG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736359590; 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: in-reply-to:in-reply-to:references:references; bh=P81z0YmLvdT8m64Ix0hrS5vpO81KiskzyN+gGNFCQNI=; b=LxuBkGJGO2dzjgcQsryGFfqvRyWIt+q3CfwpTMt6LqzV6UScCHpE7cI0NXpnY+5BGH2l+T 9+82CKERC2FkA6/8F/5HYSzOtaYrtiDtmz/yewgCtIkraD6k8AFQx89P60vqgENZvx7NMr OvhpyhQ4LYPewvITBoLg5/GOqmySTLc= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-277-pMru0CXDPgur_0LwUljotg-1; Wed, 08 Jan 2025 13:06:29 -0500 X-MC-Unique: pMru0CXDPgur_0LwUljotg-1 X-Mimecast-MFC-AGG-ID: pMru0CXDPgur_0LwUljotg Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-385ed79291eso578495f8f.0 for ; Wed, 08 Jan 2025 10:06:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736359586; x=1736964386; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=P81z0YmLvdT8m64Ix0hrS5vpO81KiskzyN+gGNFCQNI=; b=AHcTAx7wga2n1mDhO5u7XBSfsq1IbLKO3Uy0Xt+C1pCaq6hgums6MgcNTOPYh1CbMG TTAmXX/YaXZg6pq08iLKXHd2mQghQMqX7TB7X0mmAQiL29brZdodZYMpghBx+1dli/V9 RgBu/IxUZDgWh6seTCeJyeX7F/etpYm/aYabqqUfmkkV3sSKfKJ8FIQxnzefNtPLf6vH ZiUPp2G2k9KVL12crLEE1GrGIKi1A0kywU36265AxJHNN96UfTOXB5ZdS4s4UI2x3cG4 6bDrReg9eycDTwbKUt9r89DNs2byA5tAfeKwjelblEEw505uHzCWysrhph2sAmeK2o9P obfg== X-Forwarded-Encrypted: i=1; AJvYcCUajyjAi/h/7RFh26HTe9Jy1QF4y1CAeQfypdAzgu6GdIOHHro380IaruKp5YNgP7YIv34=@vger.kernel.org X-Gm-Message-State: AOJu0YzDhEffhmQDm+AJi65MT/hDgVFPNQ5OmT6ASKLTD3ZZC8iyRz6R 4MecGzILioM9zKnZiDN9yTeXP4OSWhfdLFFRlIGmvJSaiMhFAe2aOCJPjrzwCK5wUScZE/KCUBs MEBeFEk4r/jwjLg3P9xp6bUUc+ajWfb1WQjYmbV896C3Xk/Ph1g== X-Gm-Gg: ASbGncsZpynoo31mukX9bFgScTtsquBpFMzmSLRGu+TJnjfN/p99RCz2sDO040n7a5W 6Yilhd0z0yS7mpnEGxgYBumw6/yrGB1+vycH9Krtk88uQUipvzEjxFAUuyZAay73awcLWVxz+5z 39NpbYxj/YTIdAyoqUfWmssW5MpQ9defI6ooGq4Hm3/03dVwRMxomQo9xigjQ8TRbrJxThswECa 6xRhOGVnMWX+V8IyofQl4jYcOLLC7i1uh8zOg90zSwsC3o= X-Received: by 2002:a5d:64af:0:b0:386:1ab3:11f0 with SMTP id ffacd0b85a97d-38a8b0fa39dmr213701f8f.28.1736359586606; Wed, 08 Jan 2025 10:06:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHyImssYlGKFMwIIkelKFtn5EBvRpRzebX/KbLVJOoRVPg+AYW6dhCm61RcqZ/eZtr4R9nAwQ== X-Received: by 2002:a5d:64af:0:b0:386:1ab3:11f0 with SMTP id ffacd0b85a97d-38a8b0fa39dmr213656f8f.28.1736359585987; Wed, 08 Jan 2025 10:06:25 -0800 (PST) Received: from step1.. ([5.77.93.126]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8292f4sm54344839f8f.3.2025.01.08.10.06.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2025 10:06:25 -0800 (PST) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: Simon Horman , Stefan Hajnoczi , linux-kernel@vger.kernel.org, Eric Dumazet , Xuan Zhuo , Wongi Lee , "David S. Miller" , Paolo Abeni , Jason Wang , Bobby Eshleman , virtualization@lists.linux.dev, =?utf-8?q?Eugenio_P=C3=A9rez?= , Stefano Garzarella , Luigi Leonardi , bpf@vger.kernel.org, Jakub Kicinski , "Michael S. Tsirkin" , Hyunwoo Kim , Michal Luczaj , kvm@vger.kernel.org Subject: [PATCH net 1/2] vsock/virtio: discard packets if the transport changes Date: Wed, 8 Jan 2025 19:06:16 +0100 Message-ID: <20250108180617.154053-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250108180617.154053-1-sgarzare@redhat.com> References: <20250108180617.154053-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org If the socket has been de-assigned or assigned to another transport, we must discard any packets received because they are not expected and would cause issues when we access vsk->transport. A possible scenario is described by Hyunwoo Kim in the attached link, where after a first connect() interrupted by a signal, and a second connect() failed, we can find `vsk->transport` at NULL, leading to a NULL pointer dereference. Fixes: c0cfa2d8a788 ("vsock: add multi-transports support") Reported-by: Hyunwoo Kim Reported-by: Wongi Lee Closes: https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/ Signed-off-by: Stefano Garzarella --- net/vmw_vsock/virtio_transport_common.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index 9acc13ab3f82..51a494b69be8 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1628,8 +1628,11 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, lock_sock(sk); - /* Check if sk has been closed before lock_sock */ - if (sock_flag(sk, SOCK_DONE)) { + /* Check if sk has been closed or assigned to another transport before + * lock_sock (note: listener sockets are not assigned to any transport) + */ + if (sock_flag(sk, SOCK_DONE) || + (sk->sk_state != TCP_LISTEN && vsk->transport != &t->transport)) { (void)virtio_transport_reset_no_sock(t, skb); release_sock(sk); sock_put(sk); From patchwork Wed Jan 8 18:06:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 13931420 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 0C3B6202C4A for ; Wed, 8 Jan 2025 18:06:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736359596; cv=none; b=NB2aPmBMFBL3q2JfUtprPymPOjZS9pvzRP6g/AT8N6EKMkysFs1a297k0J+LBHhaVEgijwN9iQ4aV0OgjjYUiIqxALIlT3vqqBn3QXX0nutUPPXILXalqZFcs80/1V9NVU0PBrsSzUZdPHYgGTANSTqV82VB4hxLsDfn57MMa/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736359596; c=relaxed/simple; bh=NW1NN64WPOb8zvTk9pQc9y9wJMAUw7k5KI4tbn3qy4c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PTNCKAIWPZpoT1h18RoPo0iVM8/uBq+KZa8znLodjTpXE94XcZOpKTo1ZI2q851h6xtqdQV86hSf3ODSnSFKIuY7MBo2aJV6ldLPtoI2uReweRcH1WAiDf4wiB/qNHB/90zQYwkXuxMId08SPrl9dlc52Ut6WMl2COXKp1GB+Nc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=bc1qGnB3; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bc1qGnB3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736359594; 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: in-reply-to:in-reply-to:references:references; bh=xTpnjYdFe65i9cQrDrcRqhmpYDaSR/fVnChURrprIX4=; b=bc1qGnB3bW6W0rYY89SyhwbWMM/ahLBNdXr2GQO6uG6SYN8HHNUl6Rnonz7kEhh1MqChEt yVCoTEPT1pPX15jQbSqgFZg22CI3SsoRvOSZthh2ypsAR113lIXnAupD0EqX4dLaQjgziX lv/3zT9M2BZIrz1DvTTLH9XhPtaItfU= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-695-0KKISGKPPQGXP4ItUjm-fg-1; Wed, 08 Jan 2025 13:06:33 -0500 X-MC-Unique: 0KKISGKPPQGXP4ItUjm-fg-1 X-Mimecast-MFC-AGG-ID: 0KKISGKPPQGXP4ItUjm-fg Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4362b9c1641so468125e9.3 for ; Wed, 08 Jan 2025 10:06:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736359592; x=1736964392; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xTpnjYdFe65i9cQrDrcRqhmpYDaSR/fVnChURrprIX4=; b=lTifMP6hyktXKAPBpy4tt6spGRNpNa2MxLH2BiqlPjxJQf+kEnssLaxGHnfa6y46eG s55D+NcC6TTAL9WAw9ylSM4XfoJRSVTtkXDlnQQ31kmT+sr2ySqR+cu+o499j+kEI5ou hLCEqpZvV0NA9JlORJe2Fuk58rGTMBUyMCG2FOTE1724cWw6WT5zL3pslaaXI/E1iJE5 /12QLAIonCbMuk04kno7S4AgNrhDsCAmgMhBQ7RGkvPDv1drkjm4TKMeCWoazagZI9I/ nYyZQ9GtRktCSNMuT28hO71I8/Z1zvRaJHiYFQdKQ8F0NtT1FsOaEhuJPvUv9gPrBYBi C31A== X-Forwarded-Encrypted: i=1; AJvYcCW8GKaUKQsv8jFRILnimePaD/guB92/Sclc5uaxf065OzLKhCOKr9KLybnTDnKv0lkYXjs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6dvY2LRxTvabiiRqxXu/5vSdiTClAE8eOLKbpcqj8X47O8zxU n/JNiOw4m4nVjK7kDNKe9c1PYeMbJG39LkVL+8EouSdRLZ0wUtmcUbMhtUN3KQpiUnDbTsyrSjA sz/hgkWtcD6B9QtjC0QkYMDyUVhHt4hbvJgsLkcrEqdx2VbFvYw== X-Gm-Gg: ASbGncu41sBe4I628JKOuPkhxKn6ZEYcVBtBMZQ/WiZNY03JJ6D+rAbR1FrUIfQYjYy E1YoKj/A0HNDBjx2EL6mCzaSqHPSwguYL7TjDweq1M1u/8MY9560ZVRvAmRK5OCWMz2ul67YhO2 /78eX6rLM3rlYuy4WcXnA0SCGyCNzjA2XJvtmqJaUGCvuLhFMrsGVrPwrmD94XwphtAZQY4yvO+ lkaivJQqKqTJBI1eg5C38V02Nt2HHKfSmfJ1amyE4yz+1U= X-Received: by 2002:a05:600c:1d14:b0:436:5fc9:309d with SMTP id 5b1f17b1804b1-436e26f6d80mr37371055e9.30.1736359591820; Wed, 08 Jan 2025 10:06:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IHTv/Bx6C2k5LOcLLY/XN2JmilQSz5qyKDCjlMy44PV1BJ3rFBTpqkU5iqtg5HftWkhRLAW+A== X-Received: by 2002:a05:600c:1d14:b0:436:5fc9:309d with SMTP id 5b1f17b1804b1-436e26f6d80mr37370685e9.30.1736359591257; Wed, 08 Jan 2025 10:06:31 -0800 (PST) Received: from step1.. ([5.77.93.126]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-436e2e89fc3sm28849325e9.30.2025.01.08.10.06.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2025 10:06:30 -0800 (PST) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: Simon Horman , Stefan Hajnoczi , linux-kernel@vger.kernel.org, Eric Dumazet , Xuan Zhuo , Wongi Lee , "David S. Miller" , Paolo Abeni , Jason Wang , Bobby Eshleman , virtualization@lists.linux.dev, =?utf-8?q?Eugenio_P=C3=A9rez?= , Stefano Garzarella , Luigi Leonardi , bpf@vger.kernel.org, Jakub Kicinski , "Michael S. Tsirkin" , Hyunwoo Kim , Michal Luczaj , kvm@vger.kernel.org Subject: [PATCH net 2/2] vsock/bpf: return early if transport is not assigned Date: Wed, 8 Jan 2025 19:06:17 +0100 Message-ID: <20250108180617.154053-3-sgarzare@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250108180617.154053-1-sgarzare@redhat.com> References: <20250108180617.154053-1-sgarzare@redhat.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Some of the core functions can only be called if the transport has been assigned. As Michal reported, a socket might have the transport at NULL, for example after a failed connect(), causing the following trace: BUG: kernel NULL pointer dereference, address: 00000000000000a0 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 12faf8067 P4D 12faf8067 PUD 113670067 PMD 0 Oops: Oops: 0000 [#1] PREEMPT SMP NOPTI CPU: 15 UID: 0 PID: 1198 Comm: a.out Not tainted 6.13.0-rc2+ RIP: 0010:vsock_connectible_has_data+0x1f/0x40 Call Trace: vsock_bpf_recvmsg+0xca/0x5e0 sock_recvmsg+0xb9/0xc0 __sys_recvfrom+0xb3/0x130 __x64_sys_recvfrom+0x20/0x30 do_syscall_64+0x93/0x180 entry_SYSCALL_64_after_hwframe+0x76/0x7e So we need to check the `vsk->transport` in vsock_bpf_recvmsg(), especially for connected sockets (stream/seqpacket) as we already do in __vsock_connectible_recvmsg(). Fixes: 634f1a7110b4 ("vsock: support sockmap") Reported-by: Michal Luczaj Closes: https://lore.kernel.org/netdev/5ca20d4c-1017-49c2-9516-f6f75fd331e9@rbox.co/ Signed-off-by: Stefano Garzarella Reviewed-by: Hyunwoo Kim Acked-by: Michael S. Tsirkin Reviewed-By: Luigi Leonardi Tested-by: Michal Luczaj --- net/vmw_vsock/vsock_bpf.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/net/vmw_vsock/vsock_bpf.c b/net/vmw_vsock/vsock_bpf.c index 4aa6e74ec295..f201d9eca1df 100644 --- a/net/vmw_vsock/vsock_bpf.c +++ b/net/vmw_vsock/vsock_bpf.c @@ -77,6 +77,7 @@ static int vsock_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags, int *addr_len) { struct sk_psock *psock; + struct vsock_sock *vsk; int copied; psock = sk_psock_get(sk); @@ -84,6 +85,13 @@ static int vsock_bpf_recvmsg(struct sock *sk, struct msghdr *msg, return __vsock_recvmsg(sk, msg, len, flags); lock_sock(sk); + vsk = vsock_sk(sk); + + if (!vsk->transport) { + copied = -ENODEV; + goto out; + } + if (vsock_has_data(sk, psock) && sk_psock_queue_empty(psock)) { release_sock(sk); sk_psock_put(sk, psock); @@ -108,6 +116,7 @@ static int vsock_bpf_recvmsg(struct sock *sk, struct msghdr *msg, copied = sk_msg_recvmsg(sk, psock, msg, len, flags); } +out: release_sock(sk); sk_psock_put(sk, psock);