Message ID | cover.1703324146.git.wqu@suse.com |
---|---|
Headers |
Return-Path: <linux-kernel+bounces-10369-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp1586480dyi; Sat, 23 Dec 2023 01:59:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IEydLVEwhdOwii36sFiK0+BTvWwqjHXraKx3Qv069FebG5KSZQheAo2fwfVe/Ja2cI+5itz X-Received: by 2002:a17:903:11c9:b0:1d3:ce25:ecc with SMTP id q9-20020a17090311c900b001d3ce250eccmr4704585plh.54.1703325556157; Sat, 23 Dec 2023 01:59:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703325556; cv=none; d=google.com; s=arc-20160816; b=jIjySHZr3FrowHwvdlOf8qceQ0ltOJwXhWxHig2WOPdXyIUEvZZRgwh6aTBNPI4TuR D0WfSuyllXeVK8gegnMRHrZqcF56jUuj2KeUhaWKTQlJgAJIhXb5EZQYFRxIC3Vuz6V1 qsEKH2w/4P4/ZgfAB/HGiAZsLgMy8ocATJGb2+C3DLpCORp22npk1YK3fwMynP+Augyl f/2Yn6p8xkde9LyNURu7VThwMHZDqbgfwV+5XJ3QlIrQ5LcTLu3w+fC6QhytAk/szjBB L9DEZTpW2/Yuaop6cp+AHbyf+tU9cJ20k6bwcQLFcvr6ZLtTwYNpVtttbXsaV6ffa847 X2dA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:to:from :dkim-signature:dkim-signature; bh=uZZNVmiXAnORfKDw7Spx/yDLniWTiCC/ccjJyHvjL+A=; fh=aq23pSLwbr03HoKdXnGr0/uCxlT9OZ+QQ/0yofwyUC8=; b=olXqPfwyH7Va5wKORDl4yW7mZzB2Uq1QAoIQ9jMyvnUVo2sbm3cWGb+Nj3kvrhHqqn 7l4moDW/TO+HxiehKiFv790lvtOTGYELJxvSJ9QOep2YI48X5MTMq/qD+erVpmxEvA8f X5B13eDPp1LtH4OgSwog7YTee8BIH1AP8xIORbEKMse0ZgjvHN4gf50MMIAwMNOjNBSO yEtKDyDdzrG7CvlMMhacMlLRNdG03sSIL7/MjGb67FyhPQTmKTSW5Kvae4avfh2rk22I NiTjbQiIbMerKJTjkVK3//7HPvhmaTCrLwAKjfT+2sXtq1ljzzkloVGAXBrTu9MmVOvC e4Yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=CcM9o1T9; dkim=pass header.i=@suse.com header.s=susede1 header.b=Er9z0CDZ; spf=pass (google.com: domain of linux-kernel+bounces-10369-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-10369-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id q16-20020a656850000000b005ce08e2dc9esi210646pgt.408.2023.12.23.01.59.15 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Dec 2023 01:59:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-10369-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=CcM9o1T9; dkim=pass header.i=@suse.com header.s=susede1 header.b=Er9z0CDZ; spf=pass (google.com: domain of linux-kernel+bounces-10369-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-10369-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 5B627B21E84 for <ouuuleilei@gmail.com>; Sat, 23 Dec 2023 09:58:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8FC72CA58; Sat, 23 Dec 2023 09:58:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="CcM9o1T9"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="Er9z0CDZ" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA0968F79; Sat, 23 Dec 2023 09:58:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D3E7521EFF; Sat, 23 Dec 2023 09:58:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1703325514; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=uZZNVmiXAnORfKDw7Spx/yDLniWTiCC/ccjJyHvjL+A=; b=CcM9o1T9Yhfx+Zw+7ZbYw1jBC0MQctpi0FJx/2+Rd9qOktYIFLrrz+csJ9HhrzBAjFl2nk 0xyj/WOyFkcBcNLR8XV7YZMdWtLeSk2TnXorOs6/nBE/0GLg+VULJNBZmGBchv5sdJqVMH colW3vl10h70ShklEz2pc6Di/1jclkA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1703325513; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=uZZNVmiXAnORfKDw7Spx/yDLniWTiCC/ccjJyHvjL+A=; b=Er9z0CDZkQhfiiNuwVSx1vYzP7xlUJEBwFA50QJmHAJ5P/+K9U9Z9MI9ocg5nRXCTUMe5o O/OIJhb8UQ734d1vf7EyfXfltzAY2gz6dJFxkQl30BhleFlbR46W+iOddvJu/OUgnmBelF X6Jh/tuWedlrqlwhm6whCfD+8Vs+Qo4= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 685051392C; Sat, 23 Dec 2023 09:58:30 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id yLm6BEavhmXmcgAAD6G6ig (envelope-from <wqu@suse.com>); Sat, 23 Dec 2023 09:58:30 +0000 From: Qu Wenruo <wqu@suse.com> To: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, christophe.jaillet@wanadoo.fr, andriy.shevchenko@linux.intel.com, David.Laight@ACULAB.COM, ddiss@suse.de Subject: [PATCH 0/3] kstrtox: introduce memparse_safe() Date: Sat, 23 Dec 2023 20:28:04 +1030 Message-ID: <cover.1703324146.git.wqu@suse.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Level: ****** X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Level: X-Spam-Flag: NO X-Spamd-Result: default: False [-1.81 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; FROM_HAS_DN(0.00)[]; FREEMAIL_ENVRCPT(0.00)[wanadoo.fr]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; DWL_DNSWL_HI(-3.50)[suse.com:dkim]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+]; MX_GOOD(-0.01)[]; RCPT_COUNT_SEVEN(0.00)[7]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim]; FREEMAIL_TO(0.00)[vger.kernel.org,linux-foundation.org,wanadoo.fr,linux.intel.com,ACULAB.COM,suse.de]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=Er9z0CDZ X-Spam-Score: -1.81 X-Rspamd-Queue-Id: D3E7521EFF X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786066298361846614 X-GMAIL-MSGID: 1786066298361846614 |
Series |
kstrtox: introduce memparse_safe()
|
|
Message
Qu Wenruo
Dec. 23, 2023, 9:58 a.m. UTC
Function memparse() lacks error handling: - If no valid number string at all In that case @retptr would just be updated and return value would be zero. - No overflown detection This applies to both the number string part, and the suffixes part. And since we have no way to indicate errors, we can get weird results like: "25E" -> 10376293541461622784 (9E) This is due to the fact that for "E" suffix, there is only 4 bits left, and 25 with 60 bits left shift would lead to overflow. (And decision to support for that "E" suffix is already cursed) So here we introduce a safer version of it: memparse_safe(), and mark the original one deprecated. Unfortunately I didn't find a good way to mark it deprecated, as with recent -Werror changes, '__deprecated' marco does not seem to warn anymore. The new helper has the following advantages: - Better overflow and invalid string detection The overflow detection is for both the numberic part, and with the suffix. Thus above "25E" would be rejected correctly. The invalid string part means if there is no valid number starts at the buffer, we return -EINVAL. - Allow caller to select the suffixes, and saner default ones The new default one would be "KMGTP", without the cursed and overflow prone "E". Some older code like setup_elfcorehdr() would benefit from it, if the code really wants to only allow "KMG" suffixes. - Keep the old @retptr behavior So the existing callers can easily migrate to the new one, without the need to do extra strsep() work. - Finally test cases The test case would cover more things other than the existing kstrtox tests: * The @retptr behavior Either for bad cases, which @retptr should not be touched, or for good cases, the @retptr is properly advanced, * Return value verification Make sure we distinguish -EINVAL and -ERANGE correctly. With the new helper, migrate btrfs to the interface, and since the @retptr behavior is the same, we won't cause any behavior change. Qu Wenruo (3): kstrtox: introduce a safer version of memparse() kstrtox: add unit tests for memparse_safe() btrfs: migrate to the newer memparse_safe() helper fs/btrfs/ioctl.c | 8 +- fs/btrfs/super.c | 8 ++ fs/btrfs/sysfs.c | 14 ++- include/linux/kernel.h | 8 +- include/linux/kstrtox.h | 15 +++ lib/cmdline.c | 5 +- lib/kstrtox.c | 96 ++++++++++++++++++ lib/test-kstrtox.c | 217 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 364 insertions(+), 7 deletions(-)
Comments
On Sat, Dec 23, 2023 at 08:28:04PM +1030, Qu Wenruo wrote: > Function memparse() lacks error handling: > > - If no valid number string at all > In that case @retptr would just be updated and return value would be > zero. > > - No overflown detection > This applies to both the number string part, and the suffixes part. > And since we have no way to indicate errors, we can get weird results > like: > > "25E" -> 10376293541461622784 (9E) > > This is due to the fact that for "E" suffix, there is only 4 bits > left, and 25 with 60 bits left shift would lead to overflow. > (And decision to support for that "E" suffix is already cursed) > > So here we introduce a safer version of it: memparse_safe(), and mark > the original one deprecated. > Unfortunately I didn't find a good way to mark it deprecated, as with > recent -Werror changes, '__deprecated' marco does not seem to warn > anymore. > > The new helper has the following advantages: > > - Better overflow and invalid string detection > The overflow detection is for both the numberic part, and with the > suffix. Thus above "25E" would be rejected correctly. > The invalid string part means if there is no valid number starts at > the buffer, we return -EINVAL. > > - Allow caller to select the suffixes, and saner default ones > The new default one would be "KMGTP", without the cursed and overflow > prone "E". > Some older code like setup_elfcorehdr() would benefit from it, if the > code really wants to only allow "KMG" suffixes. > > - Keep the old @retptr behavior > So the existing callers can easily migrate to the new one, without the > need to do extra strsep() work. > > - Finally test cases > The test case would cover more things other than the existing kstrtox > tests: > * The @retptr behavior > Either for bad cases, which @retptr should not be touched, > or for good cases, the @retptr is properly advanced, > > * Return value verification > Make sure we distinguish -EINVAL and -ERANGE correctly. > > With the new helper, migrate btrfs to the interface, and since the > @retptr behavior is the same, we won't cause any behavior change. Thank you for the prompt update, I will be off till end of January, and in any case this is material for v6.9+, so I will look at this afterwards.