From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 844BCC433E0 for ; Mon, 15 Mar 2021 11:21:39 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E8E5E64E61 for ; Mon, 15 Mar 2021 11:21:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E8E5E64E61 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:38508 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lLlI1-0005to-RG for qemu-devel@archiver.kernel.org; Mon, 15 Mar 2021 07:21:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35438) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lLlGL-000598-4z for qemu-devel@nongnu.org; Mon, 15 Mar 2021 07:19:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:51834) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lLlGJ-0006yD-0R for qemu-devel@nongnu.org; Mon, 15 Mar 2021 07:19:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615807190; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fLZbLe6oL0ffU0NzKcBSnGDvtVFN3uJxIlOZYBwX+0g=; b=Y7nHqPyfvAEVipDKadiKAHhGTCsghGmO/AWJCAKY0anhhkcV9hz6rwClf4YdoRVXO5HDjl YDQRDOciBnXaLq1BzM3HmE8HDyWKP/yrwqKSOUz+EhPNcmrWGiIYf2LFagIxpgDM+JO+eR CG8VSXWbL8SGYJdo4Zj2jSwvq2gBuI0= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-59-zRZn7NlVNP6DxhtrY04Dhw-1; Mon, 15 Mar 2021 07:19:46 -0400 X-MC-Unique: zRZn7NlVNP6DxhtrY04Dhw-1 Received: by mail-wr1-f70.google.com with SMTP id z17so14928170wrv.23 for ; Mon, 15 Mar 2021 04:19:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=fLZbLe6oL0ffU0NzKcBSnGDvtVFN3uJxIlOZYBwX+0g=; b=M2mS7DU+XMTDgAXYMIjS0m9vw53AWzNz7piQMVPwLAb00sZRugE+ebOELOho5ZIIhl /qoaVRbEqWzgA2nedGyrTZAbwaiReTwWj7yelb9FhpKt72aa3ochtDFU0kt7eajYhXkg h28HOl1I+OhW7vEa4jKKc7VlqTmYg2Gq8uh2SW0o3T2J8XscxeQqOCCnGbz1tUHIAg/t Q/II0Mm3fskj56MSO6TZl02hAScm2zPSgascOAQTLQq02vameh41+hH56HCuPNOQrPpZ q2hy1UZJTxjIDtPrlV+PgrKoM71LQCvnfa2IyY7shQ4rRpWknmBRhQGQNFXftXlCNE5F PK1Q== X-Gm-Message-State: AOAM532aNVQwHNbjaYLdukLlBoRNHxgHUZ3XITT3n9q7VD/U/zU3dJKZ r0ZRjHYgyZlk/Us03K+5ataXJG2CKWWkPvQxAAF8qKgF9R5sYiCi0/g9nuURr54kMvRG7zccy+/ iqwCjnawwnLKQ5Gw= X-Received: by 2002:a7b:c0c4:: with SMTP id s4mr26306608wmh.9.1615807185722; Mon, 15 Mar 2021 04:19:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnEHCxEeFv1es3Ch4osKjLc97TUZcabLSLKYSeBwgYFyqN0EA0RKSR5mOXCBeolKKOX6rlSw== X-Received: by 2002:a7b:c0c4:: with SMTP id s4mr26306582wmh.9.1615807185362; Mon, 15 Mar 2021 04:19:45 -0700 (PDT) Received: from [192.168.1.36] (17.red-88-21-201.staticip.rima-tde.net. [88.21.201.17]) by smtp.gmail.com with ESMTPSA id d204sm12332065wmc.17.2021.03.15.04.19.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Mar 2021 04:19:44 -0700 (PDT) Subject: Re: [PATCH 2/2] hw/block/nvme: assert namespaces array indices To: Klaus Jensen , qemu-devel@nongnu.org References: <20210315110359.51450-1-its@irrelevant.dk> <20210315110359.51450-3-its@irrelevant.dk> From: =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9?= Message-ID: Date: Mon, 15 Mar 2021 12:19:43 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: <20210315110359.51450-3-its@irrelevant.dk> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.25, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Klaus Jensen , Max Reitz , Minwoo Im , Keith Busch Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" On 3/15/21 12:03 PM, Klaus Jensen wrote: > From: Klaus Jensen > > Coverity complains about a possible memory corruption in the > nvme_ns_attach and _detach functions. While we should not (famous last > words) be able to reach this function without nsid having previously > been validated, this is still an open door for future misuse. > > Make Coverity and maintainers happy by asserting that the index into the > array is valid. Also, while not detected by Coverity (yet), add an > assert in nvme_subsys_ns and nvme_subsys_register_ns as well since a > similar issue is exists there. > > Fixes: 037953b5b299 ("hw/block/nvme: support namespace detach") > Fixes: CID 1450757 > Fixes: CID 1450758 > Cc: Minwoo Im > Signed-off-by: Klaus Jensen > --- > hw/block/nvme-subsys.h | 2 ++ > hw/block/nvme.h | 10 ++++++++-- > hw/block/nvme-subsys.c | 7 +++++-- > 3 files changed, 15 insertions(+), 4 deletions(-) > > diff --git a/hw/block/nvme-subsys.h b/hw/block/nvme-subsys.h > index fb66ae752ad5..aafa04b84829 100644 > --- a/hw/block/nvme-subsys.h > +++ b/hw/block/nvme-subsys.h > @@ -54,6 +54,8 @@ static inline NvmeNamespace *nvme_subsys_ns(NvmeSubsystem *subsys, > return NULL; > } > > + assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES); > + > return subsys->namespaces[nsid]; > } > > diff --git a/hw/block/nvme.h b/hw/block/nvme.h > index 4955d649c7d4..45ba9dbc2131 100644 > --- a/hw/block/nvme.h > +++ b/hw/block/nvme.h > @@ -236,12 +236,18 @@ static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns) > > static inline void nvme_ns_attach(NvmeCtrl *n, NvmeNamespace *ns) > { > - n->namespaces[nvme_nsid(ns) - 1] = ns; > + uint32_t nsid = ns->params.nsid; Why not keep using nvme_nsid(ns)? > + assert(nsid && nsid <= NVME_MAX_NAMESPACES); > + > + n->namespaces[nsid - 1] = ns; > } > > static inline void nvme_ns_detach(NvmeCtrl *n, NvmeNamespace *ns) > { > - n->namespaces[nvme_nsid(ns) - 1] = NULL; > + uint32_t nsid = ns->params.nsid; Ditto. > + assert(nsid && nsid <= NVME_MAX_NAMESPACES); > + > + n->namespaces[nsid - 1] = NULL; > } > > static inline NvmeCQueue *nvme_cq(NvmeRequest *req) > diff --git a/hw/block/nvme-subsys.c b/hw/block/nvme-subsys.c > index af4804a819ee..2f6d3b47bacf 100644 > --- a/hw/block/nvme-subsys.c > +++ b/hw/block/nvme-subsys.c > @@ -47,15 +47,18 @@ int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp) > { > NvmeSubsystem *subsys = ns->subsys; > NvmeCtrl *n; > + uint32_t nsid = ns->params.nsid; Ditto. Preferably using nvme_nsid(): Reviewed-by: Philippe Mathieu-Daudé > int i; > > - if (subsys->namespaces[nvme_nsid(ns)]) { > + assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES); > + > + if (subsys->namespaces[nsid]) { > error_setg(errp, "namespace %d already registerd to subsy %s", > nvme_nsid(ns), subsys->parent_obj.id); > return -1; > } > > - subsys->namespaces[nvme_nsid(ns)] = ns; > + subsys->namespaces[nsid] = ns; > > for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) { > n = subsys->ctrls[i]; >