From nobody Wed Nov 5 05:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1530612270160248.01247013871443; Tue, 3 Jul 2018 03:04:30 -0700 (PDT) Received: from localhost ([::1]:39307 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faIAf-0002vk-H0 for importer@patchew.org; Tue, 03 Jul 2018 06:04:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41107) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faI8k-00021y-NT for qemu-devel@nongnu.org; Tue, 03 Jul 2018 06:02:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1faI8g-0002wH-0J for qemu-devel@nongnu.org; Tue, 03 Jul 2018 06:02:30 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:54424 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1faI8f-0002w3-Pl for qemu-devel@nongnu.org; Tue, 03 Jul 2018 06:02:25 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 50BF14071181 for ; Tue, 3 Jul 2018 10:02:25 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-239.ams2.redhat.com [10.36.117.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id CBF8B1C5AA for ; Tue, 3 Jul 2018 10:02:24 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 3 Jul 2018 12:02:24 +0200 Message-Id: <20180703100224.6203-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 03 Jul 2018 10:02:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 03 Jul 2018 10:02:25 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'pbonzini@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH] pr-helper: avoid error on PR IN command with zero request size X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" After reading a PR IN command with zero request size in prh_read_request, the resp->result field will be uninitialized and the resp.sz field will be also uninitialized when returning to prh_co_entry. If resp->result =3D=3D GOOD (from a previous successful reply or just luck), then the assert in prh_write_response might not be triggered and uninitialized response will be sent. The fix is to remove the whole handling of sz =3D=3D 0 in prh_co_entry. Those errors apply only to PR OUT commands and it's perfectly okay to catch them later in do_pr_out and multipath_pr_out; the check for too-short parameters in fact doesn't apply in the easy SG_IO case, as it can be left to the target firmware even. The result is that prh_read_request does not fail requests anymore and prh_co_entry becomes simpler. Reported-by: Dima Stepanov Signed-off-by: Paolo Bonzini --- scsi/qemu-pr-helper.c | 63 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c index 0218d65bbf..c89a446a45 100644 --- a/scsi/qemu-pr-helper.c +++ b/scsi/qemu-pr-helper.c @@ -455,6 +455,14 @@ static int multipath_pr_out(int fd, const uint8_t *cdb= , uint8_t *sense, char transportids[PR_HELPER_DATA_SIZE]; int r; =20 + if (sz < PR_OUT_FIXED_PARAM_SIZE) { + /* Illegal request, Parameter list length error. This isn't fatal; + * we have read the data, send an error without closing the socket. + */ + scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM_LEN)); + return CHECK_CONDITION; + } + switch (rq_servact) { case MPATH_PROUT_REG_SA: case MPATH_PROUT_RES_SA: @@ -574,6 +582,12 @@ static int do_pr_out(int fd, const uint8_t *cdb, uint8= _t *sense, const uint8_t *param, int sz) { int resp_sz; + + if ((fcntl(fd, F_GETFL) & O_ACCMODE) =3D=3D O_RDONLY) { + scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE)); + return CHECK_CONDITION; + } + #ifdef CONFIG_MPATH if (is_mpath(fd)) { return multipath_pr_out(fd, cdb, sense, param, sz); @@ -690,21 +704,6 @@ static int coroutine_fn prh_read_request(PRHelperClien= t *client, errp) < 0) { goto out_close; } - if ((fcntl(client->fd, F_GETFL) & O_ACCMODE) =3D=3D O_RDONLY) { - scsi_build_sense(resp->sense, SENSE_CODE(INVALID_OPCODE)); - sz =3D 0; - } else if (sz < PR_OUT_FIXED_PARAM_SIZE) { - /* Illegal request, Parameter list length error. This isn't f= atal; - * we have read the data, send an error without closing the so= cket. - */ - scsi_build_sense(resp->sense, SENSE_CODE(INVALID_PARAM_LEN)); - sz =3D 0; - } - if (sz =3D=3D 0) { - resp->result =3D CHECK_CONDITION; - close(client->fd); - client->fd =3D -1; - } } =20 req->fd =3D client->fd; @@ -785,25 +784,23 @@ static void coroutine_fn prh_co_entry(void *opaque) break; } =20 - if (sz > 0) { - num_active_sockets++; - if (req.cdb[0] =3D=3D PERSISTENT_RESERVE_OUT) { - r =3D do_pr_out(req.fd, req.cdb, resp.sense, - client->data, sz); - resp.sz =3D 0; - } else { - resp.sz =3D sizeof(client->data); - r =3D do_pr_in(req.fd, req.cdb, resp.sense, - client->data, &resp.sz); - resp.sz =3D MIN(resp.sz, sz); - } - num_active_sockets--; - close(req.fd); - if (r =3D=3D -1) { - break; - } - resp.result =3D r; + num_active_sockets++; + if (req.cdb[0] =3D=3D PERSISTENT_RESERVE_OUT) { + r =3D do_pr_out(req.fd, req.cdb, resp.sense, + client->data, sz); + resp.sz =3D 0; + } else { + resp.sz =3D sizeof(client->data); + r =3D do_pr_in(req.fd, req.cdb, resp.sense, + client->data, &resp.sz); + resp.sz =3D MIN(resp.sz, sz); + } + num_active_sockets--; + close(req.fd); + if (r =3D=3D -1) { + break; } + resp.result =3D r; =20 if (prh_write_response(client, &req, &resp, &local_err) < 0) { break; --=20 2.17.1