From patchwork Tue Jan 2 04:12:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 184213 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4262445dyb; Mon, 1 Jan 2024 20:13:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IFhqqZR7J3Mwa4GKeF0dQjWT3Ivuz3BpinA0cke90WukxRGMqs83hPxzSfN0BAV/vcIdYBM X-Received: by 2002:a05:6214:2307:b0:680:c1e3:74e7 with SMTP id gc7-20020a056214230700b00680c1e374e7mr622538qvb.100.1704168802284; Mon, 01 Jan 2024 20:13:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704168802; cv=none; d=google.com; s=arc-20160816; b=UTwO1g/E1SmRqBPtHdRdAES4ppZZML33HEpvwd5xq/VJQhDXTsLDU3VU4/piFTOpS9 Gm+pnvXcl5S+g0gtBYN8Bb8qXwu/a5MQnau9I+3wDvSJLZv9ASwDqBuKCmW1Va934rBc wXIDZF/aPojVjczY6RD3e4SfJ0u/FlUqxouR+0fR3AI3IqwMrUvlhvw/siBGiN9WMX4D b+p2rt4C1HzD6y6PmBjTjQfHgGRa+UCJ945WK2qA7/RUNVldvPedr2XwjFe9D2mouaFb X1bi+u5uwG5CXBTtLT24Dy0RBnFkPmUYikSb0W1GyczOdvsi2xWWFgO6lKQh5B4bbHUn tPIQ== 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:references:in-reply-to:message-id :date:subject:to:from:dkim-signature:dkim-signature; bh=zyETOa+ZPNwt5BzUQx7GY54PiItFOjBXBVV0dOwevSc=; fh=aq23pSLwbr03HoKdXnGr0/uCxlT9OZ+QQ/0yofwyUC8=; b=saw9RgRRtxBjy9ZgYeGSg3Lvtdxh55JOzAqdfBoDQSQtjTij4teTOU5uABNg+BgE/N 3bn8wz60RN6G7S4jQXklHLQsQkeCrIALUhVKGdBOoYnMJxlHsCI/BjVbdUmQW5ddExxM 2LCBAKY8i2ebWNLtH13hs8dnk0tkWUmXofpgdk8ZcIcT8rFZPqW8mdpntseT7qsUOep7 4kIpUVLhdecxCc4dJ7dqeRtb9WuSR0iPT/o2ar80DDSCcG3IR1mrlTe+6P8rlAAUmP42 BXefCTEuOtV3W7M9N/TxnsaMy9aWOUveG9ymEE+HqnDUcUWuKQfvJLTQxnk8MDhxCZDU Et4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=Ew3wJR+A; dkim=pass header.i=@suse.com header.s=susede1 header.b=Ew3wJR+A; spf=pass (google.com: domain of linux-kernel+bounces-14010-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14010-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id j15-20020a0ce00f000000b0067f8d43804dsi25125050qvk.453.2024.01.01.20.13.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 20:13:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14010-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=Ew3wJR+A; dkim=pass header.i=@suse.com header.s=susede1 header.b=Ew3wJR+A; spf=pass (google.com: domain of linux-kernel+bounces-14010-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14010-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 19D6A1C20F59 for ; Tue, 2 Jan 2024 04:13:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2F90B612B; Tue, 2 Jan 2024 04:12:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="Ew3wJR+A"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="Ew3wJR+A" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 9D55546A0; Tue, 2 Jan 2024 04:12:52 +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 [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-out2.suse.de (Postfix) with ESMTPS id 45F381FC85; Tue, 2 Jan 2024 04:12:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168765; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zyETOa+ZPNwt5BzUQx7GY54PiItFOjBXBVV0dOwevSc=; b=Ew3wJR+Ayz0OXED0tGwcXI2y1Tc20jrvhUhK/ynmFMrMDimAFcYuPJHraN9k9CdciuEDo1 jUmgSOHCzCrV5G4PBfkUZuD+W2NOVET8T91g5CJ+MmIOxtUDVs5SB95NcfZuKGAAXr7qWd dcGdh+F1tCotfK00pO2ViFeUB0J8+wI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168765; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zyETOa+ZPNwt5BzUQx7GY54PiItFOjBXBVV0dOwevSc=; b=Ew3wJR+Ayz0OXED0tGwcXI2y1Tc20jrvhUhK/ynmFMrMDimAFcYuPJHraN9k9CdciuEDo1 jUmgSOHCzCrV5G4PBfkUZuD+W2NOVET8T91g5CJ+MmIOxtUDVs5SB95NcfZuKGAAXr7qWd dcGdh+F1tCotfK00pO2ViFeUB0J8+wI= 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 ACB15136D1; Tue, 2 Jan 2024 04:12:41 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id ELQgFjmNk2UHQQAAD6G6ig (envelope-from ); Tue, 02 Jan 2024 04:12:41 +0000 From: Qu Wenruo 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 v2 1/4] kstrtox: always skip the leading "0x" even if no more valid chars Date: Tue, 2 Jan 2024 14:42:11 +1030 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: *** X-Spam-Score: 3.71 X-Spamd-Result: default: False [3.71 / 50.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; RCVD_COUNT_THREE(0.00)[3]; NEURAL_HAM_SHORT(-0.19)[-0.967]; RCPT_COUNT_SEVEN(0.00)[7]; FREEMAIL_TO(0.00)[vger.kernel.org,linux-foundation.org,wanadoo.fr,linux.intel.com,ACULAB.COM,suse.de]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-0.00)[22.48%]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_ENVRCPT(0.00)[wanadoo.fr]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-1.00)[-1.000]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786950505952208798 X-GMAIL-MSGID: 1786950505952208798 Currently the invalid string "0x" (only hex prefix, no valid chars followed) would make _parse_integer_fixup_radix() to treat it as octal. This is due to the fact that the function would only auto-detect hex if and only if there is any valid hex char after "0x". Or it would only go octal instead. Thankfully this won't affect our unit test, as "0x" would still be treated as failure. Since we treat the result as octal, the result value would be 0, leaving "x" as the tailing char and still fail kstrtox() functions. But for the incoming memparse_safe(), the remaining string would still be consumed by the caller, and we need to properly treat "0x" as an invalid string. So this patch would make _parse_integer_fixup_radix() to forcefully go hex no matter if there is any valid char following. Signed-off-by: Qu Wenruo --- lib/kstrtox.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kstrtox.c b/lib/kstrtox.c index d586e6af5e5a..41c9a499bbf3 100644 --- a/lib/kstrtox.c +++ b/lib/kstrtox.c @@ -27,7 +27,7 @@ const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) { if (*base == 0) { if (s[0] == '0') { - if (_tolower(s[1]) == 'x' && isxdigit(s[2])) + if (_tolower(s[1]) == 'x') *base = 16; else *base = 8; From patchwork Tue Jan 2 04:12:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 184215 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4262566dyb; Mon, 1 Jan 2024 20:13:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IEzh1sz1u9b61QfTmXouLwYWBZKYauOzCJCSlnrKleYVO09MYyljFU1CqpzrQ9CF+xot3M/ X-Received: by 2002:a05:6358:290b:b0:173:22a:635a with SMTP id y11-20020a056358290b00b00173022a635amr20688392rwb.30.1704168838699; Mon, 01 Jan 2024 20:13:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704168838; cv=none; d=google.com; s=arc-20160816; b=QxPPOJPCvXZOiIq2PBa95BZUa7iv39+8SSsiWsMPFNuDHbpzoQ5z7ACtSBKz4jzyij 8jLLlHWs+d2tjaOznU7r0B3zgYejxBzn7IocdHCSmvirrf2RBUqArJgdafDnnSO6B+LP VA7CGmMYg2iwJUiZuwm96jUNSIhYZUuF1zmw+tenUcrMmPF3w7rsfn7iSaxPo1cNABYW 0Sxtv7dfhLGl7812n3MuNIHei2SEeKVBfxo9qD+zEh1VL/Y5Kloy7KAuV7wFgfW+Rkwn oQoX41aiLhmJgW6SIQOZ8ZhrVa27650N8Oek/3jYYytTAoAfdvsTb2jN9JVPBCSKVsIH 7Cgg== 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:references:in-reply-to:message-id :date:subject:to:from:dkim-signature:dkim-signature; bh=9vrFCaFrttC4Fah/84T7IMgmejqZNCixLGCqqgGu32g=; fh=aq23pSLwbr03HoKdXnGr0/uCxlT9OZ+QQ/0yofwyUC8=; b=cYuOhE3C6nhw52DwMtT0n5Aeuc8glIfVagWO1Vwq6E8rXe4EZUxWb7SdDKO19mGL+5 4Y6U9xGiLT3ZR738TnPP2DjYyi2ju0TNov9AcFaQ6Zc7XS/cqBLkgeHUO2EcPZ24vdgG D9Cf380JWUJZfunEkgEaMkQUxobT2s1QIt75by+rVIWbapaq31H9foh1wy8nKUoxH+1G j7wa0emQiy2Unksiafv/W9qQylzFQ8yoFqdqZS/ZWk5+0VRswGmEEBLj+6CKXlmLWkNo PKhBxO8Ku9uQGDHTkWNFUfKsU9/MrKRqZETynpqR3he7yt1AG8wNNjgoS3bdmEF6rv6J CXdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=GfBzRyJ8; dkim=pass header.i=@suse.com header.s=susede1 header.b=GfBzRyJ8; spf=pass (google.com: domain of linux-kernel+bounces-14012-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14012-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id p2-20020a170903248200b001d4b0311cacsi3535524plw.234.2024.01.01.20.13.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 20:13:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14012-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=GfBzRyJ8; dkim=pass header.i=@suse.com header.s=susede1 header.b=GfBzRyJ8; spf=pass (google.com: domain of linux-kernel+bounces-14012-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14012-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 6D8F328211A for ; Tue, 2 Jan 2024 04:13:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BCF2ED285; Tue, 2 Jan 2024 04:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="GfBzRyJ8"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="GfBzRyJ8" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 44403568D; Tue, 2 Jan 2024 04:12:56 +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-out2.suse.de (Postfix) with ESMTPS id 5DCD31FC86; Tue, 2 Jan 2024 04:12:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168769; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9vrFCaFrttC4Fah/84T7IMgmejqZNCixLGCqqgGu32g=; b=GfBzRyJ88r27sLQoU0J7ZrMsDYH3KINcPwAFmCYcH0TeDGwUg585rGosJvvK6FSIZKju0I sgw0I/qAbldtzEAFcLZEk3l5T/9UXQscmstKVvbpjRoPxqMlUGH1R1EdaNam1CmKgeKHUK A2F0i9sz8ogmJvykksberbJuQkvGLs4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168769; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9vrFCaFrttC4Fah/84T7IMgmejqZNCixLGCqqgGu32g=; b=GfBzRyJ88r27sLQoU0J7ZrMsDYH3KINcPwAFmCYcH0TeDGwUg585rGosJvvK6FSIZKju0I sgw0I/qAbldtzEAFcLZEk3l5T/9UXQscmstKVvbpjRoPxqMlUGH1R1EdaNam1CmKgeKHUK A2F0i9sz8ogmJvykksberbJuQkvGLs4= 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 02037136D1; Tue, 2 Jan 2024 04:12:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id 6KFDKD2Nk2UHQQAAD6G6ig (envelope-from ); Tue, 02 Jan 2024 04:12:45 +0000 From: Qu Wenruo 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 v2 2/4] kstrtox: introduce a safer version of memparse() Date: Tue, 2 Jan 2024 14:42:12 +1030 Message-ID: <12f3cbc956800709b2d5e1072bd22edc5720cbae.1704168510.git.wqu@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Level: * X-Spam-Level: Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=GfBzRyJ8 X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-1.00 / 50.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[suse.com:+]; MX_GOOD(-0.01)[]; RCPT_COUNT_SEVEN(0.00)[7]; NEURAL_HAM_SHORT(-0.19)[-0.963]; FREEMAIL_TO(0.00)[vger.kernel.org,linux-foundation.org,wanadoo.fr,linux.intel.com,ACULAB.COM,suse.de]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; RCVD_DKIM_ARC_DNSWL_HI(-1.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_ENVRCPT(0.00)[wanadoo.fr]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-1.00)[-1.000]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_IN_DNSWL_HI(-0.50)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_TLS_ALL(0.00)[] X-Spam-Score: -1.00 X-Rspamd-Queue-Id: 5DCD31FC86 X-Spam-Flag: NO X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786950544527888526 X-GMAIL-MSGID: 1786950544527888526 [BUGS] 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. [CAUSE] The root cause is already mentioned in the comments of the function, the usage of simple_strtoull() is the source of evil. Furthermore the function prototype is no good either, just returning an unsigned long long gives us no way to indicate an error. [FIX] Due to the prototype limits, we can not have a drop-in replacement for memparse(). This patch can only help by introduce a new helper, memparse_safe(), and mark the old memparse() deprecated. The new memparse_safe() has the following improvement: - Invalid string detection If no number string can be detected at all, -EINVAL would be returned. - Better overflow detection Both the string part and the extra left shift would have overflow detection. Any overflow would result -ERANGE. - Safer default suffix selection The helper allows the caller to choose the suffixes that they want to use. But only "KMGTP" are recommended by default since the "E" leaves only 4 bits before overflow. For those callers really know what they are doing, they can still manually to include all suffixes. Due to the prototype change, callers should migrate to the new one and change their code and add extra error handling. Signed-off-by: Qu Wenruo Reviewed-by: David Disseldorp --- include/linux/kernel.h | 8 +++- include/linux/kstrtox.h | 15 +++++++ lib/cmdline.c | 5 ++- lib/kstrtox.c | 96 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 2 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index d9ad21058eed..b1b6da60ea43 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -201,7 +201,13 @@ void do_exit(long error_code) __noreturn; extern int get_option(char **str, int *pint); extern char *get_options(const char *str, int nints, int *ints); -extern unsigned long long memparse(const char *ptr, char **retptr); + +/* + * DEPRECATED, lack of any kind of error handling. + * + * Use memparse_safe() from lib/kstrtox.c instead. + */ +extern __deprecated unsigned long long memparse(const char *ptr, char **retptr); extern bool parse_option_str(const char *str, const char *option); extern char *next_arg(char *args, char **param, char **val); diff --git a/include/linux/kstrtox.h b/include/linux/kstrtox.h index 7fcf29a4e0de..53a1e059dd31 100644 --- a/include/linux/kstrtox.h +++ b/include/linux/kstrtox.h @@ -9,6 +9,21 @@ int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); int __must_check _kstrtol(const char *s, unsigned int base, long *res); +enum memparse_suffix { + MEMPARSE_SUFFIX_K = 1 << 0, + MEMPARSE_SUFFIX_M = 1 << 1, + MEMPARSE_SUFFIX_G = 1 << 2, + MEMPARSE_SUFFIX_T = 1 << 3, + MEMPARSE_SUFFIX_P = 1 << 4, + MEMPARSE_SUFFIX_E = 1 << 5, +}; + +#define MEMPARSE_SUFFIXES_DEFAULT (MEMPARSE_SUFFIX_K | MEMPARSE_SUFFIX_M |\ + MEMPARSE_SUFFIX_G | MEMPARSE_SUFFIX_T |\ + MEMPARSE_SUFFIX_P) + +int __must_check memparse_safe(const char *s, enum memparse_suffix suffixes, + unsigned long long *res, char **retptr); int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); int __must_check kstrtoll(const char *s, unsigned int base, long long *res); diff --git a/lib/cmdline.c b/lib/cmdline.c index 90ed997d9570..d379157de349 100644 --- a/lib/cmdline.c +++ b/lib/cmdline.c @@ -139,12 +139,15 @@ char *get_options(const char *str, int nints, int *ints) EXPORT_SYMBOL(get_options); /** - * memparse - parse a string with mem suffixes into a number + * memparse - DEPRECATED, parse a string with mem suffixes into a number * @ptr: Where parse begins * @retptr: (output) Optional pointer to next char after parse completes * + * There is no way to handle errors, and no overflown detection and string + * sanity checks. * Parses a string into a number. The number stored at @ptr is * potentially suffixed with K, M, G, T, P, E. + * */ unsigned long long memparse(const char *ptr, char **retptr) diff --git a/lib/kstrtox.c b/lib/kstrtox.c index 41c9a499bbf3..a1e4279f52b3 100644 --- a/lib/kstrtox.c +++ b/lib/kstrtox.c @@ -113,6 +113,102 @@ static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res) return 0; } +/** + * memparse_safe - convert a string to an unsigned long long, safer version of + * memparse() + * + * @s: The start of the string. Must be null-terminated. + * The base would be determined automatically, if it starts with + * "0x" the base would be 16, if it starts with "0" the base + * would be 8, otherwise the base would be 10. + * After a valid number string, there can be at most one + * case-insensive suffix character, specified by the @suffixes + * parameter. + * + * @suffixes: The suffixes which should be parsed. Use logical ORed + * memparse_suffix enum to indicate the supported suffixes. + * The suffixes are case-insensive, all 2 ^ 10 based. + * Supported ones are "KMGPTE". + * NOTE: If one suffix out of the supported one is hit, it would + * end the parse normally, with @retptr pointed to the unsupported + * suffix. + * + * @res: Where to write the result. + * + * @retptr: (output) Optional pointer to the next char after parse completes. + * + * Return 0 if any valid numberic string can be parsed, and @retptr updated. + * Return -INVALID if no valid number string can be found. + * Return -ERANGE if the number overflows. + * For minus return values, @retptr would not be updated. + */ +noinline int memparse_safe(const char *s, enum memparse_suffix suffixes, + unsigned long long *res, char **retptr) +{ + unsigned long long value; + unsigned int rv; + int shift = 0; + int base = 0; + + s = _parse_integer_fixup_radix(s, &base); + rv = _parse_integer(s, base, &value); + if (rv & KSTRTOX_OVERFLOW) + return -ERANGE; + if (rv == 0) + return -EINVAL; + + s += rv; + switch (*s) { + case 'K': + case 'k': + if (!(suffixes & MEMPARSE_SUFFIX_K)) + break; + shift = 10; + break; + case 'M': + case 'm': + if (!(suffixes & MEMPARSE_SUFFIX_M)) + break; + shift = 20; + break; + case 'G': + case 'g': + if (!(suffixes & MEMPARSE_SUFFIX_G)) + break; + shift = 30; + break; + case 'T': + case 't': + if (!(suffixes & MEMPARSE_SUFFIX_T)) + break; + shift = 40; + break; + case 'P': + case 'p': + if (!(suffixes & MEMPARSE_SUFFIX_P)) + break; + shift = 50; + break; + case 'E': + case 'e': + if (!(suffixes & MEMPARSE_SUFFIX_E)) + break; + shift = 60; + break; + } + if (shift) { + s++; + if (value >> (64 - shift)) + return -ERANGE; + value <<= shift; + } + *res = value; + if (retptr) + *retptr = (char *)s; + return 0; +} +EXPORT_SYMBOL(memparse_safe); + /** * kstrtoull - convert a string to an unsigned long long * @s: The start of the string. The string must be null-terminated, and may also From patchwork Tue Jan 2 04:12:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 184214 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4262555dyb; Mon, 1 Jan 2024 20:13:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IG2nM3WgVPKGJSouFdNkeKIPk4q7sOibX9uoGJRDGZQ0kUrFGqcvGq/LpgbKDxD2CVRMuwR X-Received: by 2002:a05:6602:44d:b0:7ba:b1c7:864c with SMTP id e13-20020a056602044d00b007bab1c7864cmr16203570iov.30.1704168836988; Mon, 01 Jan 2024 20:13:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704168836; cv=none; d=google.com; s=arc-20160816; b=mFCcZDI0iGJNMzr+nLG71xE7JLfAJ/+Ybc0yueydkrwYBSjZbwGG7TsR/vU38wJa6Z 0t++uoP/dhbzjq8H8JO1AtfhPBXHukXBOf1emJKX0qVlJn348nabN7TuGoJ+MA1fYjju +icczF89N+Csxmqx/yUJS6TPzIkj8UXJAyIeFDBQfWYSjU12ivg/3hcED34LnUysjNPB 03N07Su0FQHXrPH/4LPnZl7RuQj7KZTc7lXTjRebN9Rz7Tiqc7cMM0FpZKZR57d1Dgxv XIqPojwWA/bY64M7xUpjXE7sgvmJTl+f4oxHtp6+oj4GguXiOKuPvdZBeAQOvWwGE5Wj p0/Q== 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:references:in-reply-to:message-id :date:subject:to:from:dkim-signature:dkim-signature; bh=RGAPanbcY3aSQ5XKab0EYeqAbKJkWE5EesQDMeSqGeU=; fh=aq23pSLwbr03HoKdXnGr0/uCxlT9OZ+QQ/0yofwyUC8=; b=DF24wl4U7E2rMDbPg6Csi06G/yvbDVnFV0hNwKkGEwG/wr4uESSz/9PgV5Ym0sO/z/ CBCKgLIQcFN0qKbIM31LkIqGtbbUT1zUVmwG7aXV0uUncqk+hescAjtBpi7rb40/shM6 XOTnzV2BAM93LcDE/QooqWR4vwGNhYDfbe1o0OziyaKZRD2zk+1X862oNvNdcTgM0l7I kPLs79UlYuin5ZolHQ0kMFrxqNEOEg++an/0196KH1/dBdJNgJjulKhfJgK1Jp5qr6Og 1qrUuaoCoxBOuoyvfqvdK4jZ38Hz7bJuBvIPsekEJE40mN/IKwAeNius5ftF7rDaEpDU ygpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=edpmjcjn; dkim=pass header.i=@suse.com header.s=susede1 header.b=edpmjcjn; spf=pass (google.com: domain of linux-kernel+bounces-14011-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14011-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. [147.75.48.161]) by mx.google.com with ESMTPS id 31-20020a63105f000000b005bdf597ed49si19379237pgq.56.2024.01.01.20.13.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 20:13:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14011-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=edpmjcjn; dkim=pass header.i=@suse.com header.s=susede1 header.b=edpmjcjn; spf=pass (google.com: domain of linux-kernel+bounces-14011-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14011-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 991B8B212B9 for ; Tue, 2 Jan 2024 04:13:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2CA7BD262; Tue, 2 Jan 2024 04:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="edpmjcjn"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="edpmjcjn" 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 A80D653B8; Tue, 2 Jan 2024 04:12:55 +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 BFD2C21E95; Tue, 2 Jan 2024 04:12:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168773; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RGAPanbcY3aSQ5XKab0EYeqAbKJkWE5EesQDMeSqGeU=; b=edpmjcjnFyd7kmi8+omttzkmDyAZf+rTF6cjvs/OmPp8xmoumK5jaVLW48nfQl05eMokhl 0Phw4LInDRtxwEdxp1EsP3w+G8gcLPOM9TFEaT7c43mjBhADs+ECxhbixKb/dr4WZ2IQHt 8JVfxIg6lt5s+BO6PVJj4aU15dYULwg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168773; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RGAPanbcY3aSQ5XKab0EYeqAbKJkWE5EesQDMeSqGeU=; b=edpmjcjnFyd7kmi8+omttzkmDyAZf+rTF6cjvs/OmPp8xmoumK5jaVLW48nfQl05eMokhl 0Phw4LInDRtxwEdxp1EsP3w+G8gcLPOM9TFEaT7c43mjBhADs+ECxhbixKb/dr4WZ2IQHt 8JVfxIg6lt5s+BO6PVJj4aU15dYULwg= 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 440B7136D1; Tue, 2 Jan 2024 04:12:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id 6EngM0GNk2UHQQAAD6G6ig (envelope-from ); Tue, 02 Jan 2024 04:12:49 +0000 From: Qu Wenruo 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 v2 3/4] kstrtox: add unit tests for memparse_safe() Date: Tue, 2 Jan 2024 14:42:13 +1030 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Level: ** X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Level: X-Spamd-Bar: / X-Spam-Flag: NO X-Spamd-Result: default: False [0.19 / 50.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[suse.com:+]; MX_GOOD(-0.01)[]; RCPT_COUNT_SEVEN(0.00)[7]; FREEMAIL_TO(0.00)[vger.kernel.org,linux-foundation.org,wanadoo.fr,linux.intel.com,ACULAB.COM,suse.de]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; RCVD_DKIM_ARC_DNSWL_HI(-1.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_ENVRCPT(0.00)[wanadoo.fr]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_IN_DNSWL_HI(-0.50)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_TLS_ALL(0.00)[] Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=edpmjcjn X-Spam-Score: 0.19 X-Rspamd-Queue-Id: BFD2C21E95 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786950542016858559 X-GMAIL-MSGID: 1786950542016858559 The new tests cases for memparse_safe() include: - The existing test cases for kstrtoull() Including all the 3 bases (8, 10, 16), and all the ok and failure cases. Although there are something we need to verify specific for memparse_safe(): * @retptr and @value are not modified for failure cases * return value are correct for failure cases * @retptr is correct for the good cases - New test cases Not only testing the result value, but also the @retptr, including: * good cases with extra tailing chars, but without valid prefix The @retptr should point to the first char after a valid string. 3 cases for all the 3 bases. * good cases with extra tailing chars, with valid prefix 5 cases for all the suffixes. * bad cases without any number but stray suffix Should be rejected with -EINVAL Signed-off-by: Qu Wenruo Reviewed-by: David Disseldorp --- lib/test-kstrtox.c | 235 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) diff --git a/lib/test-kstrtox.c b/lib/test-kstrtox.c index f355f67169b6..97c2f65a16cb 100644 --- a/lib/test-kstrtox.c +++ b/lib/test-kstrtox.c @@ -268,6 +268,237 @@ static void __init test_kstrtoll_ok(void) TEST_OK(kstrtoll, long long, "%lld", test_ll_ok); } +/* + * The special pattern to make sure the result is not modified for error cases. + */ +#define ULL_PATTERN (0xefefefef7a7a7a7aULL) +#if BITS_PER_LONG == 32 +#define POINTER_PATTERN (0xefef7a7a7aUL) +#else +#define POINTER_PATTERN (ULL_PATTERN) +#endif + +/* Want to include "E" suffix for full coverage. */ +#define MEMPARSE_TEST_SUFFIX (MEMPARSE_SUFFIX_K | MEMPARSE_SUFFIX_M |\ + MEMPARSE_SUFFIX_G | MEMPARSE_SUFFIX_T |\ + MEMPARSE_SUFFIX_P | MEMPARSE_SUFFIX_E) + +static void __init test_memparse_safe_fail(void) +{ + struct memparse_test_fail { + const char *str; + /* Expected error number, either -EINVAL or -ERANGE. */ + unsigned int expected_ret; + }; + static const struct memparse_test_fail tests[] __initconst = { + /* No valid string can be found at all. */ + {"", -EINVAL}, + {"\n", -EINVAL}, + {"\n0", -EINVAL}, + {"+", -EINVAL}, + {"-", -EINVAL}, + + /* Only hex prefix, but no valid string. */ + {"0x", -EINVAL}, + {"0X", -EINVAL}, + + /* Only hex prefix, with suffix but still no valid string. */ + {"0xK", -EINVAL}, + {"0xM", -EINVAL}, + {"0xG", -EINVAL}, + + /* Only hex prefix, with invalid chars. */ + {"0xH", -EINVAL}, + {"0xy", -EINVAL}, + + /* + * No support for any leading "+-" chars, even followed by a valid + * number. + */ + {"-0", -EINVAL}, + {"+0", -EINVAL}, + {"-1", -EINVAL}, + {"+1", -EINVAL}, + + /* Stray suffix would also be rejected. */ + {"K", -EINVAL}, + {"P", -EINVAL}, + + /* Overflow in the string itself*/ + {"18446744073709551616", -ERANGE}, + {"02000000000000000000000", -ERANGE}, + {"0x10000000000000000", -ERANGE}, + + /* + * Good string but would overflow with suffix. + * + * Note, for "E" suffix, one should not use with hex, or "0x1E" + * would be treated as 0x1e (30 in decimal), not 0x1 and "E" suffix. + * Another reason "E" suffix is cursed. + */ + {"16E", -ERANGE}, + {"020E", -ERANGE}, + {"16384P", -ERANGE}, + {"040000P", -ERANGE}, + {"16777216T", -ERANGE}, + {"0100000000T", -ERANGE}, + {"17179869184G", -ERANGE}, + {"0200000000000G", -ERANGE}, + {"17592186044416M", -ERANGE}, + {"0400000000000000M", -ERANGE}, + {"18014398509481984K", -ERANGE}, + {"01000000000000000000K", -ERANGE}, + }; + unsigned int i; + + for_each_test(i, tests) { + const struct memparse_test_fail *t = &tests[i]; + unsigned long long tmp = ULL_PATTERN; + char *retptr = (char *)POINTER_PATTERN; + int ret; + + ret = memparse_safe(t->str, MEMPARSE_TEST_SUFFIX, &tmp, &retptr); + if (ret != t->expected_ret) { + WARN(1, "str '%s', expected ret %d got %d\n", t->str, + t->expected_ret, ret); + continue; + } + if (tmp != ULL_PATTERN) + WARN(1, "str '%s' failed as expected, but result got modified", + t->str); + if (retptr != (char *)POINTER_PATTERN) + WARN(1, "str '%s' failed as expected, but pointer got modified", + t->str); + } +} + +static void __init test_memparse_safe_ok(void) +{ + struct memparse_test_ok { + const char *str; + unsigned long long expected_value; + /* How many bytes the @retptr pointer should be moved forward. */ + unsigned int retptr_off; + }; + static DEFINE_TEST_OK(struct memparse_test_ok, tests) = { + /* + * The same pattern of kstrtoull, just with extra @retptr + * verification. + */ + {"0", 0ULL, 1}, + {"1", 1ULL, 1}, + {"127", 127ULL, 3}, + {"128", 128ULL, 3}, + {"129", 129ULL, 3}, + {"255", 255ULL, 3}, + {"256", 256ULL, 3}, + {"257", 257ULL, 3}, + {"32767", 32767ULL, 5}, + {"32768", 32768ULL, 5}, + {"32769", 32769ULL, 5}, + {"65535", 65535ULL, 5}, + {"65536", 65536ULL, 5}, + {"65537", 65537ULL, 5}, + {"2147483647", 2147483647ULL, 10}, + {"2147483648", 2147483648ULL, 10}, + {"2147483649", 2147483649ULL, 10}, + {"4294967295", 4294967295ULL, 10}, + {"4294967296", 4294967296ULL, 10}, + {"4294967297", 4294967297ULL, 10}, + {"9223372036854775807", 9223372036854775807ULL, 19}, + {"9223372036854775808", 9223372036854775808ULL, 19}, + {"9223372036854775809", 9223372036854775809ULL, 19}, + {"18446744073709551614", 18446744073709551614ULL, 20}, + {"18446744073709551615", 18446744073709551615ULL, 20}, + + {"00", 00ULL, 2}, + {"01", 01ULL, 2}, + {"0177", 0177ULL, 4}, + {"0200", 0200ULL, 4}, + {"0201", 0201ULL, 4}, + {"0377", 0377ULL, 4}, + {"0400", 0400ULL, 4}, + {"0401", 0401ULL, 4}, + {"077777", 077777ULL, 6}, + {"0100000", 0100000ULL, 7}, + {"0100001", 0100001ULL, 7}, + {"0177777", 0177777ULL, 7}, + {"0200000", 0200000ULL, 7}, + {"0200001", 0200001ULL, 7}, + {"017777777777", 017777777777ULL, 12}, + {"020000000000", 020000000000ULL, 12}, + {"020000000001", 020000000001ULL, 12}, + {"037777777777", 037777777777ULL, 12}, + {"040000000000", 040000000000ULL, 12}, + {"040000000001", 040000000001ULL, 12}, + {"0777777777777777777777", 0777777777777777777777ULL, 22}, + {"01000000000000000000000", 01000000000000000000000ULL, 23}, + {"01000000000000000000001", 01000000000000000000001ULL, 23}, + {"01777777777777777777776", 01777777777777777777776ULL, 23}, + {"01777777777777777777777", 01777777777777777777777ULL, 23}, + + {"0x0", 0x0ULL, 3}, + {"0x1", 0x1ULL, 3}, + {"0x7f", 0x7fULL, 4}, + {"0x80", 0x80ULL, 4}, + {"0x81", 0x81ULL, 4}, + {"0xff", 0xffULL, 4}, + {"0x100", 0x100ULL, 5}, + {"0x101", 0x101ULL, 5}, + {"0x7fff", 0x7fffULL, 6}, + {"0x8000", 0x8000ULL, 6}, + {"0x8001", 0x8001ULL, 6}, + {"0xffff", 0xffffULL, 6}, + {"0x10000", 0x10000ULL, 7}, + {"0x10001", 0x10001ULL, 7}, + {"0x7fffffff", 0x7fffffffULL, 10}, + {"0x80000000", 0x80000000ULL, 10}, + {"0x80000001", 0x80000001ULL, 10}, + {"0xffffffff", 0xffffffffULL, 10}, + {"0x100000000", 0x100000000ULL, 11}, + {"0x100000001", 0x100000001ULL, 11}, + {"0x7fffffffffffffff", 0x7fffffffffffffffULL, 18}, + {"0x8000000000000000", 0x8000000000000000ULL, 18}, + {"0x8000000000000001", 0x8000000000000001ULL, 18}, + {"0xfffffffffffffffe", 0xfffffffffffffffeULL, 18}, + {"0xffffffffffffffff", 0xffffffffffffffffULL, 18}, + + /* Now with extra non-suffix chars to test @retptr update. */ + {"1q84", 1, 1}, + {"02o45", 2, 2}, + {"0xffvii", 0xff, 4}, + + /* + * Finally one suffix then tailing chars, to test the @retptr + * behavior. + */ + {"68k ", 69632, 3}, + {"8MS", 8388608, 2}, + {"0xaeGis", 0x2b80000000, 5}, + {"0xaTx", 0xa0000000000, 4}, + {"3E8", 0x3000000000000000, 2}, + }; + unsigned int i; + + for_each_test(i, tests) { + const struct memparse_test_ok *t = &tests[i]; + unsigned long long tmp; + char *retptr; + int ret; + + ret = memparse_safe(t->str, MEMPARSE_TEST_SUFFIX, &tmp, &retptr); + if (ret != 0) { + WARN(1, "str '%s', expected ret 0 got %d\n", t->str, ret); + continue; + } + if (tmp != t->expected_value) + WARN(1, "str '%s' incorrect result, expected %llu got %llu", + t->str, t->expected_value, tmp); + if (retptr != t->str + t->retptr_off) + WARN(1, "str '%s' incorrect endptr, expected %u got %zu", + t->str, t->retptr_off, retptr - t->str); + } +} static void __init test_kstrtoll_fail(void) { static DEFINE_TEST_FAIL(test_ll_fail) = { @@ -710,6 +941,10 @@ static int __init test_kstrtox_init(void) test_kstrtoll_ok(); test_kstrtoll_fail(); + test_memparse_safe_ok(); + test_memparse_safe_fail(); + + test_kstrtou64_ok(); test_kstrtou64_fail(); test_kstrtos64_ok(); From patchwork Tue Jan 2 04:12:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 184216 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4262640dyb; Mon, 1 Jan 2024 20:14:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IEnxRiGCHzf7c4tajN685dUMZ+klQwhfBn2ublPwLotwktKbvCwcOZfqvVdNxxp/Th2N0AA X-Received: by 2002:a17:906:1c04:b0:a28:2ad3:8124 with SMTP id k4-20020a1709061c0400b00a282ad38124mr413614ejg.47.1704168858562; Mon, 01 Jan 2024 20:14:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704168858; cv=none; d=google.com; s=arc-20160816; b=r/Ku++zScJtLCuKqjEGg9ZTquXCaq8losEYxLtUIA/JEeu13TScePdDlDoVaJU6O+G AGZgRRWjcwu89NlSI4W+4KhiTWSM5EV6Ko/aaBI7brhb97uCO30nsMZzRwEPRA4qpbQC FpSUjKoYB2NPDhGN7t8/wi8wnPFdKD8fsTze/82FKUEPcIKO0nIIOyhxNVXIX4hhmQoo o621RMIWsT+p5UCaEHILVyunIaQbPB4sO0aP3/yRG/9k0XTWK1Uh49O2qBPLWNpjC/K4 FrJbdhX4zaFew1z+evVG23LDdi+LlSuwpM/RGECTwUjlAxjLNiF27DDykMY7i4IIFy31 kmyQ== 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:references:in-reply-to:message-id :date:subject:to:from:dkim-signature:dkim-signature; bh=SYSOONXMVgPEycKwvBPu1abrQtkqdF3VIp134lU1tnc=; fh=aq23pSLwbr03HoKdXnGr0/uCxlT9OZ+QQ/0yofwyUC8=; b=ZDHU/PK6G4HMpt7eovQfMJrcKKAbdULM6v4U50Xi1XU+z/ViMBHy/pBZvXgTNTezBq W9o7m7xWng5WYhamnFbaPHLfXAEZwccMuu3ausoWkSMjSgEZKwIItMgx0WkCfQGoiESa jFsXt/lS14XZagrA4WGHLmVpA9wP+LgWUOTMaCFyrleHZ/rX0WIyq+cQ2QhaVjH+7S6D xrX1h16U7jL2/Rew6lSxNnii9pcnOC8kkez0WJdMWirI/3GAqzktY8mU2+13uciRyCGd o7xqQ1CwrXSNIat25gcli4hhVkBPa97nCE17A2oAQHgwa35CrTzZ+XOVKC5ZgenVKqSc nSsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=lX7r6j6r; dkim=pass header.i=@suse.com header.s=susede1 header.b=uBvf6HeW; spf=pass (google.com: domain of linux-kernel+bounces-14013-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14013-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id li22-20020a170906f99600b00a236378bbb8si10386223ejb.1052.2024.01.01.20.14.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 20:14:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14013-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=lX7r6j6r; dkim=pass header.i=@suse.com header.s=susede1 header.b=uBvf6HeW; spf=pass (google.com: domain of linux-kernel+bounces-14013-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14013-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 01B6A1F2190F for ; Tue, 2 Jan 2024 04:14:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC45ADDC7; Tue, 2 Jan 2024 04:13:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="lX7r6j6r"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="uBvf6HeW" 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 77C33C8F5; Tue, 2 Jan 2024 04:12:59 +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 E25FE21E95; Tue, 2 Jan 2024 04:12:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168778; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SYSOONXMVgPEycKwvBPu1abrQtkqdF3VIp134lU1tnc=; b=lX7r6j6rdp5YdZLwov4YOKJNf9q/CYe4iTn4uYlDkXBxn/hep7secxFmqlkltNSB6Hd/Hu TK6kLWp2q9qi00C+Otw7v+xdt+yF6b2GsTsopjdZpi1dQitRVrIzJfXBayOLjgnEUaTaE+ TJ3co8EMgojp3Rbfj8seRcQWLPTwMUM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1704168777; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SYSOONXMVgPEycKwvBPu1abrQtkqdF3VIp134lU1tnc=; b=uBvf6HeWx+uUgCG2aVjtgde31tHUWrYubE+oy+aoJQep8rAgNzv+XDY7BuMgtr/WlXYS57 ttXTkKUiO0+C4Dg2UlVrT+SIvRzeFfzWQhl2+K1XKv9GPAGOTpMFr3h3RbKqihKDDEDdEr t9DySxG746ptIOLp8EdwPstRKjIeifE= 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 74A8C136D1; Tue, 2 Jan 2024 04:12:54 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id aH+9BUaNk2UHQQAAD6G6ig (envelope-from ); Tue, 02 Jan 2024 04:12:54 +0000 From: Qu Wenruo 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 v2 4/4] btrfs: migrate to the newer memparse_safe() helper Date: Tue, 2 Jan 2024 14:42:14 +1030 Message-ID: <04b551a30763f776303c7ca8b0d0ffc0ed665e2a.1704168510.git.wqu@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Level: * X-Spam-Level: Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=uBvf6HeW X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-1.00 / 50.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[suse.com:+]; MX_GOOD(-0.01)[]; RCPT_COUNT_SEVEN(0.00)[7]; NEURAL_HAM_SHORT(-0.19)[-0.962]; FREEMAIL_TO(0.00)[vger.kernel.org,linux-foundation.org,wanadoo.fr,linux.intel.com,ACULAB.COM,suse.de]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; RCVD_DKIM_ARC_DNSWL_HI(-1.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_ENVRCPT(0.00)[wanadoo.fr]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-1.00)[-1.000]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_IN_DNSWL_HI(-0.50)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_TLS_ALL(0.00)[] X-Spam-Score: -1.00 X-Rspamd-Queue-Id: E25FE21E95 X-Spam-Flag: NO X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786950564606586441 X-GMAIL-MSGID: 1786950564606586441 The new helper has better error report and correct overflow detection, furthermore the old @retptr behavior is also kept, thus there should be no behavior change. Signed-off-by: Qu Wenruo Reviewed-by: David Disseldorp --- fs/btrfs/ioctl.c | 6 +++++- fs/btrfs/super.c | 9 ++++++++- fs/btrfs/sysfs.c | 14 +++++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 4e50b62db2a8..cb63f50a2078 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -1175,7 +1175,11 @@ static noinline int btrfs_ioctl_resize(struct file *file, mod = 1; sizestr++; } - new_size = memparse(sizestr, &retptr); + + ret = memparse_safe(sizestr, MEMPARSE_SUFFIXES_DEFAULT, + &new_size, &retptr); + if (ret < 0) + goto out_finish; if (*retptr != '\0' || new_size == 0) { ret = -EINVAL; goto out_finish; diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 3a677b808f0f..0f29fd692e0f 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -263,6 +263,8 @@ static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param) { struct btrfs_fs_context *ctx = fc->fs_private; struct fs_parse_result result; + /* Only for memparse_safe() caller. */ + int ret; int opt; opt = fs_parse(fc, btrfs_fs_parameters, param, &result); @@ -400,7 +402,12 @@ static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param) ctx->thread_pool_size = result.uint_32; break; case Opt_max_inline: - ctx->max_inline = memparse(param->string, NULL); + ret = memparse_safe(param->string, MEMPARSE_SUFFIXES_DEFAULT, + &ctx->max_inline, NULL); + if (ret < 0) { + btrfs_err(NULL, "invalid string \"%s\"", param->string); + return ret; + } break; case Opt_acl: if (result.negated) { diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 84c05246ffd8..6846572496a6 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -762,6 +762,7 @@ static ssize_t btrfs_chunk_size_store(struct kobject *kobj, struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj)); char *retptr; u64 val; + int ret; if (!capable(CAP_SYS_ADMIN)) return -EPERM; @@ -776,7 +777,10 @@ static ssize_t btrfs_chunk_size_store(struct kobject *kobj, if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM) return -EPERM; - val = memparse(buf, &retptr); + ret = memparse_safe(buf, MEMPARSE_SUFFIXES_DEFAULT, &val, &retptr); + if (ret < 0) + return ret; + /* There could be trailing '\n', also catch any typos after the value */ retptr = skip_spaces(retptr); if (*retptr != 0 || val == 0) @@ -1779,10 +1783,14 @@ static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj, { struct btrfs_device *device = container_of(kobj, struct btrfs_device, devid_kobj); - char *endptr; unsigned long long limit; + char *endptr; + int ret; + + ret = memparse_safe(buf, MEMPARSE_SUFFIXES_DEFAULT, &limit, &endptr); + if (ret < 0) + return ret; - limit = memparse(buf, &endptr); /* There could be trailing '\n', also catch any typos after the value. */ endptr = skip_spaces(endptr); if (*endptr != 0)