From nobody Mon Nov 25 00:27:09 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39833170A15 for ; Fri, 1 Nov 2024 10:36:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730457369; cv=none; b=JHpqO2H9VLmBm17A3AUmPB5K33vmrh9j63ur7gT+Aig4N15i8DwtHUZN/jWok5OmbcRoXjMu7dHreABNWbq07/Ka9BhwNgatisP3sfpPH303vhjSUKDqMeFfeBidYGfcTnquxhDMW8xmLqxj9yz0UcTNLHR4qWqeskyg2SPQIzM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730457369; c=relaxed/simple; bh=SMhlUXDywZmsmR5JCajBo7bNNnC0nDLpb7NZhtflYME=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=OgX0SgH1CMXkjaQALWJD+K+gLEw7NVz9ogBtyXVT6yp31FZ2wYa/sSIHBYL9RXGHQkJlprkQgMDHXlF749HWVynLCGf2Ar8LiJoCQ4/FmK263E6LAzN8YpQYeKWU1H5wsKr9XQUsgxUcSz7NWYA58uRHW8RTLlsoO1RsmmQOssQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1CEB3C4CED7; Fri, 1 Nov 2024 10:36:09 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1t6p1f-00000005S5b-1mLU; Fri, 01 Nov 2024 06:37:07 -0400 Message-ID: <20241101103707.290109005@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 01 Nov 2024 06:36:50 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Yuran Pereira , Nir Lichtman , Douglas Anderson Subject: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main References: <20241101103647.011707614@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yuran Pereira The simple_str* family of functions perform no error checking in scenarios where the input value overflows the intended output variable. This results in these functions successfully returning even when the output does not match the input string. Or as it was mentioned [1], "...simple_strtol(), simple_strtoll(), simple_strtoul(), and simple_strtoull() functions explicitly ignore overflows, which may lead to unexpected results in callers." Hence, the use of those functions is discouraged. This patch replaces all uses of the simple_strto* series of functions with their safer kstrto* alternatives. Side effects of this patch: - Every string to long or long long conversion using kstrto* is now checked for failure. - kstrto* errors are handled with appropriate `KDB_BADINT` wherever applicable. - A good side effect is that we end up saving a few lines of code since unlike in simple_strto* functions, kstrto functions do not need an additional "end pointer" variable, and the return values of the latter can be directly checked in an "if" statement without the need to define additional `ret` or `err` variables. This, of course, results in cleaner, yet still easy to understand code. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-s= trtol-simple-strtoll-simple-strtoul-simple-strtoull Link: https://lore.kernel.org/20241028191916.GA918454@lichtman.org Signed-off-by: Yuran Pereira [nir: addressed review comments by fixing styling, invalid conversion and a= missing error return] Signed-off-by: Nir Lichtman Reviewed-by: Douglas Anderson Signed-off-by: Steven Rostedt (Google) Acked-by: Steven Rostedt (Google) --- kernel/debug/kdb/kdb_main.c | 69 +++++++++++-------------------------- 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c index f5f7d7fb5936..f8703ab760d9 100644 --- a/kernel/debug/kdb/kdb_main.c +++ b/kernel/debug/kdb/kdb_main.c @@ -306,8 +306,8 @@ static int kdbgetulenv(const char *match, unsigned long= *value) return KDB_NOTENV; if (strlen(ep) =3D=3D 0) return KDB_NOENVVALUE; - - *value =3D simple_strtoul(ep, NULL, 0); + if (kstrtoul(ep, 0, value)) + return KDB_BADINT; =20 return 0; } @@ -402,42 +402,23 @@ static void kdb_printenv(void) */ int kdbgetularg(const char *arg, unsigned long *value) { - char *endp; - unsigned long val; - - val =3D simple_strtoul(arg, &endp, 0); - - if (endp =3D=3D arg) { - /* - * Also try base 16, for us folks too lazy to type the - * leading 0x... - */ - val =3D simple_strtoul(arg, &endp, 16); - if (endp =3D=3D arg) + /* + * If the first fails, also try base 16, for us + * folks too lazy to type the leading 0x... + */ + if (kstrtoul(arg, 0, value)) { + if (kstrtoul(arg, 16, value)) return KDB_BADINT; } - - *value =3D val; - return 0; } =20 int kdbgetu64arg(const char *arg, u64 *value) { - char *endp; - u64 val; - - val =3D simple_strtoull(arg, &endp, 0); - - if (endp =3D=3D arg) { - - val =3D simple_strtoull(arg, &endp, 16); - if (endp =3D=3D arg) + if (kstrtou64(arg, 0, value)) { + if (kstrtou64(arg, 16, value)) return KDB_BADINT; } - - *value =3D val; - return 0; } =20 @@ -473,10 +454,10 @@ int kdb_set(int argc, const char **argv) */ if (strcmp(argv[1], "KDBDEBUG") =3D=3D 0) { unsigned int debugflags; - char *cp; + int ret; =20 - debugflags =3D simple_strtoul(argv[2], &cp, 0); - if (cp =3D=3D argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) { + ret =3D kstrtouint(argv[2], 0, &debugflags); + if (ret || debugflags & ~KDB_DEBUG_FLAG_MASK) { kdb_printf("kdb: illegal debug flags '%s'\n", argv[2]); return 0; @@ -1619,10 +1600,10 @@ static int kdb_md(int argc, const char **argv) if (!argv[0][3]) valid =3D 1; else if (argv[0][3] =3D=3D 'c' && argv[0][4]) { - char *p; - repeat =3D simple_strtoul(argv[0] + 4, &p, 10); + if (kstrtouint(argv[0] + 4, 10, &repeat)) + return KDB_BADINT; mdcount =3D ((repeat * bytesperword) + 15) / 16; - valid =3D !*p; + valid =3D 1; } last_repeat =3D repeat; } else if (strcmp(argv[0], "md") =3D=3D 0) @@ -2083,15 +2064,10 @@ static int kdb_dmesg(int argc, const char **argv) if (argc > 2) return KDB_ARGCOUNT; if (argc) { - char *cp; - lines =3D simple_strtol(argv[1], &cp, 0); - if (*cp) + if (kstrtoint(argv[1], 0, &lines)) lines =3D 0; - if (argc > 1) { - adjust =3D simple_strtoul(argv[2], &cp, 0); - if (*cp || adjust < 0) - adjust =3D 0; - } + if (argc > 1 && (kstrtoint(argv[2], 0, &adjust) || adjust < 0)) + adjust =3D 0; } =20 /* disable LOGGING if set */ @@ -2428,14 +2404,12 @@ static int kdb_help(int argc, const char **argv) static int kdb_kill(int argc, const char **argv) { long sig, pid; - char *endp; struct task_struct *p; =20 if (argc !=3D 2) return KDB_ARGCOUNT; =20 - sig =3D simple_strtol(argv[1], &endp, 0); - if (*endp) + if (kstrtol(argv[1], 0, &sig)) return KDB_BADINT; if ((sig >=3D 0) || !valid_signal(-sig)) { kdb_printf("Invalid signal parameter.<-signal>\n"); @@ -2443,8 +2417,7 @@ static int kdb_kill(int argc, const char **argv) } sig =3D -sig; =20 - pid =3D simple_strtol(argv[2], &endp, 0); - if (*endp) + if (kstrtol(argv[2], 0, &pid)) return KDB_BADINT; if (pid <=3D 0) { kdb_printf("Process ID must be large than 0.\n"); --=20 2.45.2