Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp10419018rwl; Wed, 11 Jan 2023 20:12:29 -0800 (PST) X-Google-Smtp-Source: AMrXdXt/unhRUk3MyfCa5ynYW9QlORiVO/W6KOCf6xzbGo6BCb29AxMNC15q04ESSVkn5U7oHBnh X-Received: by 2002:a17:907:20d6:b0:844:5d1f:95cc with SMTP id qq22-20020a17090720d600b008445d1f95ccmr59991972ejb.15.1673496749116; Wed, 11 Jan 2023 20:12:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673496749; cv=none; d=google.com; s=arc-20160816; b=a6LeLfBGWxWvTZcRgheszKxEBKQ7YaH7mlM5iheMhsftjl87qDbxHC4L4I4b5jh/41 VF9rRFit9GhNlPGNHml7fFZh91O5rVUFy/uvF1LR+hCK0KptyoAzWYazeErznkfGGonC y0nr+oDs89M4UHReXaZB++FTKyWG66JABRJaNmtqqSWRVf0MHjGuJMVCxj3KhRTedIIb w1MrJXCxKglOJyFRuNnf//bJLp/NFCQ9UtPJTi0ps91fg1ksARZZ4Ec230JaKwBrkL15 aImWUeLI/cAuqxLvMivWs1jPxTLxeZBNEJHB++MT4huknUwMyYV3qQ5JMGe6+NSsH7uq fN3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=9Ead6NbYjGEcOLExh9Mv61ETFXQQrhvNXO4fmiWeGhg=; b=uPGDtAuIrfzKXJbt0KB0MEO3Gkt1oNXu8XE/LCcpJbMoSBdME5VKQ+BldVuJih+1ZI Jc4etO7CCC+ceGRGoGHDuLiH2OiQzEsDd3cctmwqaQwLSdEKyIQBQ80m7BwoBPs66O6G YGJFuOANP/JuwgyluOMFHnzU36sO7Ds0ZnJ/HLt9dBTlmOT4hnfYpRXJJTR6lD6l1rXZ 2UrHrYLB3rPXXj4OwZ/t/Fh10L9af+ThCMtp9HASWtnIkaOCxR5qpSwDrphg2Gd9CBD2 JTIazwH6c0S6e3qiIkCiXMYvNAlFW5ofcF6Pw1g6DqRc4045DOsJyyFn7H81TzBMBO0p U+eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SM57v6Fy; 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 js22-20020a17090797d600b00860c51f7df4si3105887ejc.769.2023.01.11.20.12.16; Wed, 11 Jan 2023 20:12:29 -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=SM57v6Fy; 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 S230326AbjALEDm (ORCPT + 52 others); Wed, 11 Jan 2023 23:03:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230329AbjALEDh (ORCPT ); Wed, 11 Jan 2023 23:03:37 -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 CB67F4A97A for ; Wed, 11 Jan 2023 20:02:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673496168; 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=9Ead6NbYjGEcOLExh9Mv61ETFXQQrhvNXO4fmiWeGhg=; b=SM57v6Fy6RLpJYzThdkqInXSkLBBZW4mqglQjKbgRg8lRHP2nOKjDKobdoAL+qEyxdQEKj rgwwJmratw+yX0SKKQh+BoWm55ntWgMhSgece0A7xjRynSOOgiYizZsQ7aZdCcNL+tgGYx 6fFVelosa+DV1XUFguqGhCWIAyXhl1k= Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-125-CPzxIWB1PsGBXFO5ABZv5A-1; Wed, 11 Jan 2023 23:02:47 -0500 X-MC-Unique: CPzxIWB1PsGBXFO5ABZv5A-1 Received: by mail-oo1-f69.google.com with SMTP id z24-20020a4a9c98000000b004f209cc2866so3161646ooj.2 for ; Wed, 11 Jan 2023 20:02:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9Ead6NbYjGEcOLExh9Mv61ETFXQQrhvNXO4fmiWeGhg=; b=M8Ds0bG9p+O/Y1DgSqgyy8ylJ2/Ut9dC/UKu5SX3/yFrw3iLLuVXc+RqqcJppCo+rj Eb45a53hPB4N0BlEY3mivMZWuuNfIs00yCi6yhbGsfg+Np4r9cTA87ZxS2eBEk4vpEvi sAPq2nBvOMIX2nW4co8epd9yfYbII53paDPL2PzLz1VHA2Cea1vV2UZCKKKaOowsmz+W o2FJPWpcrGF7t3VYRpvo54u+mink2ZW/bkXLKjWGvWI5m2OFi2/xiwj2jCTJdPPxiues 7SzCE77/yEA9Kn2Kk09E9QK9xirN2NR9amOWBVmqatCC5NNcNabJZ2LNewr73XgLHqYf aFbg== X-Gm-Message-State: AFqh2krjlqp/TPz2XapgNdVvFK3pk7IvWxnmHgPtIcAag/xGOYZh09IH A2awUDt90eHR4g6A+ppAdsrXil2DIVbAOqLiqJqFm6dzZ0cBOpcfFiYnlFtbxIdrUvcbwdcoKR0 IUeKhOiyTMAs01jPwA3Tu0My9b126s/MCNwFjgDEm X-Received: by 2002:a05:6870:4413:b0:144:a97b:1ae2 with SMTP id u19-20020a056870441300b00144a97b1ae2mr4131307oah.35.1673496166794; Wed, 11 Jan 2023 20:02:46 -0800 (PST) X-Received: by 2002:a05:6870:4413:b0:144:a97b:1ae2 with SMTP id u19-20020a056870441300b00144a97b1ae2mr4131300oah.35.1673496166467; Wed, 11 Jan 2023 20:02:46 -0800 (PST) MIME-Version: 1.0 References: <20230111060730.24779-1-jasowang@redhat.com> In-Reply-To: From: Jason Wang Date: Thu, 12 Jan 2023 12:02:35 +0800 Message-ID: Subject: Re: [PATCH] vhost-scsi: unbreak any layout for response To: Stefan Hajnoczi Cc: mst@redhat.com, pbonzini@redhat.com, bcodding@redhat.com, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , Nicholas Bellinger Content-Type: text/plain; charset="UTF-8" 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 12, 2023 at 3:18 AM Stefan Hajnoczi wrote: > > On Wed, Jan 11, 2023 at 02:07:30PM +0800, Jason Wang wrote: > > Al Viro said: > > > > """ > > Since "vhost/scsi: fix reuse of &vq->iov[out] in response" > > we have this: > > cmd->tvc_resp_iov = vq->iov[vc.out]; > > cmd->tvc_in_iovs = vc.in; > > combined with > > iov_iter_init(&iov_iter, ITER_DEST, &cmd->tvc_resp_iov, > > cmd->tvc_in_iovs, sizeof(v_rsp)); > > in vhost_scsi_complete_cmd_work(). We used to have ->tvc_resp_iov > > _pointing_ to vq->iov[vc.out]; back then iov_iter_init() asked to > > set an iovec-backed iov_iter over the tail of vq->iov[], with > > length being the amount of iovecs in the tail. > > > > Now we have a copy of one element of that array. Fortunately, the members > > following it in the containing structure are two non-NULL kernel pointers, > > so copy_to_iter() will not copy anything beyond the first iovec - kernel > > pointer is not (on the majority of architectures) going to be accepted by > > access_ok() in copyout() and it won't be skipped since the "length" (in > > reality - another non-NULL kernel pointer) won't be zero. > > > > So it's not going to give a guest-to-qemu escalation, but it's definitely > > a bug. Frankly, my preference would be to verify that the very first iovec > > is long enough to hold rsp_size. Due to the above, any users that try to > > give us vq->iov[vc.out].iov_len < sizeof(struct virtio_scsi_cmd_resp) > > would currently get a failure in vhost_scsi_complete_cmd_work() > > anyway. > > """ > > > > However, the spec doesn't say anything about the legacy descriptor > > layout for the respone. So this patch tries to not assume the response > > to reside in a single separate descriptor which is what commit > > 79c14141a487 ("vhost/scsi: Convert completion path to use") tries to > > achieve towards to ANY_LAYOUT. > > > > This is done by allocating and using dedicate resp iov in the > > command. To be safety, start with UIO_MAXIOV to be consistent with the > > vhost core. > > > > Testing with the hacked virtio-scsi driver that use 1 descriptor for 1 > > byte in the response. > > > > Reported-by: Al Viro > > Cc: Benjamin Coddington > > Cc: Nicholas Bellinger > > Fixes: a77ec83a5789 ("vhost/scsi: fix reuse of &vq->iov[out] in response") > > Signed-off-by: Jason Wang > > --- > > drivers/vhost/scsi.c | 21 +++++++++++++++++---- > > 1 file changed, 17 insertions(+), 4 deletions(-) > > > > diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c > > index dca6346d75b3..7d6d70072603 100644 > > --- a/drivers/vhost/scsi.c > > +++ b/drivers/vhost/scsi.c > > @@ -80,7 +80,7 @@ struct vhost_scsi_cmd { > > struct scatterlist *tvc_prot_sgl; > > struct page **tvc_upages; > > /* Pointer to response header iovec */ > > - struct iovec tvc_resp_iov; > > + struct iovec *tvc_resp_iov; > > /* Pointer to vhost_scsi for our device */ > > struct vhost_scsi *tvc_vhost; > > /* Pointer to vhost_virtqueue for the cmd */ > > @@ -563,7 +563,7 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work) > > memcpy(v_rsp.sense, cmd->tvc_sense_buf, > > se_cmd->scsi_sense_length); > > > > - iov_iter_init(&iov_iter, ITER_DEST, &cmd->tvc_resp_iov, > > + iov_iter_init(&iov_iter, ITER_DEST, cmd->tvc_resp_iov, > > cmd->tvc_in_iovs, sizeof(v_rsp)); > > ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter); > > if (likely(ret == sizeof(v_rsp))) { > > @@ -594,6 +594,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, > > struct vhost_scsi_cmd *cmd; > > struct vhost_scsi_nexus *tv_nexus; > > struct scatterlist *sg, *prot_sg; > > + struct iovec *tvc_resp_iov; > > struct page **pages; > > int tag; > > > > @@ -613,6 +614,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, > > sg = cmd->tvc_sgl; > > prot_sg = cmd->tvc_prot_sgl; > > pages = cmd->tvc_upages; > > + tvc_resp_iov = cmd->tvc_resp_iov; > > memset(cmd, 0, sizeof(*cmd)); > > cmd->tvc_sgl = sg; > > cmd->tvc_prot_sgl = prot_sg; > > @@ -625,6 +627,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, > > cmd->tvc_data_direction = data_direction; > > cmd->tvc_nexus = tv_nexus; > > cmd->inflight = vhost_scsi_get_inflight(vq); > > + cmd->tvc_resp_iov = tvc_resp_iov; > > > > memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE); > > > > @@ -935,7 +938,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) > > struct iov_iter in_iter, prot_iter, data_iter; > > u64 tag; > > u32 exp_data_len, data_direction; > > - int ret, prot_bytes, c = 0; > > + int ret, prot_bytes, i, c = 0; > > u16 lun; > > u8 task_attr; > > bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI); > > @@ -1092,7 +1095,8 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) > > } > > cmd->tvc_vhost = vs; > > cmd->tvc_vq = vq; > > - cmd->tvc_resp_iov = vq->iov[vc.out]; > > + for (i = 0; i < vc.in ; i++) > > + cmd->tvc_resp_iov[i] = vq->iov[vc.out + i]; > > Where is the guarantee that vc.in < UIO_MAXIOV? > We limit it here in the vhost_virtqueue structure: struct iovec iov[UIO_MAXIOV]; And we pass sizeof(vq->iov) to vhost_get_vq_desc(): vc->head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), &vc->out, &vc->in, NULL, NULL); > > cmd->tvc_in_iovs = vc.in; > > > > pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", > > @@ -1461,6 +1465,7 @@ static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq) > > kfree(tv_cmd->tvc_sgl); > > kfree(tv_cmd->tvc_prot_sgl); > > kfree(tv_cmd->tvc_upages); > > + kfree(tv_cmd->tvc_resp_iov); > > } > > > > sbitmap_free(&svq->scsi_tags); > > @@ -1508,6 +1513,14 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds) > > goto out; > > } > > > > + tv_cmd->tvc_resp_iov = kcalloc(UIO_MAXIOV, > > + sizeof(struct page *), > > + GFP_KERNEL); > > Should sizeof(struct page *) be sizeof(struct iovec)? Yes, I will fix it. Thanks > > > + if (!tv_cmd->tvc_resp_iov) { > > + pr_err("Unable to allocate tv_cmd->tvc_resp_iov\n"); > > + goto out; > > + } > > + > > tv_cmd->tvc_prot_sgl = kcalloc(VHOST_SCSI_PREALLOC_PROT_SGLS, > > sizeof(struct scatterlist), > > GFP_KERNEL); > > -- > > 2.25.1 > >