RE: [PATCH 1/1] scripts: kstack_erase: use relative stackleak plugin path

Jaihind Yadav <[email protected]>
Newsgroups org.kernel.vger.linux-hardening,org.kernel.vger.linux-kbuild,org.kernel.vger.linux-kernel
Message-ID <CY5PR02MB89911C30B7E51FE706061818E2DC2@CY5PR02MB8991.namprd02.prod.outlook.com>
Hi Nathan And Nicolas ,

Thanks for the feedback.

I took another look at this and experimented with a different approach for external modules:

diff --git a/scripts/Makefile.kstack_erase b/scripts/Makefile.kstack_erase
index ee7e4ef7b892..xxxxxxxxxxxx 100644
--- a/scripts/Makefile.kstack_erase
+++ b/scripts/Makefile.kstack_erase
@@ -5,6 +5,10 @@ kstack-erase-cflags-y += -fplugin=$(objtree)/scripts/gcc-plugins/stackleak_plugin.so
 kstack-erase-cflags-y += -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_KSTACK_ERASE_TRACK_MIN_SIZE)
 kstack-erase-cflags-y += -fplugin-arg-stackleak_plugin-arch=$(SRCARCH)
 kstack-erase-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK_VERBOSE) += -fplugin-arg-stackleak_plugin-verbose
+ifneq ($(KBUILD_EXTMOD),)
+# Avoid embedding absolute -fplugin paths into external module DWARF metadata.
+kstack-erase-cflags-y += -gno-record-gcc-switches
+endif
 DISABLE_KSTACK_ERASE := -fplugin-arg-stackleak_plugin-disable
 endif

In my testing, this prevents the absolute stackleak plugin path specified via

    -fplugin=$(objtree)/scripts/gcc-plugins/stackleak_plugin.so

from being recorded in the DWARF information of out-of-tree modules, while avoiding the use of platform-specific utilities such as realpath/readlink --relative-to.

I agree this does not solve the broader issue for all GCC plugins, but it appears to address the specific KSTACK_ERASE case that originally motivated the discussion.

Would this direction be more acceptable than the previous relative-path approach, or would suppressing GCC switch recording for external modules be considered undesirable from a debugging-information perspective?

Thanks,
Jaihind

-----Original Message-----
From: Nathan Chancellor <[email protected]> 
Sent: Monday, August 3, 2026 11:42 PM
To: Nicolas Schier <[email protected]>
Cc: Jaihind Yadav <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 1/1] scripts: kstack_erase: use relative stackleak plugin path

WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.

On Fri, Jul 24, 2026 at 10:18:57PM +0200, Nicolas Schier wrote:
> On Wed, Jul 22, 2026 at 07:00:43PM +0000, Jaihind Yadav wrote:
> >
> > Hi Nicolas Schier,
> >
> > Thanks for the suggestion. I tested the proposed change:
> >
> > -kstack-erase-cflags-y += 
> > -fplugin=$(objtree)/scripts/gcc-plugins/stackleak_plugin.so
> > +kstack-erase-cflags-y += 
> > +-fplugin=scripts/gcc-plugins/stackleak_plugin.so
> >
> >
> > Unfortunately, it does not work for the out-of-tree module builds in 
> > my environment.
>
> Thanks for checking; and yes I forgot out-of-tree modules.  When 
> building them, Kbuild changes to their respective output directory so 
> my suggestion had to fail for oot kmods.

Technically, this is only a problem for out of tree kernel modules, right? When building in-tree kernel modules, objtree is always '.', so we will never have the output directory path in the debug information (at least based on my brief testing).

> > Given that the proposed relative path breaks out-of-tree module 
> > builds, could you please take a look at the patch I posted? If the 
> > approach looks reasonable, I would appreciate your review and 
> > consideration for merging it.
>
> I am not sure, if we can take your path, as 'readlink --relative-to' 
> is only available in coreutils/Linux but coreutils are not a 
> documented requirement for Linux builds (cp. 
> Documentation/process/changes.rst),
> thus this would probably kill Linux builds on BSDs and Darwin/MacOS.
>
> But let me think about that a few days;  and perhaps someone else 
> comes up with an idea.

Yeah, I don't really like 'realpath --relative-to' either, it feels a bit like a hack since it will only avoid including a full path if the module output directory includes part of the build directory. For
example:

  O=/mnt/build/kernel
  MO=/mnt/build/extmod

$ realpath -m --relative-to /mnt/build/extmod /mnt/build/kernel ../kernel

vs.

  O=/home/nathan/build/kernel
  MO=/mnt/build/extmod

$ realpath -m --relative-to /mnt/build/extmod /home/nathan/build/kernel ../../../home/nathan/build/kernel

Furthermore, this patch only fixes KSTACK_ERASE but we have other GCC plugins that presumably have the same problem.

I do not have a good solution for this either but since it only affects external kernel modules, I am not particularly inclined to think too hard about it.

--
Cheers,
Nathan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.