Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3678898lfo; Mon, 23 May 2022 11:10:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyngtZ4ezuBd2AH3ueno7Q1ew+AQS0iDdYc6KfqdjEjpjP2UsN1k5+W4W47hGAy9RMPrQ/g X-Received: by 2002:a17:902:82c7:b0:161:4936:f068 with SMTP id u7-20020a17090282c700b001614936f068mr23863195plz.145.1653329458690; Mon, 23 May 2022 11:10:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653329458; cv=none; d=google.com; s=arc-20160816; b=PTxzg2y9TjwzUYuuq9Qr+N4w0skJq8W0bUhJS6kVbZaewux+8Wndh4cODsVpJMChRd YDeJrnAItBCY6waDn3rHWZrksyebZ8rMUXua9DYrE2ZonKMaF8nLz2Qo5K0eMPkqRZcE EP1GuKohtOGoT/UxCQzrr+MBSM1u1NkNQyrDZpMvmwQRfSVvDMXha9hWL/1H3IiPv3LM z958VjtmEZo+9EskwQ8isNda1pepjs3xtoiEGBrZ+OjtOr2JUYXKK0GvTCt4pi0PexsR n0zTicE10ixeKrBXELBSOORCtKQyC/MLeMGB3t51Ru+9Zl/xkOkQ1nFcQhMVpP2mytVS 7Tbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fswIM3Lw2qjDzQdbQJ1o/djke7PPD+RtX3dgZg6HZjs=; b=XMVUfHjw7L1QzkqIVgdP/xxKej5xuqMabS6zdpA7cnMWgqV36OGTQV84yBnuN856aq SggUKHYOsvH0MZomiGcBnrCRizrsmbUL/pLus8fprFl71TIapOvtj5cKcGKAe2qZrL6P 9KDJv+Vzwe50A48hVs5ZD3P7Sf87xI76ahkPvDQvbeOEUfs2x0hoqje8UPw7l6v+a88p nx0cbD0kF6I7gOkXcq36U6qQ8Wqj4/kEEwpy9TZjG6aK0AEjPlrG1uYA5M/iYiKtLbL6 dj665ebxEUInVBAYxQ3obRzxfkFslchb9F3bBQWNKo6uqkHzE+E0dDzUjLayMnRyRcTH XUmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=agjidTwo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id pm13-20020a17090b3c4d00b001dc1bd7d5afsi7346pjb.61.2022.05.23.11.10.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 11:10:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=agjidTwo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6B6ED14CDD9; Mon, 23 May 2022 11:10:37 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245344AbiEWSHs (ORCPT + 99 others); Mon, 23 May 2022 14:07:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244197AbiEWRi4 (ORCPT ); Mon, 23 May 2022 13:38:56 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD1EA9A997; Mon, 23 May 2022 10:33:26 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 94552B81221; Mon, 23 May 2022 17:14:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E478FC385A9; Mon, 23 May 2022 17:14:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653326095; bh=LD/W7uHd1vOMPsmRa4IOtCa8tUX9XnTD7hg00Yjc+7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=agjidTwoxWpU5JzjKflnlDv3QvpPvXQjkWUPZ2NZUZ6gpVJ0KxuWCAVPoVarKa7ay 4VlWhlUGeJOSeJ+2H+8x3gTbcusU6cBsrrpCpWkJcXab+RtETAtI7R4v/ZTN/Wcym6 ciJ0MpUc6A22YeJTQupCp+xKW9lG5q5JiYI2dGMw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, "Michael S. Tsirkin" , Jason Wang , Al Viro Subject: [PATCH 5.4 28/68] Fix double fget() in vhost_net_set_backend() Date: Mon, 23 May 2022 19:04:55 +0200 Message-Id: <20220523165807.276439249@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220523165802.500642349@linuxfoundation.org> References: <20220523165802.500642349@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Al Viro commit fb4554c2232e44d595920f4d5c66cf8f7d13f9bc upstream. Descriptor table is a shared resource; two fget() on the same descriptor may return different struct file references. get_tap_ptr_ring() is called after we'd found (and pinned) the socket we'll be using and it tries to find the private tun/tap data structures associated with it. Redoing the lookup by the same file descriptor we'd used to get the socket is racy - we need to same struct file. Thanks to Jason for spotting a braino in the original variant of patch - I'd missed the use of fd == -1 for disabling backend, and in that case we can end up with sock == NULL and sock != oldsock. Cc: stable@kernel.org Acked-by: Michael S. Tsirkin Signed-off-by: Jason Wang Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- drivers/vhost/net.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -1446,13 +1446,9 @@ err: return ERR_PTR(r); } -static struct ptr_ring *get_tap_ptr_ring(int fd) +static struct ptr_ring *get_tap_ptr_ring(struct file *file) { struct ptr_ring *ring; - struct file *file = fget(fd); - - if (!file) - return NULL; ring = tun_get_tx_ring(file); if (!IS_ERR(ring)) goto out; @@ -1461,7 +1457,6 @@ static struct ptr_ring *get_tap_ptr_ring goto out; ring = NULL; out: - fput(file); return ring; } @@ -1548,8 +1543,12 @@ static long vhost_net_set_backend(struct r = vhost_net_enable_vq(n, vq); if (r) goto err_used; - if (index == VHOST_NET_VQ_RX) - nvq->rx_ring = get_tap_ptr_ring(fd); + if (index == VHOST_NET_VQ_RX) { + if (sock) + nvq->rx_ring = get_tap_ptr_ring(sock->file); + else + nvq->rx_ring = NULL; + } oldubufs = nvq->ubufs; nvq->ubufs = ubufs;