Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp5598695pxb; Mon, 28 Mar 2022 14:46:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/QF/kZK9MwufSX8uQFmdldZ0P1eVrCg/yPD9vT/RpwnmjVe44sR7vUb0t/EmQc4dWqpTo X-Received: by 2002:a05:6870:f70a:b0:da:bbb7:ab59 with SMTP id ej10-20020a056870f70a00b000dabbb7ab59mr580914oab.93.1648504000812; Mon, 28 Mar 2022 14:46:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648504000; cv=none; d=google.com; s=arc-20160816; b=bxsw37GfMUNeDmMPxYMdvkUVrsKN0xIPJpOSIVLHE96L4of6Es+/ByVDvPZAp4ISTI leo0HTOs8XzpaGc4SmSSjQNNMG2nnXUpDOz5qH1PPDaR1OaXOUMnQy6Y5CsMrINLpYho p8mSjSoxpB450958cI977zY8BwwHVsENji1GlWAMIkXfBnjegQDJHl/9r3xpRie+1lLK X5z77ogv7W7IzrMmq68n5YqJnDLJzMjW8iptnKP5Vzkt+hSrE+8QSK3pETpmvF17Jf/G mEGLBkrw88nH9Wt7LEDtKBbULb0Qw1FKgnpsv2geGSZIbxKZmo+lKve3CzwI10t20qbq iv4g== 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; bh=EsJ1GNPbDRLBCSmhbQALbs3yy0LSwX0BZnql9HpOyT8=; b=r5xp0CdF1ISknVwQ3w0lYug46GFyI1ln1RX1wqSlMqSZG9ds/daaIW4FAH8fbDOrPz G34FxpktJ5qsHE6/efz2zLs8GMMgcquQTnYOt2wRXJSDaozWi5uB/Wa44rZq9EzqyJlC br1Xn4u8cEZZ+Dmp6hUhhLQYNdqQUn0czXUMyCcCSxW+0sHYLvUP1JQ0dCclR4mARgRr UIX8D0Umi8oSfF/GiZ0yKThx52bp3fvzLLRlwL+l3rcLnWa4pheNa6W8aHCSgC2/BDay bAPiHSkkwD2i7kI7BAWjhLUDRxZlJpE1QLP5W0kYmbMb41Jf31z9klj6gFVn3CHUp7cZ 5yGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J0MB8FtA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d21-20020a9d5e15000000b005cb2fc137efsi11304195oti.107.2022.03.28.14.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 14:46:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J0MB8FtA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1261CBD888; Mon, 28 Mar 2022 14:19:19 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240640AbiC1LVP (ORCPT + 99 others); Mon, 28 Mar 2022 07:21:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240610AbiC1LU2 (ORCPT ); Mon, 28 Mar 2022 07:20:28 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9E0E5574E; Mon, 28 Mar 2022 04:18:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id E1E51B81054; Mon, 28 Mar 2022 11:18:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E903FC34110; Mon, 28 Mar 2022 11:18:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1648466320; bh=hOasPOHGeymPIR9CFGjXih8tpW9A6b/MeZtf9fDUetE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J0MB8FtAX+/NaSGpM8TsW1rd5bTScgxjUQUmgzN+Qy4SQSflbVQFXJJ0D1lj5h8uY +a5XyWdRLxSzp3dekq3O/D5YcgQzUgjVhE8EMliipwROpSVhoKM3SDmkHHwunhS8Av AkO8WKndhRjbjVMKSpflHz/6C6ZkHG/dj5xikU5T5Uc4kQ7inqTkyivXcO7J4YA9wS R/8EHfEe21h6eQ8wMSxQ/9U7bNc1JnLaiYQ6xpIA7xeaCejJNrus88i4McAEbQRKqD 0dWM6Yrq3lsAzaiKHeHVg8Ntb81pRxI+3PfLiCGEtk1NqzsqlbpiCHuXQjUmIZppiI urCPcvISqeXtA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kees Cook , Alexander Popov , Sasha Levin , linux-hardening@vger.kernel.org Subject: [PATCH AUTOSEL 5.17 05/43] gcc-plugins/stackleak: Exactly match strings instead of prefixes Date: Mon, 28 Mar 2022 07:17:49 -0400 Message-Id: <20220328111828.1554086-5-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220328111828.1554086-1-sashal@kernel.org> References: <20220328111828.1554086-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook [ Upstream commit 27e9faf415dbf94af19b9c827842435edbc1fbbc ] Since STRING_CST may not be NUL terminated, strncmp() was used for check for equality. However, this may lead to mismatches for longer section names where the start matches the tested-for string. Test for exact equality by checking for the presences of NUL termination. Cc: Alexander Popov Signed-off-by: Kees Cook Signed-off-by: Sasha Levin --- scripts/gcc-plugins/stackleak_plugin.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/scripts/gcc-plugins/stackleak_plugin.c b/scripts/gcc-plugins/stackleak_plugin.c index e9db7dcb3e5f..b04aa8e91a41 100644 --- a/scripts/gcc-plugins/stackleak_plugin.c +++ b/scripts/gcc-plugins/stackleak_plugin.c @@ -429,6 +429,23 @@ static unsigned int stackleak_cleanup_execute(void) return 0; } +/* + * STRING_CST may or may not be NUL terminated: + * https://gcc.gnu.org/onlinedocs/gccint/Constant-expressions.html + */ +static inline bool string_equal(tree node, const char *string, int length) +{ + if (TREE_STRING_LENGTH(node) < length) + return false; + if (TREE_STRING_LENGTH(node) > length + 1) + return false; + if (TREE_STRING_LENGTH(node) == length + 1 && + TREE_STRING_POINTER(node)[length] != '\0') + return false; + return !memcmp(TREE_STRING_POINTER(node), string, length); +} +#define STRING_EQUAL(node, str) string_equal(node, str, strlen(str)) + static bool stackleak_gate(void) { tree section; @@ -438,13 +455,13 @@ static bool stackleak_gate(void) if (section && TREE_VALUE(section)) { section = TREE_VALUE(TREE_VALUE(section)); - if (!strncmp(TREE_STRING_POINTER(section), ".init.text", 10)) + if (STRING_EQUAL(section, ".init.text")) return false; - if (!strncmp(TREE_STRING_POINTER(section), ".devinit.text", 13)) + if (STRING_EQUAL(section, ".devinit.text")) return false; - if (!strncmp(TREE_STRING_POINTER(section), ".cpuinit.text", 13)) + if (STRING_EQUAL(section, ".cpuinit.text")) return false; - if (!strncmp(TREE_STRING_POINTER(section), ".meminit.text", 13)) + if (STRING_EQUAL(section, ".meminit.text")) return false; } -- 2.34.1