tpm: Preserve TPM measurement log across kexec
Subsystems (6)
v2 cover tpm: Preserve TPM measurement log across kexec
The of-tree driver does not currently preserve the IBM vTPM 1.2 and vTPM 2.0 measurement logs across a kexec. This series fixes this for the kexec_file_load() syscall using the flattened device tree (fdt) to carry the measurement log's buffer across kexec.
Stefan
v2:
- rearranged patches
- fixed compilation issues for x86
Stefan Berger (3):
tpm: of: Move of-tree specific code from tpm driver into of driver
of: kexec: Refactor IMA buffer related functions to make them reusable tpm/kexec: Duplicate TPM measurement log in of-tree for kexec
drivers/char/tpm/eventlog/of.c | 31 +--- drivers/of/Makefile | 2 +- drivers/of/device_node.c | 27 +++ drivers/of/kexec.c | 293 +++++++++++++++++++++++++++++---- include/linux/kexec.h | 6 + include/linux/of.h | 8 +- include/linux/of_device_node.h | 9 + kernel/kexec_file.c | 6 + 8 files changed, 318 insertions(+), 64 deletions(-) create mode 100644 drivers/of/device_node.c create mode 100644 include/linux/of_device_node.h
Signature · 1 line
2.35.1
PATCH v2 1/3 tpm: of: Move of-tree specific code from tpm driver into of driver
Simplify tpm_read_log_of() by moving Openfirmware-specific code into the Openfirmware driver to make the code reusable. Call the new of_tpm_get_sml_parameters() function from the TPM Openfirmware driver.
--- drivers/char/tpm/eventlog/of.c | 31 +++++-------------------------- drivers/of/Makefile | 2 +- drivers/of/device_node.c | 27 +++++++++++++++++++++++++++ include/linux/of_device_node.h | 9 +++++++++ 4 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 drivers/of/device_node.c create mode 100644 include/linux/of_device_node.h
drivers/char/tpm/eventlog/of.c +5 -26
@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/of.h> +#include <linux/of_device_node.h> #include <linux/tpm_eventlog.h> #include "../tpm.h"
@@ -20,11 +21,10 @@int tpm_read_log_of (struct tpm_chip *chip) {struct device_node *np; -const u32 *sizep; -const u64 *basep;struct tpm_bios_log *log;u32 size;u64 base; +int ret; log = &chip->log;if (chip->dev.parent && chip->dev.parent->of_node)
@@ -35,30 +35,9 @@ int tpm_read_log_of(struct tpm_chip *chip)if (of_property_read_bool (np,"powered-while-suspended" )) chip->flags |=TPM_CHIP_FLAG_ALWAYS_POWERED ; - sizep =of_get_property (np,"linux,sml-size" ,NULL ); - basep =of_get_property (np,"linux,sml-base" ,NULL ); -if (sizep ==NULL && basep ==NULL ) -return -ENODEV ; -if (sizep ==NULL || basep ==NULL ) -return -EIO ; - -/* -* For both vtpm/tpm, firmware has log addr and log size in big -* endian format. But in case of vtpm, there is a method called -* sml-handover which is run during kernel init even before -* device tree is setup. This sml-handover function takes care -* of endianness and writes to sml-base and sml-size in little -* endian format. For this reason, vtpm doesn't need conversion -* but physical tpm needs the conversion. -*/ -if (of_property_match_string (np,"compatible" ,"IBM,vtpm" ) <0 && -of_property_match_string (np,"compatible" ,"IBM,vtpm20" ) <0 ) { - size =be32_to_cpup ((__force __be32 *)sizep); - base =be64_to_cpup ((__force __be64 *)basep); - }else { - size = *sizep; - base = *basep; - } + ret =of_tpm_get_sml_parameters (np, &base, &size); +if (ret <0 ) +return ret;if (size ==0 ) {dev_warn (&chip->dev,"%s: Event log area empty\n" , __func__);
drivers/of/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y = base.o device.o platform.o property.o +obj-y = base.o device.o platform.o property.o device_node.o obj-$(CONFIG_OF_KOBJ) += kobj.o obj-$(CONFIG_OF_DYNAMIC) += dynamic.o obj-$(CONFIG_OF_FLATTREE) += fdt.o
drivers/of/device_node.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/export.h> +#include <linux/of_device_node.h> + +int of_tpm_get_sml_parameters (struct device_node *np,u64 *base,u32 *size) +{ +const u32 *sizep; +const u64 *basep; + + sizep =of_get_property (np,"linux,sml-size" ,NULL ); + basep =of_get_property (np,"linux,sml-base" ,NULL ); +if (sizep ==NULL && basep ==NULL ) +return -ENODEV ; +if (sizep ==NULL || basep ==NULL ) +return -EIO ; + +if (of_property_match_string (np,"compatible" ,"IBM,vtpm" ) <0 && +of_property_match_string (np,"compatible" ,"IBM,vtpm20" ) <0 ) { + *size =be32_to_cpup ((__force __be32 *)sizep); + *base =be64_to_cpup ((__force __be64 *)basep); + }else { + *size = *sizep; + *base = *basep; + } +return 0 ; +} +EXPORT_SYMBOL_GPL (of_tpm_get_sml_parameters);
include/linux/of_device_node.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_OF_DEVICE_NODE_H +#define _LINUX_OF_DEVICE_NODE_H + +#include <linux/of.h> + +int of_tpm_get_sml_parameters (struct device_node *np,u64 *base,u32 *size); + +#endif /* _LINUX_OF_DEVICE_NODE_H */
Signature · 1 line
2.35.1
Rob Herring
maintainerRe: [PATCH v2 1/3] tpm: of: Move of-tree specific code from tpm driver into of driver
Quoted 2 lines · Stefan Berger
Simplify
tpm_read_log_of()by moving Openfirmware-specific code into the Openfirmware driver to make the code reusable. Call the new
There is no such 'Openfirmware driver'.
Quoted 11 lines
of_tpm_get_sml_parameters()function from the TPM Openfirmware driver.Signed-off-by Stefan Berger <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Rob Herring <[email protected]>Cc Frank Rowand <[email protected]>--- drivers/char/tpm/eventlog/of.c | 31 +++++-------------------------- drivers/of/Makefile | 2 +- drivers/of/device_node.c | 27 +++++++++++++++++++++++++++
Humm, definitely the wrong direction. Generally, code for specific bindings does not go in drivers/of/. There used to be some, but we've moved it to the appropriate subsystems. kexec was an exception to not have 2 copies of the same code in arch/.
Quoted 1 line
include/linux/of_device_node.h | 9 +++++++++
of_tpm.h would be the right name assuming we kept this structure which we shouldn't. Probably linux/tpm.h? Just a guess, I'm not familar with the TPM code really.
Quoted 126 lines
4 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 drivers/of/device_node.c create mode 100644 include/linux/of_device_node.hdrivers/char/tpm/eventlog/of.c +5
-26@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/of.h> +#include <linux/of_device_node.h> #include <linux/tpm_eventlog.h> #include "../tpm.h" @@ -20,11 +21,10 @@int tpm_read_log_of (struct tpm_chip *chip) {struct device_node *np; -const u32 *sizep; -const u64 *basep;struct tpm_bios_log *log;u32 size;u64 base; +int ret; log = &chip->log;if (chip->dev.parent && chip->dev.parent->of_node)@@ -35,30 +35,9 @@ int tpm_read_log_of(struct tpm_chip *chip)if (of_property_read_bool (np,"powered-while-suspended" )) chip->flags |=TPM_CHIP_FLAG_ALWAYS_POWERED ; - sizep =of_get_property (np,"linux,sml-size" ,NULL ); - basep =of_get_property (np,"linux,sml-base" ,NULL ); -if (sizep ==NULL && basep ==NULL ) -return -ENODEV ; -if (sizep ==NULL || basep ==NULL ) -return -EIO ; - -/* -* For both vtpm/tpm, firmware has log addr and log size in big -* endian format. But in case of vtpm, there is a method called -* sml-handover which is run during kernel init even before -* device tree is setup. This sml-handover function takes care -* of endianness and writes to sml-base and sml-size in little -* endian format. For this reason, vtpm doesn't need conversion -* but physical tpm needs the conversion. -*/ -if (of_property_match_string (np,"compatible" ,"IBM,vtpm" ) <0 && -of_property_match_string (np,"compatible" ,"IBM,vtpm20" ) <0 ) { - size =be32_to_cpup ((__force __be32 *)sizep); - base =be64_to_cpup ((__force __be64 *)basep); - }else { - size = *sizep; - base = *basep; - } + ret =of_tpm_get_sml_parameters (np, &base, &size); +if (ret <0 ) +return ret;if (size ==0 ) {dev_warn (&chip->dev,"%s: Event log area empty\n" , __func__);drivers/of/Makefile +1
-1@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y = base.o device.o platform.o property.o +obj-y = base.o device.o platform.o property.o device_node.o obj-$(CONFIG_OF_KOBJ) += kobj.o obj-$(CONFIG_OF_DYNAMIC) += dynamic.o obj-$(CONFIG_OF_FLATTREE) += fdt.odrivers/of/device_node.c new file +27
-0@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/export.h> +#include <linux/of_device_node.h> + +int of_tpm_get_sml_parameters (struct device_node *np,u64 *base,u32 *size) +{ +const u32 *sizep; +const u64 *basep; + + sizep =of_get_property (np,"linux,sml-size" ,NULL ); + basep =of_get_property (np,"linux,sml-base" ,NULL ); +if (sizep ==NULL && basep ==NULL ) +return -ENODEV ; +if (sizep ==NULL || basep ==NULL ) +return -EIO ; + +if (of_property_match_string (np,"compatible" ,"IBM,vtpm" ) <0 && +of_property_match_string (np,"compatible" ,"IBM,vtpm20" ) <0 ) { + *size =be32_to_cpup ((__force __be32 *)sizep); + *base =be64_to_cpup ((__force __be64 *)basep); + }else { + *size = *sizep; + *base = *basep; + } +return 0 ; +} +EXPORT_SYMBOL_GPL (of_tpm_get_sml_parameters);include/linux/of_device_node.h new file +9
-0@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_OF_DEVICE_NODE_H +#define _LINUX_OF_DEVICE_NODE_H + +#include <linux/of.h> + +int of_tpm_get_sml_parameters (struct device_node *np,u64 *base,u32 *size); + +#endif /* _LINUX_OF_DEVICE_NODE_H */ Signature · 1 line
2.35.1
Re: [PATCH v2 1/3] tpm: of: Move of-tree specific code from tpm driver into of driver
Quoted 18 lines · Rob Herring
Quoted 2 lines · Stefan Berger
Simplify
tpm_read_log_of()by moving Openfirmware-specific code into the Openfirmware driver to make the code reusable. Call the newThere is no such 'Openfirmware driver'.
Quoted 11 lines
of_tpm_get_sml_parameters()function from the TPM Openfirmware driver.Signed-off-by Stefan Berger <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Rob Herring <[email protected]>Cc Frank Rowand <[email protected]>--- drivers/char/tpm/eventlog/of.c | 31 +++++-------------------------- drivers/of/Makefile | 2 +- drivers/of/device_node.c | 27 +++++++++++++++++++++++++++
Quoted 4 lines
Humm, definitely the wrong direction. Generally, code for specific bindings does not go in
drivers/of/. There used to be some, but we've moved it to the appropriate subsystems. kexec was an exception to not have 2 copies of the same code inarch/.
The function I am moving here is called by the TPM subsystem and also now by of/kexec.c. The latter is compiled under the following conditions:
ifdef CONFIG_KEXEC_FILE
ifdef CONFIG_OF_FLATTREE
obj-y += kexec.o
endif
endif
The code that current calls it is compiled under the following conditions:
tpm-$(CONFIG_OF) += eventlog/of.o
To make it available to both I could keep it in the TPM subsystem like this:
obj-$(CONFIG_OF) = tpm_of.o
Jarrko, if you read this, any comment?
Stefan
Quoted 126 lines
Quoted 2 lines
include/linux/of_device_node.h | 9 +++++++++
of_tpm.hwould be the right name assuming we kept this structure which we shouldn't. Probablylinux/tpm.h? Just a guess, I'm not familar with the TPM code really.Quoted 118 lines
4 files changed, 42 insertions(+), 27 deletions(-)create mode 100644
drivers/of/device_node.ccreate mode 100644include/linux/of_device_node.hdrivers/char/tpm/eventlog/of.c +5
-26@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/of.h> +#include <linux/of_device_node.h> #include <linux/tpm_eventlog.h> #include "../tpm.h" @@ -20,11 +21,10 @@int tpm_read_log_of (struct tpm_chip *chip) {struct device_node *np; -const u32 *sizep; -const u64 *basep;struct tpm_bios_log *log;u32 size;u64 base; +int ret; log = &chip->log;if (chip->dev.parent && chip->dev.parent->of_node)@@ -35,30 +35,9 @@ int tpm_read_log_of(struct tpm_chip *chip)if (of_property_read_bool (np,"powered-while-suspended" )) chip->flags |=TPM_CHIP_FLAG_ALWAYS_POWERED ; - sizep =of_get_property (np,"linux,sml-size" ,NULL ); - basep =of_get_property (np,"linux,sml-base" ,NULL ); -if (sizep ==NULL && basep ==NULL ) -return -ENODEV ; -if (sizep ==NULL || basep ==NULL ) -return -EIO ; - -/* -* For both vtpm/tpm, firmware has log addr and log size in big -* endian format. But in case of vtpm, there is a method called -* sml-handover which is run during kernel init even before -* device tree is setup. This sml-handover function takes care -* of endianness and writes to sml-base and sml-size in little -* endian format. For this reason, vtpm doesn't need conversion -* but physical tpm needs the conversion. -*/ -if (of_property_match_string (np,"compatible" ,"IBM,vtpm" ) <0 && -of_property_match_string (np,"compatible" ,"IBM,vtpm20" ) <0 ) { - size =be32_to_cpup ((__force __be32 *)sizep); - base =be64_to_cpup ((__force __be64 *)basep); - }else { - size = *sizep; - base = *basep; - } + ret =of_tpm_get_sml_parameters (np, &base, &size); +if (ret <0 ) +return ret;if (size ==0 ) {dev_warn (&chip->dev,"%s: Event log area empty\n" , __func__);drivers/of/Makefile +1
-1@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y = base.o device.o platform.o property.o +obj-y = base.o device.o platform.o property.o device_node.o obj-$(CONFIG_OF_KOBJ) += kobj.o obj-$(CONFIG_OF_DYNAMIC) += dynamic.o obj-$(CONFIG_OF_FLATTREE) += fdt.odrivers/of/device_node.c new file +27
-0@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/export.h> +#include <linux/of_device_node.h> + +int of_tpm_get_sml_parameters (struct device_node *np,u64 *base,u32 *size) +{ +const u32 *sizep; +const u64 *basep; + + sizep =of_get_property (np,"linux,sml-size" ,NULL ); + basep =of_get_property (np,"linux,sml-base" ,NULL ); +if (sizep ==NULL && basep ==NULL ) +return -ENODEV ; +if (sizep ==NULL || basep ==NULL ) +return -EIO ; + +if (of_property_match_string (np,"compatible" ,"IBM,vtpm" ) <0 && +of_property_match_string (np,"compatible" ,"IBM,vtpm20" ) <0 ) { + *size =be32_to_cpup ((__force __be32 *)sizep); + *base =be64_to_cpup ((__force __be64 *)basep); + }else { + *size = *sizep; + *base = *basep; + } +return 0 ; +} +EXPORT_SYMBOL_GPL (of_tpm_get_sml_parameters);include/linux/of_device_node.h new file +5
-0@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_OF_DEVICE_NODE_H +#define _LINUX_OF_DEVICE_NODE_H + +#include <linux/of.h>
Quoted 8 lines
Quoted 8 lines
include/linux/of_device_node.h new file +4
-0+ +int of_tpm_get_sml_parameters (struct device_node *np,u64 *base,u32 *size); + +#endif /* _LINUX_OF_DEVICE_NODE_H */ Signature · 1 line
2.35.1
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Jarkko Sakkinen
maintainerRe: [PATCH v2 1/3] tpm: of: Move of-tree specific code from tpm driver into of driver
Quoted 41 lines · Stefan Berger
Quoted 18 lines · Rob Herring
Quoted 2 lines · Stefan Berger
Simplify
tpm_read_log_of()by moving Openfirmware-specific code into the Openfirmware driver to make the code reusable. Call the newThere is no such 'Openfirmware driver'.
Quoted 11 lines
of_tpm_get_sml_parameters()function from the TPM Openfirmware driver.Signed-off-by Stefan Berger <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Rob Herring <[email protected]>Cc Frank Rowand <[email protected]>--- drivers/char/tpm/eventlog/of.c | 31 +++++-------------------------- drivers/of/Makefile | 2 +- drivers/of/device_node.c | 27 +++++++++++++++++++++++++++Quoted 4 lines
Humm, definitely the wrong direction. Generally, code for specific bindings does not go in
drivers/of/. There used to be some, but we've moved it to the appropriate subsystems. kexec was an exception to not have 2 copies of the same code inarch/.The function I am moving here is called by the TPM subsystem and also now by
of/kexec.c. The latter is compiled under the following conditions:ifdef
CONFIG_KEXEC_FILE
ifdefCONFIG_OF_FLATTREE
obj-y += kexec.o
endif
endifThe code that current calls it is compiled under the following conditions:
tpm-$(CONFIG_OF) +=
eventlog/of.oTo make it available to both I could keep it in the TPM subsystem like this:
Quoted 7 lines
obj-$(CONFIG_OF) = tpm_of.o
Jarrko, if you read this, any comment?
Stefan
Why can't you convert of_tpm_get_sml_parameters() to inline function?
BR, Jarkko
Re: [PATCH v2 1/3] tpm: of: Move of-tree specific code from tpm driver into of driver
Quoted 45 lines · Jarkko Sakkinen
Quoted 44 lines · Stefan Berger
Quoted 18 lines · Rob Herring
Quoted 2 lines · Stefan Berger
Simplify
tpm_read_log_of()by moving Openfirmware-specific code into the Openfirmware driver to make the code reusable. Call the newThere is no such 'Openfirmware driver'.
Quoted 11 lines
of_tpm_get_sml_parameters()function from the TPM Openfirmware driver.Signed-off-by Stefan Berger <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Rob Herring <[email protected]>Cc Frank Rowand <[email protected]>--- drivers/char/tpm/eventlog/of.c | 31 +++++-------------------------- drivers/of/Makefile | 2 +- drivers/of/device_node.c | 27 +++++++++++++++++++++++++++Quoted 4 lines
Humm, definitely the wrong direction. Generally, code for specific bindings does not go in
drivers/of/. There used to be some, but we've moved it to the appropriate subsystems. kexec was an exception to not have 2 copies of the same code inarch/.The function I am moving here is called by the TPM subsystem and also now by
of/kexec.c. The latter is compiled under the following conditions:ifdef
CONFIG_KEXEC_FILE
ifdefCONFIG_OF_FLATTREE
obj-y += kexec.o
endif
endifThe code that current calls it is compiled under the following conditions:
tpm-$(CONFIG_OF) +=
eventlog/of.oTo make it available to both I could keep it in the TPM subsystem like this:
obj-$(CONFIG_OF) = tpm_of.o
Quoted 6 lines
Quoted 4 lines
Jarrko, if you read this, any comment?
Stefan
Why can't you convert
of_tpm_get_sml_parameters()to inline function?
I can do that and put it into include/linux/tpm.h. The only concern would have been the size of the function.
Thanks,
Stefan
Quoted 2 lines
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 1/3] tpm: of: Move of-tree specific code from tpm driver into of driver
Quoted 51 lines · Stefan Berger
Quoted 48 lines · Jarkko Sakkinen
Quoted 47 lines · Stefan Berger
Quoted 18 lines · Rob Herring
Quoted 2 lines · Stefan Berger
Simplify
tpm_read_log_of()by moving Openfirmware-specific code into the Openfirmware driver to make the code reusable. Call the newThere is no such 'Openfirmware driver'.
Quoted 11 lines
of_tpm_get_sml_parameters()function from the TPM Openfirmware driver.Signed-off-by Stefan Berger <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Rob Herring <[email protected]>Cc Frank Rowand <[email protected]>--- drivers/char/tpm/eventlog/of.c | 31 +++++-------------------------- drivers/of/Makefile | 2 +- drivers/of/device_node.c | 27 +++++++++++++++++++++++++++Quoted 4 lines
Humm, definitely the wrong direction. Generally, code for specific bindings does not go in
drivers/of/. There used to be some, but we've moved it to the appropriate subsystems. kexec was an exception to not have 2 copies of the same code inarch/.The function I am moving here is called by the TPM subsystem and also now by
of/kexec.c. The latter is compiled under the following conditions:ifdef
CONFIG_KEXEC_FILE
ifdefCONFIG_OF_FLATTREE
obj-y += kexec.o
endif
endifThe code that current calls it is compiled under the following conditions:
tpm-$(CONFIG_OF) +=
eventlog/of.oTo make it available to both I could keep it in the TPM subsystem like this:
obj-$(CONFIG_OF) = tpm_of.o
Jarrko, if you read this, any comment?
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Stefan
Why can't you convert
of_tpm_get_sml_parameters()to inline function?I can do that and put it into
include/linux/tpm.h. The only concern would have been the size of the function.
It is somewhat insignificant amount (dozens of bytes at most).
BR, Jarkko
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
PATCH v2 2/3 of: kexec: Refactor IMA buffer related functions to make them reusable
In reply to Stefan BergerRefactor IMA buffer related functions to make them reusable for carrying TPM logs across kexec.
--- drivers/of/kexec.c | 99 +++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 37 deletions(-)
drivers/of/kexec.c +62 -37
@@ -115,6 +115,18 @@ static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr,return 0 ; } +static int get_kexec_buffer (const char *name,unsigned long *addr,size_t *size) +{ +const void *prop; +int len; + + prop =of_get_property (of_chosen, name, &len); +if (!prop) +return -ENOENT ; + +return do_get_kexec_buffer (prop, len, addr, size); +} +/** * ima_get_kexec_buffer - get IMA buffer from the previous kernel * @addr: On successful return, set to point to the buffer contents.
@@ -124,19 +136,14 @@ static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr,*/ int ima_get_kexec_buffer (void **addr,size_t *size) { -int ret, len; +int ret;unsigned long tmp_addr;size_t tmp_size; -const void *prop;if (!IS_ENABLED (CONFIG_HAVE_IMA_KEXEC ))return -ENOTSUPP ; - prop =of_get_property (of_chosen,"linux,ima-kexec-buffer" , &len); -if (!prop) -return -ENOENT ; - - ret =do_get_kexec_buffer (prop, len, &tmp_addr, &tmp_size); + ret =get_kexec_buffer ("linux,ima-kexec-buffer" , &tmp_addr, &tmp_size);if (ret)return ret;
@@ -174,6 +181,28 @@ int ima_free_kexec_buffer(void)return memblock_phys_free (addr, size); } +static int remove_buffer (void *fdt,int chosen_node,const char *name) +{ +int ret, len; +unsigned long addr; +size_t size; +const void *prop; + + prop =fdt_getprop (fdt, chosen_node, name, &len); +if (!prop) +return -ENOENT ; + + ret =do_get_kexec_buffer (prop, len, &addr, &size); +fdt_delprop (fdt, chosen_node, name); +if (ret) +return ret; + + ret =fdt_find_and_del_mem_rsv (fdt, addr, size); +if (!ret) +pr_debug ("Remove old %s buffer reserveration" , name); +return ret; +} +/** * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt *
@@ -185,29 +214,34 @@ int ima_free_kexec_buffer(void)*/ static void remove_ima_buffer (void *fdt,int chosen_node) { -int ret, len; -unsigned long addr; -size_t size; -const void *prop; -if (!IS_ENABLED (CONFIG_HAVE_IMA_KEXEC ))return ; - prop =fdt_getprop (fdt, chosen_node,"linux,ima-kexec-buffer" , &len); -if (!prop) -return ; +remove_buffer (fdt, chosen_node,"linux,ima-kexec-buffer" ); +} - ret =do_get_kexec_buffer (prop, len, &addr, &size); -fdt_delprop (fdt, chosen_node,"linux,ima-kexec-buffer" ); +#ifdef CONFIG_IMA_KEXEC +static int setup_buffer (void *fdt,int chosen_node,const char *name, +uint64_t addr,uint64_t size) +{ +int ret; + +if (!size) +return 0 ; + + ret =fdt_appendprop_addrrange (fdt,0 , chosen_node, + name, addr, size); +if (ret <0 ) +return -EINVAL ; + + ret =fdt_add_mem_rsv (fdt, addr, size);if (ret) -return ; +return -EINVAL ; + +return 0 ; - ret =fdt_find_and_del_mem_rsv (fdt, addr, size); -if (!ret) -pr_debug ("Removed old IMA buffer reservation.\n" ); } -#ifdef CONFIG_IMA_KEXEC /** * setup_ima_buffer - add IMA buffer information to the fdt * @image: kexec image being loaded.
@@ -221,23 +255,14 @@ static int setup_ima_buffer(const struct kimage *image, void *fdt, {int ret; -if (!image->ima_buffer_size) -return 0 ; - - ret =fdt_appendprop_addrrange (fdt,0 , chosen_node, -"linux,ima-kexec-buffer" , - image->ima_buffer_addr, - image->ima_buffer_size); -if (ret <0 ) -return -EINVAL ; - - ret =fdt_add_mem_rsv (fdt, image->ima_buffer_addr, - image->ima_buffer_size); + ret =setup_buffer (fdt, chosen_node,"linux,ima-kexec-buffer" , + image->ima_buffer_addr, image->ima_buffer_size);if (ret) -return -EINVAL ; +return ret; -pr_debug ("IMA buffer at 0x%llx, size = 0x%zx\n" , - image->ima_buffer_addr, image->ima_buffer_size); +if (image->ima_buffer_addr) +pr_debug ("IMA buffer at 0x%llx, size = 0x%zx\n" , + image->ima_buffer_addr, image->ima_buffer_size);return 0 ; }
Signature · 1 line
2.35.1
PATCH v2 3/3 tpm/kexec: Duplicate TPM measurement log in of-tree for kexec
In reply to Stefan BergerThe memory area of the TPM measurement log is currently not properly duplicated for carrying it across kexec when an Open Firmware Devicetree is used. Therefore, the contents of the log get corrupted. Fix this for the kexec_file_load() syscall by allocating a buffer and copying the contents of the existing log into it. The new buffer is preserved across the kexec and a pointer to it is available when the new kernel is started. To achieve this, store the allocated buffer's address in the flattened device tree (fdt) under the name linux,tpm-kexec-buffer and search for this entry early in the kernel startup before the TPM subsystem starts up. Adjust the pointer in the of-tree stored under linux,sml-base to point to this buffer holding the preserved log. The TPM driver can then read the base address from this entry when making the log available.
Use postcore_initcall() to call the function to restore the buffer even if the TPM subsystem or driver are not used. This allows the buffer to be carried across the next kexec without involvement of the TPM subsystem and ensures a valid buffer pointed to by the of-tree.
--- drivers/of/kexec.c | 198 +++++++++++++++++++++++++++++++++++++++++- include/linux/kexec.h | 6 ++ include/linux/of.h | 8 +- kernel/kexec_file.c | 6 ++ 4 files changed, 216 insertions(+), 2 deletions(-)
drivers/of/kexec.c +197 -1
@@ -14,6 +14,7 @@#include <linux/memblock.h> #include <linux/libfdt.h> #include <linux/of.h> +#include <linux/of_device_node.h> #include <linux/of_fdt.h> #include <linux/random.h> #include <linux/slab.h>
@@ -220,7 +221,6 @@ static void remove_ima_buffer(void *fdt, int chosen_node)remove_buffer (fdt, chosen_node,"linux,ima-kexec-buffer" ); } -#ifdef CONFIG_IMA_KEXEC static int setup_buffer (void *fdt,int chosen_node,const char *name,uint64_t addr,uint64_t size) {
@@ -242,6 +242,7 @@ static int setup_buffer(void *fdt, int chosen_node, const char *name, } +#ifdef CONFIG_IMA_KEXEC /** * setup_ima_buffer - add IMA buffer information to the fdt * @image: kexec image being loaded.
@@ -274,6 +275,198 @@ static inline int setup_ima_buffer(const struct kimage *image, void *fdt, }#endif /* CONFIG_IMA_KEXEC */ +/** +* tpm_get_kexec_buffer - get TPM log buffer from the previous kernel +* @phyaddr: On successful return, set to physical address of buffer +* @size: On successful return, set to the buffer size. +* +* Return: 0 on success, negative errno on error. +*/ +static int tpm_get_kexec_buffer (void **phyaddr,size_t *size) +{ +int ret; +unsigned long tmp_addr; +size_t tmp_size; + + ret =get_kexec_buffer ("linux,tpm-kexec-buffer" , &tmp_addr, &tmp_size); +if (ret) +return ret; + + *phyaddr = (void *)tmp_addr; + *size = tmp_size; + +return 0 ; +} + +/** +* tpm_free_kexec_buffer - free memory used by the IMA buffer +*/ +static int tpm_of_remove_kexec_buffer (void ) +{ +struct property *prop; + + prop =of_find_property (of_chosen,"linux,tpm-kexec-buffer" ,NULL ); +if (!prop) +return -ENOENT ; + +return of_remove_property (of_chosen, prop); +} + +/** +* remove_tpm_buffer - remove the TPM log buffer property and reservation from @fdt +* +* @fdt: Flattened Device Tree to update +* @chosen_node: Offset to the chosen node in the device tree +* +* The TPM log measurement buffer is of no use to a subsequent kernel, so we always +* remove it from the device tree. +*/ +static void remove_tpm_buffer (void *fdt,int chosen_node) +{ +if (!IS_ENABLED (CONFIG_PPC64 )) +return ; + +remove_buffer (fdt, chosen_node,"linux,tpm-kexec-buffer" ); +} + +/** +* setup_tpm_buffer - add TPM measurement log buffer information to the fdt +* @image: kexec image being loaded. +* @fdt: Flattened device tree for the next kernel. +* @chosen_node: Offset to the chosen node. +* +* Return: 0 on success, or negative errno on error. +*/ +static int setup_tpm_buffer (const struct kimage *image,void *fdt, +int chosen_node) +{ +if (!IS_ENABLED (CONFIG_PPC64 )) +return 0 ; + +return setup_buffer (fdt, chosen_node,"linux,tpm-kexec-buffer" , + image->tpm_buffer_addr, image->tpm_buffer_size); +} + +void tpm_add_kexec_buffer (struct kimage *image) +{ +struct kexec_buf kbuf = { .image = image, .buf_align =1 , + .buf_min =0 , .buf_max =ULONG_MAX , + .top_down = true }; +struct device_node *np; +void *buffer; +u32 size; +u64 base; +int ret; + +if (!IS_ENABLED (CONFIG_PPC64 )) +return ; + + np =of_find_node_by_name (NULL ,"vtpm" ); +if (!np) +return ; + +if (of_tpm_get_sml_parameters (np, &base, &size) <0 ) +return ; + + buffer =vmalloc (size); +if (!buffer) +return ; +memcpy (buffer,__va (base), size); + + kbuf.buffer = buffer; + kbuf.bufsz = size; + kbuf.memsz = size; + ret =kexec_add_buffer (&kbuf); +if (ret) { +pr_err ("Error passing over kexec TPM measurement log buffer: %d\n" , + ret); +return ; + } + + image->tpm_buffer = buffer; + image->tpm_buffer_addr = kbuf.mem; + image->tpm_buffer_size = size; +} + +/** +* tpm_post_kexec - Make stored TPM log buffer available in of-tree +*/ +static int __init tpm_post_kexec (void ) +{ +struct property *newprop; +struct device_node *np; +void *phyaddr; +size_t size; +u32 oflogsize; +u64 unused; +int ret; + +if (!IS_ENABLED (CONFIG_PPC64 )) +return 0 ; + + ret =tpm_get_kexec_buffer (&phyaddr, &size); +if (ret) +return 0 ; + +/* +* If any one of the following steps fails then the next kexec will +* cause issues due to linux,sml-base pointing to a stale buffer. +*/ + np =of_find_node_by_name (NULL ,"vtpm" ); +if (!np) + goto err_free_memblock; + +/* logsize must not have changed */ +if (of_tpm_get_sml_parameters (np, &unused, &oflogsize) <0 ) + goto err_free_memblock; +if (oflogsize != size) + goto err_free_memblock; + +/* replace linux,sml-base with new physical address of buffer */ + ret = -ENOMEM ; + newprop =kzalloc (sizeof (*newprop),GFP_KERNEL ); +if (!newprop) + goto err_free_memblock; + + newprop->name =kstrdup ("linux,sml-base" ,GFP_KERNEL ); +if (!newprop->name) + goto err_free_newprop; + + newprop->length =sizeof (phyaddr); + + newprop->value =kmalloc (sizeof (u64),GFP_KERNEL ); +if (!newprop->value) + goto err_free_newprop_struct; + +if (of_property_match_string (np,"compatible" ,"IBM,vtpm" ) <0 && +of_property_match_string (np,"compatible" ,"IBM,vtpm20" ) <0 ) { + ret = -ENODEV ; + goto err_free_newprop_struct; + }else { + *(u64 *)newprop->value = (u64 )phyaddr; + } + + ret =of_update_property (np, newprop); +if (ret) { +pr_err ("Could not update linux,sml-base with new address" ); + goto err_free_newprop_struct; + } + + goto exit; + +err_free_newprop_struct: +kfree (newprop->name); +err_free_newprop: +kfree (newprop); +err_free_memblock: +memblock_phys_free ((phys_addr_t )phyaddr, size); +exit: +tpm_of_remove_kexec_buffer (); + +return ret; +} +postcore_initcall (tpm_post_kexec); +/* * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree *
@@ -463,6 +656,9 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,remove_ima_buffer (fdt, chosen_node); ret =setup_ima_buffer (image, fdt,fdt_path_offset (fdt,"/chosen" )); +remove_tpm_buffer (fdt, chosen_node); + ret =setup_tpm_buffer (image, fdt,fdt_path_offset (fdt,"/chosen" )); + out:if (ret) {kvfree (fdt);
include/linux/kexec.h +6 -0
@@ -319,6 +319,12 @@ struct kimage {void *elf_headers;unsigned long elf_headers_sz;unsigned long elf_load_addr; + +/* Virtual address of TPM log buffer for kexec syscall */ +void *tpm_buffer; + +phys_addr_t tpm_buffer_addr; +size_t tpm_buffer_size; };/* kexec interface functions */
include/linux/of.h +7 -1
@@ -100,6 +100,8 @@ struct of_reconfig_data {struct property *old_prop; }; +struct kimage ; +/* initialize a node */ extern struct kobj_type of_node_ktype;extern const struct fwnode_operations of_fwnode_ops;
@@ -436,13 +438,13 @@ int of_map_id(struct device_node *np, u32 id,phys_addr_t of_dma_get_max_cpu_address (struct device_node *np); -struct kimage ;void *of_kexec_alloc_and_setup_fdt (const struct kimage *image,unsigned long initrd_load_addr,unsigned long initrd_len,const char *cmdline,size_t extra_fdt_size);int ima_get_kexec_buffer (void **addr,size_t *size);int ima_free_kexec_buffer (void ); +void tpm_add_kexec_buffer (struct kimage *image);#else /* CONFIG_OF */ static inline void of_core_init (void )
@@ -844,6 +846,10 @@ static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np) return PHYS_ADDR_MAX; } +static inline void tpm_add_kexec_buffer (struct kimage *image) +{ +} +#define of_match_ptr (_ptr) NULL#define of_match_node (_matches, _node) NULL#endif /* CONFIG_OF */
kernel/kexec_file.c +6 -0
@@ -27,6 +27,7 @@#include <linux/kernel_read_file.h> #include <linux/syscalls.h> #include <linux/vmalloc.h> +#include <linux/of.h> #include "kexec_internal.h" static int kexec_calculate_store_digests (struct kimage *image);
@@ -171,6 +172,9 @@ void kimage_file_post_load_cleanup(struct kimage *image) image->ima_buffer =NULL ;#endif /* CONFIG_IMA_KEXEC */ +vfree (image->tpm_buffer); + image->tpm_buffer =NULL ; +/* See if architecture has anything to cleanup post load */ arch_kimage_file_post_load_cleanup (image);
@@ -277,6 +281,8 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,/* IMA needs to pass the measurement list to the next kernel. */ ima_add_kexec_buffer (image); +/* Pass the TPM measurement log to next kernel */ +tpm_add_kexec_buffer (image);/* Call arch image load handlers */ ldata =arch_kexec_kernel_image_load (image);
Signature · 1 line
2.35.1
kernel test robot
build botRe: [PATCH v2 3/3] tpm/kexec: Duplicate TPM measurement log in of-tree for kexec
Thank you for the patch! Perhaps something to improve:
Hi Stefan,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on char-misc/char-misc-testing] [also build test WARNING on linus/master v5.19-rc2 next-20220616] [cannot apply to robh/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Stefan-Berger/tpm-Preserve-TPM-measurement-log-across-kexec/20220616-234240 base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git 0a35780c755ccec097d15c6b4ff8b246a89f1689 config: x86_64-randconfig-a016 (https://download.01.org/0day-ci/archive/20220617/[email protected]/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project f0e608de27b3d568000046eebf3712ab542979d6)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/c28e0f7321d0b7245454e811a3dd0f2134d9dd74
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Stefan-Berger/tpm-Preserve-TPM-measurement-log-across-kexec/20220616-234240 git checkout c28e0f7321d0b7245454e811a3dd0f2134d9dd74
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash drivers/of/
If you fix the issue, kindly add following tag where applicable
All warnings (new ones prefixed by >>):
>> drivers/of/kexec.c:305: warning: expecting prototype for tpm_free_kexec_buffer(). Prototype was for tpm_of_remove_kexec_buffer() instead
vim +305 drivers/of/kexec.c
300
301 /**
302 * tpm_free_kexec_buffer - free memory used by the IMA buffer
303 */
304 static int tpm_of_remove_kexec_buffer(void)
> 305 { 306 struct property *prop;
307
308 prop = of_find_property(of_chosen, "linux,tpm-kexec-buffer", NULL);
309 if (!prop)
310 return -ENOENT;
311
312 return of_remove_property(of_chosen, prop);
313 }
314
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
kernel test robot
build botRe: [PATCH v2 3/3] tpm/kexec: Duplicate TPM measurement log in of-tree for kexec
In reply to Stefan BergerThank you for the patch! Yet something to improve:
Hi Stefan,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on linus/master v5.19-rc4 next-20220628] [cannot apply to robh/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Stefan-Berger/tpm-Preserve-TPM-measurement-log-across-kexec/20220616-234240 base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git 0a35780c755ccec097d15c6b4ff8b246a89f1689 config: parisc-randconfig-r012-20220627 (https://download.01.org/0day-ci/archive/20220629/[email protected]/config) compiler: hppa-linux-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/c28e0f7321d0b7245454e811a3dd0f2134d9dd74
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Stefan-Berger/tpm-Preserve-TPM-measurement-log-across-kexec/20220616-234240 git checkout c28e0f7321d0b7245454e811a3dd0f2134d9dd74
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=parisc SHELL=/bin/bash drivers/of/
If you fix the issue, kindly add following tag where applicable
All errors (new ones prefixed by >>):
drivers/of/kexec.c: In function 'tpm_add_kexec_buffer':
>> drivers/of/kexec.c:371:18: error: implicit declaration of function 'vmalloc'; did you mean 'kvmalloc'? [-Werror=implicit-function-declaration]
371 | buffer = vmalloc(size);
| ^~~~~~~
| kvmalloc
drivers/of/kexec.c:371:16: warning: assignment to 'void *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
371 | buffer = vmalloc(size);
| ^
drivers/of/kexec.c: In function 'tpm_post_kexec':
drivers/of/kexec.c:446:42: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
446 | *(u64 *)newprop->value = (u64)phyaddr;
| ^cc1: some warnings being treated as errors
vim +371 drivers/of/kexec.c
349
350 void tpm_add_kexec_buffer(struct kimage *image)
351 {
352 struct kexec_buf kbuf = { .image = image, .buf_align = 1,
353 .buf_min = 0, .buf_max = ULONG_MAX,
354 .top_down = true };
355 struct device_node *np;
356 void *buffer;
357 u32 size;
358 u64 base;
359 int ret;
360
361 if (!IS_ENABLED(CONFIG_PPC64))
362 return;
363
364 np = of_find_node_by_name(NULL, "vtpm");
365 if (!np)
366 return;
367
368 if (of_tpm_get_sml_parameters(np, &base, &size) < 0)
369 return;
370
> 371 buffer = vmalloc(size);
372 if (!buffer)
373 return;
374 memcpy(buffer, __va(base), size);
375
376 kbuf.buffer = buffer;
377 kbuf.bufsz = size;
378 kbuf.memsz = size;
379 ret = kexec_add_buffer(&kbuf);
380 if (ret) {
381 pr_err("Error passing over kexec TPM measurement log buffer: %d\n",
382 ret);
383 return;
384 }
385
386 image->tpm_buffer = buffer;
387 image->tpm_buffer_addr = kbuf.mem;
388 image->tpm_buffer_size = size;
389 }
390
Signature · 7 lines
0-DAY CI Kernel Test Service https://01.org/lkp _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Rob Herring
maintainerQuoted 4 lines · Stefan Berger
The of-tree driver does not currently preserve the IBM vTPM 1.2 and vTPM 2.0 measurement logs across a kexec. This series fixes this for the
kexec_file_load()syscall using the flattened device tree (fdt) to carry the measurement log's buffer across kexec.
As mentioned in v1, please Cc other folks that might care about TPMs and kexec. I'm sure it's not only IBM.
Rob
Quoted 8 lines · Rob Herring
Quoted 4 lines · Stefan Berger
The of-tree driver does not currently preserve the IBM vTPM 1.2 and vTPM 2.0 measurement logs across a kexec. This series fixes this for the
kexec_file_load()syscall using the flattened device tree (fdt) to carry the measurement log's buffer across kexec.As mentioned in v1, please Cc other folks that might care about TPMs and kexec. I'm sure it's not only IBM.
That's why I cc'ed the linux-integrity mailing list now where all the TPM related development is happening.
Stefan
Quoted 2 lines
Rob