Skip to contentSkip to conversation

kbuild: detect module name collisions between '-' and '_'

PATCH In review Hamdy Abou El Anein 4 participants 6 messages Nov 3 – Nov 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: detect module name collisions between '-' and '_'

From: Hamdy Abou El Anein <[email protected]>

Kbuild converts hyphens to underscores in KBUILD_MODNAME, and the module loader treats both characters as equivalent. However, modules-check.sh compares module basenames without normalization. Consequently, modules named foo-bar.o and foo_bar.o pass the conflict check even though they have the same effective module name.

Normalize basenames before checking for duplicates, and use awk to show the original module paths in diagnostics. Preserve detection of exact duplicates and leave distinct names unaffected.

---
 scripts/modules-check.sh | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
scripts/modules-check.sh +11 -2
index 4c8da90..3d12dc5 100755
@@ -13,10 +13,19 @@ exit_code=0
 # Check uniqueness of module names
 check_same_name_modules()
 {
-	for m in $(sed 's:.*/::' "$1" | sort | uniq -d)
+	for m in $(sed 's:.*/::; s/-/_/g' "$1" | sort | uniq -d)
 	do
 		echo "error: the following would cause module name conflict:" >&2
-		sed -n "/\/$m/s:^\(.*\)\.o\$:  \1.ko:p" "$1" >&2
+		awk -v name="$m" '
+			{
+				basename = $0
+				sub(/^.*\//, "", basename)
+				gsub(/-/, "_", basename)
+				if (basename == name) {
+					sub(/\.o$/, ".ko")
+					print "  " $0
+				}
+			}' "$1" >&2
 		exit_code=1
 	done
 }

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.56.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines

Kbuild converts hyphens to underscores in KBUILD_MODNAME, and the module loader treats both characters as equivalent. However, modules-check.sh compares module basenames without normalization. Consequently, modules named foo-bar.o and foo_bar.o pass the conflict check even though they have the same effective module name.

Normalize basenames before checking for duplicates, and use awk to show the original module paths in diagnostics. Preserve detection of exact duplicates and leave distinct names unaffected.

Good catch, I was able to reproduce this type of miss. This resolves it. I suppose this has been a problem since this patch was first merged?

Signature · 2 lines
Cheers,
Nathan
#2 lore raw

Hi Nathan,

Yes, it appears so. The module-name uniqueness check dates back to 2019 and compared basenames without normalizing '-' and '_', so this case seems to have been missed since its introduction.

Thanks for reviewing,
Hamdy

maintainer
#3 lore raw
In reply to Hamdy Abou El Anein
Quoted 11 lines · Hamdy Abou El Anein

Kbuild converts hyphens to underscores in KBUILD_MODNAME, and the module loader treats both characters as equivalent. However, modules-check.sh compares module basenames without normalization. Consequently, modules named foo-bar.o and foo_bar.o pass the conflict check even though they have the same effective module name.

Normalize basenames before checking for duplicates, and use awk to show the original module paths in diagnostics. Preserve detection of exact duplicates and leave distinct names unaffected.

[...]

Applied to kbuild/linux.git (kbuild-next-unstable), with 'Fixes:' tag added, thanks!

[1/1] kbuild: detect module name collisions between '-' and '_'

      https://git.kernel.org/kbuild/c/bc348949

Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped, reverted or modified (e.g. trailers).

Patches applied to the kbuild-next-unstable branch are accepted pending wider testing in linux-next and any post-commit review; they will generally be moved to the kbuild-next branch in about a week if no issues are found.

Best regards,

Signature · 1 line
Nicolas
#4 lore raw
Quoted 4 lines · Nicolas Schier

Applied to kbuild/linux.git (kbuild-next-unstable), with 'Fixes:' tag added, thanks!

[1/1] kbuild: detect module name collisions between '-' and '_'

     https://git.kernel.org/kbuild/c/bc348949

With this applied, ARCH=arm allmodconfig fails in modules_check on two pairs of modules that have been in the tree for years:

error: the following would cause module name conflict:
drivers/input/apm-power.ko drivers/power/supply/apm_power.ko
error: the following would cause module name conflict:
arch/arm/probes/kprobes/test-kprobes.ko lib/tests/test_kprobes.ko

KernelCI report:

https://d.kernelci.org/i/maestro:a0850dfdbf5eccb82fb3e7100a7178920a91c662

--
Thanks,
Sasha

#5 lore raw
Quoted 8 lines · Sasha Levin
Quoted 4 lines · Nicolas Schier

Applied to kbuild/linux.git (kbuild-next-unstable), with 'Fixes:' tag added, thanks!

[1/1] kbuild: detect module name collisions between '-' and '_'

     https://git.kernel.org/kbuild/c/bc348949

With this applied, ARCH=arm allmodconfig fails in modules_check on two pairs of modules that have been in the tree for years:

Thanks a lot for the report. I tested arm64 and x86_64 allmodconfig but failed to do arm. I sent the following patches for these.

Quoted 3 lines

error: the following would cause module name conflict:
drivers/input/apm-power.ko drivers/power/supply/apm_power.ko

https://lore.kernel.org/[email protected]/

Quoted 3 lines

error: the following would cause module name conflict:
arch/arm/probes/kprobes/test-kprobes.ko lib/tests/test_kprobes.ko

https://lore.kernel.org/[email protected]/

Signature · 2 lines
Cheers,
Nathan