On Fri, May 09, 2025 at 01:17:23PM -0700, Josh Poimboeuf wrote:
> +# Build and post-process livepatch module in $KMOD_DIR
> +build_patch_module() {
> +     local makefile="$KMOD_DIR/Kbuild"
> +     local log="$KMOD_DIR/build.log"
> +     local cflags=()
> +     local files=()
> +     local cmd=()
> +
> +     rm -rf "$KMOD_DIR"
> +     mkdir -p "$KMOD_DIR"
> +
> +     cp -f "$SRC/scripts/livepatch/init.c" "$KMOD_DIR"
> +
> +     echo "obj-m := $NAME.o" > "$makefile"
> +     echo -n "$NAME-y := init.o" >> "$makefile"
> +
> +     find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files
> +     [[ ${#files[@]} -eq 0 ]] && die "no changes detected"
> +
> +     for file in "${files[@]}"; do
> +             local rel_file="${file#"$DIFF_DIR"/}"
> +             local kmod_file="$KMOD_DIR/$rel_file"
> +             local cmd_file
> +
> +             mkdir -p "$(dirname "$kmod_file")"
> +             cp -f "$file" "$kmod_file"
> +
> +             # Tell kbuild this is a prebuilt object
> +             cp -f "$file" "${kmod_file}_shipped"
> +
> +             echo -n " $rel_file" >> "$makefile"
> +
> +             cmd_file="$ORIG_DIR/$(dirname "$rel_file")/.$(basename 
> "$rel_file").cmd"
> +             [[ -e "$cmd_file" ]] && cp -f "$cmd_file" "$(dirname 
> "$kmod_file")"
> +     done
> +
> +     echo >> "$makefile"
> +
> +     cflags=("-ffunction-sections")
> +     cflags+=("-fdata-sections")
> +     [[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE")
> +
> +     cmd=("make")
> +     cmd+=("$VERBOSE")
> +     cmd+=("-j$CPUS")
> +     cmd+=("--directory=.")
> +     cmd+=("M=$KMOD_DIR")
> +     cmd+=("KCFLAGS=${cflags[*]}")
> +
> +     # Build a "normal" kernel module with init.c and the diffed objects
> +     (
> +             cd "$SRC"
> +             "${cmd[@]}"                                                     
> \
> +                     >  >(tee -a "$log")                                     
> \
> +                     2> >(tee -a "$log" >&2)
> +     )
> +
> +     # Save off the intermediate binary for debugging
> +     cp -f "$KMOD_DIR/$NAME.ko" "$KMOD_DIR/$NAME.ko.orig"
> +
> +     # Fix (and work around) linker wreckage for klp syms / relocs
> +     "$SRC/tools/objtool/objtool" klp post-link "$KMOD_DIR/$NAME.ko" || die 
> "objtool klp post-link failed"
> +
> +     cp -f "$KMOD_DIR/$NAME.ko" "$OUTFILE"
> +}

Hi Josh,

Another small bug feature? report: module symbol namespaces.

If you touch sound/soc/sof/intel/, klp-build will error out with:

  Building patch module: livepatch-unCVE-2024-58012.ko
  ERROR: modpost: module livepatch-unCVE-2024-58012 uses symbol hda_dai_config 
from namespace SND_SOC_SOF_INTEL_HDA_COMMON, but does not import it.
  ERROR: modpost: module livepatch-unCVE-2024-58012 uses symbol 
hdac_bus_eml_sdw_map_stream_ch from namespace SND_SOC_SOF_HDA_MLINK, but does 
not import it.
  make[2]: *** [scripts/Makefile.modpost:145: 
/home/jolawren/src/centos-stream-10/klp-tmp/kmod/Module.symvers] Error 1
  make[1]: *** [/home/jolawren/src/centos-stream-10/Makefile:1936: modpost] 
Error 2
  make: *** [Makefile:236: __sub-make] Error 2

since the diff objects do not necessarily carry forward the namespace
import.

There's several options to how to handle it (cross-reference with
Modules.symvers, copy out the .modinfo sections, include the section in
the diff .o, etc.) ... my late afternoon hack just snarfed it from the
original objects with a modinfo hack.  Anyway, you get the idea.

-- Joe

-->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8--

@@ -687,7 +700,9 @@ build_patch_module() {
        cp -f "$SRC/scripts/livepatch/init.c" "$KMOD_DIR"
 
        echo "obj-m := $NAME.o" > "$makefile"
-       echo -n "$NAME-y := init.o" >> "$makefile"
+
+       echo "#include <linux/module.h>" >> "$KMOD_DIR/namespaces.c"
+       echo -n "$NAME-y := init.o namespaces.o" >> "$makefile"
 
        find "$DIFF_DIR" -type f -name "*.o" | mapfile -t files
        [[ ${#files[@]} -eq 0 ]] && die "no changes detected"
@@ -697,6 +712,13 @@ build_patch_module() {
                local kmod_file="$KMOD_DIR/$rel_file"
                local cmd_file
 
+               # Symbol namespace hack
+               echo ln -s -f "$file" ns-temp.ko
+               ln -s -f "$ORIG_DIR/$rel_file" ns-temp.ko
+               for ns in $(modinfo ns-temp.ko -F import_ns); do
+                       echo "MODULE_IMPORT_NS(\"$ns\");" >> 
"$KMOD_DIR/namespaces.c"
+               done
+
                mkdir -p "$(dirname "$kmod_file")"
                cp -f "$file" "$kmod_file"
 
--
Joe


Reply via email to