Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp466060rdb; Tue, 5 Dec 2023 10:06:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEkUI2FVU5EleGFuEOed/AODu6WYkpUKHq2PawhLK+BTAVg5Gw4f8oxTpPV/gi7xDo1MeiX X-Received: by 2002:a05:6a00:1583:b0:6ce:6b7c:ba1b with SMTP id u3-20020a056a00158300b006ce6b7cba1bmr1797099pfk.60.1701799566967; Tue, 05 Dec 2023 10:06:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701799566; cv=none; d=google.com; s=arc-20160816; b=UBwF8xGilSflBuO6ojWk5ypOY2ZVavchcsPoVV9zggbtlfsJmEHfpVIjE2omMj6sFh 4+NR2ZcuTtwoFjilLybgI3Z4Bi9HdJlArdzuf1PEPj5hmFVmvXzb0t+VHk1LgEiIskbH Y2QqPYowsi6cezeh9AwoGqGf8mv64PaBDj2eq2Ejs/IfiaqU8JxJkrV6R1RL7qRKfoIU paoNk10m3lSAVquOyvw+gpgCl8gkl6VU7m0O0YragchI8H/OiAQiKKkC6g8B3dipjxdB 0kQDYn6BMq3xqMeAaxAd2uQOx/O91K33sO1W1Rsgs1d9wcC+gXphMf73SC9RGTxiQjlb F2DQ== 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=iSQFzh6kLnBCcudLetoO0SevfeUo1ajFojtQyoqXpBQ=; fh=ZM3VoIALjCpG+NKGQ//xV8LjNmVpkgwohdMjQmKLq6Y=; b=LLLG79orY7apG2U71twsXkydsPfI9IvX4xd7ezZvwBrgPk2ZaxqRElApOK7bj7HAqx qvDx9+L9o2IUskKZZQ9lMCFjlUDKupUibulHYh6+jg1g/Vh3DdetO81S/NVtoH7QFuJt pKLoxXV7MSxShxsXeG0NImrH3+5Y8jaiEBYBqgWFQB0OAyZ59rOCRFT/lU4zcYW0EH2X Stc6GP7RiJ+3bzCV+Yu+xp6b74Ue+ojKkAgsiGQckNYzLKQ772py4zdP3QV8Ih1hkQ8k dNl0VRZhl2N0l3ZAGhsl9Y9xYzOFLrbak2Nv7gGxYsXDxsUcCxVJoRpe4gyYwCbx1XyO E1Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=qjGGvX+q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id l15-20020a65680f000000b005c67dd98b12si4576071pgt.280.2023.12.05.10.06.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 10:06:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=qjGGvX+q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 5E29B8040D5B; Tue, 5 Dec 2023 10:06:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345576AbjLESFe (ORCPT + 99 others); Tue, 5 Dec 2023 13:05:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230271AbjLESF3 (ORCPT ); Tue, 5 Dec 2023 13:05:29 -0500 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC6EBB2; Tue, 5 Dec 2023 10:05:34 -0800 (PST) Received: from localhost.localdomain (unknown [46.242.8.170]) by mail.ispras.ru (Postfix) with ESMTPSA id 60EAF40F1DE8; Tue, 5 Dec 2023 18:05:32 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 60EAF40F1DE8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1701799532; bh=iSQFzh6kLnBCcudLetoO0SevfeUo1ajFojtQyoqXpBQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qjGGvX+q5/okA2ccONfrW/JIILDtVsCtbi/3lK6KMl7/MKeXrk9Y7g1krpec6W8Q4 MeNVeYPnw637K1E2S+L0ZBw1Sj4vTg4eGXzUDMIoe2stuRvJ82adZVg/+lcNdXhrtu LxQuq8jpwgyfD99rtJTxly4iAOqyk1RvNMLheh3I= From: Fedor Pchelkin To: Dominique Martinet Cc: Fedor Pchelkin , Christian Schoenebeck , Eric Van Hensbergen , Latchesar Ionkov , "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 v3] net: 9p: avoid freeing uninit memory in p9pdu_vreadf Date: Tue, 5 Dec 2023 21:05:22 +0300 Message-ID: <20231205180523.11318-1-pchelkin@ispras.ru> X-Mailer: git-send-email 2.43.0 In-Reply-To: <9f21f00b-0806-4811-8d0a-9b6175eaedeb-pchelkin@ispras.ru> 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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 05 Dec 2023 10:06:04 -0800 (PST) If some of p9pdu_readf() calls inside case 'T' in p9pdu_vreadf() fails, the error path is not handled properly. *wnames or members of *wnames array may be left uninitialized and invalidly freed. In order not to complicate the code with array index processing, fix the problem with initializing *wnames to NULL in beginning of case 'T' and using kcalloc() to allocate and initialize the array. For assurance, nullify the failing *wnames element (the callee handles that already - e.g. see 's' case). 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. v3: Simplify the patch by using kcalloc() instead of array indices manipulation per Christian Schoenebeck's remark. Update the commit message accordingly. net/9p/protocol.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/net/9p/protocol.c b/net/9p/protocol.c index 4e3a2a1ffcb3..7067fb49d713 100644 --- a/net/9p/protocol.c +++ b/net/9p/protocol.c @@ -394,13 +394,14 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, uint16_t *nwname = va_arg(ap, uint16_t *); char ***wnames = va_arg(ap, char ***); + *wnames = NULL; + errcode = p9pdu_readf(pdu, proto_version, "w", nwname); if (!errcode) { *wnames = - kmalloc_array(*nwname, - sizeof(char *), - GFP_NOFS); + kcalloc(*nwname, sizeof(char *), + GFP_NOFS); if (!*wnames) errcode = -ENOMEM; } @@ -414,8 +415,10 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, proto_version, "s", &(*wnames)[i]); - if (errcode) + if (errcode) { + (*wnames)[i] = NULL; break; + } } } @@ -425,9 +428,9 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, for (i = 0; i < *nwname; i++) kfree((*wnames)[i]); + kfree(*wnames); + *wnames = NULL; } - kfree(*wnames); - *wnames = NULL; } } break; -- 2.43.0