From 24ae78034432754bcf3a2f957843ddc37093ba87 Mon Sep 17 00:00:00 2001 From: hubin Date: Tue, 10 May 2022 15:37:18 +0800 Subject: [PATCH] fix segment fault and hotpatch compilation problem when using openEuler 22.03 LTS kernel --- ...hook-disable-HAVE_SIMPLE_ENABLE-to-k.patch | 27 ++++++++++ ...-KBUILD_MODPOST_WARN-1-to-avoid-modp.patch | 51 +++++++++++++++++++ ...ate-find_parent_obj-to-avoid-error-t.patch | 29 +++++++++++ ...ct-fix-segment-fault-when-using-KPAT.patch | 34 +++++++++++++ ...place-__section-with-__kpatch_sectio.patch | 27 ++++++++++ ...ct-ignore-changed-of-section-__patch.patch | 35 +++++++++++++ ...ct-fix-null-pointer-dereference-in-k.patch | 27 ++++++++++ ...ct-ignore-.note.gnu.property-section.patch | 36 +++++++++++++ ...ct-skip-creating-.kpatch.arch-sectio.patch | 27 ++++++++++ kpatch.spec | 17 ++++++- 10 files changed, 309 insertions(+), 1 deletion(-) create mode 100644 0029-livepatch-patch-hook-disable-HAVE_SIMPLE_ENABLE-to-k.patch create mode 100644 0030-kpatch-build-add-KBUILD_MODPOST_WARN-1-to-avoid-modp.patch create mode 100644 0031-kpatch-build-update-find_parent_obj-to-avoid-error-t.patch create mode 100644 0032-create-diff-object-fix-segment-fault-when-using-KPAT.patch create mode 100644 0033-kpatch-macros-replace-__section-with-__kpatch_sectio.patch create mode 100644 0034-create-diff-object-ignore-changed-of-section-__patch.patch create mode 100644 0035-create-diff-object-fix-null-pointer-dereference-in-k.patch create mode 100644 0036-create-diff-object-ignore-.note.gnu.property-section.patch create mode 100644 0037-create-diff-object-skip-creating-.kpatch.arch-sectio.patch diff --git a/0029-livepatch-patch-hook-disable-HAVE_SIMPLE_ENABLE-to-k.patch b/0029-livepatch-patch-hook-disable-HAVE_SIMPLE_ENABLE-to-k.patch new file mode 100644 index 0000000..2be8692 --- /dev/null +++ b/0029-livepatch-patch-hook-disable-HAVE_SIMPLE_ENABLE-to-k.patch @@ -0,0 +1,27 @@ +From e8a1f76c0409720106df90e6358d2b00c496b74d Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:26:25 +0800 +Subject: [PATCH 1/9] livepatch-patch-hook: disable HAVE_SIMPLE_ENABLE to keep + register and unregister step of hotpatch + +Signed-off-by: Bin Hu +--- + kmod/patch/livepatch-patch-hook.c | 2 -- + 1 file changed, 2 deletions(-) + +diff --git a/kmod/patch/livepatch-patch-hook.c b/kmod/patch/livepatch-patch-hook.c +index 9e56fe3..339b00f 100644 +--- a/kmod/patch/livepatch-patch-hook.c ++++ b/kmod/patch/livepatch-patch-hook.c +@@ -78,8 +78,6 @@ + # if RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(8, 2) + # define HAVE_KLP_REPLACE + # endif +-#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0) +-# define HAVE_KLP_REPLACE + #endif + + #ifndef KLP_REPLACE_ENABLE +-- +2.31.0.windows.1 + diff --git a/0030-kpatch-build-add-KBUILD_MODPOST_WARN-1-to-avoid-modp.patch b/0030-kpatch-build-add-KBUILD_MODPOST_WARN-1-to-avoid-modp.patch new file mode 100644 index 0000000..dd68bc4 --- /dev/null +++ b/0030-kpatch-build-add-KBUILD_MODPOST_WARN-1-to-avoid-modp.patch @@ -0,0 +1,51 @@ +From f435e99fc2652cd6263f3a2fe02b0947a5279a82 Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:27:28 +0800 +Subject: [PATCH 2/9] kpatch-build: add KBUILD_MODPOST_WARN=1 to avoid modpost + undefined error + +Signed-off-by: hubin +--- + kpatch-build/kpatch-build | 10 +++++----- + 1 file changed, 5 insertions(+), 5 deletions(-) + +diff --git a/kpatch-build/kpatch-build b/kpatch-build/kpatch-build +index 6a9e818..ba33a3e 100644 +--- a/kpatch-build/kpatch-build ++++ b/kpatch-build/kpatch-build +@@ -1030,9 +1030,9 @@ fi + # $TARGETS used as list, no quotes. + # shellcheck disable=SC2086 + if [[ -z "$USERMODBUILDDIR" ]]; then +- make "${MAKEVARS[@]}" "-j$CPUS" $TARGETS 2>&1 | logger || die ++ KBUILD_MODPOST_WARN=1 make "${MAKEVARS[@]}" "-j$CPUS" $TARGETS 2>&1 | logger || die + else +- make "${MAKEVARS[@]}" -C "$USERMODBUILDDIR" M="$USERMODBUILDDIR" $USERMODFLAGS "-j$CPUS" $TARGETS 2>&1 | logger || die ++ KBUILD_MODPOST_WARN=1 make "${MAKEVARS[@]}" -C "$USERMODBUILDDIR" M="$USERMODBUILDDIR" $USERMODFLAGS "-j$CPUS" $TARGETS 2>&1 | logger || die + fi + + # Save original module symvers +@@ -1175,9 +1175,9 @@ for i in $FILES; do + remove_patches + cd "$SRCDIR" || die + if [ -z "$USERMODBUILDDIR" ];then +- make "-j$CPUS" $TARGETS 2>&1 | logger || die ++ KBUILD_MODPOST_WARN=1 make "-j$CPUS" $TARGETS 2>&1 | logger || die + else +- make -C "$USERMODBUILDDIR" M="$USERMODBUILDDIR" $USERMODFLAGS "-j$CPUS" $TARGETS 2>&1 | logger || die ++ KBUILD_MODPOST_WARN=1 make -C "$USERMODBUILDDIR" M="$USERMODBUILDDIR" $USERMODFLAGS "-j$CPUS" $TARGETS 2>&1 | logger || die + fi + cp ${KOBJFILE} ${KOBJFILE_PATH} + apply_patches +@@ -1297,7 +1297,7 @@ KBUILD_EXTRA_SYMBOLS="$KBUILD_EXTRA_SYMBOLS" \ + KPATCH_LDFLAGS="$KPATCH_LDFLAGS" + save_env + +-make "${MAKEVARS[@]}" 2>&1 | logger || die ++KBUILD_MODPOST_WARN=1 make "${MAKEVARS[@]}" 2>&1 | logger || die + strip -g "$TEMPDIR/patch/$MODNAME.ko" + + if [[ "$USE_KLP" -eq 1 ]]; then +-- +2.31.0.windows.1 + diff --git a/0031-kpatch-build-update-find_parent_obj-to-avoid-error-t.patch b/0031-kpatch-build-update-find_parent_obj-to-avoid-error-t.patch new file mode 100644 index 0000000..df5e01c --- /dev/null +++ b/0031-kpatch-build-update-find_parent_obj-to-avoid-error-t.patch @@ -0,0 +1,29 @@ +From f1de68ea87426e7ed65dc615a46f5ce1d15df994 Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:27:52 +0800 +Subject: [PATCH 3/9] kpatch-build: update find_parent_obj to avoid error that + two parent match same object + +Signed-off-by: hubin +--- + kpatch-build/kpatch-build | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/kpatch-build/kpatch-build b/kpatch-build/kpatch-build +index ba33a3e..ce163e5 100644 +--- a/kpatch-build/kpatch-build ++++ b/kpatch-build/kpatch-build +@@ -527,8 +527,8 @@ find_parent_obj() { + [[ "$num" -eq 1 ]] && last_deep_find="$(dirname "$parent")" + fi + if [[ "$num" -eq 0 ]]; then +- parent="$(find $USERMODBUILDDIR -name ".*.cmd" -print0 | xargs -0 grep -l "$grepname" | grep -Fv "$pdir/.${file}.cmd" | head -n1)" +- num="$(find $USERMODBUILDDIR -name ".*.cmd" -print0 | xargs -0 grep -l "$grepname" | grep -Fvc "$pdir/.${file}.cmd")" ++ parent="$(find $USERMODBUILDDIR -name ".*.cmd" -print0 | xargs -0 grep -l "$grepname" | filter_parent_obj "$pdir" "${file}" | head -n1)" ++ num="$(find $USERMODBUILDDIR -name ".*.cmd" -print0 | xargs -0 grep -l "$grepname" | filter_parent_obj "$pdir" "${file}" | wc -l)" + [[ "$num" -eq 1 ]] && last_deep_find="$(dirname "$parent")" + fi + else +-- +2.31.0.windows.1 + diff --git a/0032-create-diff-object-fix-segment-fault-when-using-KPAT.patch b/0032-create-diff-object-fix-segment-fault-when-using-KPAT.patch new file mode 100644 index 0000000..4803e65 --- /dev/null +++ b/0032-create-diff-object-fix-segment-fault-when-using-KPAT.patch @@ -0,0 +1,34 @@ +From b1be16f5c42bae41f15a2579cb6ce39406648e19 Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:28:00 +0800 +Subject: [PATCH 4/9] create-diff-object: fix segment fault when using + KPATCH_FORCE_UNSAFE + +Signed-off-by: hubin +--- + kpatch-build/create-diff-object.c | 7 ++++++- + 1 file changed, 6 insertions(+), 1 deletion(-) + +diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c +index bbb40ed..2cca6f3 100644 +--- a/kpatch-build/create-diff-object.c ++++ b/kpatch-build/create-diff-object.c +@@ -1969,9 +1969,14 @@ static void kpatch_include_force_elements(struct kpatch_elf *kelf) + continue; + } + /* .rela.kpatch.force */ +- list_for_each_entry(rela, &sec->relas, list) ++ list_for_each_entry(rela, &sec->relas, list) { ++ if (rela->sym->status != CHANGED) { ++ ERROR("function '%s' marked with KPATCH_FORCE_UNSAFE, but not changed! check your patch.\n", ++ rela->sym->name); ++ } + log_normal("function '%s' marked with KPATCH_FORCE_UNSAFE!\n", + rela->sym->name); ++ } + } + } + +-- +2.31.0.windows.1 + diff --git a/0033-kpatch-macros-replace-__section-with-__kpatch_sectio.patch b/0033-kpatch-macros-replace-__section-with-__kpatch_sectio.patch new file mode 100644 index 0000000..6d8b006 --- /dev/null +++ b/0033-kpatch-macros-replace-__section-with-__kpatch_sectio.patch @@ -0,0 +1,27 @@ +From 83b98afd69c8be39587bfcd2260f03b2b3082a94 Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:28:17 +0800 +Subject: [PATCH 5/9] kpatch-macros: replace __section with __kpatch_section in + kpatch macro definition + +Signed-off-by: hubin +--- + kmod/patch/kpatch-macros.h | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/kmod/patch/kpatch-macros.h b/kmod/patch/kpatch-macros.h +index ee455d2..c20b061 100644 +--- a/kmod/patch/kpatch-macros.h ++++ b/kmod/patch/kpatch-macros.h +@@ -13,7 +13,7 @@ + #endif + + #define KPATCH_IGNORE_STATIC(_static) \ +- char *__UNIQUE_ID(kpatch_ignore_static_) __section(.kpatch.ignore.statics) = _static; ++ char *__UNIQUE_ID(kpatch_ignore_static_) __kpatch_section(.kpatch.ignore.statics) = _static; + + + /* +-- +2.31.0.windows.1 + diff --git a/0034-create-diff-object-ignore-changed-of-section-__patch.patch b/0034-create-diff-object-ignore-changed-of-section-__patch.patch new file mode 100644 index 0000000..5eb6dcf --- /dev/null +++ b/0034-create-diff-object-ignore-changed-of-section-__patch.patch @@ -0,0 +1,35 @@ +From 41334ab046ad2c89324b61f76b262228a6bc36ad Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:28:26 +0800 +Subject: [PATCH 6/9] create-diff-object: ignore changed of section + __patchable_function_entries + +Linux 5.10 aarch64 uses -fpatchable-function-entry=N to generate N NOPs at the +beginning of each function, Section __patchable_function_entries is generated +to record the starting address of these NOPs. + +Section __patchable_function_entries will change if the number of functions +changed during making hotpatch, here we just ignore the change of this section. + +Signed-off-by: hubin +--- + kpatch-build/create-diff-object.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c +index 2cca6f3..8d3f377 100644 +--- a/kpatch-build/create-diff-object.c ++++ b/kpatch-build/create-diff-object.c +@@ -3062,7 +3062,8 @@ static void kpatch_process_special_sections(struct kpatch_elf *kelf, + list_for_each_entry(sec, &kelf->sections, list) { + if (strcmp(sec->name, "__tracepoints") && + strcmp(sec->name, "__tracepoints_ptrs") && +- strcmp(sec->name, "__tracepoints_strings")) ++ strcmp(sec->name, "__tracepoints_strings") && ++ strcmp(sec->name, "__patchable_function_entries")) + continue; + + sec->status = SAME; +-- +2.31.0.windows.1 + diff --git a/0035-create-diff-object-fix-null-pointer-dereference-in-k.patch b/0035-create-diff-object-fix-null-pointer-dereference-in-k.patch new file mode 100644 index 0000000..958c4b7 --- /dev/null +++ b/0035-create-diff-object-fix-null-pointer-dereference-in-k.patch @@ -0,0 +1,27 @@ +From e1b12b68efc7de9e3e3db52ceda2c1b713dc7cb7 Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:28:34 +0800 +Subject: [PATCH 7/9] create-diff-object: fix null pointer dereference in + kpatch_ignore_debug_section + +Signed-off-by: hubin +--- + kpatch-build/create-diff-object.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c +index 8d3f377..03c877e 100644 +--- a/kpatch-build/create-diff-object.c ++++ b/kpatch-build/create-diff-object.c +@@ -2796,7 +2796,7 @@ static void kpatch_ignore_debug_sections(struct kpatch_elf *kelf) + if (is_debug_section(sec)) { + sec->include = 0; + sec->status = SAME; +- if (!is_rela_section(sec)) { ++ if (!is_rela_section(sec) && sec->secsym) { + sec->secsym->include = 0; + sec->secsym->status = SAME; + } +-- +2.31.0.windows.1 + diff --git a/0036-create-diff-object-ignore-.note.gnu.property-section.patch b/0036-create-diff-object-ignore-.note.gnu.property-section.patch new file mode 100644 index 0000000..f9e9a29 --- /dev/null +++ b/0036-create-diff-object-ignore-.note.gnu.property-section.patch @@ -0,0 +1,36 @@ +From de34b86536bde114f481a73fdbf2f58b643c3e48 Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:28:46 +0800 +Subject: [PATCH 8/9] create-diff-object: ignore .note.gnu.property section + +Linux GABI introduced new .note.gnu.property section which contains a program +property note which describes special handling requirements for linker and run-time loader. + +Linux GABI specifies 2 types of entries: +GNU_PROPERTY_STACK_SIZE and GNU_PROPERTY_NO_COPY_ON_PROTECTED + +These entries should not take effect when using live-patching, thus, ignore the change of +this section + +Signed-off-by: hubin +--- + kpatch-build/create-diff-object.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c +index 03c877e..c2ecefa 100644 +--- a/kpatch-build/create-diff-object.c ++++ b/kpatch-build/create-diff-object.c +@@ -3063,7 +3063,8 @@ static void kpatch_process_special_sections(struct kpatch_elf *kelf, + if (strcmp(sec->name, "__tracepoints") && + strcmp(sec->name, "__tracepoints_ptrs") && + strcmp(sec->name, "__tracepoints_strings") && +- strcmp(sec->name, "__patchable_function_entries")) ++ strcmp(sec->name, "__patchable_function_entries") && ++ strcmp(sec->name, ".note.gnu.property")) + continue; + + sec->status = SAME; +-- +2.31.0.windows.1 + diff --git a/0037-create-diff-object-skip-creating-.kpatch.arch-sectio.patch b/0037-create-diff-object-skip-creating-.kpatch.arch-sectio.patch new file mode 100644 index 0000000..02b1332 --- /dev/null +++ b/0037-create-diff-object-skip-creating-.kpatch.arch-sectio.patch @@ -0,0 +1,27 @@ +From 19b6926284ad2811f6de0ec29e0b45f3ae8b09dd Mon Sep 17 00:00:00 2001 +From: hubin +Date: Tue, 10 May 2022 15:28:55 +0800 +Subject: [PATCH 9/9] create-diff-object: skip creating .kpatch.arch section + for sections with empty secsym + +Signed-off-by: hubin +--- + kpatch-build/create-diff-object.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c +index c2ecefa..6b46e1f 100644 +--- a/kpatch-build/create-diff-object.c ++++ b/kpatch-build/create-diff-object.c +@@ -2952,7 +2952,7 @@ static void kpatch_create_kpatch_arch_section(struct kpatch_elf *kelf, char *obj + continue; + + sec = find_section_by_name(&kelf->sections, special->name); +- if (!sec) ++ if (!sec || !sec->secsym) + continue; + + /* entries[index].sec */ +-- +2.31.0.windows.1 + diff --git a/kpatch.spec b/kpatch.spec index 0aa289c..1735f86 100644 --- a/kpatch.spec +++ b/kpatch.spec @@ -1,7 +1,7 @@ Name: kpatch Epoch: 1 Version: 0.9.5 -Release: 5 +Release: 6 Summary: A Linux dynamic kernel patching infrastructure License: GPLv2 @@ -41,6 +41,15 @@ Patch0025:0025-create-diff-object-update-for-__already_done.patch Patch0026:0026-kpatch-build-Add-missing-allocation-failure-checks.patch Patch0027:0027-create-diff-object-add-support-for-.retpoline_sites-.patch Patch0028:0028-Fix-sym-type-condition-in-locals_match.patch +Patch0029:0029-livepatch-patch-hook-disable-HAVE_SIMPLE_ENABLE-to-k.patch +Patch0030:0030-kpatch-build-add-KBUILD_MODPOST_WARN-1-to-avoid-modp.patch +Patch0031:0031-kpatch-build-update-find_parent_obj-to-avoid-error-t.patch +Patch0032:0032-create-diff-object-fix-segment-fault-when-using-KPAT.patch +Patch0033:0033-kpatch-macros-replace-__section-with-__kpatch_sectio.patch +Patch0034:0034-create-diff-object-ignore-changed-of-section-__patch.patch +Patch0035:0035-create-diff-object-fix-null-pointer-dereference-in-k.patch +Patch0036:0036-create-diff-object-ignore-.note.gnu.property-section.patch +Patch0037:0037-create-diff-object-skip-creating-.kpatch.arch-sectio.patch BuildRequires: gcc elfutils-libelf-devel kernel-devel git Requires: bc make gcc patch bison flex openssl-devel @@ -101,6 +110,12 @@ popd %{_mandir}/man1/*.1.gz %changelog +* Tue May 10 2022 Bin Hu -1:0.9.5-6 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:fix segment fault and hotpatch compilation problem when using openEuler 22.03 LTS kernel + * Wed Mar 30 2022 Zhipeng Xie -1:0.9.5-5 - Type:clean code - ID:NA -- Gitee