Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp943493pxb; Wed, 6 Apr 2022 05:00:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDCyZoA+2Uy1SdSR8wGyJUfA09WTScZQU/BDsVbaCal9ptYJ3bje2OJVo8j/CoRyx2gKJW X-Received: by 2002:a17:903:2488:b0:153:8f59:8c03 with SMTP id p8-20020a170903248800b001538f598c03mr8435393plw.54.1649246417164; Wed, 06 Apr 2022 05:00:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649246417; cv=none; d=google.com; s=arc-20160816; b=DPlQ6hiUyDpRA69yWrUF8C7E9WLQCIodlBNdhwciK515z/OKLjJt6MzK+phF3p81sL eW4Z9/CFonvFFtiD+22IO47lkHrZA2GXv37TY+FPC/7B5KAVeY9SOqemAj6PozBmUUBq MEN+29/25LpuKqHIT7i945v3RNAnx/bIOv3tukmMbKrWKAwabfLc7AVZ9E8gTxQefTKR GbNu/P9pQlbv+USbvYCsOteKEwKB9mXDmHW3GeX7bQnQe/GIizmmVnD9HpfAiecrrnNb 0ur8yAVkzEiVZMt9ddwACIYQ2T02rdzhS6X2QG3CHymM1OvLdSi+Vucvmgg1XQkMyxgm CTGA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EsJ1GNPbDRLBCSmhbQALbs3yy0LSwX0BZnql9HpOyT8=; b=TIhbJbIp9f+XSvKF1/QMa9IYsMwc3zs4RkSLzEMxZJ1WCpfpxlZ1gXBXrpxTakSLef +pHC+4qkNCoa7m0bfroIhp9SWP+ezBGh21kYuOfDcr4T3kSro/nk0wtqEpajHHIECqn5 h48SoTb7fv2jzt/7G+ntA8E42VXQon393JJYhHYCPE9LxLQkUhNS313AtJoKoJSgciKf AAgSMf7eenfrPX4pOjo6CqbrftBZGjU8Suyuu5X7qPzDzInAsTXnsFkYVfUnp26wp+1R UH9JlhCRWPNybS3Wgq6rNR/SdBq4ZSy2lCPstWD5nTguheKHkHSh5JnoOa1WLJZRg7dO t3Xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yEXXz5AJ; 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=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h5-20020a170902f54500b00156c0b0a42asi6211139plf.113.2022.04.06.05.00.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:00:17 -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=@linuxfoundation.org header.s=korg header.b=yEXXz5AJ; 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=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8B373662EE9; Wed, 6 Apr 2022 03:23:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1575619AbiDEXIY (ORCPT + 99 others); Tue, 5 Apr 2022 19:08:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349822AbiDEJvm (ORCPT ); Tue, 5 Apr 2022 05:51:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A5852314B; Tue, 5 Apr 2022 02:49:44 -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 dfw.source.kernel.org (Postfix) with ESMTPS id EB3AB61577; Tue, 5 Apr 2022 09:49:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 049E5C385A2; Tue, 5 Apr 2022 09:49:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152183; bh=hOasPOHGeymPIR9CFGjXih8tpW9A6b/MeZtf9fDUetE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yEXXz5AJkVizsK2uA0nxVtcWEr1eoVQeQ7cUWT3AmX6ipbk4a1Dh9CT7mzDAmo7u1 3tToIIDF0LV5N9Sh2jwNYqlmxpkUfgPZ02DqaJvcs7x/El6Nh+LiVKh5NCiTtocesB GHLwrDaw4X0awvjTEs9zQapZMZ+kZrLszO61v6To= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexander Popov , Kees Cook , Sasha Levin Subject: [PATCH 5.15 685/913] gcc-plugins/stackleak: Exactly match strings instead of prefixes Date: Tue, 5 Apr 2022 09:29:07 +0200 Message-Id: <20220405070400.368089868@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,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 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