Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp157276rdb; Tue, 5 Dec 2023 01:21:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGkNdTdSIwi47neBroH1nRX/GPs2VSVWPgKAVSwpx9RGHJbzFiF7qejRR5z6Pd06Ysml3tJ X-Received: by 2002:a17:902:e74f:b0:1d0:874a:5f98 with SMTP id p15-20020a170902e74f00b001d0874a5f98mr1622731plf.24.1701768059876; Tue, 05 Dec 2023 01:20:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701768059; cv=none; d=google.com; s=arc-20160816; b=SFQvH2Iz2SQPms58N/dxqsZUl23xd1Hjja6ypcpa5ImUNuT/T259X7Bs0Z+NsnCObO T1Ll0rzgZtNy8eRTpltu6KAPA6bqCYARk5sXRGozjDU6zUuhVUpV8sNrQVx1Di9z/VC1 8aBk5Ptlq0OLUdrBz1QRfWMvwcp6dItpXkNigZR44q/anqtBXsLprToqRSIU/ZUDmRtB vqTNhlSLXreZjZKpP4OMEBp7/N7rO9sxue7iw/T1Sh23GH1pwNlvsQjWlGxdz0KIzyZB IDcQWJzXzLq3t8T9MFLOBz/kp5YGfbBIK77jVMbtuX5vnOxTP9/hEj02b2YObbkaGZBA FI6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=Xkj0nuUUERVo3DmD0W5CbQml3tG2cDGWBXJRyDsrHq8=; fh=PiwcU1SnKSd+B7vTdLgfllmhsd7uYDomZCqbbHf7Qxw=; b=oNzsn/tccFOranl8rm2Z27bjMN0d9Xh+08o+3ju/wu+1Y4UDDJG/o0E5uzJHZ9KU/E mdTYGr5L2PAHcBoEZePz8pvuU5pGgneF7OoAVWwBcWvCZTs97/yGWpQri4HcMgYdN9te jkTWpvL/aaKAfbj99Yodm6SPrYTkBIbXOZwTlxbaF89sjKMr4QXuQlzaUdG9zOAdGQJr npiVqZBbulXpPjTk9jH1vsh+DFThtpZc0/W0UTHr5sRuh5AEZwBL/rdcuXbLvwQUF+3Q bQREWpcwnObyvChL/LvCoMaB2TxzOewHekzk/dhRu7DDaa/QRj4wp1knTMYY7e//wbXa HSwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=OOs6rrNx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id w4-20020a63d744000000b005c2203bfc01si9659861pgi.170.2023.12.05.01.20.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 01:20:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=OOs6rrNx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id BC33E807BEF8; Tue, 5 Dec 2023 01:20:32 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234928AbjLEJUF (ORCPT + 99 others); Tue, 5 Dec 2023 04:20:05 -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: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 05 Dec 2023 01:20:32 -0800 (PST) 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 --- 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, const char *fmt, case 'T':{ uint16_t *nwname = va_arg(ap, uint16_t *); char ***wnames = va_arg(ap, char ***); + int i; + *wnames = NULL; errcode = p9pdu_readf(pdu, proto_version, "w", nwname); @@ -406,8 +408,6 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, } if (!errcode) { - int i; - for (i = 0; i < *nwname; i++) { errcode = p9pdu_readf(pdu, @@ -421,13 +421,11 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, if (errcode) { if (*wnames) { - int i; - - for (i = 0; i < *nwname; i++) + while (--i >= 0) kfree((*wnames)[i]); + kfree(*wnames); + *wnames = NULL; } - kfree(*wnames); - *wnames = NULL; } } break; -- 2.43.0