Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1722712 > unrolled thread
| Started by | Joao Moreira <jmoreira@suse.de> |
|---|---|
| First post | 2017-08-29 21:10 +0200 |
| Last post | 2017-08-30 20:10 +0200 |
| Articles | 7 — 3 participants |
Back to article view | Back to linux.kernel
[PATCH 0/8] livepatch: klp-convert tool Joao Moreira <jmoreira@suse.de> - 2017-08-29 21:10 +0200
[PATCH 2/8] kbuild: Support for Symbols.list creation Joao Moreira <jmoreira@suse.de> - 2017-08-29 21:10 +0200
Re: [PATCH 2/8] kbuild: Support for Symbols.list creation Joe Lawrence <joe.lawrence@redhat.com> - 2017-08-31 17:30 +0200
Re: [PATCH 2/8] kbuild: Support for Symbols.list creation Josh Poimboeuf <jpoimboe@redhat.com> - 2017-08-31 19:40 +0200
Re: [PATCH 2/8] kbuild: Support for Symbols.list creation Joao Moreira <jmoreira@suse.de> - 2017-09-04 09:30 +0200
[PATCH 7/8] livepatch: Add sample livepatch module Joao Moreira <jmoreira@suse.de> - 2017-08-29 21:10 +0200
Re: [PATCH 0/8] livepatch: klp-convert tool Josh Poimboeuf <jpoimboe@redhat.com> - 2017-08-30 20:10 +0200
| From | Joao Moreira <jmoreira@suse.de> |
|---|---|
| Date | 2017-08-29 21:10 +0200 |
| Subject | [PATCH 0/8] livepatch: klp-convert tool |
| Message-ID | <ujU3D-6AB-5@gated-at.bofh.it> |
Livepatches may use symbols which are not contained in its own scope, and, because of that, may end up compiled with relocations that will only be resolved during module load. Yet, when the referenced symbols are not exported, solving this relocation requires information on the object that holds the symbol (either vmlinux or modules) and its position inside the object, as an object may contain multiple symbols with the same name. Providing such information must be done accordingly to what is specified in Documentation/livepatch/module-elf-format.txt. Currently, there is no trivial way to embed the required information as requested in the final livepatch elf object. klp-convert solves this problem in two different forms: (i) by relying on a symbol map, which is built during kernel compilation, to automatically infers the relocation targeted symbol, and, when such inference is not possible (ii) by using annotations in the elf object to convert the relocation accordingly to the specification, enabling it to be handled by the livepatch loader. Given the above, add support for symbol mapping in the form of Symbols.list file; add klp-convert tool; integrate klp-convert tool into kbuild; make livepatch modules discernible during kernel compilation pipeline; add data-structure and macros to enable users to annotate livepatch source code; make modpost stage compatible with livepatches; update livepatch-sample and update documentation. The patch was tested under three use-cases: use-case 1: There is a relocation in the lp that can be automatically resolved by klp-convert (tested by removing the annotations from samples/livepatch/livepatch-annotated-sample.c) use-case 2: There is a relocation in the lp that cannot be automatically resolved, as the name of the respective symbol appears in multiple objects. The livepatch contains an annotation to enable a correct relocation - reproducible with this livepatch sample: www.livewire.com.br/suse/klp/livepatch-sample.1.c use-case 3: There is a relocation in the lp that cannot be automatically resolved similarly as 2, but no annotation was provided in the livepatch, triggering an error during compilation - reproducible with this livepatch sample: www.livewire.com.br/suse/klp/livepatch-sample.2.c Joao Moreira (2): kbuild: Support for Symbols.list creation documentation: Update on livepatch elf format Josh Poimboeuf (5): livepatch: Create and include UAPI headers livepatch: Add klp-convert tool livepatch: Add klp-convert annotation helpers modpost: Integrate klp-convert livepatch: Add sample livepatch module Miroslav Benes (1): modpost: Add modinfo flag to livepatch modules .gitignore | 1 + Documentation/livepatch/module-elf-format.txt | 47 +- MAINTAINERS | 2 + Makefile | 29 +- include/linux/livepatch.h | 12 + include/uapi/linux/livepatch.h | 33 ++ kernel/livepatch/core.c | 4 +- samples/livepatch/Makefile | 5 +- samples/livepatch/livepatch-annotated-sample.c | 128 +++++ samples/livepatch/livepatch-sample.c | 1 - scripts/Kbuild.include | 4 +- scripts/Makefile | 1 + scripts/Makefile.build | 6 + scripts/Makefile.modpost | 24 +- scripts/livepatch/.gitignore | 1 + scripts/livepatch/Makefile | 7 + scripts/livepatch/elf.c | 696 +++++++++++++++++++++++++ scripts/livepatch/elf.h | 84 +++ scripts/livepatch/klp-convert.c | 567 ++++++++++++++++++++ scripts/livepatch/list.h | 389 ++++++++++++++ scripts/mod/modpost.c | 80 ++- scripts/mod/modpost.h | 1 + 22 files changed, 2104 insertions(+), 18 deletions(-) create mode 100644 include/uapi/linux/livepatch.h create mode 100644 samples/livepatch/livepatch-annotated-sample.c create mode 100644 scripts/livepatch/.gitignore create mode 100644 scripts/livepatch/Makefile create mode 100644 scripts/livepatch/elf.c create mode 100644 scripts/livepatch/elf.h create mode 100644 scripts/livepatch/klp-convert.c create mode 100644 scripts/livepatch/list.h -- 2.12.0
[toc] | [next] | [standalone]
| From | Joao Moreira <jmoreira@suse.de> |
|---|---|
| Date | 2017-08-29 21:10 +0200 |
| Subject | [PATCH 2/8] kbuild: Support for Symbols.list creation |
| Message-ID | <ujU3E-6AB-23@gated-at.bofh.it> |
| In reply to | #1722712 |
For automatic resolution of livepatch relocations, a file called
Symbols.list is used. This file maps symbols within every compiled
kernel object allowing the identification of symbols whose name is
unique, thus relocation can be automatically inferred, or providing
information that helps developers when code annotation is required for
solving the matter.
Add support for creating Symbols.list in the main Makefile. First,
ensure that built-in is compiled when CONFIG_LIVEPATCH is enabled (as
required to achieve a complete Symbols.list file). Define the command to
build Symbols.list (cmd_klp_map) and hook it in the modules rule.
As it is undesirable to have symbols from livepatch objects inside
Symbols.list, make livepatches discernible by modifying
scripts/Makefile.build to create a .livepatch file for each livepatch
in $(MODVERDIR). This file is then used by cmd_klp_map to identify and
bypass livepatches.
For identifying livepatches during the build process, a flag variable
LIVEPATCH_$(basetarget).o is considered in scripts/Makefile.build. This
way, set this flag for the livepatch sample Makefile in
samples/livepatch/Makefile.
Finally, Add a clean rule to ensure that Symbols.list is removed during
clean.
Notes:
To achieve a correct Symbols.list file, all kernel objects must be
considered, thus, its construction require these objects to be priorly
built. On the other hand, invoking scripts/Makefile.modpost without
having a complete Symbols.list in place would occasionally lead to
in-tree livepatches being post-processed incorrectly. To prevent this
from becoming a circular dependency, the construction of Symbols.list
uses non-post-processed kernel objects and such does not cause harm as
the symbols normally referenced from within livepatches are visible at
this stage. Also due to these requirements, the spot in-between modules
compilation and the invocation of scripts/Makefile.modpost was picked
for hooking cmd_klp_map.
The approach based on .livepatch files was proposed as an alternative
to using MODULE_INFO statementes. This approach was originally
proposed by Miroslav Benes as a workaround for identifying livepathces
without depending on modinfo during the modpost stage. It was moved to
this patch as the approach also shown to be useful while building
Symbols.list.
Signed-off-by: Joao Moreira <jmoreira@suse.de>
---
.gitignore | 1 +
Makefile | 27 ++++++++++++++++++++++++---
samples/livepatch/Makefile | 1 +
scripts/Makefile.build | 6 ++++++
4 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/.gitignore b/.gitignore
index 0c39aa20b6ba..e6a67517a3bc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -39,6 +39,7 @@ Module.symvers
*.dwo
*.su
*.c.[012]*.*
+Symbols.list
#
# Top-level generic files
diff --git a/Makefile b/Makefile
index e40c471abe29..e1d315e585d8 100644
--- a/Makefile
+++ b/Makefile
@@ -320,10 +320,13 @@ KBUILD_BUILTIN := 1
# If we have only "make modules", don't compile built-in objects.
# When we're building modules with modversions, we need to consider
# the built-in objects during the descend as well, in order to
-# make sure the checksums are up to date before we record them.
+# make sure the checksums are up to date before we record them. The
+# same applies for building livepatches, as built-in objects may hold
+# symbols which are referenced from livepatches and are required by
+# klp-convert post-processing tool for resolving these cases.
ifeq ($(MAKECMDGOALS),modules)
- KBUILD_BUILTIN := $(if $(CONFIG_MODVERSIONS),1)
+ KBUILD_BUILTIN := $(if $(or $(CONFIG_MODVERSIONS), $(CONFIG_LIVEPATCH)),1)
endif
# If we have "make <whatever> modules", compile modules
@@ -1207,9 +1210,24 @@ all: modules
# duplicate lines in modules.order files. Those are removed
# using awk while concatenating to the final file.
+quiet_cmd_klp_map = LIVEPATCH Symbols.list
+SLIST = $(objtree)/Symbols.list
+
+define cmd_klp_map
+ $(shell echo "*vmlinux" > $(SLIST)) \
+ $(shell nm -f posix $(objtree)/vmlinux | cut -d\ -f1 >> $(SLIST)) \
+ $(foreach m, $(wildcard $(MODVERDIR)/*.mod), \
+ $(eval mod = $(patsubst %.ko,%.o,$(shell head -n1 $(m)))) \
+ $(if $(wildcard $(MODVERDIR)/$(shell basename -s .o $(mod)).livepatch),,\
+ $(eval fmod = $(subst $(quote),_,$(subst -,_,$(mod)))) \
+ $(shell echo "*$(shell basename -s .o $(fmod))" >> $(SLIST)) \
+ $(shell nm -f posix $(mod) | cut -d\ -f1 >> $(SLIST))))
+endef
+
PHONY += modules
modules: $(vmlinux-dirs) $(if $(KBUILD_BUILTIN),vmlinux) modules.builtin
$(Q)$(AWK) '!x[$$0]++' $(vmlinux-dirs:%=$(objtree)/%/modules.order) > $(objtree)/modules.order
+ $(if $(CONFIG_LIVEPATCH), $(call cmd,klp_map))
@$(kecho) ' Building modules, stage 2.';
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.fwinst obj=firmware __fw_modbuild
@@ -1306,7 +1324,10 @@ vmlinuxclean:
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/link-vmlinux.sh clean
$(Q)$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) clean)
-clean: archclean vmlinuxclean
+klpclean:
+ $(Q) rm -f $(objtree)/Symbols.list
+
+clean: archclean vmlinuxclean klpclean
# mrproper - Delete all generated files, including .config
#
diff --git a/samples/livepatch/Makefile b/samples/livepatch/Makefile
index 10319d7ea0b1..955e7ac12d2b 100644
--- a/samples/livepatch/Makefile
+++ b/samples/livepatch/Makefile
@@ -1 +1,2 @@
+LIVEPATCH_livepatch-sample.o := y
obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 733e044fff8b..d0c32a50cce7 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -276,6 +276,11 @@ objtool_obj = $(if $(patsubst y%,, \
endif # SKIP_STACK_VALIDATION
endif # CONFIG_STACK_VALIDATION
+ifdef CONFIG_LIVEPATCH
+cmd_livepatch = $(if $(LIVEPATCH_$(basetarget).o), \
+ $(shell touch $(MODVERDIR)/$(basetarget).livepatch))
+endif
+
define rule_cc_o_c
$(call echo-cmd,checksrc) $(cmd_checksrc) \
$(call cmd_and_fixdep,cc_o_c) \
@@ -309,6 +314,7 @@ $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_obj) F
$(call if_changed_rule,cc_o_c)
@{ echo $(@:.o=.ko); echo $@; \
$(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
+ $(call cmd_livepatch)
quiet_cmd_cc_lst_c = MKLST $@
cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
--
2.12.0
[toc] | [prev] | [next] | [standalone]
| From | Joe Lawrence <joe.lawrence@redhat.com> |
|---|---|
| Date | 2017-08-31 17:30 +0200 |
| Subject | Re: [PATCH 2/8] kbuild: Support for Symbols.list creation |
| Message-ID | <ukzzP-7k7-9@gated-at.bofh.it> |
| In reply to | #1722713 |
Hi Joao,
A few nitpicks in-line below...
On Tue, Aug 29, 2017 at 04:01:34PM -0300, Joao Moreira wrote:
> For automatic resolution of livepatch relocations, a file called
> Symbols.list is used. This file maps symbols within every compiled
> kernel object allowing the identification of symbols whose name is
> unique, thus relocation can be automatically inferred, or providing
> information that helps developers when code annotation is required for
> solving the matter.
>
> Add support for creating Symbols.list in the main Makefile. First,
> ensure that built-in is compiled when CONFIG_LIVEPATCH is enabled (as
> required to achieve a complete Symbols.list file). Define the command to
> build Symbols.list (cmd_klp_map) and hook it in the modules rule.
>
> As it is undesirable to have symbols from livepatch objects inside
> Symbols.list, make livepatches discernible by modifying
> scripts/Makefile.build to create a .livepatch file for each livepatch
> in $(MODVERDIR). This file is then used by cmd_klp_map to identify and
> bypass livepatches.
>
> For identifying livepatches during the build process, a flag variable
> LIVEPATCH_$(basetarget).o is considered in scripts/Makefile.build. This
> way, set this flag for the livepatch sample Makefile in
> samples/livepatch/Makefile.
I've never tried building a livepatch out of tree (is that even
possible?) but does this make it any more/less harder?
> Finally, Add a clean rule to ensure that Symbols.list is removed during
> clean.
>
> Notes:
>
> To achieve a correct Symbols.list file, all kernel objects must be
> considered, thus, its construction require these objects to be priorly
> built. On the other hand, invoking scripts/Makefile.modpost without
> having a complete Symbols.list in place would occasionally lead to
> in-tree livepatches being post-processed incorrectly. To prevent this
> from becoming a circular dependency, the construction of Symbols.list
> uses non-post-processed kernel objects and such does not cause harm as
> the symbols normally referenced from within livepatches are visible at
> this stage. Also due to these requirements, the spot in-between modules
> compilation and the invocation of scripts/Makefile.modpost was picked
> for hooking cmd_klp_map.
>
> The approach based on .livepatch files was proposed as an alternative
> to using MODULE_INFO statementes. This approach was originally
^^^^^^^^^^^
nit: s/statementes/statements
> proposed by Miroslav Benes as a workaround for identifying livepathces
^^^^^^^^^^^
nit: s/livepathces/livepatches
> without depending on modinfo during the modpost stage. It was moved to
> this patch as the approach also shown to be useful while building
> Symbols.list.
>
> Signed-off-by: Joao Moreira <jmoreira@suse.de>
> ---
> .gitignore | 1 +
> Makefile | 27 ++++++++++++++++++++++++---
> samples/livepatch/Makefile | 1 +
> scripts/Makefile.build | 6 ++++++
> 4 files changed, 32 insertions(+), 3 deletions(-)
>
> diff --git a/.gitignore b/.gitignore
> index 0c39aa20b6ba..e6a67517a3bc 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -39,6 +39,7 @@ Module.symvers
> *.dwo
> *.su
> *.c.[012]*.*
> +Symbols.list
>
> #
> # Top-level generic files
> diff --git a/Makefile b/Makefile
> index e40c471abe29..e1d315e585d8 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -320,10 +320,13 @@ KBUILD_BUILTIN := 1
> # If we have only "make modules", don't compile built-in objects.
> # When we're building modules with modversions, we need to consider
> # the built-in objects during the descend as well, in order to
> -# make sure the checksums are up to date before we record them.
> +# make sure the checksums are up to date before we record them. The
> +# same applies for building livepatches, as built-in objects may hold
> +# symbols which are referenced from livepatches and are required by
> +# klp-convert post-processing tool for resolving these cases.
>
> ifeq ($(MAKECMDGOALS),modules)
> - KBUILD_BUILTIN := $(if $(CONFIG_MODVERSIONS),1)
> + KBUILD_BUILTIN := $(if $(or $(CONFIG_MODVERSIONS), $(CONFIG_LIVEPATCH)),1)
> endif
>
> # If we have "make <whatever> modules", compile modules
> @@ -1207,9 +1210,24 @@ all: modules
> # duplicate lines in modules.order files. Those are removed
> # using awk while concatenating to the final file.
>
> +quiet_cmd_klp_map = LIVEPATCH Symbols.list
nit: I don't think any other quiet_cmd invocations put a tab between the
label and file list. That said, "LIVEPATCH" is > 8 chars, so it's not
going to line up anyway.
> +SLIST = $(objtree)/Symbols.list
> +
> +define cmd_klp_map
> + $(shell echo "*vmlinux" > $(SLIST)) \
> + $(shell nm -f posix $(objtree)/vmlinux | cut -d\ -f1 >> $(SLIST)) \
> + $(foreach m, $(wildcard $(MODVERDIR)/*.mod), \
> + $(eval mod = $(patsubst %.ko,%.o,$(shell head -n1 $(m)))) \
> + $(if $(wildcard $(MODVERDIR)/$(shell basename -s .o $(mod)).livepatch),,\
> + $(eval fmod = $(subst $(quote),_,$(subst -,_,$(mod)))) \
> + $(shell echo "*$(shell basename -s .o $(fmod))" >> $(SLIST)) \
> + $(shell nm -f posix $(mod) | cut -d\ -f1 >> $(SLIST))))
> +endef
> +
> PHONY += modules
> modules: $(vmlinux-dirs) $(if $(KBUILD_BUILTIN),vmlinux) modules.builtin
> $(Q)$(AWK) '!x[$$0]++' $(vmlinux-dirs:%=$(objtree)/%/modules.order) > $(objtree)/modules.order
> + $(if $(CONFIG_LIVEPATCH), $(call cmd,klp_map))
> @$(kecho) ' Building modules, stage 2.';
> $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
> $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.fwinst obj=firmware __fw_modbuild
> @@ -1306,7 +1324,10 @@ vmlinuxclean:
> $(Q)$(CONFIG_SHELL) $(srctree)/scripts/link-vmlinux.sh clean
> $(Q)$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) clean)
>
> -clean: archclean vmlinuxclean
> +klpclean:
> + $(Q) rm -f $(objtree)/Symbols.list
> +
> +clean: archclean vmlinuxclean klpclean
Does the klpclean target need to be added to the PHONY variable?
% touch klpclean
% make klpclean
make: 'klpclean' is up to date.
% ls -l Symbols.list
-rw-r--r--. 1 jolawren jolawren 2323179 Aug 29 16:55 Symbols.list
>
> # mrproper - Delete all generated files, including .config
> #
> diff --git a/samples/livepatch/Makefile b/samples/livepatch/Makefile
> index 10319d7ea0b1..955e7ac12d2b 100644
> --- a/samples/livepatch/Makefile
> +++ b/samples/livepatch/Makefile
> @@ -1 +1,2 @@
> +LIVEPATCH_livepatch-sample.o := y
> obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 733e044fff8b..d0c32a50cce7 100644<F2>
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -276,6 +276,11 @@ objtool_obj = $(if $(patsubst y%,, \
> endif # SKIP_STACK_VALIDATION
> endif # CONFIG_STACK_VALIDATION
>
> +ifdef CONFIG_LIVEPATCH
> +cmd_livepatch = $(if $(LIVEPATCH_$(basetarget).o), \
> + $(shell touch $(MODVERDIR)/$(basetarget).livepatch))
> +endif
> +
> define rule_cc_o_c
> $(call echo-cmd,checksrc) $(cmd_checksrc) \
> $(call cmd_and_fixdep,cc_o_c) \
> @@ -309,6 +314,7 @@ $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_obj) F
> $(call if_changed_rule,cc_o_c)
> @{ echo $(@:.o=.ko); echo $@; \
> $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
> + $(call cmd_livepatch)
>
> quiet_cmd_cc_lst_c = MKLST $@
> cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
> --
> 2.12.0
>
I'll try to dig in deeper and tinker with the patchset next week.
Regards,
-- Joe
[toc] | [prev] | [next] | [standalone]
| From | Josh Poimboeuf <jpoimboe@redhat.com> |
|---|---|
| Date | 2017-08-31 19:40 +0200 |
| Subject | Re: [PATCH 2/8] kbuild: Support for Symbols.list creation |
| Message-ID | <ukBBF-ap-39@gated-at.bofh.it> |
| In reply to | #1724318 |
On Thu, Aug 31, 2017 at 11:24:39AM -0400, Joe Lawrence wrote: > > +quiet_cmd_klp_map = LIVEPATCH Symbols.list > > nit: I don't think any other quiet_cmd invocations put a tab between the > label and file list. That said, "LIVEPATCH" is > 8 chars, so it's not > going to line up anyway. Maybe "SYMBOLS" would be more appropriate (and would fit). -- Josh
[toc] | [prev] | [next] | [standalone]
| From | Joao Moreira <jmoreira@suse.de> |
|---|---|
| Date | 2017-09-04 09:30 +0200 |
| Subject | Re: [PATCH 2/8] kbuild: Support for Symbols.list creation |
| Message-ID | <ulTZw-2yO-13@gated-at.bofh.it> |
| In reply to | #1724318 |
On 08/31/2017 12:24 PM, Joe Lawrence wrote: > Hi Joao, > Hi Joe and Josh, Thanks for the quick feedback, I'll be looking forward for your comments once you have the change to dig deeper :). I'll apply all typo-fixes, add klpclean to the PHONY targets and change the quiet_cmd invocation as suggested in v2. >> For identifying livepatches during the build process, a flag variable >> LIVEPATCH_$(basetarget).o is considered in scripts/Makefile.build. This >> way, set this flag for the livepatch sample Makefile in >> samples/livepatch/Makefile. > > I've never tried building a livepatch out of tree (is that even > possible?) but does this make it any more/less harder? Tbh, I never had tried it before, so I just emulated the process and it was simple and successful. All it required in addition to what is regular, was the introduction of the "LIVEPATCH_($bastarget).o := y" into the Makefile. All else was implicitly handled by Kbuild. Best, Joao
[toc] | [prev] | [next] | [standalone]
| From | Joao Moreira <jmoreira@suse.de> |
|---|---|
| Date | 2017-08-29 21:10 +0200 |
| Subject | [PATCH 7/8] livepatch: Add sample livepatch module |
| Message-ID | <ujU3E-6AB-35@gated-at.bofh.it> |
| In reply to | #1722712 |
From: Josh Poimboeuf <jpoimboe@redhat.com>
Add a new livepatch sample in samples/livepatch/ to make use of
symbols that must be post-processed to enable load-time relocation
resolution. As the new sample is to be used as an example, it is
annotated with KLP_MODULE_REOC and with KLP_SYMPOS macros.
The livepatch sample updates the function cmdline_proc_show to
print the string referenced by the symbol saved_command_line
appended by the string "livepatch=1".
[jmoreira:
* Switched from "updating existing" module to "adding a new" module
* Update module to use KLP_SYMPOS
* Comments on symbol resolution scheme
* Update Makefile
* Changelog
]
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Joao Moreira <jmoreira@suse.de>
---
samples/livepatch/Makefile | 4 +-
samples/livepatch/livepatch-annotated-sample.c | 128 +++++++++++++++++++++++++
2 files changed, 131 insertions(+), 1 deletion(-)
create mode 100644 samples/livepatch/livepatch-annotated-sample.c
diff --git a/samples/livepatch/Makefile b/samples/livepatch/Makefile
index 955e7ac12d2b..2690ef439bec 100644
--- a/samples/livepatch/Makefile
+++ b/samples/livepatch/Makefile
@@ -1,2 +1,4 @@
LIVEPATCH_livepatch-sample.o := y
-obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o
+LIVEPATCH_livepatch-annotated-sample.o := y
+obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o \
+ livepatch-annotated-sample.o
diff --git a/samples/livepatch/livepatch-annotated-sample.c b/samples/livepatch/livepatch-annotated-sample.c
new file mode 100644
index 000000000000..93da32610076
--- /dev/null
+++ b/samples/livepatch/livepatch-annotated-sample.c
@@ -0,0 +1,128 @@
+/*
+ * livepatch-annotated-sample.c - Kernel Live Patching Sample Module
+ *
+ * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/livepatch.h>
+#include <linux/seq_file.h>
+
+/*
+ * This (dumb) live patch overrides the function that prints the
+ * kernel boot cmdline when /proc/cmdline is read.
+ *
+ * This livepatch uses the symbol saved_command_line whose relocation
+ * must be resolved during load time. To enable that, this module
+ * must be post-processed by a tool called klp-convert, which embeds
+ * information to be used by the loader to solve the relocation.
+ *
+ * The module is annotated with KLP_MODULE_RELOC/KLP_SYMPOS macros.
+ * These annotations are used by klp-convert to infer that the symbol
+ * saved_command_line is in the object vmlinux.
+ *
+ * As saved_command_line has no other homonimous symbol across
+ * kernel objects, this annotation is not a requirement, and can be
+ * suppressed with no harm to klp-convert. Yet, it is kept here as an
+ * example on how to annotate livepatch modules that contain symbols
+ * whose names are used in more than one kernel object.
+ *
+ * Example:
+ *
+ * $ cat /proc/cmdline
+ * <your cmdline>
+ *
+ * $ insmod livepatch-sample.ko
+ * $ cat /proc/cmdline
+ * <your cmdline> livepatch=1
+ *
+ * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
+ * $ cat /proc/cmdline
+ * <your cmdline>
+ */
+
+extern char *saved_command_line;
+
+static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
+{
+ seq_printf(m, "%s livepatch=1\n", saved_command_line);
+ return 0;
+}
+
+KLP_MODULE_RELOC(vmlinux) vmlinux_relocs[] = {
+ KLP_SYMPOS(saved_command_line, 0)
+};
+
+static struct klp_func vmlinux_funcs[] = {
+ {
+ .old_name = "cmdline_proc_show",
+ .new_func = livepatch_cmdline_proc_show,
+ }, { }
+};
+
+static struct klp_object objs[] = {
+ {
+ /* name being NULL means vmlinux */
+ .funcs = vmlinux_funcs,
+ }, { }
+};
+
+static struct klp_patch patch = {
+ .mod = THIS_MODULE,
+ .objs = objs,
+};
+
+static int livepatch_init(void)
+{
+ int ret;
+
+ if (!klp_have_reliable_stack() && !patch.immediate) {
+ /*
+ * WARNING: Be very careful when using 'patch.immediate' in
+ * your patches. It's ok to use it for simple patches like
+ * this, but for more complex patches which change function
+ * semantics, locking semantics, or data structures, it may not
+ * be safe. Use of this option will also prevent removal of
+ * the patch.
+ *
+ * See Documentation/livepatch/livepatch.txt for more details.
+ */
+ patch.immediate = true;
+ pr_notice("The consistency model isn't supported for your architecture. Bypassing safety mechanisms and applying the patch immediately.\n");
+ }
+
+ ret = klp_register_patch(&patch);
+ if (ret)
+ return ret;
+ ret = klp_enable_patch(&patch);
+ if (ret) {
+ WARN_ON(klp_unregister_patch(&patch));
+ return ret;
+ }
+ return 0;
+}
+
+static void livepatch_exit(void)
+{
+ WARN_ON(klp_unregister_patch(&patch));
+}
+
+module_init(livepatch_init);
+module_exit(livepatch_exit);
+MODULE_LICENSE("GPL");
--
2.12.0
[toc] | [prev] | [next] | [standalone]
| From | Josh Poimboeuf <jpoimboe@redhat.com> |
|---|---|
| Date | 2017-08-30 20:10 +0200 |
| Message-ID | <ukfB9-3fk-53@gated-at.bofh.it> |
| In reply to | #1722712 |
On Tue, Aug 29, 2017 at 04:01:32PM -0300, Joao Moreira wrote: > Livepatches may use symbols which are not contained in its own scope, > and, because of that, may end up compiled with relocations that will > only be resolved during module load. Yet, when the referenced symbols are > not exported, solving this relocation requires information on the object > that holds the symbol (either vmlinux or modules) and its position inside > the object, as an object may contain multiple symbols with the same name. > Providing such information must be done accordingly to what is specified > in Documentation/livepatch/module-elf-format.txt. > > Currently, there is no trivial way to embed the required information as > requested in the final livepatch elf object. klp-convert solves this > problem in two different forms: (i) by relying on a symbol map, which is > built during kernel compilation, to automatically infers the relocation > targeted symbol, and, when such inference is not possible (ii) by using > annotations in the elf object to convert the relocation accordingly to > the specification, enabling it to be handled by the livepatch loader. > > Given the above, add support for symbol mapping in the form of > Symbols.list file; add klp-convert tool; integrate klp-convert tool into > kbuild; make livepatch modules discernible during kernel compilation > pipeline; add data-structure and macros to enable users to annotate > livepatch source code; make modpost stage compatible with livepatches; > update livepatch-sample and update documentation. > > The patch was tested under three use-cases: > > use-case 1: There is a relocation in the lp that can be automatically > resolved by klp-convert (tested by removing the annotations from > samples/livepatch/livepatch-annotated-sample.c) > > use-case 2: There is a relocation in the lp that cannot be automatically > resolved, as the name of the respective symbol appears in multiple > objects. The livepatch contains an annotation to enable a correct > relocation - reproducible with this livepatch sample: > www.livewire.com.br/suse/klp/livepatch-sample.1.c > > use-case 3: There is a relocation in the lp that cannot be automatically > resolved similarly as 2, but no annotation was provided in the livepatch, > triggering an error during compilation - reproducible with this livepatch > sample: www.livewire.com.br/suse/klp/livepatch-sample.2.c > > Joao Moreira (2): > kbuild: Support for Symbols.list creation > documentation: Update on livepatch elf format > > Josh Poimboeuf (5): > livepatch: Create and include UAPI headers > livepatch: Add klp-convert tool > livepatch: Add klp-convert annotation helpers > modpost: Integrate klp-convert > livepatch: Add sample livepatch module > > Miroslav Benes (1): > modpost: Add modinfo flag to livepatch modules Thanks a lot for picking these patches up and improving them. I've only glanced at the code, but so far it's looking good. It may be a few weeks before a I get a chance to do a proper review. One quick question, possibly for Miroslav. Do we have a plan yet for dealing with GCC optimizations? https://lkml.kernel.org/r/20161110161053.heua3abuaekz4yy7@treble I still like the '-fpreserve-function-abi' idea, but maybe it's not realistic. -- Josh
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web