Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2613123rwb; Fri, 20 Jan 2023 05:20:47 -0800 (PST) X-Google-Smtp-Source: AMrXdXt6nXKvDGMdysFcxgxhIfA47zzEwSSW07V0TIiT/rUjgVCvEiqzwJMroTKkZPAL6iHvDt0k X-Received: by 2002:a05:6402:14d0:b0:498:1995:ac54 with SMTP id f16-20020a05640214d000b004981995ac54mr13729858edx.40.1674220847218; Fri, 20 Jan 2023 05:20:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674220847; cv=none; d=google.com; s=arc-20160816; b=pimiwmYn7zjDM33BvALc9nB2P06lsCkiTVkqNwENhRJcPWvdhgSUQwSVfnAKn3xROp ni0lEYHFVVThgbmvW8cnR/3n7bq2ALi8WQ2BzA44H37DpByD0sa0PfB2OMM9Ds+1u6iG 2bbCBvTxC1Fby0BBeNsrwTgvOX7xv28HYB/g0VjHEyfpnKnLiBIRdOlTUFDvVgmA7X4Q 6LCXdCYAjdmjDd5SJjIISy6xzEikPHR7gv9FcJwR8ugR8m/6h8r0zfBwmjlBTU5RTDt9 IRdpV6wH7u2dSMM6jZrxDMid5v5FUjIGLWGcVIvcKeqNeTERPxZnNDEz6e3LBi+QEIEC iM+g== 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=67RdguR+gl433cYi2o595AZGinbUWlEV2GdMEq9B1vY=; b=c3jyRlEU0kbYe8GYpMfghCCk0hEBWb16LQJnb/5sFf5RzqYWKP67GczOFUUVFPt8vC 6wWbnmb4RWNI97eF+OxpBx99lkfbWg8BLVtwRId7dvLSX6+krhSpz3eliuQl8YTihPmi 5VWSc0H2o/rM5IW/PZdd9rSn5Ev2kdsnKpjLVdxpwlq+wotIJUW9p8idot1pWpw8VyBW +i+W7h6630FgFkCJWC4B7h/bsdOgr4fXxh2d4yE7yK0A1lBjXc51E8oSAA6B3p5L/vHq CNatFX9oHB7i4vAeWHutS/owHnWnn56jQ0WU0MJ/G+HTswvcEmk3/KykBXQtrb9z4iTo oPUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ibQqfBf8; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b17-20020a056402279100b0049e369ed236si13332487ede.260.2023.01.20.05.20.31; Fri, 20 Jan 2023 05:20:47 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=ibQqfBf8; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230431AbjATM5h (ORCPT + 51 others); Fri, 20 Jan 2023 07:57:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230424AbjATM5f (ORCPT ); Fri, 20 Jan 2023 07:57:35 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54CDB5FD4 for ; Fri, 20 Jan 2023 04:56:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674219414; 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: in-reply-to:in-reply-to:references:references; bh=67RdguR+gl433cYi2o595AZGinbUWlEV2GdMEq9B1vY=; b=ibQqfBf8xUOFAxxwa4tqgFK0ce5TPafx662OosOtKb4tNWNJBzwOwtFntWvx9pBir1Fldh itzotDIfnTHNDiMjrGRW9FJPP28QCHru3ZPhM2a5QQITvb1jm003cdioccI52SBN5U5x7w zaqBRAYkjVtRpnIF+SnlElEqI+Me5gE= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-665-byBuMZM6Oiy41gtnO0OnJQ-1; Fri, 20 Jan 2023 07:56:53 -0500 X-MC-Unique: byBuMZM6Oiy41gtnO0OnJQ-1 Received: by mail-wr1-f71.google.com with SMTP id v3-20020adfa1c3000000b002bdd6ce1358so938127wrv.23 for ; Fri, 20 Jan 2023 04:56:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=67RdguR+gl433cYi2o595AZGinbUWlEV2GdMEq9B1vY=; b=foAwfe18YeX738F1TTAsjJE2Him31JPFIn0sG39shPf1zFgmqbnbshumuWoWIYQjaE 39S3W/JzSip964I5ii0TL754/4tDiF6KwsiUxg+IR3k4DZl3ppt3IRZjCoLrd0tWRIlt RatUzJmWP5BYMuO8YFbY75/dzM/L7gGHac5f12Y3xnYhwnZty2uk6DcmwCI+rL3hhv8y 2SizPJRTVqRylSqkTFw7AZ7BUR0ZllZW/XAwJZ8SjuKfm8keHLWUH4TcIBnlGf7ssNFh XmpmNC18IvuTgOMar6tiJxx9rxYy54TG8blDVMYeOjp27LIHD2XOYKpo0PIXWIV5ReaN quUA== X-Gm-Message-State: AFqh2ko8X8Rg89EYfdeDPxq/+Uef9KXT7LqiAVkL6Tk8J47iptIIKBz2 m/j2p2c55YQw0d7qty5ipwRccpLCRe2AbVYO3G3fHfpfdEiDEABd6M8FOwqzX7zkI2uGWNH7Q5E PKNcJJxcwJ6J0w4Pp8lY8hv+G X-Received: by 2002:a05:6000:1f0f:b0:2b4:e5e:c0a3 with SMTP id bv15-20020a0560001f0f00b002b40e5ec0a3mr8416450wrb.21.1674219412145; Fri, 20 Jan 2023 04:56:52 -0800 (PST) X-Received: by 2002:a05:6000:1f0f:b0:2b4:e5e:c0a3 with SMTP id bv15-20020a0560001f0f00b002b40e5ec0a3mr8416440wrb.21.1674219411869; Fri, 20 Jan 2023 04:56:51 -0800 (PST) Received: from redhat.com ([2.52.19.29]) by smtp.gmail.com with ESMTPSA id t14-20020adfeb8e000000b002baa780f0fasm35571506wrn.111.2023.01.20.04.56.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jan 2023 04:56:51 -0800 (PST) Date: Fri, 20 Jan 2023 07:56:48 -0500 From: "Michael S. Tsirkin" To: Alexander Shishkin Cc: jasowang@redhat.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, elena.reshetova@intel.com, kirill.shutemov@linux.intel.com Subject: Re: [PATCH v1 6/6] virtio_ring: Prevent bounds check bypass on descriptor index Message-ID: <20230120075448-mutt-send-email-mst@kernel.org> References: <20230119135721.83345-1-alexander.shishkin@linux.intel.com> <20230119135721.83345-7-alexander.shishkin@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230119135721.83345-7-alexander.shishkin@linux.intel.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 On Thu, Jan 19, 2023 at 03:57:21PM +0200, Alexander Shishkin wrote: > The descriptor index in virtqueue_get_buf_ctx_split() comes from the > device/VMM.a Use array_index_nospec() to prevent the CPU from speculating > beyond the descriptor array bounds and providing a primitive for building > a side channel. > > Signed-off-by: Alexander Shishkin > --- > drivers/virtio/virtio_ring.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > index 2e7689bb933b..c42d070ab68d 100644 > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -9,6 +9,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -819,6 +820,8 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, > BAD_RING(vq, "id %u out of range\n", i); > return NULL; > } > + > + i = array_index_nospec(i, vq->split.vring.num); I suspect plain i &= split.vring.num - 1 is more efficient. We know num is a power of two but compiler doesn't. And pls add a comment explaining what's going on. > if (unlikely(!vq->split.desc_state[i].data)) { > BAD_RING(vq, "id %u is not a head!\n", i); > return NULL; > -- > 2.39.0