Skip to contentSkip to conversation

ima: export the measurement list when needed

RFC PATCH Superseded Janne Karhunen 3 participants 7 messages Dec 20, 2019 – Jan 1, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 ima: export the measurement list when needed

Some systems can end up carrying lots of entries in the ima measurement list. Since every entry is using a bit of kernel memory, add a new Kconfig variable to allow the sysadmin to define the maximum measurement list size and the location of the exported list.

The list is written out in append mode, so the system will keep writing new entries as long as it stays running or runs out of space. File is also automatically truncated on startup.

---
 security/integrity/ima/Kconfig     |  10 ++
 security/integrity/ima/ima.h       |   7 +-
 security/integrity/ima/ima_fs.c    | 188 +++++++++++++++++++++++++++++
 security/integrity/ima/ima_queue.c |   2 +-
 4 files changed, 202 insertions(+), 5 deletions(-)
security/integrity/ima/Kconfig +10 -0
index 897bafc59a33..3aefead28864 100644
@@ -310,3 +310,13 @@ config IMA_APPRAISE_SIGNED_INIT
 	default n
 	help
 	   This option requires user-space init to be signed.
+
+config IMA_MEASUREMENT_LIST_SIZE
+	int
+	depends on IMA
+	default 2000
+	help
+	   This option defines the maximum amount of entries in the
+	   measurement list. Once the limit is reached, the entire
+	   list is exported to a user defined file and the kernel
+	   list is freed.
security/integrity/ima/ima.h +3 -4
index 19769bf5f6ab..78f0b706848d 100644
@@ -151,20 +151,19 @@ int template_desc_init_fields(const char *template_fmt,
 struct ima_template_desc *ima_template_desc_current(void);
 struct ima_template_desc *lookup_template_desc(const char *name);
 bool ima_template_has_modsig(const struct ima_template_desc *ima_template);
+void ima_free_template_entry(struct ima_template_entry *entry);
 int ima_restore_measurement_entry(struct ima_template_entry *entry);
 int ima_restore_measurement_list(loff_t bufsize, void *buf);
 int ima_measurements_show(struct seq_file *m, void *v);
 unsigned long ima_get_binary_runtime_size(void);
 int ima_init_template(void);
 void ima_init_template_list(void);
+int ima_export_list(const char *from);
 int __init ima_init_digests(void);
 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
 			  void *lsm_data);
 
-/*
- * used to protect h_table and sha_table
- */
-extern spinlock_t ima_queue_lock;
+extern struct mutex ima_extend_list_mutex;
 
 struct ima_h_table {
 	atomic_long_t len;	/* number of stored measurements in the list */
security/integrity/ima/ima_fs.c +188 -0

221 lines not on this page · raw message

security/integrity/ima/ima_queue.c +1 -1
index 1ce8b1701566..77c538ec8474 100644
@@ -44,7 +44,7 @@ struct ima_h_table ima_htable = {
  * and extending the TPM PCR aggregate. Since tpm_extend can take
  * long (and the tpm driver uses a mutex), we can't use the spinlock.
  */
-static DEFINE_MUTEX(ima_extend_list_mutex);
+DEFINE_MUTEX(ima_extend_list_mutex);
 
 /* lookup up the digest value in the hash table, and return the entry */
 static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value,
Signature · 1 line
2.17.1
#1 lore raw
Quoted 5 lines · Janne Karhunen

Some systems can end up carrying lots of entries in the ima measurement list. Since every entry is using a bit of kernel memory, add a new Kconfig variable to allow the sysadmin to define the maximum measurement list size and the location of the exported list.

This patch now passes some basic test runs and looks to me it's doing things correctly, if others are willing to give it a spin. My basic test case for this is a simple bash for loop, creating thousands of files and using this trigger to free the list.

Once we have the workqueue, the list free job should probably be triggered from the ima_store_measurement() when the htable length says so. This way the list will always stay truncated to the right size and no-one has to poll.

When it comes to the oddity that kernel is creating files, the facts are:
1) the data cannot be kept in the memory forever,
2) the data cannot be thrown away.
So, what are the options. I didn't see any other obvious way. Luckily there was a ready API for the job.

--
Janne

maintainer
#2 lore raw
In reply to Janne Karhunen

[Cc'ing LSM mailing list for a wider audience]

Quoted 11 lines · Janne Karhunen

Some systems can end up carrying lots of entries in the ima measurement list. Since every entry is using a bit of kernel memory, add a new Kconfig variable to allow the sysadmin to define the maximum measurement list size and the location of the exported list.

The list is written out in append mode, so the system will keep writing new entries as long as it stays running or runs out of space. File is also automatically truncated on startup.

Continually adding new measurements, without limiting or removing the measurement list seems to becoming more of an issue.

From Dave Safford's TLV patch description[1]:
A second goal of the [TLV] patch set is to test the more radical idea of being able to copy the measurement list data out of the kernel. The data is verifiable with the TPM PCR value, and need not be kept in kernel memory. In some cases, this "memory leak" can grow large enough to cause issues, and this is a test of a potential way to solve that problem.

The TLV version automatically removed the measurement list the first time the measurement list was read, which sounded very odd to me.  In an offline discussion, Dave further clarified that reading the measurement list should be similar to how a trusted userspace application reads kernel messages.  The difference being kernel messages are stored in a circular buffer and may be dropped.  In the IMA measurement list case, the measurement list would grow until the trusted userspace application gets around to reading the measurement list.

Should the kernel be involved in writing the IMA measurement list to a file or, as Dave suggested, this should be delegated to a userspace application?

Mimi

[1] https://lore.kernel.org/linux-integrity/BCA04D5D9A3B764C9B7405BBA4
[email protected]/

#3 lore raw
Quoted 3 lines · Mimi Zohar

Should the kernel be involved in writing the IMA measurement list to a file or, as Dave suggested, this should be delegated to a userspace application?

That is a good question. I went this way as it did not feel right to me that the kernel would depend on periodic, reliable userspace functionality to stay running (we would have a circular dependency). The thing is, once the kernel starts to run low on memory, it may kill that periodic daemon flushing the data for reasons unrelated to IMA.

Signature · 1 line
Janne
#4 lore raw
Quoted 9 lines · Janne Karhunen
Quoted 3 lines

Should the kernel be involved in writing the IMA measurement list to a file or, as Dave suggested, this should be delegated to a userspace application?

That is a good question. I went this way as it did not feel right to me that the kernel would depend on periodic, reliable userspace functionality to stay running (we would have a circular dependency). The thing is, once the kernel starts to run low on memory, it may kill that periodic daemon flushing the data for reasons unrelated to IMA.

Besides the dependency, I think the requirement should be that we can survive the basic test of 'while true; do touch $RANDOM; done' at least until we run out of allocated diskspace. While arranging this with userspace flushers is not impossible, it is order of magnitude more complex to do correctly than just letting the kernel write the file. Even if it feels somewhat unorthodox.

Above patch survives that test case with 3 line addition via a workqueue. Once the admin points IMA to some mount, the above test case (while loop creating files full speed) will run a long, long time. Effectively this is really just kernel doing its own memory management as it should. Flush out the dirty pages you do not really need to stay running.

--
Janne

#5 lore raw
In reply to Janne Karhunen
Quoted 8 lines · Janne Karhunen
Quoted 4 lines · Mimi Zohar

Should the kernel be involved in writing the IMA measurement list to a
file or, as Dave suggested, this should be delegated to a userspace application?

Quoted 7 lines

That is a good question. I went this way as it did not feel right to me that the kernel would depend on periodic, reliable userspace functionality to stay running (we would have a circular dependency). The thing is, once the kernel starts to run low on memory, it may kill
that periodic daemon flushing the data for reasons unrelated to IMA.

I'm happy with either way (kernel writing, or userspace reading) the data, but with the v1 patch, there is no way for userspace to force that the list be flushed - it only flushes on full. I think it is important for userspace to be able to trigger a flush, such as just prior to a kexec, or prior to an attestation.

Perhaps you could simply remove the length test in ima_export_list(), and export anytime the filename is provided? This could simplify attestation clients, which could ask for different files each time (list.1, list.2...), for automatic log maintenance. Since the template format does not have sequence numbers, this would also help keep track which records have already been seen.

dave

#6 lore raw
Quoted 8 lines · On Tue, Dec 24, 2019 at 5:35 PM <[email protected]> wrote:
Quoted 7 lines

That is a good question. I went this way as it did not feel right to me that the kernel would depend on periodic, reliable userspace functionality to stay running (we would have a circular dependency). The thing is, once the kernel starts to run low on memory, it may kill
that periodic daemon flushing the data for reasons unrelated to IMA.

Quoted 5 lines

I'm happy with either way (kernel writing, or userspace reading) the data, but with the v1 patch, there is no way for userspace to force that the list be flushed - it only flushes on full. I think it is important for userspace to be able to trigger a flush, such as just prior to a kexec, or prior to an attestation.

Indeed, will add in v2.

Quoted 6 lines

Perhaps you could simply remove the length test in ima_export_list(), and export anytime the filename is provided? This could simplify attestation clients, which could ask for different files each time (list.1, list.2...), for automatic log maintenance. Since the template format does not have sequence numbers, this would also help keep track which records have already been seen.

Yes, will do something like this. Holidays cause some latency here, but I will send an update next week.

--
Janne