Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp639427pxb; Tue, 5 Apr 2022 16:56:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx57fWqfbLF2gMFje/9gVlQZWKD6hv0ufVhAZzBflmvkJtxSyaXpEfzvjxn5IvYBtWkfmI9 X-Received: by 2002:a17:902:f149:b0:156:87ad:4698 with SMTP id d9-20020a170902f14900b0015687ad4698mr6114258plb.5.1649202985945; Tue, 05 Apr 2022 16:56:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649202985; cv=none; d=google.com; s=arc-20160816; b=J+YpoWrqYdASWf3zqp3JzpsIq6Slh1TJ18kTuq7b7tR4ft6dxOyJ9nELC9Mxyd/A+a E2E8MawT+L3C3ijK96A0sHbxVf1xUqY68jePxto4SLISyrsyEHAmjGVRwHx4JFvWI1n0 n85HkdJaaKFpqFjH2s3gqjnbenURQuODwXJNIiHDRTZBJRFbpZIJjR+AlI246us0RrxR Vx+vCEY2SYqUYxlH1Epu156PCR5fQwdLk58uA/KECXbGeqYrz8r+hkw/M9svoZt+k4aw kpMPCxZlaUF+xA4vh6hCvfFQnNMnSuz5bTRB6tSKvpWOddCRNb3+sEuPUdqIFZNpui/W YVfQ== 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=VAro6Zg/sXP5X4UGcDcZsvXipOO5d1Qm1INreleZphEh2ftW5iLxQOesJg63oTGfHa whFWLl9aFQ1rMF9mwwLf1GehkekvI3rMRyL65MN11vZ1o5cZY95ybaScJTkJFPpPR3m6 susWre4gl2D4mQNc04zoZVvVQkIstz47RoIBn0zQRIK6BfMX3qpi/VSftm4BYLlqY6Nx yEybRY5kZJ3QCj9cHHn+R8R+0LJiD8C8m0W7faKZSheXzBUrFaWPyDxSIcgMCTinKO+0 OzbxGmls6ZLbEtGVF4Osn1+6Sb7agvqNMH8vwdgqKJvncZ4qFTXCAubslx328JPHJnw5 MxzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=IPKRFskk; 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 v7-20020a63bf07000000b0039854fb2001si14761975pgf.496.2022.04.05.16.56.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 16:56:25 -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=IPKRFskk; 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 7E28C27795C; Tue, 5 Apr 2022 16:42:03 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344836AbiDENRT (ORCPT + 99 others); Tue, 5 Apr 2022 09:17:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344278AbiDEJTD (ORCPT ); Tue, 5 Apr 2022 05:19:03 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D87222B18; Tue, 5 Apr 2022 02:07:04 -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 46DEEB818F3; Tue, 5 Apr 2022 09:07:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9898AC385A0; Tue, 5 Apr 2022 09:07:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649149622; bh=hOasPOHGeymPIR9CFGjXih8tpW9A6b/MeZtf9fDUetE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IPKRFskkB0lTGoEGTBuHxdT2d+/ShH/19JDDv/5WkZ+YVQAl/BUexkMjQeBtdDJth WTHVlaTS0Ohd9YUD0rihSQTgLyPZkUVZza5IiGFubPaaZ+h3TsU3nV4g7dEAVcuFIq Xk9NGlNccGj1GihaiJgNs8EzYRn7/d9/QgFz2vwQ= 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.16 0754/1017] gcc-plugins/stackleak: Exactly match strings instead of prefixes Date: Tue, 5 Apr 2022 09:27:47 +0200 Message-Id: <20220405070416.641221640@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@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