All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sasha Levin" <sashal@kernel.org>
Cc: linux-modules@vger.kernel.org
Subject: Re: [PATCH v8 2/4] kallsyms: extend lineinfo to loadable modules
Date: Sat, 01 Aug 2026 14:42:50 +0000	[thread overview]
Message-ID: <20260801144250.E347C1F00AC4@smtp.kernel.org> (raw)
In-Reply-To: <20260801143219.3975824-3-sashal@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Integer overflow in `mod_lineinfo_filenames_off()` and `module_lookup_lineinfo_section()` size calculations bypasses bounds checks, leading to out-of-bounds memory accesses in NMI/panic context.
- [Medium] `gen_lineinfo` fails to apply `.debug_info` relocations, causing `dwarf_getsrclines` to read the wrong line program for all CUs except the first in an ET_REL module.
--

--- Patch [2]: [PATCH v8 2/4] kallsyms: extend lineinfo to loadable modules ---
commit 4466d01d38fe10716b6638bf8ed427b3c7f20a03
Author: Sasha Levin <sashal@kernel.org>
Subject: kallsyms: extend lineinfo to loadable modules

This commit extends CONFIG_KALLSYMS_LINEINFO to loadable kernel modules.
It post-processes each .ko at build time to embed a lineinfo section,
which is used at runtime to annotate stack traces from module code.

> diff --git a/include/linux/mod_lineinfo.h b/include/linux/mod_lineinfo.h
> new file mode 100644
> index 0000000000000..75d0bfba6ce57
> --- /dev/null
> +++ b/include/linux/mod_lineinfo.h
> @@ -0,0 +1,122 @@
[ ... ]
> +static inline u32 mod_lineinfo_addrs_off(void)
> +{
> +	return sizeof(struct mod_lineinfo_header);
> +}
> +
> +static inline u32 mod_lineinfo_file_ids_off(u32 num_entries)
> +{
> +	return mod_lineinfo_addrs_off() + num_entries * sizeof(u32);
> +}
> +
> +static inline u32 mod_lineinfo_lines_off(u32 num_entries)
> +{
> +	/* u16 file_ids[] may need 2-byte padding to align lines[] to 4 bytes */
> +	u32 off = mod_lineinfo_file_ids_off(num_entries) +
> +		  num_entries * sizeof(u16);
> +	return (off + 3) & ~3u;
> +}
> +
> +static inline u32 mod_lineinfo_file_offsets_off(u32 num_entries)
> +{
> +	return mod_lineinfo_lines_off(num_entries) + num_entries * sizeof(u32);
> +}
> +
> +static inline u32 mod_lineinfo_filenames_off(u32 num_entries, u32 num_files)
> +{
> +	return mod_lineinfo_file_offsets_off(num_entries) +
> +	       num_files * sizeof(u32);
> +}
[ ... ]
> diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
> index f23126d804b25..b0e703e29708b 100644
> --- a/kernel/module/kallsyms.c
> +++ b/kernel/module/kallsyms.c
[ ... ]
> +static bool module_lookup_lineinfo_section(const void *blob, u32 blob_size,
> +					   u32 hdr_offset,
> +					   unsigned int section_offset,
> +					   unsigned int min_offset,
> +					   const char **file,
> +					   unsigned int *line)
> +{
[ ... ]
> +	/*
> +	 * Validate counts before multiplying — sizing arithmetic could
> +	 * otherwise overflow on 32-bit with a malformed blob.  Each entry
> +	 * contributes one u32 (addrs), one u16 (file_ids), and one u32
> +	 * (lines); each file contributes one u32 (file_offsets).
> +	 */
> +	{
> +		u32 avail = blob_size - hdr_offset;
> +		u32 needed = mod_lineinfo_filenames_off(num_entries, num_files);
> +
> +		if (num_entries > U32_MAX / sizeof(u32))
> +			return false;
> +		if (num_files > U32_MAX / sizeof(u32))
> +			return false;
> +		if (needed > avail || filenames_size > avail - needed)
> +			return false;
> +	}

[Severity: High]
Can the needed calculation overflow before the bounds check is evaluated?

If num_entries is maliciously large (e.g., 0x33333334), the calculation in
mod_lineinfo_filenames_off() sums the array offsets using 32-bit unsigned
arithmetic (around 10 bytes per entry). This would wrap around modulo 2^32,
resulting in a small needed value that bypasses the needed > avail check.
A later binary search on addrs could then access out-of-bounds memory.

The division check on num_entries above only guards against a single u32
array's size, not the combined 10 bytes per entry.

> diff --git a/scripts/gen_lineinfo.c b/scripts/gen_lineinfo.c
> index a3ee54eaad05d..725a8dfbf011a 100644
> --- a/scripts/gen_lineinfo.c
> +++ b/scripts/gen_lineinfo.c
[ ... ]
> +static void apply_debug_line_relocations(Elf *elf)
> +{
[ ... ]
> +	/* Find the relevant sections */
> +	while ((scn = elf_nextscn(elf, scn)) != NULL) {
> +		const char *name;
> +
> +		if (!gelf_getshdr(scn, &shdr))
> +			continue;
> +		name = elf_strptr(elf, shstrndx, shdr.sh_name);
> +		if (!name)
> +			continue;
> +
> +		if (!strcmp(name, ".debug_line"))
> +			debug_line_scn = scn;
> +		else if (!strcmp(name, ".rela.debug_line"))
> +			rela_debug_line_scn = scn;
> +		else if (!strcmp(name, ".rel.debug_line"))
> +			rel_debug_line_scn = scn;
> +		else if (shdr.sh_type == SHT_SYMTAB)
> +			symtab_scn = scn;
> +	}

[Severity: Medium]
Does this miss relocations for ET_REL modules that contain multiple object
files?

This loop collects and applies relocations for .debug_line, but appears to
omit .rela.debug_info and .rel.debug_info entirely.

Without the relocations applied to .debug_info, the DW_AT_stmt_list attribute
for all compile units might remain at offset 0.

> +static void process_dwarf(Dwarf *dwarf, unsigned long long text_addr,
> +			  struct covered_section *sections,
> +			  unsigned int num_sections)
>  {
[ ... ]
> +		if (dwarf_getsrclines(&cudie, &lines, &nlines) != 0)
> +			goto next;

When dwarf_getsrclines() is later called in process_dwarf(), will it
repeatedly read the line program of only the first compile unit, leaving
most module functions without line info?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260801143219.3975824-1-sashal@kernel.org?part=2

  reply	other threads:[~2026-08-01 14:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-01 14:32 [PATCH v8 0/4] kallsyms: embed source file:line info in kernel stack traces Sasha Levin
2026-08-01 14:32 ` [PATCH v8 1/4] " Sasha Levin
2026-08-01 14:45   ` sashiko-bot
2026-08-01 14:32 ` [PATCH v8 2/4] kallsyms: extend lineinfo to loadable modules Sasha Levin
2026-08-01 14:42   ` sashiko-bot [this message]
2026-08-01 14:32 ` [PATCH v8 3/4] kallsyms: delta-compress lineinfo tables for ~2.7x size reduction Sasha Levin
2026-08-01 14:39   ` sashiko-bot
2026-08-01 14:32 ` [PATCH v8 4/4] kallsyms: add KUnit tests for lineinfo feature Sasha Levin
2026-08-01 14:45   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260801144250.E347C1F00AC4@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=sashal@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.