From patchwork Fri Jan 3 07:30:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13925268 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78BD71B0F29; Fri, 3 Jan 2025 07:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889457; cv=none; b=DB8MtD2MEC6KvFIYZK4nAO+DOPuBBtYrALyhU6wyroE14dCt1Mi5UHfcWtGZgaoAp/tYIhBGuaIBPGWrn60R84H97b9zuw322j8aEFV20jYyZ29DAbLYKGoLUPHNp2gR5Dh/cMQXfhaL9tNmB33p27PDBDIUgE1znCg7LpotXps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735889457; c=relaxed/simple; bh=Vd1RoZapGlgiKLwvIFiTdfFVnrMDlztoRQJAU9ZVH0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bwx0ilX0NNCBJ7MY+ZTTCimd314trJSwjl8+11NCrOHJ41OtVYI/cZLyLzftmMkLE/SvsjhQHziH/Y9xSsN6RAdMiXgKdfN1VvoqEOlSx066X9KGIqDpumitLiTPsd2Wd2PiiWRF1uW5s/Q37gF0OlxZiaTJ7kevW7MD7+YGQ+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ntuCP5vy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ntuCP5vy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4C7DC4CED7; Fri, 3 Jan 2025 07:30:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735889457; bh=Vd1RoZapGlgiKLwvIFiTdfFVnrMDlztoRQJAU9ZVH0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ntuCP5vythvm2ea3DV4pIf3qyfLKJC7aUQpotxCOHZNncwfPfeOlrnStUXGCPJ7QC GoWDGRpPWI/Q3S3rdWCV4QK25OhCfLJZxZpQym3HnLgWB2kQvp7NuECLbVk07xrrm/ Hqqc900nInM3Ivp2cNUtRj3zmfwvr41MMLYpSRiXtg+Iiu4HcYeeoA6pt5VxCy2Y85 CAY05b4JFKWaDsLliSPVf8rkrXkjbf4omsiA0ojESoB20eefGWm7U8YYKM5u9aR05k TRJZ+UtPTvt077MKe4kG8bHpBjxvMOPAOcswoXKkfZD7BFgPyuW6FITgUYsREoxqoq akSniTiohLQcg== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Andreas Gruenbacher , Andrew Morton , Sam Ravnborg Subject: [PATCH 2/6] genksyms: fix memory leak when the same symbol is read from *.symref file Date: Fri, 3 Jan 2025 16:30:39 +0900 Message-ID: <20250103073046.2609911-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250103073046.2609911-1-masahiroy@kernel.org> References: <20250103073046.2609911-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When a symbol that is already registered is read again from *.symref file, __add_symbol() removes the previous one from the hash table without freeing it. [Test Case] $ cat foo.c #include void foo(void); void foo(void) {} EXPORT_SYMBOL(foo); $ cat foo.symref foo void foo ( void ) foo void foo ( void ) When a symbol is removed from the hash table, it must be freed along with its ->name and ->defn members. However, sym->name cannot be freed because it is sometimes shared with node->string, but not always. If sym->name and node->string share the same memory, free(sym->name) could lead to a double-free bug. To resolve this issue, always assign a strdup'ed string to sym->name. Fixes: 64e6c1e12372 ("genksyms: track symbol checksum changes") Signed-off-by: Masahiro Yamada --- scripts/genksyms/genksyms.c | 8 ++++++-- scripts/genksyms/genksyms.h | 2 +- scripts/genksyms/parse.y | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index 8ca46f807b57..c5e8e0e0f949 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -272,11 +272,15 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type, break; } } + + free_list(sym->defn, NULL); + free(sym->name); + free(sym); --nsyms; } sym = xmalloc(sizeof(*sym)); - sym->name = name; + sym->name = xstrdup(name); sym->type = type; sym->defn = defn; sym->expansion_trail = NULL; @@ -483,7 +487,7 @@ static void read_reference(FILE *f) defn = def; def = read_node(f); } - subsym = add_reference_symbol(xstrdup(sym->string), sym->tag, + subsym = add_reference_symbol(sym->string, sym->tag, defn, is_extern); subsym->is_override = is_override; free_node(sym); diff --git a/scripts/genksyms/genksyms.h b/scripts/genksyms/genksyms.h index 21ed2ec2d98c..5621533dcb8e 100644 --- a/scripts/genksyms/genksyms.h +++ b/scripts/genksyms/genksyms.h @@ -32,7 +32,7 @@ struct string_list { struct symbol { struct symbol *hash_next; - const char *name; + char *name; enum symbol_type type; struct string_list *defn; struct symbol *expansion_trail; diff --git a/scripts/genksyms/parse.y b/scripts/genksyms/parse.y index 840371d01bf4..689cb6bb40b6 100644 --- a/scripts/genksyms/parse.y +++ b/scripts/genksyms/parse.y @@ -482,12 +482,12 @@ enumerator_list: enumerator: IDENT { - const char *name = strdup((*$1)->string); + const char *name = (*$1)->string; add_symbol(name, SYM_ENUM_CONST, NULL, 0); } | IDENT '=' EXPRESSION_PHRASE { - const char *name = strdup((*$1)->string); + const char *name = (*$1)->string; struct string_list *expr = copy_list_range(*$3, *$2); add_symbol(name, SYM_ENUM_CONST, expr, 0); }