Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp7016581ybc; Thu, 28 Nov 2019 09:19:30 -0800 (PST) X-Google-Smtp-Source: APXvYqxWrwP/KWcygUZ6/Imx9ebRbkW6K9Gaj6JW27Ghgg0erS+RXyZ0CnMbFiN07N7k7C3l2/jf X-Received: by 2002:a05:6402:651:: with SMTP id u17mr39500148edx.142.1574961570276; Thu, 28 Nov 2019 09:19:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574961570; cv=none; d=google.com; s=arc-20160816; b=dUsuWSz29AoBOz5F5sOAxpsoo9X23IfGFU2A+pbMTNfradoHx7HPzhmnkfLPibCGlp h+3VsVn4FNwqbhtBbzzxq8a+206FHjMnKkg0lWshJHg/Ee0rto8nGTdnJZ6hUgXHnnCA sLnEy3QYNmslI/5gKl+1C31jdE0pr7TL0l1U3Ys3fVtbDJ9g0apRw7U6o13OhEqo3Jb8 UIvlI+5NZ9s2EJdW2xRM91Nd//wzFdAcrLbWDqsfXZZbFoTHk/8NvlyjxsOKet97WnO+ RMZnikIkxuRdtROG85LnGOxAwDrTK/t/8aU0e+daIwV7vu8VTOkhAGl+1ciGxr5Ye/od JeWg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i43hEG8t6TDOi2Mge5CT+4KErjH/1IrCl/OdUkMsEKc=; b=gO/AwBnuA+DSq3YwS0yF772QMO2nHecsvQvfMDzrk12dG2asrCpuJ5OHXTquuWIE/h NBnCng3FAIjAVQ/dGIWRbmQNW2/hQ1iC2Ki76NAqCn+QGKRooY6hKR8XEhnOjCix41CI 10DVsUv2K0KW5OT3xVv/fcqlOEVnWuXMqVdVrETBBVO1WW4TPpB0FJX17Onz+/r1XPbZ WItwJEmiOd6FJq+Ui+Vg0WL0sTj+dgF7Pqpeh54gxmVF+6hmGcIKD/zM7A2zTK7s4AOc jYGgKT3H4kSqVx0uR2C7iFFC7T+IVTvbZgLNvJsrfFjr8fQ6PC8G92o5ReHBU1RZm+Nt f6SQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AfHoQJ3o; 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 r10si2717385eja.134.2019.11.28.09.19.06; Thu, 28 Nov 2019 09:19:30 -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=AfHoQJ3o; 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 S1727085AbfK1RPn (ORCPT + 99 others); Thu, 28 Nov 2019 12:15:43 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:53078 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727033AbfK1RPl (ORCPT ); Thu, 28 Nov 2019 12:15:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574961339; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i43hEG8t6TDOi2Mge5CT+4KErjH/1IrCl/OdUkMsEKc=; b=AfHoQJ3oweaELN1IzMvpJ9xyE47T7/jYKuDlBX526G3Q2emnjUmjJSKntNk0OIJa2FQExw BVYY7QWkkj5z+51kYpeI2tDwmlk+0Q2NgnJuQ3/2tHM1QuDvwQjfZcrK7XUsiYQ6kRWVDm PpNbamGKGIXF5IDWLI4mrWl5h984kxo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-94-V9_Me1OjMuqt2wlDS8MvuQ-1; Thu, 28 Nov 2019 12:15:38 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9A2E380183C; Thu, 28 Nov 2019 17:15:36 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-117-168.ams2.redhat.com [10.36.117.168]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35E0E600C8; Thu, 28 Nov 2019 17:15:34 +0000 (UTC) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, "Michael S. Tsirkin" , Stefano Garzarella , "David S. Miller" , Dexuan Cui , Jason Wang , Stefan Hajnoczi , Jorgen Hansen Subject: [RFC PATCH 3/3] vhost/vsock: use netns of process that opens the vhost-vsock device Date: Thu, 28 Nov 2019 18:15:19 +0100 Message-Id: <20191128171519.203979-4-sgarzare@redhat.com> In-Reply-To: <20191128171519.203979-1-sgarzare@redhat.com> References: <20191128171519.203979-1-sgarzare@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: V9_Me1OjMuqt2wlDS8MvuQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch assigns the network namespace of the process that opened vhost-vsock device (e.g. VMM) to the packets coming from the guest, allowing only host sockets in the same network namespace to communicate with the guest. This patch also allows to have different VMs, running in different network namespace, with the same CID. Signed-off-by: Stefano Garzarella --- drivers/vhost/vsock.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 31b0f3608752..e162b3604302 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -40,6 +40,7 @@ static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8); struct vhost_vsock { =09struct vhost_dev dev; =09struct vhost_virtqueue vqs[2]; +=09struct net *net; =20 =09/* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */ =09struct hlist_node hash; @@ -61,7 +62,7 @@ static u32 vhost_transport_get_local_cid(void) /* Callers that dereference the return value must hold vhost_vsock_mutex o= r the * RCU read lock. */ -static struct vhost_vsock *vhost_vsock_get(u32 guest_cid) +static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net) { =09struct vhost_vsock *vsock; =20 @@ -72,7 +73,7 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid) =09=09if (other_cid =3D=3D 0) =09=09=09continue; =20 -=09=09if (other_cid =3D=3D guest_cid) +=09=09if (other_cid =3D=3D guest_cid && net_eq(net, vsock->net)) =09=09=09return vsock; =20 =09} @@ -245,7 +246,7 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt) =09rcu_read_lock(); =20 =09/* Find the vhost_vsock according to guest context id */ -=09vsock =3D vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid)); +=09vsock =3D vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid), pkt->net); =09if (!vsock) { =09=09rcu_read_unlock(); =09=09virtio_transport_free_pkt(pkt); @@ -277,7 +278,8 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk) =09rcu_read_lock(); =20 =09/* Find the vhost_vsock according to guest context id */ -=09vsock =3D vhost_vsock_get(vsk->remote_addr.svm_cid); +=09vsock =3D vhost_vsock_get(vsk->remote_addr.svm_cid, +=09=09=09=09sock_net(sk_vsock(vsk))); =09if (!vsock) =09=09goto out; =20 @@ -474,7 +476,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_wor= k *work) =09=09=09continue; =09=09} =20 -=09=09pkt->net =3D vsock_default_net(); +=09=09pkt->net =3D vsock->net; =09=09len =3D pkt->len; =20 =09=09/* Deliver to monitoring devices all received packets */ @@ -606,7 +608,14 @@ static int vhost_vsock_dev_open(struct inode *inode, s= truct file *file) =09vqs =3D kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL)= ; =09if (!vqs) { =09=09ret =3D -ENOMEM; -=09=09goto out; +=09=09goto out_vsock; +=09} + +=09/* Derive the network namespace from the pid opening the device */ +=09vsock->net =3D get_net_ns_by_pid(current->pid); +=09if (IS_ERR(vsock->net)) { +=09=09ret =3D PTR_ERR(vsock->net); +=09=09goto out_vqs; =09} =20 =09vsock->guest_cid =3D 0; /* no CID assigned yet */ @@ -628,7 +637,9 @@ static int vhost_vsock_dev_open(struct inode *inode, st= ruct file *file) =09vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work); =09return 0; =20 -out: +out_vqs: +=09kfree(vqs); +out_vsock: =09vhost_vsock_free(vsock); =09return ret; } @@ -653,7 +664,7 @@ static void vhost_vsock_reset_orphans(struct sock *sk) =09 */ =20 =09/* If the peer is still valid, no need to reset connection */ -=09if (vhost_vsock_get(vsk->remote_addr.svm_cid)) +=09if (vhost_vsock_get(vsk->remote_addr.svm_cid, sock_net(sk))) =09=09return; =20 =09/* If the close timeout is pending, let it expire. This avoids races @@ -701,6 +712,7 @@ static int vhost_vsock_dev_release(struct inode *inode,= struct file *file) =09spin_unlock_bh(&vsock->send_pkt_list_lock); =20 =09vhost_dev_cleanup(&vsock->dev); +=09put_net(vsock->net); =09kfree(vsock->dev.vqs); =09vhost_vsock_free(vsock); =09return 0; @@ -727,7 +739,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsoc= k, u64 guest_cid) =20 =09/* Refuse if CID is already in use */ =09mutex_lock(&vhost_vsock_mutex); -=09other =3D vhost_vsock_get(guest_cid); +=09other =3D vhost_vsock_get(guest_cid, vsock->net); =09if (other && other !=3D vsock) { =09=09mutex_unlock(&vhost_vsock_mutex); =09=09return -EADDRINUSE; --=20 2.23.0