Received: by 10.213.65.68 with SMTP id h4csp2120067imn; Sun, 8 Apr 2018 20:12:01 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+Xf0Anr6LkF1uWe5ZEEzOA9lppZHqh/edZYYJRY+IbTu4oPUbhhthu+2P74oWErw+GIyuO X-Received: by 10.101.98.90 with SMTP id q26mr24392494pgv.113.1523243521223; Sun, 08 Apr 2018 20:12:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523243521; cv=none; d=google.com; s=arc-20160816; b=iJjVLODWxoImDXwUaWWAOrH2iuV0iyl/FG5VMxLydNfRLAEv6gu0UwZsX6NDGhO0uH /Z3wY4hiU59OFnwylNp+odZrsZl+vmAeoSXASCfdbyV716hiW//b2cMM+Sibe092R0X7 p/ucmT4KsJ07kum66agnaCRL1P/Q1BjgfNW2NxKXahYt8mDiFKLqqUdgdcVJhcVIvAws TdMQxMAugulo3pbA+U/PE7fqZqx2d75NCo5LEBbu5q3It8s4hfrjML4lnsohdEOu9qsk nH2CoBHaMfEijf1MtLEJhXREJ/dL3U/NW8LAHsILCWvrvAuK66IBr5KG+NZq/F+f9c2t t2SA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=/s312puWS88UrLu1WagIDH5Qdn8TsbspiVKhMCOtwt4=; b=gG8vqpNcGVKjcU8xoe4fu2fJIeA69CW8MbuybfoRpTTjJBdj+FvLv3AiPqJTbFpR2m yeVfJTnWW2ctO3/KC48FFHEbIPTQ5AOP18bGygdbQMRGpKRiW3rJd5d6fc45IroZcX0+ TpO98/toDc/hmJkI/MYTypxm1Yqqd9PvvDPqOrmO3TBPgdxA5hTOtI/8dZlHgAlYG+ld iWDZkwIzWa2x1sXyVtU6+zqhS+36UsdRx5HMERuWyuIZm8rsL45fRD/sxtA+P7gbMPRv QXLUOPlpnc1JjbbMH8Q4qJQIbmTMk1I5qugNWHuCCzg358x+s8bbJsBW9umReVi5ph6O m+3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=SHw1IfQ5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q7si10539200pgf.461.2018.04.08.20.11.23; Sun, 08 Apr 2018 20:12:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=SHw1IfQ5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755953AbeDICgV (ORCPT + 99 others); Sun, 8 Apr 2018 22:36:21 -0400 Received: from mail-cys01nam02on0094.outbound.protection.outlook.com ([104.47.37.94]:22752 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755629AbeDIA1g (ORCPT ); Sun, 8 Apr 2018 20:27:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=/s312puWS88UrLu1WagIDH5Qdn8TsbspiVKhMCOtwt4=; b=SHw1IfQ5wG9OfUfW7MaYBVxWLnD4NyeLOnBfxbh8tlvhK1sLYb5+K9jb4m72rZeFmc005BUSkJWzE05HxNz3iTHBMOP/JzpzBHRgjQoUeoeeXoy8DELq+IzYcjOG/eqFrKgwsqy8Q85ZJ2MagQgdioH3MtmEsU7cF9Ir7apOuaE= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB1031.namprd21.prod.outlook.com (52.132.128.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.696.0; Mon, 9 Apr 2018 00:27:29 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059%2]) with mapi id 15.20.0696.003; Mon, 9 Apr 2018 00:27:29 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Kees Cook , Daniel Micay , "Martin K . Petersen" , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 050/293] scsi: csiostor: Avoid content leaks and casts Thread-Topic: [PATCH AUTOSEL for 4.9 050/293] scsi: csiostor: Avoid content leaks and casts Thread-Index: AQHTz5j7Ov/h/F4C+0aasIraEihH7g== Date: Mon, 9 Apr 2018 00:23:29 +0000 Message-ID: <20180409002239.163177-50-alexander.levin@microsoft.com> References: <20180409002239.163177-1-alexander.levin@microsoft.com> In-Reply-To: <20180409002239.163177-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB1031;7:PSFTNZtmotjmgdCWbZoc3gD1TiltziA5wyr/N9olBGnu4d8xxwmVKlEJHCmLe1M+BEisK65Xu2qxU0UXz7SIoIlsLyi7Jar7EDCg92DOPwgy0dNFoWDTc1fw59GK8brdmZEGqFZQfkJe32LksCCvQEx7s+kwOSfXhVece9AKymojtBpRymLVm8lNmoARuLWNWMFX46VQjQPRLfFUy989C4WXYyCOw7AkysFUQXSh6Gm/1QR4zTXpV0Yl8DTFSh+z;20:/cLH+r77uoV4oxazOb89XAQ7pzlFl3Xo/GydBGjaw5f5ONYskheKeicwB1+5NRXOSM65m/G7Csk4TrJRn4Mk1ufcZmMHDZx0buj3f392HDghz2/2o72d+Lf0gwJvbH3FUE4jtSgpQG25TL+pL0k2Cwn2n/f4v8z34fiwF1d5d9Y= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: 6a15c08f-51f9-48cc-684e-08d59db0add6 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB1031; x-ms-traffictypediagnostic: DM5PR2101MB1031: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(85827821059158)(146099531331640); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(5005006)(8121501046)(93006095)(93001095)(3231221)(944501327)(52105095)(3002001)(10201501046)(6055026)(61426038)(61427038)(6041310)(20161123558120)(20161123562045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(6072148)(201708071742011);SRVR:DM5PR2101MB1031;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB1031; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(376002)(366004)(39380400002)(346002)(39860400002)(189003)(199004)(2900100001)(10290500003)(99286004)(76176011)(14454004)(7736002)(11346002)(26005)(86612001)(72206003)(478600001)(36756003)(106356001)(66066001)(2616005)(186003)(22452003)(86362001)(68736007)(305945005)(54906003)(102836004)(486006)(110136005)(316002)(6506007)(446003)(107886003)(53936002)(4326008)(105586002)(6436002)(6486002)(476003)(6512007)(39060400002)(5660300001)(5250100002)(25786009)(3846002)(6116002)(1076002)(97736004)(3280700002)(2501003)(2906002)(81156014)(8676002)(10090500001)(81166006)(8936002)(3660700001)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB1031;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: oLlrzOSybHZZau2kseFUX7BCnWiMKkyTT/4kVN0TNP+IH+DXhb1KMAIRJjoK5KPvBLgGy8AVlca07/RBnp5BLNEDqP2nZrePZPU7hHq13fs9m3DlPObc154aFgsMYdKIggVIpxvQ4ARrsq2k2raiZPjSrKglGbHyxY6nb5fWCkPm9jmR9INs4fjrRCEoUkJnIc1YMrytHNuqnGcBJXbWGcyTTiOPMkAGAeo3gUYz9aIPBrbLCZgTx0cl/ksznTErljPidti7049gY5gr8yWnaVgBeRbr0CayJCuCWqxvTKKiBRspG2zxaTqkw3h5b+fsFzOpRKvJyP4kqc7b4V35+RbzBuKdbG+WXJ9tn8YjtuTpuLneDRgIHHYhPqDbnbE9EpnqTj0uhezhA+AL8cA8X/bySrY388k9ZKOMytwZDZA= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6a15c08f-51f9-48cc-684e-08d59db0add6 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:23:29.0655 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1031 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook [ Upstream commit 42c335f7e67029d2e01711f2f2bc6252277c8993 ] When copying attributes, the len argument was padded out and the resulting memcpy() would copy beyond the end of the source buffer. Avoid this, and use size_t for val_len to avoid all the casts. Similarly, avoid source buffer casts and use void *. Additionally enforces val_len can be represented by u16 and that the DMA buffer was not overflowed. Fixes the size of mfa, which is not FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN (but it will be padded up to 4). This was noticed by the future CONFIG_FORTIFY_SOURCE checks. Cc: Daniel Micay Signed-off-by: Kees Cook Acked-by: Varun Prakash Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/csiostor/csio_lnode.c | 43 +++++++++++++++++++++++-----------= ---- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/drivers/scsi/csiostor/csio_lnode.c b/drivers/scsi/csiostor/csi= o_lnode.c index c00b2ff72b55..be5ee2d37815 100644 --- a/drivers/scsi/csiostor/csio_lnode.c +++ b/drivers/scsi/csiostor/csio_lnode.c @@ -238,14 +238,23 @@ csio_osname(uint8_t *buf, size_t buf_len) } =20 static inline void -csio_append_attrib(uint8_t **ptr, uint16_t type, uint8_t *val, uint16_t le= n) +csio_append_attrib(uint8_t **ptr, uint16_t type, void *val, size_t val_len= ) { + uint16_t len; struct fc_fdmi_attr_entry *ae =3D (struct fc_fdmi_attr_entry *)*ptr; + + if (WARN_ON(val_len > U16_MAX)) + return; + + len =3D val_len; + ae->type =3D htons(type); len +=3D 4; /* includes attribute type and length */ len =3D (len + 3) & ~3; /* should be multiple of 4 bytes */ ae->len =3D htons(len); - memcpy(ae->value, val, len); + memcpy(ae->value, val, val_len); + if (len > val_len) + memset(ae->value + val_len, 0, len - val_len); *ptr +=3D len; } =20 @@ -335,7 +344,7 @@ csio_ln_fdmi_rhba_cbfn(struct csio_hw *hw, struct csio_= ioreq *fdmi_req) numattrs++; val =3D htonl(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT); csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_SUPPORTEDSPEED, - (uint8_t *)&val, + &val, FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN); numattrs++; =20 @@ -346,23 +355,22 @@ csio_ln_fdmi_rhba_cbfn(struct csio_hw *hw, struct csi= o_ioreq *fdmi_req) else val =3D htonl(CSIO_HBA_PORTSPEED_UNKNOWN); csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED, - (uint8_t *)&val, - FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN); + &val, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN); numattrs++; =20 mfs =3D ln->ln_sparm.csp.sp_bb_data; csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_MAXFRAMESIZE, - (uint8_t *)&mfs, FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN); + &mfs, sizeof(mfs)); numattrs++; =20 strcpy(buf, "csiostor"); csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_OSDEVICENAME, buf, - (uint16_t)strlen(buf)); + strlen(buf)); numattrs++; =20 if (!csio_hostname(buf, sizeof(buf))) { csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_HOSTNAME, - buf, (uint16_t)strlen(buf)); + buf, strlen(buf)); numattrs++; } attrib_blk->numattrs =3D htonl(numattrs); @@ -444,33 +452,32 @@ csio_ln_fdmi_dprt_cbfn(struct csio_hw *hw, struct csi= o_ioreq *fdmi_req) =20 strcpy(buf, "Chelsio Communications"); csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MANUFACTURER, buf, - (uint16_t)strlen(buf)); + strlen(buf)); numattrs++; csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_SERIALNUMBER, - hw->vpd.sn, (uint16_t)sizeof(hw->vpd.sn)); + hw->vpd.sn, sizeof(hw->vpd.sn)); numattrs++; csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODEL, hw->vpd.id, - (uint16_t)sizeof(hw->vpd.id)); + sizeof(hw->vpd.id)); numattrs++; csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODELDESCRIPTION, - hw->model_desc, (uint16_t)strlen(hw->model_desc)); + hw->model_desc, strlen(hw->model_desc)); numattrs++; csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_HARDWAREVERSION, - hw->hw_ver, (uint16_t)sizeof(hw->hw_ver)); + hw->hw_ver, sizeof(hw->hw_ver)); numattrs++; csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_FIRMWAREVERSION, - hw->fwrev_str, (uint16_t)strlen(hw->fwrev_str)); + hw->fwrev_str, strlen(hw->fwrev_str)); numattrs++; =20 if (!csio_osname(buf, sizeof(buf))) { csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_OSNAMEVERSION, - buf, (uint16_t)strlen(buf)); + buf, strlen(buf)); numattrs++; } =20 csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD, - (uint8_t *)&maxpayload, - FC_FDMI_HBA_ATTR_MAXCTPAYLOAD_LEN); + &maxpayload, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD_LEN); len =3D (uint32_t)(pld - (uint8_t *)cmd); numattrs++; attrib_blk->numattrs =3D htonl(numattrs); @@ -1794,6 +1801,8 @@ csio_ln_mgmt_submit_req(struct csio_ioreq *io_req, struct csio_mgmtm *mgmtm =3D csio_hw_to_mgmtm(hw); int rv; =20 + BUG_ON(pld_len > pld->len); + io_req->io_cbfn =3D io_cbfn; /* Upper layer callback handler */ io_req->fw_handle =3D (uintptr_t) (io_req); io_req->eq_idx =3D mgmtm->eq_idx; --=20 2.15.1