Received: by 10.213.65.68 with SMTP id h4csp2117462imn; Sun, 8 Apr 2018 20:08:30 -0700 (PDT) X-Google-Smtp-Source: AIpwx48THavOoR3Ylr2+sRYzteSn9AqdjCM7DpfC2gkpy8NyUNCxKXRRP5qOWBq3eYPQZJ9i/TVv X-Received: by 2002:a17:902:594c:: with SMTP id e12-v6mr10123778plj.233.1523243310553; Sun, 08 Apr 2018 20:08:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523243310; cv=none; d=google.com; s=arc-20160816; b=RF7LRtZt/O0vYQBwy0fO1f2YD40yC2Ake+Fa6ZdUysN6YEiSduRqZ+P45nnF95et5H HX68jCi4TyhY/9s7ypmYVyX0jYL5LbmHkH/rooKEZuB+9zP+wqGv8H1IpyNOU/R2im+6 omHdkWjKm8lnaFc+WekViAj/jS87u0MjqCph+5L7agx217gWirxdEhLSzC4ry9ezzcu8 DAkumBQznagjW4uVMKUzp34yFE8alAMBWst+v3E0PVoEy28bqTo5rsYivO1ZCMi97ycp xSiflQU89QU/BLhgvvEcWNs5OHntO2iHkeheobadP4ewaNjhhAGYPuQnDSZOvUeI4kwQ CbPg== 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=oIdgS9vF2iJ02j2w/3AUPx2TG0SaJE3cZ67FGQYaQMM=; b=N2q0mw2qaQvDIuHO2eqQVwOk9SLujZB4riWMa2x9P2q3m1fzzfDOrVTwm3lcA4Q+Yi vt86elqLB9iQd7CzJt6seig01aWaE31RHc0ldpb3+Vjxv2MW7oTsfXQXZRpi6JnqXBQw LV8hs2lq1P5ic5p58QMeIy9NIY4XnyriGUnKehnzDaigWeTouFCq2D+mF0I0pj/jG0XW d1q4onG4NGEBqOfr+vJVwJiEi5MYK+AEw0aUs61wwyq2lFp81CEacYTkZhhyCswMdCFO aUU744DNYhRLUXlrCD0wpZoD0WW4mjxqF27SX+7BFSJiStuF7t+TOqNt08OGD+zxjo1I RkLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=nv+qvJcT; 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 f32-v6si16804471plf.415.2018.04.08.20.07.53; Sun, 08 Apr 2018 20:08:30 -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=nv+qvJcT; 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 S932195AbeDICCL (ORCPT + 99 others); Sun, 8 Apr 2018 22:02:11 -0400 Received: from mail-sn1nam01on0103.outbound.protection.outlook.com ([104.47.32.103]:45281 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756356AbeDIAcK (ORCPT ); Sun, 8 Apr 2018 20:32:10 -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=oIdgS9vF2iJ02j2w/3AUPx2TG0SaJE3cZ67FGQYaQMM=; b=nv+qvJcTtctho+W1/DILSReOfI170AdqqpWoWalY6V7HakVS+ebjJSnFZVRLT6MmRlhAny306c5s+EoEaZQbmmdDMHWIbL9CcUBQ3lYKr80Z9y2n4xKhf3C+yDg2w8GVESkm1q8aa6YjEf4yeEeTTR+SW1cxtyqlC9OtLoPwRKg= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0870.namprd21.prod.outlook.com (10.167.110.165) 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:32:06 +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:32:06 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Dmitry Monakhov , Christoph Hellwig , Jens Axboe , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 199/293] block: guard bvec iteration logic Thread-Topic: [PATCH AUTOSEL for 4.9 199/293] block: guard bvec iteration logic Thread-Index: AQHTz5lDrae7ZH+8K0ycsFbaLTq56Q== Date: Mon, 9 Apr 2018 00:25:29 +0000 Message-ID: <20180409002239.163177-199-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;DM5PR2101MB0870;7:14gI5Q5Z+DKzfV0CTMwdKKThmt+sHxT+sxt4pKQv5cEzM9Vp0vQtnZo4vOSNf2CsirdpR1FXyyfs+tJiVx8lL2M90Hmg3BdEzXEgUqfgsFsIB7cux/FRMwp+fAn785NdLhdJJuaebXWFcNkkoHhnQuUq8n/zRlKCNG6rLdgja097536SYzqW49lm81MSPytxa1B3x3hheTz/7wSzdTWFICdDhXJsmry0qPAfVCptm1MoLYIvuCdMDl2GbUbKcsS+;20:DlM9U3PPy6nfpRfu3TWD0s42l2D6sTMRIlmtDG4FykBTSn8uOmudd/p/1m6yHMyzeU/9GAWirUylGrqUVXo82zNFUUV3Urxl4AexidrHh2NtBtFdgOIBHsASsRUsYC5zo/j47EIVRLchfFhtBfd0FdLlri4epYLM6GDtlLuX0RQ= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: 11953471-8584-4edb-d11d-08d59db152d9 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0870; x-ms-traffictypediagnostic: DM5PR2101MB0870: 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)(146099531331640); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(8121501046)(5005006)(3002001)(10201501046)(3231221)(944501327)(52105095)(93006095)(93001095)(6055026)(61426038)(61427038)(6041310)(20161123558120)(20161123560045)(20161123564045)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:DM5PR2101MB0870;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0870; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(346002)(39860400002)(366004)(396003)(39380400002)(376002)(199004)(189003)(53936002)(2906002)(6512007)(102836004)(2616005)(76176011)(14454004)(446003)(10090500001)(11346002)(476003)(486006)(107886003)(2900100001)(97736004)(66066001)(6506007)(6436002)(106356001)(99286004)(105586002)(3660700001)(6116002)(86612001)(3280700002)(6666003)(2501003)(10290500003)(26005)(8676002)(8936002)(478600001)(68736007)(5250100002)(5660300001)(316002)(4326008)(72206003)(186003)(110136005)(6486002)(7736002)(25786009)(3846002)(86362001)(36756003)(22452003)(81156014)(81166006)(305945005)(54906003)(1076002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0870;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: /YElMYFTmGNLEDLq7ZqaWFFwYGmOd8c+Fp3QBO/23Wpe35ele7+sOxNahycJeoixamU2Qla6PBYQ3aolkaeguLsUDeanJm+ZeBvog1HipOUKM96Z3G/HWedvv55HWeUJbJUPn092QE7SYhggcqzY6DkWI0Qboa2rYvSBYTGoBHB6RRbBFqRESoIZO8FZPivZKuttwOUG0g+Cju3mo6OJrZJwAbsSQKOPFr3APQH+LynEpeUbYgWZjRjqfbSSXy2FZYQmZEjsEM9QJhC/0hP0+3gh5fmeIDsubI0z7T/Mf2QETVk5wwP0oZqXoPaTLWaoW+VRUjVdKOu6p/STUsCT8kNt8jKd/XRLwD+PIhgOBht1I4rrxCGuyIl0wMsFr3ipGVxOHbVhtDnu9FXyoaAhEBFeW6PMaq9WQzrRHslDRns= 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: 11953471-8584-4edb-d11d-08d59db152d9 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:25:29.5033 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0870 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dmitry Monakhov [ Upstream commit b1fb2c52b2d85f51f36f1661409f9aeef94265ff ] Currently if some one try to advance bvec beyond it's size we simply dump WARN_ONCE and continue to iterate beyond bvec array boundaries. This simply means that we endup dereferencing/corrupting random memory region. Sane reaction would be to propagate error back to calling context But bvec_iter_advance's calling context is not always good for error handling. For safity reason let truncate iterator size to zero which will break external iteration loop which prevent us from unpredictable memory range corruption. And even it caller ignores an error, it will corrupt it's own bvecs, not others. This patch does: - Return error back to caller with hope that it will react on this - Truncate iterator size Code was added long time ago here 4550dd6c, luckily no one hit it in real life :) Signed-off-by: Dmitry Monakhov Reviewed-by: Ming Lei Reviewed-by: Martin K. Petersen [hch: switch to true/false returns instead of errno values] Signed-off-by: Christoph Hellwig Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/nvdimm/blk.c | 3 ++- drivers/nvdimm/btt.c | 3 ++- include/linux/bio.h | 4 +++- include/linux/bvec.h | 14 +++++++++----- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/nvdimm/blk.c b/drivers/nvdimm/blk.c index 77db9795510f..ac6d6771d47c 100644 --- a/drivers/nvdimm/blk.c +++ b/drivers/nvdimm/blk.c @@ -106,7 +106,8 @@ static int nd_blk_rw_integrity(struct nd_namespace_blk = *nsblk, =20 len -=3D cur_len; dev_offset +=3D cur_len; - bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len); + if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len)) + return -EIO; } =20 return err; diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c index 0c46ada027cf..add695bc2cb9 100644 --- a/drivers/nvdimm/btt.c +++ b/drivers/nvdimm/btt.c @@ -1075,7 +1075,8 @@ static int btt_rw_integrity(struct btt *btt, struct b= io_integrity_payload *bip, =20 len -=3D cur_len; meta_nsoff +=3D cur_len; - bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len); + if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len)) + return -EIO; } =20 return ret; diff --git a/include/linux/bio.h b/include/linux/bio.h index 97cb48f03dc7..9a804d65a50e 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -171,8 +171,10 @@ static inline void bio_advance_iter(struct bio *bio, s= truct bvec_iter *iter, =20 if (bio_no_advance_iter(bio)) iter->bi_size -=3D bytes; - else + else { bvec_iter_advance(bio->bi_io_vec, iter, bytes); + /* TODO: It is reasonable to complete bio with error here. */ + } } =20 #define __bio_for_each_segment(bvl, bio, iter, start) \ diff --git a/include/linux/bvec.h b/include/linux/bvec.h index 89b65b82d98f..de317b4c13c1 100644 --- a/include/linux/bvec.h +++ b/include/linux/bvec.h @@ -22,6 +22,7 @@ =20 #include #include +#include =20 /* * was unsigned short, but we might as well be ready for > 64kB I/O pages @@ -66,12 +67,14 @@ struct bvec_iter { .bv_offset =3D bvec_iter_offset((bvec), (iter)), \ }) =20 -static inline void bvec_iter_advance(const struct bio_vec *bv, - struct bvec_iter *iter, - unsigned bytes) +static inline bool bvec_iter_advance(const struct bio_vec *bv, + struct bvec_iter *iter, unsigned bytes) { - WARN_ONCE(bytes > iter->bi_size, - "Attempted to advance past end of bvec iter\n"); + if (WARN_ONCE(bytes > iter->bi_size, + "Attempted to advance past end of bvec iter\n")) { + iter->bi_size =3D 0; + return false; + } =20 while (bytes) { unsigned iter_len =3D bvec_iter_len(bv, *iter); @@ -86,6 +89,7 @@ static inline void bvec_iter_advance(const struct bio_vec= *bv, iter->bi_idx++; } } + return true; } =20 #define for_each_bvec(bvl, bio_vec, iter, start) \ --=20 2.15.1