Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp37215563rwd; Tue, 11 Jul 2023 11:00:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlFfLHAC1q9F3CryCH0sc1H25fiB73ymOu81OcdQnkZkkYpPEjte/Fg4Q5dEgJqXpIuStOOV X-Received: by 2002:a05:6a00:398c:b0:67b:8602:aa1e with SMTP id fi12-20020a056a00398c00b0067b8602aa1emr21335505pfb.28.1689098426342; Tue, 11 Jul 2023 11:00:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689098426; cv=none; d=google.com; s=arc-20160816; b=DovS5OS8cmhjwv9jgQC7U01G7ZBCu+SA+8hXtYj+q2VNzEDY1ASUx6qRtG84EdxOoy +ZNbSh1Du/CZ7SqAZH67n3tfVhx6/teghHvG3LOxL72fFxmMjlLDXmYzKasbiyKISWKJ iGr/gMoH0xvYKvvzfg3pklxgim1+TlyIGSXWnNp+s7UIxwqjUfeLoHXfupcCwdP8yqFI BJNeM2OD+TBG+I2181VR2QJuV//njrlNithbwbhWvZLV0teQ/tafgdKJ6B9Y6B397jnL WEO/E6T0gtIeLKx+bkBDuKpY4CRgr1yhB/Pq8ybQANhDyoBTLOUeu4mxYkPch0Ie0lcM aR2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=UP9NHIPEs7eAK32Ec3qd1Vhn5bNgHUMRV4D1nEcnLYU=; fh=l3Nj16NSJ5ws+FXMfQO91yVlqKlphNvp906vEhdPncQ=; b=gG8MpVJvWmTk7qYrYY+Kg3Mu8OKj/jMF9eOLz+YkY7+40s8AnsWZo/pT3wQfCgwqym zctL+gFMgrA5hI8Pv2v3BEtZWrdjurNBNHE9RVkEXw7T7i9TGytHN9b1NdJuhq+TXQEm jLyrhMYgm3ippAGfKbx9SL2beH90KUcA9fbKQOrr1pq7L7DY+CBwy0KRRj2J2J1hMX7e BEsBDLOVvRdrswwt9PVb0ujjx656QTaMmvWFWavIbcftQbj50u6Rtml+b90T40MRlwiF C+WYQp/BRUnElNgGbQC91IVZmR0ymmlGMp10WRkeF6Fpd40PUdwl63ssm6LUsj6978og pQFQ== ARC-Authentication-Results: i=1; mx.google.com; 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 y9-20020a056a00190900b0066871b54e15si1760753pfi.359.2023.07.11.11.00.13; Tue, 11 Jul 2023 11:00:26 -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; 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 S230227AbjGKRhN convert rfc822-to-8bit (ORCPT + 99 others); Tue, 11 Jul 2023 13:37:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229491AbjGKRhM (ORCPT ); Tue, 11 Jul 2023 13:37:12 -0400 Received: from mail-vk1-f176.google.com (mail-vk1-f176.google.com [209.85.221.176]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51E61E49 for ; Tue, 11 Jul 2023 10:37:11 -0700 (PDT) Received: by mail-vk1-f176.google.com with SMTP id 71dfb90a1353d-45739737afcso1669635e0c.2 for ; Tue, 11 Jul 2023 10:37:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689097030; x=1691689030; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4KI4vVyq/o225EnjYmGJ0/PX8RgV9ghoYoSQ/HAeigc=; b=eqBkMqq9CZl/jivwdxKVY72hF4DIlLnDx2lR+bOnQeJuCOIMAWFYIUoWhwjEe1HyIh oF2uGJJUdqvtzxZlnxFp7BGzzzCNPfF4v4DpO/vYvnnmhuA3BKinUn/P6OCY3fvtQsUx FxgTdTijglXcmLYoec0jDs+GpC1AlGl1DyDONOQcwILSW1Nx3XBFouIekDHZwkJoWZWv /5zl6VupCefhiJQEUgFy964aiXeKrohEqTdRf9GbUw1NpMiWs8sIrXpCSRTJMVddfA4K 0Z+yAAt88oMdJs+7ZUmpDSaXHIKDP4Z/1VNjRt9Awbz/bvHDjBIRbzl6uHlqaYa8X5Aa TZBA== X-Gm-Message-State: ABy/qLai1PWljKZETz8qCjQ4oJnsKil+Uu14hojmek6zbjXCy9WSVHei Cm/KbbqgUocB+6T7C5s8dkx3vW4M9ZO8/dVbu70= X-Received: by 2002:a1f:bd58:0:b0:46e:7c85:acb0 with SMTP id n85-20020a1fbd58000000b0046e7c85acb0mr5269796vkf.14.1689097030271; Tue, 11 Jul 2023 10:37:10 -0700 (PDT) MIME-Version: 1.0 References: <20230707230926.841086-1-irogers@google.com> In-Reply-To: <20230707230926.841086-1-irogers@google.com> From: Namhyung Kim Date: Tue, 11 Jul 2023 10:36:59 -0700 Message-ID: Subject: Re: [PATCH v2] lib subcmd: Avoid segv/use-after-free when commands aren't excluded To: Ian Rogers Cc: Arnaldo Carvalho de Melo , Chenyuan Mi , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 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, 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 >