From patchwork Tue Jun 5 23:59:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 10449519 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BDA3E60146 for ; Wed, 6 Jun 2018 00:00:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB45529706 for ; Wed, 6 Jun 2018 00:00:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9F9B129756; Wed, 6 Jun 2018 00:00:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 445D229706 for ; Wed, 6 Jun 2018 00:00:08 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 1B787211E3717; Tue, 5 Jun 2018 17:00:08 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.65; helo=mga03.intel.com; envelope-from=vishal.l.verma@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 1C916211E3708 for ; Tue, 5 Jun 2018 17:00:07 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Jun 2018 17:00:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,481,1520924400"; d="scan'208";a="56848792" Received: from vverma7-mobl4.lm.intel.com ([10.254.41.126]) by orsmga003.jf.intel.com with ESMTP; 05 Jun 2018 17:00:06 -0700 From: Vishal Verma To: Subject: [ndctl PATCH v3] ndctl: refactor validation of the ars_status command Date: Tue, 5 Jun 2018 17:59:59 -0600 Message-Id: <20180605235959.11974-1-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.14.3 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP The APIs that iterate over the information contained in an ars_atatus command require a prior, successfully completed ars_status command struct. We were neglecting to verify that the firmware status too indicates a success. We were also incorrectly requiring that ars_status->status be zero, where as a positive status only indicates an underrun. The underrun is fine as the firmware is not expected to completely fill the max_ars_out sized buffer. Refactor this checking to mimic validate_ars_cap() which checks the firmware status, and fixes the check for the cmd status. Use this for ndctl_cmd_ars_in_progress as well which had the same (incorrect) cmd->status check. Reported-by: Tomasz Rochumski Tested-by: Jacek Zloch Cc: Dan Williams Signed-off-by: Vishal Verma --- ndctl/lib/ars.c | 69 ++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 26 deletions(-) v3: do cmd validation before dereferencing any of the fields in the command. diff --git a/ndctl/lib/ars.c b/ndctl/lib/ars.c index 1ff6cf7..1e0cfdc 100644 --- a/ndctl/lib/ars.c +++ b/ndctl/lib/ars.c @@ -195,24 +195,44 @@ NDCTL_EXPORT unsigned int ndctl_cmd_ars_cap_get_clear_unit( return 0; } +static bool __validate_ars_stat(struct ndctl_cmd *ars_stat) +{ + /* + * A positive status indicates an underrun, but that is fine since + * the firmware is not expected to completely fill the max_ars_out + * sized buffer. + */ + if (ars_stat->type != ND_CMD_ARS_STATUS || ars_stat->status < 0) + return false; + if ((ndctl_cmd_get_firmware_status(ars_stat) & ARS_STATUS_MASK) != 0) + return false; + return true; +} + +#define validate_ars_stat(ctx, ars_stat) \ +({ \ + bool __valid = __validate_ars_stat(ars_stat); \ + if (!__valid) \ + dbg(ctx, "expected sucessfully completed ars_stat command\n"); \ + __valid; \ +}) + NDCTL_EXPORT int ndctl_cmd_ars_in_progress(struct ndctl_cmd *cmd) { struct ndctl_ctx *ctx = ndctl_bus_get_ctx(cmd_to_bus(cmd)); - if (cmd->type == ND_CMD_ARS_STATUS && cmd->status == 0) { - if (cmd->ars_status->status == 1 << 16) { - /* - * If in-progress, invalidate the ndctl_cmd, so - * that if we're called again without a fresh - * ars_status command, we fail. - */ - cmd->status = 1; - return 1; - } + if (!validate_ars_stat(ctx, cmd)) return 0; - } - dbg(ctx, "invalid ars_status\n"); + if (ndctl_cmd_get_firmware_status(cmd) == 1 << 16) { + /* + * If in-progress, invalidate the ndctl_cmd, so + * that if we're called again without a fresh + * ars_status command, we fail. + */ + cmd->status = 1; + return 1; + } return 0; } @@ -220,11 +240,10 @@ NDCTL_EXPORT unsigned int ndctl_cmd_ars_num_records(struct ndctl_cmd *ars_stat) { struct ndctl_ctx *ctx = ndctl_bus_get_ctx(cmd_to_bus(ars_stat)); - if (ars_stat->type == ND_CMD_ARS_STATUS && ars_stat->status == 0) - return ars_stat->ars_status->num_records; + if (!validate_ars_stat(ctx, ars_stat)) + return 0; - dbg(ctx, "invalid ars_status\n"); - return 0; + return ars_stat->ars_status->num_records; } NDCTL_EXPORT unsigned long long ndctl_cmd_ars_get_record_addr( @@ -232,16 +251,15 @@ NDCTL_EXPORT unsigned long long ndctl_cmd_ars_get_record_addr( { struct ndctl_ctx *ctx = ndctl_bus_get_ctx(cmd_to_bus(ars_stat)); + if (!validate_ars_stat(ctx, ars_stat)) + return 0; + if (rec_index >= ars_stat->ars_status->num_records) { dbg(ctx, "invalid record index\n"); return 0; } - if (ars_stat->type == ND_CMD_ARS_STATUS && ars_stat->status == 0) - return ars_stat->ars_status->records[rec_index].err_address; - - dbg(ctx, "invalid ars_status\n"); - return 0; + return ars_stat->ars_status->records[rec_index].err_address; } NDCTL_EXPORT unsigned long long ndctl_cmd_ars_get_record_len( @@ -249,16 +267,15 @@ NDCTL_EXPORT unsigned long long ndctl_cmd_ars_get_record_len( { struct ndctl_ctx *ctx = ndctl_bus_get_ctx(cmd_to_bus(ars_stat)); + if (!validate_ars_stat(ctx, ars_stat)) + return 0; + if (rec_index >= ars_stat->ars_status->num_records) { dbg(ctx, "invalid record index\n"); return 0; } - if (ars_stat->type == ND_CMD_ARS_STATUS && ars_stat->status == 0) - return ars_stat->ars_status->records[rec_index].length; - - dbg(ctx, "invalid ars_status\n"); - return 0; + return ars_stat->ars_status->records[rec_index].length; } NDCTL_EXPORT struct ndctl_cmd *ndctl_bus_cmd_new_clear_error(