Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1178202rdf; Wed, 22 Nov 2023 07:33:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHuNZoGcbQGE7l3cVUGjTnc0wnGJqGZ69WcgiS2/GmTRfMz+VoK7pSUosrkW153/Jvx/nTl X-Received: by 2002:a05:6808:144d:b0:3a7:8f94:743a with SMTP id x13-20020a056808144d00b003a78f94743amr3521393oiv.10.1700667232602; Wed, 22 Nov 2023 07:33:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700667232; cv=none; d=google.com; s=arc-20160816; b=QctN2kGwM2psIhTFrFXND0dslRvjMViGHAXNZ9jvOaHfnOnJA9GGct4qF2Bk4k4D9h qSCZSHAaRJlwsJoLzL7Uq92Jf0O3nC9TquS8OsOopXwwW11q4uSqd57QIKuc4fhxslxX +0M/5OwXo3cL/m+/EG/E5VxPAEJzuEzbuYW60E5It5Z7kYpdGlktFb2K4AuY8I3UndGN i5H7h4w2U7aaXfRd2jcCLGWwa3hgSzdTVcLi9oVQw26Pdi5wDkC468xktRstDAem0PQ2 NaHqdKK0ioRFAEkN6pnYiwiFoXuUJe6rfWrCP1qrSpchjWyrytN53OnTfB9aTJ5h5t4X 3nxg== 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=HFTviOW/gQyxdzQnnjwG8SQIBprfIComo+YXlyFyagQ=; fh=08xtKUG0J06gx7n/sOHpDOdXL0rL+8j4wVpZh4BTRJY=; b=0X2/cWpp3W5sFoYg619YT6wySLpmbZrLLUTr1e25PXhGMTnOEHKhmSoXcHncFnQmj0 9jRHbVDfPnlTiWPl6UfRkkbK4LMC1Twj4wmjQSxKfXXK1MERGEJx9W26LKWmFsuZtIlp n0lS2EaZi5hLMbqodkfunf4rHo0rJ6OE+ZjsZiJXUVi/EYOn21cvKy0T+RKHW0tLX8jK Ij9INm0EoNlFobLkhPv03ZZn/bD5YyiY4da5JBpG3PMRNpoLLXOwZy1toydE6/wtHdfH CCUxvRow5gMmbnUqIVinBOMo8g+ok2bEC3tRQXNsH5nEuEf7B+JABtUR7krYcLZJBl+v EFcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HZqnYWg5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id s66-20020a635e45000000b005893c5bd119si12889345pgb.425.2023.11.22.07.33.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 07:33:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HZqnYWg5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 4A0BC8212A90; Wed, 22 Nov 2023 07:33:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344417AbjKVPda (ORCPT + 99 others); Wed, 22 Nov 2023 10:33:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344374AbjKVPdP (ORCPT ); Wed, 22 Nov 2023 10:33:15 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1128910E4; Wed, 22 Nov 2023 07:32:54 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D33F2C433CD; Wed, 22 Nov 2023 15:32:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700667173; bh=cpaY/yV5WXyg9YEkeW49vlb9lDNYl17fwLO5WtfKdaE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HZqnYWg5A6oihjt485DLEacJvfJVSLWbvjNnUuqwvCkvFKTfejc18yeeNVh8sGViM 48MlpIfbMVVFujNPJylr0wSImecgsvYNlj93CQECCSd48dPCw57+8VZgKizI082XS7 1bqQGKDnqlt8viMLwuAdZcfXqO7B9fVjZ/Ha2IGXXdqZiF5stDzsiFCSy9gfzoxSOM Nv7KpWgmZTy5GoyDR5l7/MmaxfYDaCVWpDeHvpEuHVZ/PLMTyR5j6BJELr7JzkqHRl faA98HdGcbXdKpS0k0Bc7a+R05eT1h6uMQrL578ZO87GX8CKAi8lMb3bUlR02+K4dm Zvw4JcBJtme5Q== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Masahiro Yamada , Sasha Levin , linux-kbuild@vger.kernel.org Subject: [PATCH AUTOSEL 6.6 13/17] kconfig: fix memory leak from range properties Date: Wed, 22 Nov 2023 10:31:42 -0500 Message-ID: <20231122153212.852040-13-sashal@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231122153212.852040-1-sashal@kernel.org> References: <20231122153212.852040-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.2 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 22 Nov 2023 07:33:51 -0800 (PST) From: Masahiro Yamada [ Upstream commit ae1eff0349f2e908fc083630e8441ea6dc434dc0 ] Currently, sym_validate_range() duplicates the range string using xstrdup(), which is overwritten by a subsequent sym_calc_value() call. It results in a memory leak. Instead, only the pointer should be copied. Below is a test case, with a summary from Valgrind. [Test Kconfig] config FOO int "foo" range 10 20 [Test .config] CONFIG_FOO=0 [Before] LEAK SUMMARY: definitely lost: 3 bytes in 1 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 17,465 bytes in 21 blocks suppressed: 0 bytes in 0 blocks [After] LEAK SUMMARY: definitely lost: 0 bytes in 0 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 17,462 bytes in 20 blocks suppressed: 0 bytes in 0 blocks Signed-off-by: Masahiro Yamada Signed-off-by: Sasha Levin --- scripts/kconfig/symbol.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c index 0572330bf8a78..a76925b46ce63 100644 --- a/scripts/kconfig/symbol.c +++ b/scripts/kconfig/symbol.c @@ -122,9 +122,9 @@ static long long sym_get_range_val(struct symbol *sym, int base) static void sym_validate_range(struct symbol *sym) { struct property *prop; + struct symbol *range_sym; int base; long long val, val2; - char str[64]; switch (sym->type) { case S_INT: @@ -140,17 +140,15 @@ static void sym_validate_range(struct symbol *sym) if (!prop) return; val = strtoll(sym->curr.val, NULL, base); - val2 = sym_get_range_val(prop->expr->left.sym, base); + range_sym = prop->expr->left.sym; + val2 = sym_get_range_val(range_sym, base); if (val >= val2) { - val2 = sym_get_range_val(prop->expr->right.sym, base); + range_sym = prop->expr->right.sym; + val2 = sym_get_range_val(range_sym, base); if (val <= val2) return; } - if (sym->type == S_INT) - sprintf(str, "%lld", val2); - else - sprintf(str, "0x%llx", val2); - sym->curr.val = xstrdup(str); + sym->curr.val = range_sym->curr.val; } static void sym_set_changed(struct symbol *sym) -- 2.42.0