From patchwork Wed Aug 9 14:11:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Block X-Patchwork-Id: 9890733 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 96FED60384 for ; Wed, 9 Aug 2017 14:13:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6FB2D28A9C for ; Wed, 9 Aug 2017 14:13:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6475128AA4; Wed, 9 Aug 2017 14:13:28 +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=-6.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 19EED28A9C for ; Wed, 9 Aug 2017 14:13:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752988AbdHIOMN (ORCPT ); Wed, 9 Aug 2017 10:12:13 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:41106 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752926AbdHIOML (ORCPT ); Wed, 9 Aug 2017 10:12:11 -0400 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v79EBF82036892 for ; Wed, 9 Aug 2017 10:12:10 -0400 Received: from e06smtp14.uk.ibm.com (e06smtp14.uk.ibm.com [195.75.94.110]) by mx0a-001b2d01.pphosted.com with ESMTP id 2c81bwa9n4-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 09 Aug 2017 10:12:10 -0400 Received: from localhost by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 9 Aug 2017 15:12:09 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp14.uk.ibm.com (192.168.101.144) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 9 Aug 2017 15:12:07 +0100 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v79EC77i20316198 for ; Wed, 9 Aug 2017 14:12:07 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 32EB052067 for ; Wed, 9 Aug 2017 14:08:00 +0100 (BST) Received: from bblock-ThinkPad-W530 (unknown [9.152.212.81]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id 197185205A for ; Wed, 9 Aug 2017 14:08:00 +0100 (BST) Received: from bblock (uid 1000) (envelope-from bblock@linux.vnet.ibm.com) id 312cf5e6 by bblock-ThinkPad-W530 (DragonFly Mail Agent v0.9); Wed, 09 Aug 2017 16:12:05 +0200 From: Benjamin Block To: "James E . J . Bottomley" , "Martin K . Petersen" , Jens Axboe Cc: Benjamin Block , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, Johannes Thumshirn , Christoph Hellwig , Steffen Maier , open-iscsi@googlegroups.com Subject: [RFC PATCH 3/6] bsg: scsi-transport: add compile-tests to prevent reply-buffer overflows Date: Wed, 9 Aug 2017 16:11:17 +0200 X-Mailer: git-send-email 2.12.2 In-Reply-To: References: In-Reply-To: References: X-TM-AS-GCONF: 00 x-cbid: 17080914-0016-0000-0000-000004E19C48 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17080914-0017-0000-0000-00002819E76F Message-Id: <0f448e7771f438025de755530778691ff535e36c.1502120928.git.bblock@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-08-09_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1708090220 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The BSG implementations use the bsg_job's reply buffer as storage for their own custom reply structures (e.g.: struct fc_bsg_reply or struct iscsi_bsg_reply). The size of bsg_job's reply buffer and those of the implementations is not dependent in any way the compiler can currently check. To make it easier to notice accidental violations add an explicit compile- time check that tests whether the implementations' reply buffer is at most as large as bsg_job's. To do so, we have to move the size-define from bsg.c to a common header. Signed-off-by: Benjamin Block Reviewed-by: Christoph Hellwig --- block/bsg.c | 3 +-- drivers/scsi/scsi_transport_fc.c | 3 +++ drivers/scsi/scsi_transport_iscsi.c | 3 +++ include/linux/bsg-lib.h | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/block/bsg.c b/block/bsg.c index 285b1b8126c3..b924f1c23c58 100644 --- a/block/bsg.c +++ b/block/bsg.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -74,8 +75,6 @@ static int bsg_major; static struct kmem_cache *bsg_cmd_cachep; -#define BSG_COMMAND_REPLY_BUFFERSIZE SCSI_SENSE_BUFFERSIZE - /* * our internal command type */ diff --git a/drivers/scsi/scsi_transport_fc.c b/drivers/scsi/scsi_transport_fc.c index 892fbd9800d9..ce6654b5d329 100644 --- a/drivers/scsi/scsi_transport_fc.c +++ b/drivers/scsi/scsi_transport_fc.c @@ -3736,6 +3736,9 @@ static int fc_bsg_dispatch(struct bsg_job *job) { struct Scsi_Host *shost = fc_bsg_to_shost(job); + BUILD_BUG_ON(sizeof(struct fc_bsg_reply) > + BSG_COMMAND_REPLY_BUFFERSIZE); + if (scsi_is_fc_rport(job->dev)) return fc_bsg_rport_dispatch(shost, job); else diff --git a/drivers/scsi/scsi_transport_iscsi.c b/drivers/scsi/scsi_transport_iscsi.c index a424eaeafeb0..4e021c949ad7 100644 --- a/drivers/scsi/scsi_transport_iscsi.c +++ b/drivers/scsi/scsi_transport_iscsi.c @@ -1483,6 +1483,9 @@ static int iscsi_bsg_host_dispatch(struct bsg_job *job) int cmdlen = sizeof(uint32_t); /* start with length of msgcode */ int ret; + BUILD_BUG_ON(sizeof(struct iscsi_bsg_reply) > + BSG_COMMAND_REPLY_BUFFERSIZE); + /* check if we have the msgcode value at least */ if (job->request_len < sizeof(uint32_t)) { ret = -ENOMSG; diff --git a/include/linux/bsg-lib.h b/include/linux/bsg-lib.h index e34dde2da0ef..85d7c7678cc6 100644 --- a/include/linux/bsg-lib.h +++ b/include/linux/bsg-lib.h @@ -25,6 +25,8 @@ #include +#define BSG_COMMAND_REPLY_BUFFERSIZE SCSI_SENSE_BUFFERSIZE + struct request; struct device; struct scatterlist;