Re: [PATCH] klp-build: Reuse the target kernel randstruct seed

From: Josh Poimboeuf

Date: Sun Nov 22 2026 - 02:00:22 EST


On Tue, Oct 20, 2026 at 02:23:41PM -0700, Song Liu wrote:
> CONFIG_RANDSTRUCT randomizes struct layouts based on a seed which kbuild
> generates from /dev/urandom. A livepatch has to observe the same layouts
> as the kernel it is loaded into, but klp-build runs 'make clean', after
> which kbuild generates a new seed, and the resulting module can't be
> loaded:
>
> module livepatch_foo: .gnu.linkonce.this_module
> section size must match the kernel's built struct module size at run time
>
> struct module had grown 64 bytes of padding relative to the running
> kernel (0x540 vs 0x500). Every other randomized struct the patch touches
> would be read at the wrong offsets too, so this is not just a loader nit.
>
> Reuse the seed of the kernel being patched, taken from --randstruct-seed
> or from scripts/basic/randstruct.seed in the tree, and refuse to build
> when CONFIG_RANDSTRUCT is on and no valid seed is available. A wrong
> module is worse than no module.
>
> Pin scripts/gen-randstruct-seed.sh rather than the generated seed file:
> clean_kernel() removes scripts/basic/.randstruct.seed.cmd, so kbuild's
> if_changed finds no saved command and regenerates the seed no matter what
> the file already holds. The generator is stashed, so it is restored on
> exit like scripts/setlocalversion.
>
> Read the seed before set_kernelversion(), but pin the generator after it.
> set_kernelversion() runs 'make listnewconfig' and 'make syncconfig', which
> build scripts/basic as well, so after a 'make clean' they would replace
> the seed before the default seed file is read. But if the generator is
> already modified when 'make kernelrelease' runs, setlocalversion appends
> "-dirty" to the kernel release and the module's vermagic no longer
> matches the target kernel.
>
> The Clang and GCC plugin implementations both derive their layouts only
> from scripts/basic/randstruct.seed, so pinning the seed covers
> CONFIG_GCC_PLUGIN_RANDSTRUCT too. Drop the check that rejected it. The
> plugin requires a 64 character seed, which is what gen-randstruct-seed.sh
> writes, so validate exactly that format.
>
> An explicit option is needed rather than deriving the path from
> uname -r, because the build host is usually not running the kernel being
> patched.
>
> Signed-off-by: Song Liu <song@xxxxxxxxxx>

Seems fine, can you address the Sashiko comments?

--
Josh