From nobody Sun Dec 28 21:18:41 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74938C4167B for ; Tue, 5 Dec 2023 09:20:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234979AbjLEJUG (ORCPT ); Tue, 5 Dec 2023 04:20:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbjLEJUE (ORCPT ); Tue, 5 Dec 2023 04:20:04 -0500 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0875483; Tue, 5 Dec 2023 01:20:09 -0800 (PST) Received: from localhost.ispras.ru (unknown [10.10.165.7]) by mail.ispras.ru (Postfix) with ESMTPSA id 3F8CD40F1DE9; Tue, 5 Dec 2023 09:20:04 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 3F8CD40F1DE9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1701768007; bh=Xkj0nuUUERVo3DmD0W5CbQml3tG2cDGWBXJRyDsrHq8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OOs6rrNxIXNTpiu+wwKGkoIxiCIgN0JjlOkTYcgNboYScKjXAtJUNbYxCx1uhxUV8 V+dSISTy/vUdFlBok1a3MZBkLoxXytP8ZzQhgiKfvPLttDjHjp4EYZz5IX8s9V5rXa 43jfNC40GiHt+8/BgkeP+wcGGsb2DrgnsNzJMeOs= From: Fedor Pchelkin To: Dominique Martinet Cc: Fedor Pchelkin , Latchesar Ionkov , Eric Van Hensbergen , Christian Schoenebeck , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , v9fs@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Alexey Khoroshilov , lvc-project@linuxtesting.org Subject: [PATCH v2] net: 9p: avoid freeing uninit memory in p9pdu_vreadf Date: Tue, 5 Dec 2023 12:19:50 +0300 Message-ID: <20231205091952.24754-1-pchelkin@ispras.ru> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If an error occurs while processing an array of strings in p9pdu_vreadf then uninitialized members of *wnames array are freed. Fix this by iterating over only lower indices of the array. Also handle possible uninit *wnames usage if first p9pdu_readf() call inside 'T' case fails. Found by Linux Verification Center (linuxtesting.org). Fixes: ace51c4dd2f9 ("9p: add new protocol support code") Signed-off-by: Fedor Pchelkin Reviewed-by: Christian Schoenebeck --- v2: I've missed that *wnames can also be left uninitialized. Please ignore the patch v1. As an answer to Dominique's comment: my organization marks this statement in all commits. net/9p/protocol.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/net/9p/protocol.c b/net/9p/protocol.c index 4e3a2a1ffcb3..043b621f8b84 100644 --- a/net/9p/protocol.c +++ b/net/9p/protocol.c @@ -393,6 +393,8 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, c= onst char *fmt, case 'T':{ uint16_t *nwname =3D va_arg(ap, uint16_t *); char ***wnames =3D va_arg(ap, char ***); + int i; + *wnames =3D NULL; =20 errcode =3D p9pdu_readf(pdu, proto_version, "w", nwname); @@ -406,8 +408,6 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, c= onst char *fmt, } =20 if (!errcode) { - int i; - for (i =3D 0; i < *nwname; i++) { errcode =3D p9pdu_readf(pdu, @@ -421,13 +421,11 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version,= const char *fmt, =20 if (errcode) { if (*wnames) { - int i; - - for (i =3D 0; i < *nwname; i++) + while (--i >=3D 0) kfree((*wnames)[i]); + kfree(*wnames); + *wnames =3D NULL; } - kfree(*wnames); - *wnames =3D NULL; } } break; --=20 2.43.0