Re: [PATCH v6 12/14] x86: Secure Launch late initcall platform module

2023-05-12 Thread Ross Philipson

On 5/10/23 18:40, Jarkko Sakkinen wrote:

On Thu May 4, 2023 at 5:50 PM EEST, Ross Philipson wrote:

From: "Daniel P. Smith" 

The Secure Launch platform module is a late init module. During the
init call, the TPM event log is read and measurements taken in the
early boot stub code are located. These measurements are extended
into the TPM PCRs using the mainline TPM kernel driver.

The platform module also registers the securityfs nodes to allow
access to TXT register fields on Intel along with the fetching of
and writing events to the late launch TPM log.

Signed-off-by: Daniel P. Smith 
Signed-off-by: garnetgrimm 
Signed-off-by: Ross Philipson 
---
  arch/x86/kernel/Makefile   |   1 +
  arch/x86/kernel/slmodule.c | 520 +
  2 files changed, 521 insertions(+)
  create mode 100644 arch/x86/kernel/slmodule.c

diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 3d2a33e..ee3fe300 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -73,6 +73,7 @@ obj-$(CONFIG_IA32_EMULATION)  += tls.o
  obj-y += step.o
  obj-$(CONFIG_INTEL_TXT)   += tboot.o
  obj-$(CONFIG_SECURE_LAUNCH)   += slaunch.o
+obj-$(CONFIG_SECURE_LAUNCH)+= slmodule.o
  obj-$(CONFIG_ISA_DMA_API) += i8237.o
  obj-y += stacktrace.o
  obj-y += cpu/
diff --git a/arch/x86/kernel/slmodule.c b/arch/x86/kernel/slmodule.c
new file mode 100644
index 000..70dcff5
--- /dev/null
+++ b/arch/x86/kernel/slmodule.c
@@ -0,0 +1,520 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Secure Launch late validation/setup, securityfs exposure and
+ * finalization support.


/* Secure Launch late validation/setup, securityfs exposure and finalization */

79 characters (max length allowed is 100).


Yup will fix.



BR, Jarkko




___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH v6 12/14] x86: Secure Launch late initcall platform module

2023-05-12 Thread Ross Philipson

On 5/10/23 18:39, Jarkko Sakkinen wrote:

On Thu May 4, 2023 at 5:50 PM EEST, Ross Philipson wrote:

From: "Daniel P. Smith" 

The Secure Launch platform module is a late init module. During the
init call, the TPM event log is read and measurements taken in the
early boot stub code are located. These measurements are extended
into the TPM PCRs using the mainline TPM kernel driver.

The platform module also registers the securityfs nodes to allow
access to TXT register fields on Intel along with the fetching of
and writing events to the late launch TPM log.

Signed-off-by: Daniel P. Smith 
Signed-off-by: garnetgrimm 
Signed-off-by: Ross Philipson 
---
  arch/x86/kernel/Makefile   |   1 +
  arch/x86/kernel/slmodule.c | 520 +
  2 files changed, 521 insertions(+)
  create mode 100644 arch/x86/kernel/slmodule.c

diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 3d2a33e..ee3fe300 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -73,6 +73,7 @@ obj-$(CONFIG_IA32_EMULATION)  += tls.o
  obj-y += step.o
  obj-$(CONFIG_INTEL_TXT)   += tboot.o
  obj-$(CONFIG_SECURE_LAUNCH)   += slaunch.o
+obj-$(CONFIG_SECURE_LAUNCH)+= slmodule.o
  obj-$(CONFIG_ISA_DMA_API) += i8237.o
  obj-y += stacktrace.o
  obj-y += cpu/
diff --git a/arch/x86/kernel/slmodule.c b/arch/x86/kernel/slmodule.c
new file mode 100644
index 000..70dcff5
--- /dev/null
+++ b/arch/x86/kernel/slmodule.c
@@ -0,0 +1,520 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Secure Launch late validation/setup, securityfs exposure and
+ * finalization support.
+ *
+ * Copyright (c) 2022 Apertus Solutions, LLC
+ * Copyright (c) 2021 Assured Information Security, Inc.
+ * Copyright (c) 2022, Oracle and/or its affiliates.
+ *
+ * Author(s):
+ * Daniel P. Smith 
+ * Garnet T. Grimm 


I'm not sure what the authors list is good for in the days of Git,
and all the tags we have for commit messages.

Instead of this you could just put to the commit:

Co-developed-by: Garnet T. Grimm 
Signed-off-by: Garnet T. Grimm 
Signed-off-by: Daniel P. Smith 

See:

https://www.kernel.org/doc/html/latest/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by


Yes we will fix all that. And everything else you pointed out in this 
review.


Thanks




+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define DECLARE_TXT_PUB_READ_U(size, fmt, msg_size)\
+static ssize_t txt_pub_read_u##size(unsigned int offset,   \
+   loff_t *read_offset,\
+   size_t read_len,\
+   char __user *buf)   \


This would really deserve:

/*
  * Explain what DECLARE_TXT_PUB_READ_U is.
  */


+{  \
+   void __iomem *txt;  \
+   char msg_buffer[msg_size];  \


Reverse-christmas tree order.


+   u##size reg_value = 0;  \
+   txt = ioremap(TXT_PUB_CONFIG_REGS_BASE, \
+   TXT_NR_CONFIG_PAGES * PAGE_SIZE);   \
+   if (!txt)   \
+   return -EFAULT; \
+   memcpy_fromio(_value, txt + offset, sizeof(u##size));   \
+   iounmap(txt);   \
+   snprintf(msg_buffer, msg_size, fmt, reg_value); \
+   return simple_read_from_buffer(buf, read_len, read_offset,  \
+   _buffer, msg_size); \
+}
+
+DECLARE_TXT_PUB_READ_U(8, "%#04x\n", 6);
+DECLARE_TXT_PUB_READ_U(32, "%#010x\n", 12);
+DECLARE_TXT_PUB_READ_U(64, "%#018llx\n", 20);
+
+#define DECLARE_TXT_FOPS(reg_name, reg_offset, reg_size)   \
+static ssize_t txt_##reg_name##_read(struct file *flip,
\
+   char __user *buf, size_t read_len, loff_t *read_offset) \
+{  \
+   return txt_pub_read_u##reg_size(reg_offset, read_offset,\
+   read_len, buf); \
+}  \
+static const struct file_operations reg_name##_ops = { \
+   .read = txt_##reg_name##_read,  \
+}
+
+DECLARE_TXT_FOPS(sts, TXT_CR_STS, 64);
+DECLARE_TXT_FOPS(ests, TXT_CR_ESTS, 8);
+DECLARE_TXT_FOPS(errorcode, TXT_CR_ERRORCODE, 32);

Re: [PATCH v6 12/14] x86: Secure Launch late initcall platform module

2023-05-10 Thread Jarkko Sakkinen
On Thu May 4, 2023 at 5:50 PM EEST, Ross Philipson wrote:
> From: "Daniel P. Smith" 
>
> The Secure Launch platform module is a late init module. During the
> init call, the TPM event log is read and measurements taken in the
> early boot stub code are located. These measurements are extended
> into the TPM PCRs using the mainline TPM kernel driver.
>
> The platform module also registers the securityfs nodes to allow
> access to TXT register fields on Intel along with the fetching of
> and writing events to the late launch TPM log.
>
> Signed-off-by: Daniel P. Smith 
> Signed-off-by: garnetgrimm 
> Signed-off-by: Ross Philipson 
> ---
>  arch/x86/kernel/Makefile   |   1 +
>  arch/x86/kernel/slmodule.c | 520 
> +
>  2 files changed, 521 insertions(+)
>  create mode 100644 arch/x86/kernel/slmodule.c
>
> diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> index 3d2a33e..ee3fe300 100644
> --- a/arch/x86/kernel/Makefile
> +++ b/arch/x86/kernel/Makefile
> @@ -73,6 +73,7 @@ obj-$(CONFIG_IA32_EMULATION)+= tls.o
>  obj-y+= step.o
>  obj-$(CONFIG_INTEL_TXT)  += tboot.o
>  obj-$(CONFIG_SECURE_LAUNCH)  += slaunch.o
> +obj-$(CONFIG_SECURE_LAUNCH)  += slmodule.o
>  obj-$(CONFIG_ISA_DMA_API)+= i8237.o
>  obj-y+= stacktrace.o
>  obj-y+= cpu/
> diff --git a/arch/x86/kernel/slmodule.c b/arch/x86/kernel/slmodule.c
> new file mode 100644
> index 000..70dcff5
> --- /dev/null
> +++ b/arch/x86/kernel/slmodule.c
> @@ -0,0 +1,520 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Secure Launch late validation/setup, securityfs exposure and
> + * finalization support.

/* Secure Launch late validation/setup, securityfs exposure and finalization */

79 characters (max length allowed is 100).

BR, Jarkko


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH v6 12/14] x86: Secure Launch late initcall platform module

2023-05-10 Thread Jarkko Sakkinen
On Thu May 4, 2023 at 5:50 PM EEST, Ross Philipson wrote:
> From: "Daniel P. Smith" 
>
> The Secure Launch platform module is a late init module. During the
> init call, the TPM event log is read and measurements taken in the
> early boot stub code are located. These measurements are extended
> into the TPM PCRs using the mainline TPM kernel driver.
>
> The platform module also registers the securityfs nodes to allow
> access to TXT register fields on Intel along with the fetching of
> and writing events to the late launch TPM log.
>
> Signed-off-by: Daniel P. Smith 
> Signed-off-by: garnetgrimm 
> Signed-off-by: Ross Philipson 
> ---
>  arch/x86/kernel/Makefile   |   1 +
>  arch/x86/kernel/slmodule.c | 520 
> +
>  2 files changed, 521 insertions(+)
>  create mode 100644 arch/x86/kernel/slmodule.c
>
> diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> index 3d2a33e..ee3fe300 100644
> --- a/arch/x86/kernel/Makefile
> +++ b/arch/x86/kernel/Makefile
> @@ -73,6 +73,7 @@ obj-$(CONFIG_IA32_EMULATION)+= tls.o
>  obj-y+= step.o
>  obj-$(CONFIG_INTEL_TXT)  += tboot.o
>  obj-$(CONFIG_SECURE_LAUNCH)  += slaunch.o
> +obj-$(CONFIG_SECURE_LAUNCH)  += slmodule.o
>  obj-$(CONFIG_ISA_DMA_API)+= i8237.o
>  obj-y+= stacktrace.o
>  obj-y+= cpu/
> diff --git a/arch/x86/kernel/slmodule.c b/arch/x86/kernel/slmodule.c
> new file mode 100644
> index 000..70dcff5
> --- /dev/null
> +++ b/arch/x86/kernel/slmodule.c
> @@ -0,0 +1,520 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Secure Launch late validation/setup, securityfs exposure and
> + * finalization support.
> + *
> + * Copyright (c) 2022 Apertus Solutions, LLC
> + * Copyright (c) 2021 Assured Information Security, Inc.
> + * Copyright (c) 2022, Oracle and/or its affiliates.
> + *
> + * Author(s):
> + * Daniel P. Smith 
> + * Garnet T. Grimm 

I'm not sure what the authors list is good for in the days of Git,
and all the tags we have for commit messages.

Instead of this you could just put to the commit:

Co-developed-by: Garnet T. Grimm 
Signed-off-by: Garnet T. Grimm 
Signed-off-by: Daniel P. Smith 

See:

https://www.kernel.org/doc/html/latest/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by

> + */
> +
> +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define DECLARE_TXT_PUB_READ_U(size, fmt, msg_size)  \
> +static ssize_t txt_pub_read_u##size(unsigned int offset, \
> + loff_t *read_offset,\
> + size_t read_len,\
> + char __user *buf)   \

This would really deserve:

/*
 * Explain what DECLARE_TXT_PUB_READ_U is.
 */

> +{\
> + void __iomem *txt;  \
> + char msg_buffer[msg_size];  \

Reverse-christmas tree order.

> + u##size reg_value = 0;  \
> + txt = ioremap(TXT_PUB_CONFIG_REGS_BASE, \
> + TXT_NR_CONFIG_PAGES * PAGE_SIZE);   \
> + if (!txt)   \
> + return -EFAULT; \
> + memcpy_fromio(_value, txt + offset, sizeof(u##size));   \
> + iounmap(txt);   \
> + snprintf(msg_buffer, msg_size, fmt, reg_value); \
> + return simple_read_from_buffer(buf, read_len, read_offset,  \
> + _buffer, msg_size); \
> +}
> +
> +DECLARE_TXT_PUB_READ_U(8, "%#04x\n", 6);
> +DECLARE_TXT_PUB_READ_U(32, "%#010x\n", 12);
> +DECLARE_TXT_PUB_READ_U(64, "%#018llx\n", 20);
> +
> +#define DECLARE_TXT_FOPS(reg_name, reg_offset, reg_size) \
> +static ssize_t txt_##reg_name##_read(struct file *flip,  
> \
> + char __user *buf, size_t read_len, loff_t *read_offset) \
> +{\
> + return txt_pub_read_u##reg_size(reg_offset, read_offset,\
> + read_len, buf); \
> +}\
> +static const struct file_operations reg_name##_ops = {   
> \
> + .read = txt_##reg_name##_read,  \
> +}
> +
> +DECLARE_TXT_FOPS(sts, TXT_CR_STS, 64);
> +DECLARE_TXT_FOPS(ests, TXT_CR_ESTS, 8);
> 

Re: [PATCH v6 12/14] x86: Secure Launch late initcall platform module

2023-05-08 Thread Ross Philipson

On 5/5/23 15:42, Simon Horman wrote:

On Thu, May 04, 2023 at 02:50:21PM +, Ross Philipson wrote:

From: "Daniel P. Smith" 

The Secure Launch platform module is a late init module. During the
init call, the TPM event log is read and measurements taken in the
early boot stub code are located. These measurements are extended
into the TPM PCRs using the mainline TPM kernel driver.

The platform module also registers the securityfs nodes to allow
access to TXT register fields on Intel along with the fetching of
and writing events to the late launch TPM log.

Signed-off-by: Daniel P. Smith 
Signed-off-by: garnetgrimm 
Signed-off-by: Ross Philipson 


Hi Ross,

a few more items from my side.

...


diff --git a/arch/x86/kernel/slmodule.c b/arch/x86/kernel/slmodule.c


...


+/*
+ * Securityfs exposure
+ */
+struct memfile {
+   char *name;
+   void *addr;
+   size_t size;
+};
+
+static struct memfile sl_evtlog = {"eventlog", 0, 0};


I don't think the 0 fields are necessary above, memset will zero
any fields not explicitly set. But if you want to go that way, then
I think the first one should be NULL, as the addr field is a pointer.


+static void *txt_heap;
+static struct txt_heap_event_log_pointer2_1_element __iomem *evtlog20;
+static DEFINE_MUTEX(sl_evt_log_mutex);



+static ssize_t sl_evtlog_read(struct file *file, char __user *buf,
+ size_t count, loff_t *pos)
+{
+   ssize_t size;
+
+   if (!sl_evtlog.addr)
+   return 0;
+
+   mutex_lock(_evt_log_mutex);
+   size = simple_read_from_buffer(buf, count, pos, sl_evtlog.addr,
+  sl_evtlog.size);
+   mutex_unlock(_evt_log_mutex);
+
+   return size;
+}
+
+static ssize_t sl_evtlog_write(struct file *file, const char __user *buf,
+   size_t datalen, loff_t *ppos)


nit: the line above doesn't align to the '(' on the line before that.


+{
+   ssize_t result;
+   char *data;
+
+   if (!sl_evtlog.addr)
+   return 0;
+
+   /* No partial writes. */
+   result = -EINVAL;
+   if (*ppos != 0)
+   goto out;
+
+   data = memdup_user(buf, datalen);
+   if (IS_ERR(data)) {
+   result = PTR_ERR(data);
+   goto out;
+   }
+
+   mutex_lock(_evt_log_mutex);
+   if (evtlog20)
+   result = tpm20_log_event(evtlog20, sl_evtlog.addr,
+sl_evtlog.size, datalen, data);


Sparse says that the type of the first argument of tmp20_log_event is:

struct txt_heap_event_log_pointer2_1_element *

However, the type of evtlog20 is:

struct txt_heap_event_log_pointer2_1_element __iomem *


I have to look into what is going on here. The TXT heap is just a memory 
range not IO space. I will track this down.


As to all the rest of your comments here, I will fix them.

Thanks
Ross




+   else
+   result = tpm12_log_event(sl_evtlog.addr, sl_evtlog.size,
+datalen, data);
+   mutex_unlock(_evt_log_mutex);
+
+   kfree(data);
+out:
+   return result;
+}


...


+static long slaunch_expose_securityfs(void)
+{
+   long ret = 0;
+   int i;
+
+   slaunch_dir = securityfs_create_dir("slaunch", NULL);
+   if (IS_ERR(slaunch_dir))
+   return PTR_ERR(slaunch_dir);
+
+   if (slaunch_get_flags() & SL_FLAG_ARCH_TXT) {
+   txt_dir = securityfs_create_dir("txt", slaunch_dir);
+   if (IS_ERR(txt_dir)) {
+   ret = PTR_ERR(txt_dir);
+   goto remove_slaunch;
+   }
+
+   for (i = 0; i < ARRAY_SIZE(sl_txt_files); i++) {
+   txt_entries[i] = securityfs_create_file(
+   sl_txt_files[i].name, 0440,
+   txt_dir, NULL,
+   sl_txt_files[i].fops);
+   if (IS_ERR(txt_entries[i])) {
+   ret = PTR_ERR(txt_entries[i]);
+   goto remove_files;
+   }
+   }
+


nit: no blank line here.


+   }
+
+   if (sl_evtlog.addr > 0) {


addr is a pointer. So perhaps:

if (sl_evtlog.addr) {


+   event_file = securityfs_create_file(
+   sl_evtlog.name, 0440,
+   slaunch_dir, NULL,
+   _evtlog_ops);
+   if (IS_ERR(event_file)) {
+   ret = PTR_ERR(event_file);
+   goto remove_files;
+   }
+   }
+
+   return 0;
+
+remove_files:
+   if (slaunch_get_flags() & SL_FLAG_ARCH_TXT) {
+   while (--i >= 0)
+   securityfs_remove(txt_entries[i]);
+   

Re: [PATCH v6 12/14] x86: Secure Launch late initcall platform module

2023-05-05 Thread Simon Horman
On Thu, May 04, 2023 at 02:50:21PM +, Ross Philipson wrote:
> From: "Daniel P. Smith" 
> 
> The Secure Launch platform module is a late init module. During the
> init call, the TPM event log is read and measurements taken in the
> early boot stub code are located. These measurements are extended
> into the TPM PCRs using the mainline TPM kernel driver.
> 
> The platform module also registers the securityfs nodes to allow
> access to TXT register fields on Intel along with the fetching of
> and writing events to the late launch TPM log.
> 
> Signed-off-by: Daniel P. Smith 
> Signed-off-by: garnetgrimm 
> Signed-off-by: Ross Philipson 

Hi Ross,

a few more items from my side.

...

> diff --git a/arch/x86/kernel/slmodule.c b/arch/x86/kernel/slmodule.c

...

> +/*
> + * Securityfs exposure
> + */
> +struct memfile {
> + char *name;
> + void *addr;
> + size_t size;
> +};
> +
> +static struct memfile sl_evtlog = {"eventlog", 0, 0};

I don't think the 0 fields are necessary above, memset will zero
any fields not explicitly set. But if you want to go that way, then
I think the first one should be NULL, as the addr field is a pointer.

> +static void *txt_heap;
> +static struct txt_heap_event_log_pointer2_1_element __iomem *evtlog20;
> +static DEFINE_MUTEX(sl_evt_log_mutex);

> +static ssize_t sl_evtlog_read(struct file *file, char __user *buf,
> +   size_t count, loff_t *pos)
> +{
> + ssize_t size;
> +
> + if (!sl_evtlog.addr)
> + return 0;
> +
> + mutex_lock(_evt_log_mutex);
> + size = simple_read_from_buffer(buf, count, pos, sl_evtlog.addr,
> +sl_evtlog.size);
> + mutex_unlock(_evt_log_mutex);
> +
> + return size;
> +}
> +
> +static ssize_t sl_evtlog_write(struct file *file, const char __user *buf,
> + size_t datalen, loff_t *ppos)

nit: the line above doesn't align to the '(' on the line before that.

> +{
> + ssize_t result;
> + char *data;
> +
> + if (!sl_evtlog.addr)
> + return 0;
> +
> + /* No partial writes. */
> + result = -EINVAL;
> + if (*ppos != 0)
> + goto out;
> +
> + data = memdup_user(buf, datalen);
> + if (IS_ERR(data)) {
> + result = PTR_ERR(data);
> + goto out;
> + }
> +
> + mutex_lock(_evt_log_mutex);
> + if (evtlog20)
> + result = tpm20_log_event(evtlog20, sl_evtlog.addr,
> +  sl_evtlog.size, datalen, data);

Sparse says that the type of the first argument of tmp20_log_event is:

struct txt_heap_event_log_pointer2_1_element *

However, the type of evtlog20 is:

struct txt_heap_event_log_pointer2_1_element __iomem *

> + else
> + result = tpm12_log_event(sl_evtlog.addr, sl_evtlog.size,
> +  datalen, data);
> + mutex_unlock(_evt_log_mutex);
> +
> + kfree(data);
> +out:
> + return result;
> +}

...

> +static long slaunch_expose_securityfs(void)
> +{
> + long ret = 0;
> + int i;
> +
> + slaunch_dir = securityfs_create_dir("slaunch", NULL);
> + if (IS_ERR(slaunch_dir))
> + return PTR_ERR(slaunch_dir);
> +
> + if (slaunch_get_flags() & SL_FLAG_ARCH_TXT) {
> + txt_dir = securityfs_create_dir("txt", slaunch_dir);
> + if (IS_ERR(txt_dir)) {
> + ret = PTR_ERR(txt_dir);
> + goto remove_slaunch;
> + }
> +
> + for (i = 0; i < ARRAY_SIZE(sl_txt_files); i++) {
> + txt_entries[i] = securityfs_create_file(
> + sl_txt_files[i].name, 0440,
> + txt_dir, NULL,
> + sl_txt_files[i].fops);
> + if (IS_ERR(txt_entries[i])) {
> + ret = PTR_ERR(txt_entries[i]);
> + goto remove_files;
> + }
> + }
> +

nit: no blank line here.

> + }
> +
> + if (sl_evtlog.addr > 0) {

addr is a pointer. So perhaps:

if (sl_evtlog.addr) {

> + event_file = securityfs_create_file(
> + sl_evtlog.name, 0440,
> + slaunch_dir, NULL,
> + _evtlog_ops);
> + if (IS_ERR(event_file)) {
> + ret = PTR_ERR(event_file);
> + goto remove_files;
> + }
> + }
> +
> + return 0;
> +
> +remove_files:
> + if (slaunch_get_flags() & SL_FLAG_ARCH_TXT) {
> + while (--i >= 0)
> + securityfs_remove(txt_entries[i]);
> + securityfs_remove(txt_dir);
> + }
> +remove_slaunch:
> + securityfs_remove(slaunch_dir);
> +
> + return ret;
> +}

...

> +static void slaunch_intel_evtlog(void 

[PATCH v6 12/14] x86: Secure Launch late initcall platform module

2023-05-04 Thread Ross Philipson
From: "Daniel P. Smith" 

The Secure Launch platform module is a late init module. During the
init call, the TPM event log is read and measurements taken in the
early boot stub code are located. These measurements are extended
into the TPM PCRs using the mainline TPM kernel driver.

The platform module also registers the securityfs nodes to allow
access to TXT register fields on Intel along with the fetching of
and writing events to the late launch TPM log.

Signed-off-by: Daniel P. Smith 
Signed-off-by: garnetgrimm 
Signed-off-by: Ross Philipson 
---
 arch/x86/kernel/Makefile   |   1 +
 arch/x86/kernel/slmodule.c | 520 +
 2 files changed, 521 insertions(+)
 create mode 100644 arch/x86/kernel/slmodule.c

diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 3d2a33e..ee3fe300 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -73,6 +73,7 @@ obj-$(CONFIG_IA32_EMULATION)  += tls.o
 obj-y  += step.o
 obj-$(CONFIG_INTEL_TXT)+= tboot.o
 obj-$(CONFIG_SECURE_LAUNCH)+= slaunch.o
+obj-$(CONFIG_SECURE_LAUNCH)+= slmodule.o
 obj-$(CONFIG_ISA_DMA_API)  += i8237.o
 obj-y  += stacktrace.o
 obj-y  += cpu/
diff --git a/arch/x86/kernel/slmodule.c b/arch/x86/kernel/slmodule.c
new file mode 100644
index 000..70dcff5
--- /dev/null
+++ b/arch/x86/kernel/slmodule.c
@@ -0,0 +1,520 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Secure Launch late validation/setup, securityfs exposure and
+ * finalization support.
+ *
+ * Copyright (c) 2022 Apertus Solutions, LLC
+ * Copyright (c) 2021 Assured Information Security, Inc.
+ * Copyright (c) 2022, Oracle and/or its affiliates.
+ *
+ * Author(s):
+ * Daniel P. Smith 
+ * Garnet T. Grimm 
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define DECLARE_TXT_PUB_READ_U(size, fmt, msg_size)\
+static ssize_t txt_pub_read_u##size(unsigned int offset,   \
+   loff_t *read_offset,\
+   size_t read_len,\
+   char __user *buf)   \
+{  \
+   void __iomem *txt;  \
+   char msg_buffer[msg_size];  \
+   u##size reg_value = 0;  \
+   txt = ioremap(TXT_PUB_CONFIG_REGS_BASE, \
+   TXT_NR_CONFIG_PAGES * PAGE_SIZE);   \
+   if (!txt)   \
+   return -EFAULT; \
+   memcpy_fromio(_value, txt + offset, sizeof(u##size));   \
+   iounmap(txt);   \
+   snprintf(msg_buffer, msg_size, fmt, reg_value); \
+   return simple_read_from_buffer(buf, read_len, read_offset,  \
+   _buffer, msg_size); \
+}
+
+DECLARE_TXT_PUB_READ_U(8, "%#04x\n", 6);
+DECLARE_TXT_PUB_READ_U(32, "%#010x\n", 12);
+DECLARE_TXT_PUB_READ_U(64, "%#018llx\n", 20);
+
+#define DECLARE_TXT_FOPS(reg_name, reg_offset, reg_size)   \
+static ssize_t txt_##reg_name##_read(struct file *flip,
\
+   char __user *buf, size_t read_len, loff_t *read_offset) \
+{  \
+   return txt_pub_read_u##reg_size(reg_offset, read_offset,\
+   read_len, buf); \
+}  \
+static const struct file_operations reg_name##_ops = { \
+   .read = txt_##reg_name##_read,  \
+}
+
+DECLARE_TXT_FOPS(sts, TXT_CR_STS, 64);
+DECLARE_TXT_FOPS(ests, TXT_CR_ESTS, 8);
+DECLARE_TXT_FOPS(errorcode, TXT_CR_ERRORCODE, 32);
+DECLARE_TXT_FOPS(didvid, TXT_CR_DIDVID, 64);
+DECLARE_TXT_FOPS(e2sts, TXT_CR_E2STS, 64);
+DECLARE_TXT_FOPS(ver_emif, TXT_CR_VER_EMIF, 32);
+DECLARE_TXT_FOPS(scratchpad, TXT_CR_SCRATCHPAD, 64);
+
+/*
+ * Securityfs exposure
+ */
+struct memfile {
+   char *name;
+   void *addr;
+   size_t size;
+};
+
+static struct memfile sl_evtlog = {"eventlog", 0, 0};
+static void *txt_heap;
+static struct txt_heap_event_log_pointer2_1_element __iomem *evtlog20;
+static DEFINE_MUTEX(sl_evt_log_mutex);
+
+static ssize_t sl_evtlog_read(struct file *file, char __user *buf,
+ size_t count, loff_t *pos)
+{
+   ssize_t size;
+
+   if (!sl_evtlog.addr)
+