From patchwork Fri Mar 9 22:33:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Kitt X-Patchwork-Id: 10272395 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 A4FB3605CE for ; Fri, 9 Mar 2018 22:34:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8EF8E2A026 for ; Fri, 9 Mar 2018 22:34:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82EF92A028; Fri, 9 Mar 2018 22:34:12 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id A26D52A026 for ; Fri, 9 Mar 2018 22:34:11 +0000 (UTC) Received: (qmail 17647 invoked by uid 550); 9 Mar 2018 22:34:09 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 17611 invoked from network); 9 Mar 2018 22:34:09 -0000 From: Stephen Kitt To: hare@suse.com, axboe@kernel.dk, jejb@linux.vnet.ibm.com, martin.petersen@oracle.com Cc: linux-scsi@vger.kernel.org, kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Stephen Kitt Subject: [PATCH] scsi: resolve COMMAND_SIZE at compile time Date: Fri, 9 Mar 2018 23:33:55 +0100 Message-Id: <20180309223355.21222-1-steve@sk2.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180309232933.14e39858@heffalump.sk2.org> References: <20180309232933.14e39858@heffalump.sk2.org> X-Virus-Scanned: ClamAV using ClamSMTP COMMAND_SIZE currently uses an array of values in block/scsi_ioctl.c. A number of device_handler functions use this to initialise arrays, and this is flagged by -Wvla. This patch replaces COMMAND_SIZE with a variant using a formula which can be resolved at compile time in cases where the opcode is fixed, resolving the array size and avoiding the VLA. The downside is that the code is less maintainable and that some call sites end up having more complex generated code. Since scsi_command_size_tbl is dropped, we can remove the dependency on BLK_SCSI_REQUEST from drivers/target/Kconfig. This was prompted by https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Stephen Kitt --- block/scsi_ioctl.c | 8 -------- drivers/target/Kconfig | 1 - include/scsi/scsi_common.h | 13 +++++++++++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c index 60b471f8621b..b9e176e537be 100644 --- a/block/scsi_ioctl.c +++ b/block/scsi_ioctl.c @@ -41,14 +41,6 @@ struct blk_cmd_filter { static struct blk_cmd_filter blk_default_cmd_filter; -/* Command group 3 is reserved and should never be used. */ -const unsigned char scsi_command_size_tbl[8] = -{ - 6, 10, 10, 12, - 16, 12, 10, 10 -}; -EXPORT_SYMBOL(scsi_command_size_tbl); - #include static int sg_get_version(int __user *p) diff --git a/drivers/target/Kconfig b/drivers/target/Kconfig index 4c44d7bed01a..b5f05b60cf06 100644 --- a/drivers/target/Kconfig +++ b/drivers/target/Kconfig @@ -4,7 +4,6 @@ menuconfig TARGET_CORE depends on SCSI && BLOCK select CONFIGFS_FS select CRC_T10DIF - select BLK_SCSI_REQUEST # only for scsi_command_size_tbl.. select SGL_ALLOC default n help diff --git a/include/scsi/scsi_common.h b/include/scsi/scsi_common.h index 731ac09ed231..48d950666376 100644 --- a/include/scsi/scsi_common.h +++ b/include/scsi/scsi_common.h @@ -15,8 +15,17 @@ scsi_varlen_cdb_length(const void *hdr) return ((struct scsi_varlen_cdb_hdr *)hdr)->additional_cdb_length + 8; } -extern const unsigned char scsi_command_size_tbl[8]; -#define COMMAND_SIZE(opcode) scsi_command_size_tbl[((opcode) >> 5) & 7] +/* + * SCSI command sizes are as follows, in bytes, for fixed size commands, per + * group: 6, 10, 10, 12, 16, 12, 10, 10. The top three bits of an opcode + * determine its group. + * The size table is encoded into a 32-bit value by subtracting each value + * from 16, resulting in a value of 1715488362 + * (6 << 28 + 6 << 24 + 4 << 20 + 0 << 16 + 4 << 12 + 6 << 8 + 6 << 4 + 10). + * Command group 3 is reserved and should never be used. + */ +#define COMMAND_SIZE(opcode) \ + (16 - (15 & (1715488362 >> (4 * (((opcode) >> 5) & 7))))) static inline unsigned scsi_command_size(const unsigned char *cmnd)