Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp998768yba; Thu, 4 Apr 2019 02:05:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqyYkEPp7Nkuft7QD1z3k5pFNIkgAz61T4d4pMCgwQOR5Sh915EjGX6/1Gy1C9K0k00VIoOO X-Received: by 2002:a63:5858:: with SMTP id i24mr4672423pgm.222.1554368731405; Thu, 04 Apr 2019 02:05:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554368731; cv=none; d=google.com; s=arc-20160816; b=qzLDHl/et41PnZqn7/f+AIG5zdIJxt4cRgspKSomSrJuIL9EUVcWx3bJxVLSksoG0R 7LWds2nfkDZepbp6UWZjOgl2xuDeJXOF5UkgyGIyii0b75fnzkx7e5RSy2WeB6FVg4F+ uL53MivTdpNTX56uUNkL89RGffbwCX7mekmOao6EhlQG0hNjIahP7j4JbX62Qr+zBpfW i8SWe+pwL/BHJ78IGF23licYhAZ9n4+Blx6974tNut62CHWZQRAcAPa0ZvrQFoJven7X M47MbZ+4VTHs3tG0tvIXLHuPeYQGJbzbTY9qBGqdhzN7eZbNMAXUTPsRVen9CH2uuxne gNxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4TuK/cMfE9Q3pAMvr7ip7vE1QInuktdSvYI4o5K5N9A=; b=l53jN8yj2LSils8m94B2nmjRtoDv0KTuM+JV1VyrEB5XtT+opxQSC36H/N7FWV0/aE Bez+HDE5gQOfX66M+5QjVFCUpLUaCiYHHsD5Swu76PkmJ9Yllwj6iWuMkZLQxcnadXyR lyk+AaMedEvCNnSim+/I98XputADIcczwhTfCRGhhcMshM6FB+zKNWO4Y/yLvB95HNbI Rij5XhpIm003ypztSp7G04msdFT/swNf/S3blShs+15QsF3WxqQSPaUnB1Ep6V7VKAyD Y0fnyZvRZiAaFg+ROkKn4N8qgAqBkvwV44NSyQhrau8N5km/cQFhxhoUIV/HIwal9Hnk Fv7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CpVDRraC; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u9si7617137plq.162.2019.04.04.02.05.16; Thu, 04 Apr 2019 02:05:31 -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=@kernel.org header.s=default header.b=CpVDRraC; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731756AbfDDJEo (ORCPT + 99 others); Thu, 4 Apr 2019 05:04:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:42546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732009AbfDDJEl (ORCPT ); Thu, 4 Apr 2019 05:04:41 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0A762214AF; Thu, 4 Apr 2019 09:04:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554368680; bh=ys9gareobFJiWEB5jfQPxCTj2DNSm2XpswfTj28bNUo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CpVDRraClhJdrBBYN7i2e8G6wO9myBzzJCkgWa76yB0qUdHUN3TePgpJWrDKyf0Lx XI0hp1QnhkbULyPmj3MycH65eF1N/IxwzUKsL9uMI1n5WkPnKKymZTnGHjApf64IR/ CPWeh0KhxSur6jkJF9wWvZ+pk+3U1HBpFyw7y0eU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Coly Li , Jens Axboe , Sasha Levin Subject: [PATCH 4.19 114/187] bcache: improve sysfs_strtoul_clamp() Date: Thu, 4 Apr 2019 10:47:31 +0200 Message-Id: <20190404084608.637431034@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084603.119654039@linuxfoundation.org> References: <20190404084603.119654039@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 596b5a5dd1bc2fa019fdaaae522ef331deef927f ] Currently sysfs_strtoul_clamp() is defined as, 82 #define sysfs_strtoul_clamp(file, var, min, max) \ 83 do { \ 84 if (attr == &sysfs_ ## file) \ 85 return strtoul_safe_clamp(buf, var, min, max) \ 86 ?: (ssize_t) size; \ 87 } while (0) The problem is, if bit width of var is less then unsigned long, min and max may not protect var from integer overflow, because overflow happens in strtoul_safe_clamp() before checking min and max. To fix such overflow in sysfs_strtoul_clamp(), to make min and max take effect, this patch adds an unsigned long variable, and uses it to macro strtoul_safe_clamp() to convert an unsigned long value in range defined by [min, max]. Then assign this value to var. By this method, if bit width of var is less than unsigned long, integer overflow won't happen before min and max are checking. Now sysfs_strtoul_clamp() can properly handle smaller data type like unsigned int, of cause min and max should be defined in range of unsigned int too. Signed-off-by: Coly Li Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/md/bcache/sysfs.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/md/bcache/sysfs.h b/drivers/md/bcache/sysfs.h index 3fe82425859c..0ad2715a884e 100644 --- a/drivers/md/bcache/sysfs.h +++ b/drivers/md/bcache/sysfs.h @@ -81,9 +81,16 @@ do { \ #define sysfs_strtoul_clamp(file, var, min, max) \ do { \ - if (attr == &sysfs_ ## file) \ - return strtoul_safe_clamp(buf, var, min, max) \ - ?: (ssize_t) size; \ + if (attr == &sysfs_ ## file) { \ + unsigned long v = 0; \ + ssize_t ret; \ + ret = strtoul_safe_clamp(buf, v, min, max); \ + if (!ret) { \ + var = v; \ + return size; \ + } \ + return ret; \ + } \ } while (0) #define strtoul_or_return(cp) \ -- 2.19.1