Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5597056img; Wed, 27 Mar 2019 11:23:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqwn90lt1w5+e9zJPMqHKxzebVnPFQd68x0q5HtawQI6+FcVyaRloYznmgdhncTE/K5SQDuD X-Received: by 2002:a63:6ec5:: with SMTP id j188mr35747828pgc.191.1553710992894; Wed, 27 Mar 2019 11:23:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553710992; cv=none; d=google.com; s=arc-20160816; b=ymtVsTbs64TGy+tfT74bUAe/nvy+S2F5UBH2wDJj3aswiL0im3yCvh8EWVgyBM/Wc5 UyFwcRoAU8AwRaIHdVDbuvRBXNmcKy3fq+2CJjs46xgSPxhPvRZb+NAqEKt1eYQvH/PS SRborxEy2pjXDIKn7y84JA4YIK1hAEb0Ze/0UuQeSRTdRgvyjmb6dM0mDr8eHU7UvdzF /MrOIdqHemZZkkZf+A97LLS5p0dN7CGBR8/2dv2NulDq2Yt1HfluRsUHh29h3ES/ivgX U4sdXvcK0UORI/QkAlkwXhVaCNo4PWPJ5wlS8nJPLq6fXdhG8OCPmOmcdnnD3RxsoE7L mbjg== 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=eTry0XhRmtdQrMPv+jO1fMrcm4C/7hP8qX9hG2IkDGk=; b=TtQ2lU0YiZs4k4SVIRRz+BEQqtTGaNKHRrL4jkY/qG/gFIvVZbHogw6v9q5cKQrUp1 8BSfsq2nqOdxCBr8SILPgNXFRCBlof+jFMhlCXZTlJcqF4vjfdMY3EehnQCAgkoZYsED 6L+SxJyH1q+6yrcMRG9YfFJAdaUe+qESaRONi+6d50mPD91CKWocc+Bx60ryhDjM3thy jLEhgg+mXmrDNf3F0gn4ety4SBu6meAg+XoJQFz8w0a0CdHqMr5Mf2lCPo3mW2wZyCCG QqURQsoRu20wYugpF3uI3lwJAzHV0Wh0W+dEnT1VEwWpl23rfgw4865p5Z9xu+b79FIe MTlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zkedPeER; 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 t11si18271230pgp.229.2019.03.27.11.22.57; Wed, 27 Mar 2019 11:23:12 -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=zkedPeER; 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 S2391291AbfC0SWW (ORCPT + 99 others); Wed, 27 Mar 2019 14:22:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:40364 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391279AbfC0SWS (ORCPT ); Wed, 27 Mar 2019 14:22:18 -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 7C2E52147C; Wed, 27 Mar 2019 18:22:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710937; bh=FXdjSr8HVrfDYqWKqWAVa1HiI4r+mcXUJ3XGWwCbs4s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zkedPeER3NPu2AyENSPxJdGQaZnOiIGXoDut7lgMUCyVy9uLFX3ARUgpiXW0eLAaF XBZ1JQSAkC80hLRhs7haq4WossjhGaTHF5Xb6LYiY34RKpUZZ8s+DHB1NHA/PTzbn6 FH++HAzg9KY/UUauBKtdZKVJfz4qsAgUBGIqMXeU= 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 4.9 54/87] bcache: improve sysfs_strtoul_clamp() Date: Wed, 27 Mar 2019 14:20:07 -0400 Message-Id: <20190327182040.17444-54-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327182040.17444-1-sashal@kernel.org> References: <20190327182040.17444-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 0526fe92a683..e7a3c12aa66f 100644 --- a/drivers/md/bcache/sysfs.h +++ b/drivers/md/bcache/sysfs.h @@ -80,9 +80,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