From patchwork Thu Feb 4 21:00:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 8227581 Return-Path: X-Original-To: patchwork-cifs-client@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 52780BEEE5 for ; Thu, 4 Feb 2016 21:01:32 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 481EC203A5 for ; Thu, 4 Feb 2016 21:01:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 282DF203A1 for ; Thu, 4 Feb 2016 21:01:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965548AbcBDVB1 (ORCPT ); Thu, 4 Feb 2016 16:01:27 -0500 Received: from mail-pa0-f53.google.com ([209.85.220.53]:34242 "EHLO mail-pa0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965622AbcBDVBV (ORCPT ); Thu, 4 Feb 2016 16:01:21 -0500 Received: by mail-pa0-f53.google.com with SMTP id uo6so21804504pac.1 for ; Thu, 04 Feb 2016 13:01:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qUIi7NRvKUkvrj8sUb59A13SUoHdz/WG9wyaf58QpvI=; b=ZTp8x31qKq09gnJc4vGX4aIJKYulg6ZsJBhaIa1cGBcpjfsVjb3XOFJ2txPRGi80F0 LR+mRtwAMnxHlpEuZAu0BDBAHmG31fY/dFbswQWrLEaXPdyxHbmQSs9m9r6gc1lbvmWc DpIrxArFqhdjqKYCsvI2FPbrdrsj9Rzr8OfUY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qUIi7NRvKUkvrj8sUb59A13SUoHdz/WG9wyaf58QpvI=; b=XfdBa2HyqlKpiqyadoKQUGNQHRpAwXqrEnWo9NYY0IIldMSN/p3u6mJwfhMTF/HsI0 etFyovYvI0DxlEHmnqbnfrFpHEhIPlCrY6nTJGub1rdqbju1wKBKD23Eo36fTiWFx2Y0 2QmnaZi9GoSbIaT2BSiu4VOwtTpdRnkEolvgbDTXUxfvitwwqFRME8EJXxcIR6zReO6z IbBWfbn+wXNwIVY6+R4cQYsLIAC7pRgVWaXu4mT5xVct55Went+HszeXt1FdQ+mORUZK G+QQ5stq6DmL0bPyPsnMspOfl/wxrtxifQ72Xo2p+R6ifQgc//6qABlW0xNNg8Jerfop 71sQ== X-Gm-Message-State: AG10YOSLLGmg9kJoAibMqDNNCntF6peLysNgDspqKZ6UaF3BsOVBGCuURd8FsVfHivqssQ== X-Received: by 10.66.142.201 with SMTP id ry9mr14366970pab.89.1454619679302; Thu, 04 Feb 2016 13:01:19 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p9sm19270722pfa.11.2016.02.04.13.01.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Feb 2016 13:01:18 -0800 (PST) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Joe Perches , Andy Shevchenko , Rasmus Villemoes , Daniel Borkmann , Amitkumar Karwar , Nishant Sarmukadam , Kalle Valo , Steve French , Michael Ellerman , Heiko Carstens , Martin Schwidefsky , x86@kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-wireless@vger.kernel.org, netdev@vger.kernel.org, linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/4] lib: move strtobool to kstrtobool Date: Thu, 4 Feb 2016 13:00:40 -0800 Message-Id: <1454619643-14444-2-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.6.3 In-Reply-To: <1454619643-14444-1-git-send-email-keescook@chromium.org> References: <1454619643-14444-1-git-send-email-keescook@chromium.org> Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Create the kstrtobool_from_user helper and moves strtobool logic into the new kstrtobool (matching all the other kstrto* functions). Provides an inline wrapper for existing strtobool callers. Signed-off-by: Kees Cook Reviewed-by: Andy Shevchenko --- include/linux/kernel.h | 3 +++ include/linux/string.h | 6 +++++- lib/kstrtox.c | 35 +++++++++++++++++++++++++++++++++++ lib/string.c | 29 ----------------------------- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index f31638c6e873..cdc25f47a23f 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -357,6 +357,7 @@ int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); +int __must_check kstrtobool(const char *s, unsigned int base, bool *res); int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); @@ -368,6 +369,8 @@ int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigne int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); +int __must_check kstrtobool_from_user(const char __user *s, size_t count, + unsigned int base, bool *res); static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) { diff --git a/include/linux/string.h b/include/linux/string.h index 9eebc66d957a..d2fb21b1081d 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -128,7 +128,11 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp); extern void argv_free(char **argv); extern bool sysfs_streq(const char *s1, const char *s2); -extern int strtobool(const char *s, bool *res); +extern int kstrtobool(const char *s, unsigned int base, bool *res); +static inline int strtobool(const char *s, bool *res) +{ + return kstrtobool(s, 0, res); +} #ifdef CONFIG_BINARY_PRINTF int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args); diff --git a/lib/kstrtox.c b/lib/kstrtox.c index 94be244e8441..e18f088704d7 100644 --- a/lib/kstrtox.c +++ b/lib/kstrtox.c @@ -321,6 +321,40 @@ int kstrtos8(const char *s, unsigned int base, s8 *res) } EXPORT_SYMBOL(kstrtos8); +/** + * kstrtobool - convert common user inputs into boolean values + * @s: input string + * @base: ignored + * @res: result + * + * This routine returns 0 iff the first character is one of 'Yy1Nn0'. + * Otherwise it will return -EINVAL. Value pointed to by res is + * updated upon finding a match. + */ +int kstrtobool(const char *s, unsigned int base, bool *res) +{ + if (!s) + return -EINVAL; + + switch (s[0]) { + case 'y': + case 'Y': + case '1': + *res = true; + return 0; + case 'n': + case 'N': + case '0': + *res = false; + return 0; + default: + break; + } + + return -EINVAL; +} +EXPORT_SYMBOL(kstrtobool); + #define kstrto_from_user(f, g, type) \ int f(const char __user *s, size_t count, unsigned int base, type *res) \ { \ @@ -345,3 +379,4 @@ kstrto_from_user(kstrtou16_from_user, kstrtou16, u16); kstrto_from_user(kstrtos16_from_user, kstrtos16, s16); kstrto_from_user(kstrtou8_from_user, kstrtou8, u8); kstrto_from_user(kstrtos8_from_user, kstrtos8, s8); +kstrto_from_user(kstrtobool_from_user, kstrtobool, bool); diff --git a/lib/string.c b/lib/string.c index 0323c0d5629a..1a90db9bc6e1 100644 --- a/lib/string.c +++ b/lib/string.c @@ -630,35 +630,6 @@ bool sysfs_streq(const char *s1, const char *s2) } EXPORT_SYMBOL(sysfs_streq); -/** - * strtobool - convert common user inputs into boolean values - * @s: input string - * @res: result - * - * This routine returns 0 iff the first character is one of 'Yy1Nn0'. - * Otherwise it will return -EINVAL. Value pointed to by res is - * updated upon finding a match. - */ -int strtobool(const char *s, bool *res) -{ - switch (s[0]) { - case 'y': - case 'Y': - case '1': - *res = true; - break; - case 'n': - case 'N': - case '0': - *res = false; - break; - default: - return -EINVAL; - } - return 0; -} -EXPORT_SYMBOL(strtobool); - #ifndef __HAVE_ARCH_MEMSET /** * memset - Fill a region of memory with the given value