Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1158812pxb; Tue, 29 Mar 2022 19:00:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiL+7N+GTm9BZ+KRKq8LvSqmx+DVQ+F13ezvrHOBTnRH18ZGcfSbkCJ2IWO6jFMVShPscs X-Received: by 2002:a05:6808:238e:b0:2ec:e0ee:abe4 with SMTP id bp14-20020a056808238e00b002ece0eeabe4mr910395oib.187.1648605612255; Tue, 29 Mar 2022 19:00:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648605612; cv=none; d=google.com; s=arc-20160816; b=Wq/XW+qrNIisFQmmbhxDKYrAGIy9jpjfkpXqtMjCHbV9VIEmAnLAMtHXekRv0M5z2v /W87QgZfkjayNnIOKeNtuhE6RI5InZ48KXYLl2Z7zl8Qhntc539MkLS6Jr1GxyfrChDX 8DWU6gvAm02ibj1o0y5sbPirycEgGpj0gxj3bZgQBTi4+oH/I0FeIwCG/eMHgMxxRfKZ PlpQrsquWTKDyPZ/xvT37E7uL9cS3CGMafZs+pOoM08n7mxmc1HQFA0Y1kdMeSDrbLif 3fcc7NKl2smw07rqbFo9DSfRXeC1J0kFOtVZN9OfwjDDfHzL8MTpk55ML280OR37WhwY 7Vww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=jQ33q+El4vP9xa8HP+1IypLOEguzWKlg0rR13gHefXM=; b=ut9emYGHLVWc0TFHQA46Gv8p9LXzEJde6vQnPbuhxpWU/3FWHOnbDn74Y1zYkOgXHv BhQjQdY4xMv0MYYHHei0Z9/tpHeVKUCd8IY9qrw3s6O6oaKKpCdWzKqA+PLftcbnTd7Z 5Kt91aKwgoIwYSLCy2zMo0h44MyK/PRNCh3ldLVOPFtjk7/LljGQ4Mzg0aIoQMin07Pj SPe0s6+Xo5HddYvfr5d2kJl0x/2mE71uyPvlLvc2TB03GIhrovohHvXYD7xgpcbG72bQ y9xt1Z+dnBIJyzZE8y9uEIP7+I9UAtHilck+35KTeZTpwbK+l990twE8aL4mC/3gGjan g7NQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=bnZcVTZ8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a20-20020a544e14000000b002f73f0e847fsi11470713oiy.23.2022.03.29.18.59.58; Tue, 29 Mar 2022 19:00:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=bnZcVTZ8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234162AbiC2Is2 (ORCPT + 99 others); Tue, 29 Mar 2022 04:48:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234159AbiC2Is1 (ORCPT ); Tue, 29 Mar 2022 04:48:27 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8FEC2414D4 for ; Tue, 29 Mar 2022 01:46:44 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id a1so23709684wrh.10 for ; Tue, 29 Mar 2022 01:46:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=jQ33q+El4vP9xa8HP+1IypLOEguzWKlg0rR13gHefXM=; b=bnZcVTZ8/z+uyqNRXDpoBn7IL1Z9zhWZReDMCUCxvMQlEiyc93jMex/JPN0NaabVMJ Cdab0IwtAG9MWcH0gYADARUQnU+arwQjg7YrnesWLI8rfsSBN9muoHH+nG2c7sKo3KbA xjp1Btt9afAFDg14ThTHN5NjXuoi19U+6AlRUzxZt01Yl0ejeH/69Eem/ID3YiPpanxA f4WcuL4kYJgDbQbtlVwxNur5alAyxEs/m4MWLOXCEWVL8kBVTRjpUyRxBdugA+dkcmw7 rc3w4iOQV5VTIVLvKLSncRgAq6AV/CCl4DGKTIR4DWxYiGAPhxCsdt+vOAk6YVlvT4eT qNwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=jQ33q+El4vP9xa8HP+1IypLOEguzWKlg0rR13gHefXM=; b=mDvRKU+U0xBOSJILgTmNlLUMnXR3y1Q3WI8e/esItA1y/CbdPA1ajc98z8T1/NrFrc GT7zNx9yc8K0mkFRKn1CC86O9DwrzHhokSchpn3TsvdEV1nSFU5e3XoQpkLI0Ooff01R NPVxNQxFeYdm9yEGEzdUPKJKnGPb3de7uF8E842UqyfuisDiLQYJWs6JzizVF1ruxd2H PttwpJ0J0FXUrnNBBFDJ3hABWaXzs6Zt5pQvy6NjmeZIvRIPnUA4i55ANLD08O0ONtBB Ba/uVocDzWaQG44t5VOpxAFV802M+Lt7ysJnvjb95M8CNNygWNwi70glruGAaAFs64dj 9sxw== X-Gm-Message-State: AOAM533kkMVpTuOcp0gCYOwbspjGWwmVfpN7w8gQls9r60AE/MxvLIeO Y3VmRX7IpRWBBGfdfqofRldL7Q== X-Received: by 2002:a5d:510d:0:b0:203:f72e:ee24 with SMTP id s13-20020a5d510d000000b00203f72eee24mr29539618wrt.183.1648543603275; Tue, 29 Mar 2022 01:46:43 -0700 (PDT) Received: from google.com (88.140.78.34.bc.googleusercontent.com. [34.78.140.88]) by smtp.gmail.com with ESMTPSA id c5-20020a5d63c5000000b002040822b680sm20676780wrw.81.2022.03.29.01.46.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Mar 2022 01:46:42 -0700 (PDT) Date: Tue, 29 Mar 2022 08:46:38 +0000 From: Keir Fraser To: "Michael S. Tsirkin" , Jason Wang Cc: kernel-team@android.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] virtio: pci: check bar values read from virtio config space Message-ID: References: <20220323140727.3499235-1-keirf@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220323140727.3499235-1-keirf@google.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 Ping. If you prefer I could read the capability offset and length in virtio_pci_find_capability() and communicate them to vp_modern_map_capability(), perhaps via a new struct virtio_pci_cap used for only this purpose? On Wed, Mar 23, 2022 at 02:07:27PM +0000, Keir Fraser wrote: > virtio pci config structures may in future have non-standard bar > values in the bar field. We should anticipate this by skipping any > structures containing such a reserved value. > > The bar value should never change: check for harmful modified values > we re-read it from the config space in vp_modern_map_capability(). > > Also clean up an existing check to consistently use PCI_STD_NUM_BARS. > > Signed-off-by: Keir Fraser > --- > drivers/virtio/virtio_pci_modern.c | 12 +++++++++--- > drivers/virtio/virtio_pci_modern_dev.c | 9 ++++++++- > 2 files changed, 17 insertions(+), 4 deletions(-) > > diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c > index 5455bc041fb6..6adfcd0297a7 100644 > --- a/drivers/virtio/virtio_pci_modern.c > +++ b/drivers/virtio/virtio_pci_modern.c > @@ -293,7 +293,7 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id, > > for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0; > pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) { > - u8 type, cap_len, id; > + u8 type, cap_len, id, res_bar; > u32 tmp32; > u64 res_offset, res_length; > > @@ -315,9 +315,14 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id, > if (id != required_id) > continue; > > - /* Type, and ID match, looks good */ > pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, > - bar), bar); > + bar), &res_bar); > + if (res_bar >= PCI_STD_NUM_BARS) > + continue; > + > + /* Type and ID match, and the BAR value isn't reserved. > + * Looks good. > + */ > > /* Read the lower 32bit of length and offset */ > pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap, > @@ -337,6 +342,7 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id, > length_hi), &tmp32); > res_length |= ((u64)tmp32) << 32; > > + *bar = res_bar; > *offset = res_offset; > *len = res_length; > > diff --git a/drivers/virtio/virtio_pci_modern_dev.c b/drivers/virtio/virtio_pci_modern_dev.c > index e8b3ff2b9fbc..591738ad3d56 100644 > --- a/drivers/virtio/virtio_pci_modern_dev.c > +++ b/drivers/virtio/virtio_pci_modern_dev.c > @@ -35,6 +35,13 @@ vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off, > pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length), > &length); > > + /* Check if the BAR may have changed since we requested the region. */ > + if (bar >= PCI_STD_NUM_BARS || !(mdev->modern_bars & (1 << bar))) { > + dev_err(&dev->dev, > + "virtio_pci: bar unexpectedly changed to %u\n", bar); > + return NULL; > + } > + > if (length <= start) { > dev_err(&dev->dev, > "virtio_pci: bad capability len %u (>%u expected)\n", > @@ -120,7 +127,7 @@ static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type, > &bar); > > /* Ignore structures with reserved BAR values */ > - if (bar > 0x5) > + if (bar >= PCI_STD_NUM_BARS) > continue; > > if (type == cfg_type) { > -- > 2.35.1.894.gb6a874cedc-goog >