Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp37353952rwd; Tue, 11 Jul 2023 13:04:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlE3xlt6i+LLKGoFnP53uH+YDlkokrRhDqU73zRZwbcvdF8FGwnQ1pjObsMn/tDrw5TTMVAx X-Received: by 2002:a17:906:1090:b0:994:1844:caec with SMTP id u16-20020a170906109000b009941844caecmr2741462eju.77.1689105898964; Tue, 11 Jul 2023 13:04:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689105898; cv=none; d=google.com; s=arc-20160816; b=fN6HurKOcNRTCZ/2e6LgelPVeDtnwhqUfenwWgLmh8gc9vMBiVmaG7Q53baejdOY6U ChuixMhujK8exg1ukjnuDuq5Nk1IuFx+Yslfmy4/yBbok1DSbX+pFIoCMPXfA5+kc3cs TLziURgmKGQZ3x1mo04uJHgVxYkYp3wCYGR9ux+QHChYpn1BPAIb1bTDhs6TZDg5EsEE RUH4Wt7bXxihsesYTrYAem7LRsUvUrdTALSASCPJdOc1LvzwhBp+or3Z17I4Z6hvfu7b tM4RXe0NUHH4+zpHO+qR0+cFOS9YPzjYVYzRPGAVXLCYik0rvEWySHoWdqKG1DpZMMFH JTuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ZZi2lREaJRgoeuqPjTf8Xh4L5gWjJRq/YAqVYw9otqs=; fh=qKv2UtliY+5vXSsYPETkL5bLkq6ID/+X7M36K22u0ps=; b=clEpxrnp2Ur5BpgZ8CW//O9/T9hClWUpNPm5GhqlntleCxD97t5j9pErbzQEbnjueI klOfb59RlbKeNrJzccugNJ/Bz5BkgcmsRRUfbXhhiD2y/wEP7U2Gbe/PBWCZFynCP0aV Tinq/9Ip5wNkj00YyoQVINwVr1FCqLuJ8c6p7dwUTeH5T2e/6rTTLn0mFMF52SF6fbMi WF5HUFznLTJsmHU/x6/ZEd6hneu44vJG1XTNP7JTQQIRCXx+y8ttALmbHiJRRV86HHdT dfTxW/zhndK3cKIcbX2hhZIWpF507g5+7eBxRgadCKrYtmqCCLQLmMFuOd5KluhaMFm/ xl9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KUPyaibc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r25-20020a170906281900b0098652857e36si2343924ejc.282.2023.07.11.13.04.33; Tue, 11 Jul 2023 13:04:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KUPyaibc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230233AbjGKTql (ORCPT + 99 others); Tue, 11 Jul 2023 15:46:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229512AbjGKTqk (ORCPT ); Tue, 11 Jul 2023 15:46:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 822F51730 for ; Tue, 11 Jul 2023 12:46:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 164A3615E4 for ; Tue, 11 Jul 2023 19:46:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44295C433C8; Tue, 11 Jul 2023 19:46:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689104790; bh=oagomkcQa/oXl5+aZDOLNoxvsSWXn4ATh/auNAuthDQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=KUPyaibc8L/ggTxuNzTCY2ikAuY9Q0sDupvAnGogZgYxtNvM9vRGxb4GrfKTWpcpF B1h3lMl4eqljM9aFBuu8S2MIXfeNyfBjtoGltF7CpwbrMY4WVu6NpLZgommVJ0COgR TtfHwyQC1D2dGfBRvrR4PrYi/5hxViaR3gZEa2EmV+6ivIYqR4TUGVx3Nk7tB9Og+b ALtepbWoO58Refl9daZqp7toMUGkVMIQTf3skzQpw1cmrI6JeDVgKM/gYbFQBKTR5G DTDL4lC7wwCZ27wR7TpZai1UBVG2JPepcQbJh6vzsw3qr8fzf0iR+hgFqaB/wq6os4 /0Jx0sy/RNOKw== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id 7ED9140516; Tue, 11 Jul 2023 16:46:27 -0300 (-03) Date: Tue, 11 Jul 2023 16:46:27 -0300 From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ian Rogers , Arnaldo Carvalho de Melo , Chenyuan Mi , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] lib subcmd: Avoid segv/use-after-free when commands aren't excluded Message-ID: References: <20230707230926.841086-1-irogers@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Url: http://acmel.wordpress.com X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Tue, Jul 11, 2023 at 10:36:59AM -0700, Namhyung Kim escreveu: > On Fri, Jul 7, 2023 at 4:09 PM Ian Rogers wrote: > > > > The array shortening may perform unnecessary array copies. Before > > commit 657a3efee43a ("lib subcmd: Avoid memory leak in exclude_cmds") > > this was benign, but afterwards this could lead to a segv. > > > > Fixes: 657a3efee43a ("lib subcmd: Avoid memory leak in exclude_cmds") > > Signed-off-by: Ian Rogers > > Acked-by: Namhyung Kim Thanks, applied to perf-tools. - Arnaldo > Thanks, > Namhyung > > > > --- > > tools/lib/subcmd/help.c | 18 ++++++++++++------ > > 1 file changed, 12 insertions(+), 6 deletions(-) > > > > diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c > > index 67a8d6b740ea..adfbae27dc36 100644 > > --- a/tools/lib/subcmd/help.c > > +++ b/tools/lib/subcmd/help.c > > @@ -68,8 +68,13 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) > > while (ci < cmds->cnt && ei < excludes->cnt) { > > cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); > > if (cmp < 0) { > > - zfree(&cmds->names[cj]); > > - cmds->names[cj++] = cmds->names[ci++]; > > + if (ci == cj) { > > + ci++; > > + cj++; > > + } else { > > + zfree(&cmds->names[cj]); > > + cmds->names[cj++] = cmds->names[ci++]; > > + } > > } else if (cmp == 0) { > > ci++; > > ei++; > > @@ -77,10 +82,11 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) > > ei++; > > } > > } > > - > > - while (ci < cmds->cnt) { > > - zfree(&cmds->names[cj]); > > - cmds->names[cj++] = cmds->names[ci++]; > > + if (ci != cj) { > > + while (ci < cmds->cnt) { > > + zfree(&cmds->names[cj]); > > + cmds->names[cj++] = cmds->names[ci++]; > > + } > > } > > for (ci = cj; ci < cmds->cnt; ci++) > > zfree(&cmds->names[ci]); > > -- > > 2.41.0.390.g38632f3daf-goog > > -- - Arnaldo