Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5646532img; Wed, 27 Mar 2019 12:22:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqzmLuEnHlaOAXKAMZXPxTu5gOlP7xsGhV+GTd33d4DJ8bKG9932i3wmwqZM73+F08ZyjOtI X-Received: by 2002:a17:902:be0a:: with SMTP id r10mr24648947pls.4.1553714521334; Wed, 27 Mar 2019 12:22:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553714521; cv=none; d=google.com; s=arc-20160816; b=cFgkBoKpZSZgpM2TAtRhAUl6GDyhUH0Jym8X2KAeygmCNO1sD2q2HeoTj6Xc9VM0qB nrq19FZLoLEmQPVMVcxzf74S26+9LGLEy4IYWnr7XbZ8ovl0bKbQMI1oc1v9htfJDQ2L OrTGaAyUR1w7gwDshTAEA7Tqj1SCE0DHSmE8wPR0J+yziYiVL72P+c+oSlOaqJPITAtq 7g6fCBM9NI11wgjTowumSN3MIyvgfLaMnTLb8s9mt7RIj3Kf5Ehi59yeBi5s/PBqgAAY /XyGMR5oEbENJVqSuEN897UEC01za6m1DCzJNWwBwkErmgf32O/q0JWzyM4UJkbnHq8H EDYA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=YRGKSLyIeMmlV/oQDOvFi08KUb78CCBCMUQdilwtBnk=; b=wpuNWTsP7lR9Cgz+KzOYfUcjINf0Ko0ddT8NDx0dfMU4ZPb9uPiMNmN0qaOLsPLp35 kSjT6JhK/Gjlg1Tb1xJqMheN0bxYFLiRjjKIxTqS8IkAeefPaKwaWJJG0V4e4DS9kXcT v8w8b0nJKN94c0DS6seD7h4KzqbSf1rWif7Vbp+4PFdtMZMbssLQnCT0iye+gBngnNA3 s3EmIxsHmNN21PRdD+X9qS8eDRIHG6qlIwxTzcZvnULXozx7tm8OOsa98dMfq0cJI3LB eAPy/lkd+D60ohmsO3D50xPLNW2wNcPBg6sdNPip+E4Jnel2mcpIHrRJIrOUUSOt5L1e lM6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pII9+LTr; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u11si18409673pgp.205.2019.03.27.12.21.45; Wed, 27 Mar 2019 12:22: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=@kernel.org header.s=default header.b=pII9+LTr; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388804AbfC0TVA (ORCPT + 99 others); Wed, 27 Mar 2019 15:21:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:48934 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388089AbfC0SHG (ORCPT ); Wed, 27 Mar 2019 14:07:06 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AC2A12183F; Wed, 27 Mar 2019 18:07:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710026; bh=tY6m4gMFrbq290y8iLZ6xF/E/AavLNKoa550KMQGXfM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pII9+LTrObQaGbko5geEVCOYHzyhCup7ud5XkbcoIzchE2NoZhbb2+iCtRY4EqgyC 08mpIIzwtkW3vODWad8x0qxVdzvTIgUCMIy2wCbrCT8nmEBSICEjUxdZm1nKpngkd5 hqum8jyiV328rSJNnx8PJSUhNWa3DkzuuH3TWZfQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Coly Li , Jens Axboe , Sasha Levin , linux-bcache@vger.kernel.org Subject: [PATCH AUTOSEL 5.0 163/262] bcache: improve sysfs_strtoul_clamp() Date: Wed, 27 Mar 2019 14:00:18 -0400 Message-Id: <20190327180158.10245-163-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Coly Li [ 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