From patchwork Fri Feb 28 15:00:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Christophe de Dinechin X-Patchwork-Id: 11412433 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2C95D13A4 for ; Fri, 28 Feb 2020 15:02:03 +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 F29F524699 for ; Fri, 28 Feb 2020 15:02:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iOqADu2h" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F29F524699 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:48172 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7h9O-0006jk-1k for patchwork-qemu-devel@patchwork.kernel.org; Fri, 28 Feb 2020 10:02:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:41115) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j7h8b-00066K-1J for qemu-devel@nongnu.org; Fri, 28 Feb 2020 10:01:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j7h8V-0000D4-Hc for qemu-devel@nongnu.org; Fri, 28 Feb 2020 10:01:12 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:52823 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j7h8V-0000Cj-Dp for qemu-devel@nongnu.org; Fri, 28 Feb 2020 10:01:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582902067; 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; bh=lF7dFytZabe/LSqujU7qyGMtLhtI8F7FfsAnHdFPzUQ=; b=iOqADu2hHrm8HTC03m/ptlLmuUeBdj6ImFj6PqmzLNBQzPU3Uh0Msn/XywfC3RO57DKDOe Za6IjdEWaiRMkVTKsmCRx3JCq8M+IaYcbow7rheYeFiOUNjgrsKdTS2X8sxN5IR+0gk9R4 RP8kOF1u/B4jXOJVKHdi/oxKDgpbGqM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-16-bD-JyW1pNKmqxrrLWBWh9g-1; Fri, 28 Feb 2020 10:01:05 -0500 X-MC-Unique: bD-JyW1pNKmqxrrLWBWh9g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 238BB1005512; Fri, 28 Feb 2020 15:01:04 +0000 (UTC) Received: from turbo.com (unknown [10.36.117.0]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82C7460C18; Fri, 28 Feb 2020 15:01:02 +0000 (UTC) From: Christophe de Dinechin To: qemu-devel@nongnu.org Subject: [PATCH] scsi/qemu-pr-helper: Fix out-of-bounds access to trnptid_list[] Date: Fri, 28 Feb 2020 16:00:59 +0100 Message-Id: <20200228150059.2644362-1-dinechin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 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: Fam Zheng , Paolo Bonzini , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Compile error reported by gcc 10.0.1: scsi/qemu-pr-helper.c: In function ‘multipath_pr_out’: scsi/qemu-pr-helper.c:523:32: error: array subscript is outside array bounds of ‘struct transportid *[0]’ [-Werror=array-bounds] 523 | paramp.trnptid_list[paramp.num_transportid++] = id; | ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from scsi/qemu-pr-helper.c:36: /usr/include/mpath_persist.h:168:22: note: while referencing ‘trnptid_list’ 168 | struct transportid *trnptid_list[]; | ^~~~~~~~~~~~ scsi/qemu-pr-helper.c:424:35: note: defined here ‘paramp’ 424 | struct prout_param_descriptor paramp; | ^~~~~~ This highlights an actual implementation issue in function multipath_pr_out. The variable paramp is declared with type `struct prout_param_descriptor`, which is a struct terminated by an empty array in mpath_persist.h: struct transportid *trnptid_list[]; That empty array was filled with code that looked like that: trnptid_list[paramp.descr.num_transportid++] = id; This is an actual out-of-bounds access. The fix is to replace `paramp` with an anonymous struct that adds additional space for the data, called `trnptid_list_storage`. That space provides MATH_MX_TIDS entries, and is not accessed directly but through a pointer to `descr.trnptid_list`, in the unlikely case a future compiler inserts some padding between the two structs. Signed-off-by: Christophe de Dinechin --- scsi/qemu-pr-helper.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c index 0659ceef09..01013221b3 100644 --- a/scsi/qemu-pr-helper.c +++ b/scsi/qemu-pr-helper.c @@ -421,7 +421,12 @@ static int multipath_pr_out(int fd, const uint8_t *cdb, uint8_t *sense, int rq_servact = cdb[1]; int rq_scope = cdb[2] >> 4; int rq_type = cdb[2] & 0xf; - struct prout_param_descriptor paramp; + struct + { + struct prout_param_descriptor descr; + struct transportid *trnptid_list_storage[MPATH_MX_TIDS]; + } paramp; + struct transportid **trnptid_list = paramp.descr.trnptid_list; char transportids[PR_HELPER_DATA_SIZE]; int r; @@ -455,9 +460,9 @@ static int multipath_pr_out(int fd, const uint8_t *cdb, uint8_t *sense, * do the opposite). */ memset(¶mp, 0, sizeof(paramp)); - memcpy(¶mp.key, ¶m[0], 8); - memcpy(¶mp.sa_key, ¶m[8], 8); - paramp.sa_flags = param[20]; + memcpy(¶mp.descr.key, ¶m[0], 8); + memcpy(¶mp.descr.sa_key, ¶m[8], 8); + paramp.descr.sa_flags = param[20]; if (sz > PR_OUT_FIXED_PARAM_SIZE) { size_t transportid_len; int i, j; @@ -520,12 +525,13 @@ static int multipath_pr_out(int fd, const uint8_t *cdb, uint8_t *sense, return CHECK_CONDITION; } - paramp.trnptid_list[paramp.num_transportid++] = id; + assert(paramp.descr.num_transportid < MPATH_MX_TIDS); + trnptid_list[paramp.descr.num_transportid++] = id; } } r = mpath_persistent_reserve_out(fd, rq_servact, rq_scope, rq_type, - ¶mp, noisy, verbose); + ¶mp.descr, noisy, verbose); return mpath_reconstruct_sense(fd, r, sense); } #endif