* [PATCH v2 0/1] Trim N entries of IMA event logs @ 2025-12-10 23:53 steven chen 2025-12-10 23:53 ` [PATCH V2 1/1] IMA event log trimming steven chen 0 siblings, 1 reply; 5+ messages in thread From: steven chen @ 2025-12-10 23:53 UTC (permalink / raw) To: linux-integrity Cc: zohar, roberto.sassu, dmitry.kasatkin, eric.snowberg, corbet, serge, paul, jmorris, linux-security-module, anirudhve, chenste, gregorylumen, nramas, sushring, linux-doc The Integrity Measurement Architecture (IMA) maintains a measurement list —a record of system events used for integrity verification. The IMA event logs are the entries within this measurement list, each representing a specific event or measurement that contributes to the system's integrity assessment. This update introduces the ability to trim, or remove, N entries from the current measurement list. Trimming involves deleting N entries from the list and clearing the corresponding entries from the hash table. This action atomically truncates the measurement list, ensuring that no new measurements can be added until the operation is complete. Importantly, only one writer can initiate this trimming process at a time, maintaining consistency and preventing race conditions. A userspace interface, ima_trim_log, has been provided for this purpose. By writing a number N to this interface, userspace can request the kernel to trim N entries from the IMA event logs. When this interface is read, it returns the number of entries trimmed during the last operation. This value is not preserved across kexec soft reboots, as it is not considered important information. To maintain a complete record, userspace is responsible for concatenating and storing the logs before initiating trimming. Userspace can then send the collected data to remote verifiers for validation. After receiving confirmation from the remote verifiers, userspace may instruct the kernel to proceed with trimming the IMA event logs accordingly. The primary benefit of this solution is the ability to free valuable kernel memory by delegating the task of reconstructing the full measurement list from log chunks to userspace. Trust is not required in userspace for the integrity of the measurement list, as its integrity is cryptographically protected by the Trusted Platform Module (TPM). Multiple readers are allowed to access the ima_trim_log interface concurrently, while only one writer can trigger log trimming at any time. During trimming, readers do not see the list and cannot access it while deletion is in progress, ensuring atomicity. Introduce the new kernel option ima_flush_htable to decide whether or not the digests of measurement entries are flushed from the hash table. (from reference [2]) The ima_measure_users counter (protected by the ima_measure_lock mutex) has been introduced to protect access to the measurement list part. The open method of all the measurement interfaces has been extended to allow only one writer at a time or, in alternative, multiple readers. The write permission is used to delete the measurements, the read permission to read them. Write requires also the CAP_SYS_ADMIN capability. (from reference [2]) New IMA log trim event is added when trimming finish. The time required for trimming is minimal, and IMA event logs are briefly on hold during this process, preventing read or add operations. This short interruption has no impact on the overall functionality of IMA. V1 of this series is available here[1] for reference. References: ----------- [1] [PATCH 0/1] Trim N entries of IMA event logs https://lore.kernel.org/linux-integrity/20251202232857.8211-1-chenste@linux.microsoft.com/T/#t [2] [RFC][PATCH] ima: Add support for staging measurements for deletion https://lore.kernel.org/linux-integrity/207fd6d7-53c-57bb-36d8-13a0902052d1@linux.microsoft.com/T/#t Change Log v2: - Incorporated feedback from the Roberto on v1 series. - Adapted code from Roberto's RFC [Reference 2] - Add IMA log trim event log to record trim event - Updated patch descriptions as necessary. steven chen (1): IMA event log trimming .../admin-guide/kernel-parameters.txt | 4 + security/integrity/ima/ima.h | 2 + security/integrity/ima/ima_fs.c | 175 +++++++++++++++++- security/integrity/ima/ima_queue.c | 64 +++++++ 4 files changed, 241 insertions(+), 4 deletions(-) -- 2.43.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH V2 1/1] IMA event log trimming 2025-12-10 23:53 [PATCH v2 0/1] Trim N entries of IMA event logs steven chen @ 2025-12-10 23:53 ` steven chen 2025-12-11 10:14 ` Roberto Sassu 2025-12-11 18:41 ` steven chen 0 siblings, 2 replies; 5+ messages in thread From: steven chen @ 2025-12-10 23:53 UTC (permalink / raw) To: linux-integrity Cc: zohar, roberto.sassu, dmitry.kasatkin, eric.snowberg, corbet, serge, paul, jmorris, linux-security-module, anirudhve, chenste, gregorylumen, nramas, sushring, linux-doc This patch is for trimming N entries of the IMA event logs. It will also cleaning the hash table if ima_flush_htable is set. It provides a userspace interface ima_trim_log that can be used to input number N to let kernel to trim N entries of IMA event logs. When read this interface, it returns number of entries trimmed last time. Signed-off-by: steven chen <chenste@linux.microsoft.com> --- .../admin-guide/kernel-parameters.txt | 4 + security/integrity/ima/ima.h | 2 + security/integrity/ima/ima_fs.c | 175 +++++++++++++++++- security/integrity/ima/ima_queue.c | 64 +++++++ 4 files changed, 241 insertions(+), 4 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index e92c0056e4e0..cd1a1d0bf0e2 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -2197,6 +2197,10 @@ Use the canonical format for the binary runtime measurements, instead of host native format. + ima_flush_htable [IMA] + Flush the measurement list hash table when trim all + or a part of it for deletion. + ima_hash= [IMA] Format: { md5 | sha1 | rmd160 | sha256 | sha384 | sha512 | ... } diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index e3d71d8d56e3..ab0e30ee25ea 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -246,8 +246,10 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key, #ifdef CONFIG_IMA_KEXEC void ima_measure_kexec_event(const char *event_name); +long ima_purge_event_log(long number_logs); #else static inline void ima_measure_kexec_event(const char *event_name) {} +static inline long ima_purge_event_log(long number_logs) { return 0; } #endif /* diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c index 87045b09f120..410f7d03c43f 100644 --- a/security/integrity/ima/ima_fs.c +++ b/security/integrity/ima/ima_fs.c @@ -21,6 +21,9 @@ #include <linux/rcupdate.h> #include <linux/parser.h> #include <linux/vmalloc.h> +#include <linux/ktime.h> +#include <linux/timekeeping.h> +#include <linux/ima.h> #include "ima.h" @@ -38,6 +41,14 @@ __setup("ima_canonical_fmt", default_canonical_fmt_setup); static int valid_policy = 1; +#define IMA_LOG_TRIM_REQ_LENGTH 11 +#define IMA_LOG_TRIM_EVENT_LEN 256 + +static long trimcount; +/* mutex protects atomicity of trimming measurement list requests */ +static DEFINE_MUTEX(ima_measure_lock); +static long ima_measure_users; + static ssize_t ima_show_htable_value(char __user *buf, size_t count, loff_t *ppos, atomic_long_t *val) { @@ -202,16 +213,65 @@ static const struct seq_operations ima_measurments_seqops = { .show = ima_measurements_show }; +static int _ima_measurements_open(struct inode *inode, struct file *file, + const struct seq_operations *seq_ops) +{ + bool write = !!(file->f_mode & FMODE_WRITE); + int ret; + + if (write && !capable(CAP_SYS_ADMIN)) + return -EPERM; + + mutex_lock(&ima_measure_lock); + if ((write && ima_measure_users != 0) || + (!write && ima_measure_users < 0)) { + mutex_unlock(&ima_measure_lock); + return -EBUSY; + } + + ret = seq_open(file, seq_ops); + if (ret < 0) { + mutex_unlock(&ima_measure_lock); + return ret; + } + + if (write) + ima_measure_users--; + else + ima_measure_users++; + + mutex_unlock(&ima_measure_lock); + return ret; +} + static int ima_measurements_open(struct inode *inode, struct file *file) { - return seq_open(file, &ima_measurments_seqops); + return _ima_measurements_open(inode, file, &ima_measurments_seqops); +} + +static int ima_measurements_release(struct inode *inode, struct file *file) +{ + bool write = !!(file->f_mode & FMODE_WRITE); + int ret; + + mutex_lock(&ima_measure_lock); + ret = seq_release(inode, file); + if (!ret) { + if (write) + ima_measure_users++; + else + ima_measure_users--; + } + + mutex_unlock(&ima_measure_lock); + return ret; } static const struct file_operations ima_measurements_ops = { .open = ima_measurements_open, .read = seq_read, .llseek = seq_lseek, - .release = seq_release, + .release = ima_measurements_release, }; void ima_print_digest(struct seq_file *m, u8 *digest, u32 size) @@ -279,14 +339,111 @@ static const struct seq_operations ima_ascii_measurements_seqops = { static int ima_ascii_measurements_open(struct inode *inode, struct file *file) { - return seq_open(file, &ima_ascii_measurements_seqops); + return _ima_measurements_open(inode, file, &ima_ascii_measurements_seqops); } static const struct file_operations ima_ascii_measurements_ops = { .open = ima_ascii_measurements_open, .read = seq_read, .llseek = seq_lseek, - .release = seq_release, + .release = ima_measurements_release, +}; + +static void ima_measure_trim_event(const long number_logs) +{ + char ima_log_trim_event[IMA_LOG_TRIM_EVENT_LEN]; + struct timespec64 ts; + u64 time_ns; + int n; + + ktime_get_real_ts64(&ts); + time_ns = (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec; + n = scnprintf(ima_log_trim_event, IMA_LOG_TRIM_EVENT_LEN, + "time=%llu; log trim this time=%lu;", + time_ns, number_logs); + + ima_measure_critical_data("ima_log_trim", "trim ima event logs", ima_log_trim_event, n, false, NULL, 0); +} + +static int ima_log_trim_open(struct inode *inode, struct file *file) +{ + bool write = !!(file->f_mode & FMODE_WRITE); + + if (!write && capable(CAP_SYS_ADMIN)) + return 0; + else if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + return _ima_measurements_open(inode, file, &ima_measurments_seqops); +} + +static ssize_t ima_log_trim_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) +{ + char tmpbuf[IMA_LOG_TRIM_REQ_LENGTH]; /* greater than largest 'long' string value */ + ssize_t len; + + len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", trimcount); + return simple_read_from_buffer(buf, size, ppos, tmpbuf, len); +} + +static ssize_t ima_log_trim_write(struct file *file, + const char __user *buf, size_t datalen, loff_t *ppos) +{ + unsigned char req[IMA_LOG_TRIM_REQ_LENGTH]; + long count, n; + int ret; + + if (*ppos > 0 || datalen > IMA_LOG_TRIM_REQ_LENGTH || datalen < 2) { + ret = -EINVAL; + goto out; + } + + n = (int)datalen; + + ret = copy_from_user(req, buf, datalen); + if (ret < 0) + goto out; + + count = 0; + for (int i = 0; i < n; ++i) { + if (req[i] < '0' || req[i] > '9') { + ret = -EINVAL; + goto out; + } + count = count * 10 + req[i] - '0'; + } + ret = ima_purge_event_log(count); + + if (ret < 0) + goto out; + + trimcount = ret; + + if (trimcount > 0) + ima_measure_trim_event(trimcount); + + ret = datalen; +out: + return ret; +} + +static int ima_log_trim_release(struct inode *inode, struct file *file) +{ + bool write = !!(file->f_mode & FMODE_WRITE); + if (!write && capable(CAP_SYS_ADMIN)) + return 0; + else if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + return ima_measurements_release(inode, file); +} + +static const struct file_operations ima_log_trim_ops = { + .open = ima_log_trim_open, + .read = ima_log_trim_read, + .write = ima_log_trim_write, + .llseek = generic_file_llseek, + .release = ima_log_trim_release }; static ssize_t ima_read_policy(char *path) @@ -528,6 +685,16 @@ int __init ima_fs_init(void) goto out; } + dentry = securityfs_create_file("ima_trim_log", + S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP, + ima_dir, NULL, &ima_log_trim_ops); + if (IS_ERR(dentry)) { + ret = PTR_ERR(dentry); + goto out; + } + + trimcount = 0; + dentry = securityfs_create_file("runtime_measurements_count", S_IRUSR | S_IRGRP, ima_dir, NULL, &ima_measurements_count_ops); diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c index 590637e81ad1..77ab52469727 100644 --- a/security/integrity/ima/ima_queue.c +++ b/security/integrity/ima/ima_queue.c @@ -22,6 +22,14 @@ #define AUDIT_CAUSE_LEN_MAX 32 +bool ima_flush_htable; +static int __init ima_flush_htable_setup(char *str) +{ + ima_flush_htable = true; + return 1; +} +__setup("ima_flush_htable", ima_flush_htable_setup); + /* pre-allocated array of tpm_digest structures to extend a PCR */ static struct tpm_digest *digests; @@ -220,6 +228,62 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation, return result; } +/* Delete the IMA event logs */ +long ima_purge_event_log(long number_logs) +{ + struct ima_queue_entry *qe, *qe_tmp; + LIST_HEAD(ima_measurements_staged); + unsigned int i; + long cur = number_logs; + + if (number_logs <= 0) + return number_logs; + + mutex_lock(&ima_extend_list_mutex); + + + list_for_each_entry(qe, &ima_measurements, later) { + if (--number_logs == 0) + break; + } + + if (number_logs > 0) { + mutex_unlock(&ima_extend_list_mutex); + return -ENOENT; + } + + __list_cut_position(&ima_measurements_staged, &ima_measurements, + &qe->later); + atomic_long_sub(cur, &ima_htable.len); + + if (!IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE) && ima_flush_htable) { + list_for_each_entry(qe, &ima_measurements_staged, later) + /* It can race with ima_lookup_digest_entry(). */ + hlist_del_rcu(&qe->hnext); + } + + mutex_unlock(&ima_extend_list_mutex); + + + list_for_each_entry_safe(qe, qe_tmp, &ima_measurements_staged, later) { + for (i = 0; i < qe->entry->template_desc->num_fields; i++) { + kfree(qe->entry->template_data[i].data); + qe->entry->template_data[i].data = NULL; + qe->entry->template_data[i].len = 0; + } + + list_del(&qe->later); + + if (ima_flush_htable) { + kfree(qe->entry->digests); + kfree(qe->entry); + kfree(qe); + } + } + + return cur; +} + int ima_restore_measurement_entry(struct ima_template_entry *entry) { int result = 0; -- 2.43.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH V2 1/1] IMA event log trimming 2025-12-10 23:53 ` [PATCH V2 1/1] IMA event log trimming steven chen @ 2025-12-11 10:14 ` Roberto Sassu 2025-12-11 18:20 ` steven chen 2025-12-11 18:41 ` steven chen 1 sibling, 1 reply; 5+ messages in thread From: Roberto Sassu @ 2025-12-11 10:14 UTC (permalink / raw) To: steven chen, linux-integrity Cc: zohar, roberto.sassu, dmitry.kasatkin, eric.snowberg, corbet, serge, paul, jmorris, linux-security-module, anirudhve, gregorylumen, nramas, sushring, linux-doc On Wed, 2025-12-10 at 15:53 -0800, steven chen wrote: > This patch is for trimming N entries of the IMA event logs. It will also > cleaning the hash table if ima_flush_htable is set. > > It provides a userspace interface ima_trim_log that can be used to input > number N to let kernel to trim N entries of IMA event logs. When read > this interface, it returns number of entries trimmed last time. I was trying to find a common solution for both approaches, and instead you took part of my code and removed the part that you didn't like. This is not nice. Let's do the following. Please review my patch, and check if it makes you achieve your goal. Then, please send follow-up patches with the additional functionality you need (e.g. the new critical data measurement entry). Thanks Roberto > Signed-off-by: steven chen <chenste@linux.microsoft.com> > --- > .../admin-guide/kernel-parameters.txt | 4 + > security/integrity/ima/ima.h | 2 + > security/integrity/ima/ima_fs.c | 175 +++++++++++++++++- > security/integrity/ima/ima_queue.c | 64 +++++++ > 4 files changed, 241 insertions(+), 4 deletions(-) > > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt > index e92c0056e4e0..cd1a1d0bf0e2 100644 > --- a/Documentation/admin-guide/kernel-parameters.txt > +++ b/Documentation/admin-guide/kernel-parameters.txt > @@ -2197,6 +2197,10 @@ > Use the canonical format for the binary runtime > measurements, instead of host native format. > > + ima_flush_htable [IMA] > + Flush the measurement list hash table when trim all > + or a part of it for deletion. > + > ima_hash= [IMA] > Format: { md5 | sha1 | rmd160 | sha256 | sha384 > | sha512 | ... } > diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h > index e3d71d8d56e3..ab0e30ee25ea 100644 > --- a/security/integrity/ima/ima.h > +++ b/security/integrity/ima/ima.h > @@ -246,8 +246,10 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key, > > #ifdef CONFIG_IMA_KEXEC > void ima_measure_kexec_event(const char *event_name); > +long ima_purge_event_log(long number_logs); > #else > static inline void ima_measure_kexec_event(const char *event_name) {} > +static inline long ima_purge_event_log(long number_logs) { return 0; } > #endif > > /* > diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c > index 87045b09f120..410f7d03c43f 100644 > --- a/security/integrity/ima/ima_fs.c > +++ b/security/integrity/ima/ima_fs.c > @@ -21,6 +21,9 @@ > #include <linux/rcupdate.h> > #include <linux/parser.h> > #include <linux/vmalloc.h> > +#include <linux/ktime.h> > +#include <linux/timekeeping.h> > +#include <linux/ima.h> > > #include "ima.h" > > @@ -38,6 +41,14 @@ __setup("ima_canonical_fmt", default_canonical_fmt_setup); > > static int valid_policy = 1; > > +#define IMA_LOG_TRIM_REQ_LENGTH 11 > +#define IMA_LOG_TRIM_EVENT_LEN 256 > + > +static long trimcount; > +/* mutex protects atomicity of trimming measurement list requests */ > +static DEFINE_MUTEX(ima_measure_lock); > +static long ima_measure_users; > + > static ssize_t ima_show_htable_value(char __user *buf, size_t count, > loff_t *ppos, atomic_long_t *val) > { > @@ -202,16 +213,65 @@ static const struct seq_operations ima_measurments_seqops = { > .show = ima_measurements_show > }; > > +static int _ima_measurements_open(struct inode *inode, struct file *file, > + const struct seq_operations *seq_ops) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + int ret; > + > + if (write && !capable(CAP_SYS_ADMIN)) > + return -EPERM; > + > + mutex_lock(&ima_measure_lock); > + if ((write && ima_measure_users != 0) || > + (!write && ima_measure_users < 0)) { > + mutex_unlock(&ima_measure_lock); > + return -EBUSY; > + } > + > + ret = seq_open(file, seq_ops); > + if (ret < 0) { > + mutex_unlock(&ima_measure_lock); > + return ret; > + } > + > + if (write) > + ima_measure_users--; > + else > + ima_measure_users++; > + > + mutex_unlock(&ima_measure_lock); > + return ret; > +} > + > static int ima_measurements_open(struct inode *inode, struct file *file) > { > - return seq_open(file, &ima_measurments_seqops); > + return _ima_measurements_open(inode, file, &ima_measurments_seqops); > +} > + > +static int ima_measurements_release(struct inode *inode, struct file *file) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + int ret; > + > + mutex_lock(&ima_measure_lock); > + ret = seq_release(inode, file); > + if (!ret) { > + if (write) > + ima_measure_users++; > + else > + ima_measure_users--; > + } > + > + mutex_unlock(&ima_measure_lock); > + return ret; > } > > static const struct file_operations ima_measurements_ops = { > .open = ima_measurements_open, > .read = seq_read, > .llseek = seq_lseek, > - .release = seq_release, > + .release = ima_measurements_release, > }; > > void ima_print_digest(struct seq_file *m, u8 *digest, u32 size) > @@ -279,14 +339,111 @@ static const struct seq_operations ima_ascii_measurements_seqops = { > > static int ima_ascii_measurements_open(struct inode *inode, struct file *file) > { > - return seq_open(file, &ima_ascii_measurements_seqops); > + return _ima_measurements_open(inode, file, &ima_ascii_measurements_seqops); > } > > static const struct file_operations ima_ascii_measurements_ops = { > .open = ima_ascii_measurements_open, > .read = seq_read, > .llseek = seq_lseek, > - .release = seq_release, > + .release = ima_measurements_release, > +}; > + > +static void ima_measure_trim_event(const long number_logs) > +{ > + char ima_log_trim_event[IMA_LOG_TRIM_EVENT_LEN]; > + struct timespec64 ts; > + u64 time_ns; > + int n; > + > + ktime_get_real_ts64(&ts); > + time_ns = (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec; > + n = scnprintf(ima_log_trim_event, IMA_LOG_TRIM_EVENT_LEN, > + "time=%llu; log trim this time=%lu;", > + time_ns, number_logs); > + > + ima_measure_critical_data("ima_log_trim", "trim ima event logs", ima_log_trim_event, n, false, NULL, 0); > +} > + > +static int ima_log_trim_open(struct inode *inode, struct file *file) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + > + if (!write && capable(CAP_SYS_ADMIN)) > + return 0; > + else if (!capable(CAP_SYS_ADMIN)) > + return -EPERM; > + > + return _ima_measurements_open(inode, file, &ima_measurments_seqops); > +} > + > +static ssize_t ima_log_trim_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) > +{ > + char tmpbuf[IMA_LOG_TRIM_REQ_LENGTH]; /* greater than largest 'long' string value */ > + ssize_t len; > + > + len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", trimcount); > + return simple_read_from_buffer(buf, size, ppos, tmpbuf, len); > +} > + > +static ssize_t ima_log_trim_write(struct file *file, > + const char __user *buf, size_t datalen, loff_t *ppos) > +{ > + unsigned char req[IMA_LOG_TRIM_REQ_LENGTH]; > + long count, n; > + int ret; > + > + if (*ppos > 0 || datalen > IMA_LOG_TRIM_REQ_LENGTH || datalen < 2) { > + ret = -EINVAL; > + goto out; > + } > + > + n = (int)datalen; > + > + ret = copy_from_user(req, buf, datalen); > + if (ret < 0) > + goto out; > + > + count = 0; > + for (int i = 0; i < n; ++i) { > + if (req[i] < '0' || req[i] > '9') { > + ret = -EINVAL; > + goto out; > + } > + count = count * 10 + req[i] - '0'; > + } > + ret = ima_purge_event_log(count); > + > + if (ret < 0) > + goto out; > + > + trimcount = ret; > + > + if (trimcount > 0) > + ima_measure_trim_event(trimcount); > + > + ret = datalen; > +out: > + return ret; > +} > + > +static int ima_log_trim_release(struct inode *inode, struct file *file) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + if (!write && capable(CAP_SYS_ADMIN)) > + return 0; > + else if (!capable(CAP_SYS_ADMIN)) > + return -EPERM; > + > + return ima_measurements_release(inode, file); > +} > + > +static const struct file_operations ima_log_trim_ops = { > + .open = ima_log_trim_open, > + .read = ima_log_trim_read, > + .write = ima_log_trim_write, > + .llseek = generic_file_llseek, > + .release = ima_log_trim_release > }; > > static ssize_t ima_read_policy(char *path) > @@ -528,6 +685,16 @@ int __init ima_fs_init(void) > goto out; > } > > + dentry = securityfs_create_file("ima_trim_log", > + S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP, > + ima_dir, NULL, &ima_log_trim_ops); > + if (IS_ERR(dentry)) { > + ret = PTR_ERR(dentry); > + goto out; > + } > + > + trimcount = 0; > + > dentry = securityfs_create_file("runtime_measurements_count", > S_IRUSR | S_IRGRP, ima_dir, NULL, > &ima_measurements_count_ops); > diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c > index 590637e81ad1..77ab52469727 100644 > --- a/security/integrity/ima/ima_queue.c > +++ b/security/integrity/ima/ima_queue.c > @@ -22,6 +22,14 @@ > > #define AUDIT_CAUSE_LEN_MAX 32 > > +bool ima_flush_htable; > +static int __init ima_flush_htable_setup(char *str) > +{ > + ima_flush_htable = true; > + return 1; > +} > +__setup("ima_flush_htable", ima_flush_htable_setup); > + > /* pre-allocated array of tpm_digest structures to extend a PCR */ > static struct tpm_digest *digests; > > @@ -220,6 +228,62 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation, > return result; > } > > +/* Delete the IMA event logs */ > +long ima_purge_event_log(long number_logs) > +{ > + struct ima_queue_entry *qe, *qe_tmp; > + LIST_HEAD(ima_measurements_staged); > + unsigned int i; > + long cur = number_logs; > + > + if (number_logs <= 0) > + return number_logs; > + > + mutex_lock(&ima_extend_list_mutex); > + > + > + list_for_each_entry(qe, &ima_measurements, later) { > + if (--number_logs == 0) > + break; > + } > + > + if (number_logs > 0) { > + mutex_unlock(&ima_extend_list_mutex); > + return -ENOENT; > + } > + > + __list_cut_position(&ima_measurements_staged, &ima_measurements, > + &qe->later); > + atomic_long_sub(cur, &ima_htable.len); > + > + if (!IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE) && ima_flush_htable) { > + list_for_each_entry(qe, &ima_measurements_staged, later) > + /* It can race with ima_lookup_digest_entry(). */ > + hlist_del_rcu(&qe->hnext); > + } > + > + mutex_unlock(&ima_extend_list_mutex); > + > + > + list_for_each_entry_safe(qe, qe_tmp, &ima_measurements_staged, later) { > + for (i = 0; i < qe->entry->template_desc->num_fields; i++) { > + kfree(qe->entry->template_data[i].data); > + qe->entry->template_data[i].data = NULL; > + qe->entry->template_data[i].len = 0; > + } > + > + list_del(&qe->later); > + > + if (ima_flush_htable) { > + kfree(qe->entry->digests); > + kfree(qe->entry); > + kfree(qe); > + } > + } > + > + return cur; > +} > + > int ima_restore_measurement_entry(struct ima_template_entry *entry) > { > int result = 0; ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH V2 1/1] IMA event log trimming 2025-12-11 10:14 ` Roberto Sassu @ 2025-12-11 18:20 ` steven chen 0 siblings, 0 replies; 5+ messages in thread From: steven chen @ 2025-12-11 18:20 UTC (permalink / raw) To: Roberto Sassu, linux-integrity Cc: zohar, roberto.sassu, dmitry.kasatkin, eric.snowberg, corbet, serge, paul, jmorris, linux-security-module, anirudhve, gregorylumen, nramas, sushring, linux-doc, steven chen On 12/11/2025 2:14 AM, Roberto Sassu wrote: > On Wed, 2025-12-10 at 15:53 -0800, steven chen wrote: >> This patch is for trimming N entries of the IMA event logs. It will also >> cleaning the hash table if ima_flush_htable is set. >> >> It provides a userspace interface ima_trim_log that can be used to input >> number N to let kernel to trim N entries of IMA event logs. When read >> this interface, it returns number of entries trimmed last time. > I was trying to find a common solution for both approaches, and instead > you took part of my code and removed the part that you didn't like. > This is not nice. Hi Roberto, The most important is one step trimming N entries and the N come from user space. Everything else are helping this target. We need to bring the best to the open source community and I acknowledged the source. Thanks, Steven > Let's do the following. Please review my patch, and check if it makes > you achieve your goal. Then, please send follow-up patches with the > additional functionality you need (e.g. the new critical data > measurement entry). > > Thanks > > Roberto > >> Signed-off-by: steven chen <chenste@linux.microsoft.com> >> --- >> .../admin-guide/kernel-parameters.txt | 4 + >> security/integrity/ima/ima.h | 2 + >> security/integrity/ima/ima_fs.c | 175 +++++++++++++++++- >> security/integrity/ima/ima_queue.c | 64 +++++++ >> 4 files changed, 241 insertions(+), 4 deletions(-) >> >> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt >> index e92c0056e4e0..cd1a1d0bf0e2 100644 >> --- a/Documentation/admin-guide/kernel-parameters.txt >> +++ b/Documentation/admin-guide/kernel-parameters.txt >> @@ -2197,6 +2197,10 @@ >> Use the canonical format for the binary runtime >> measurements, instead of host native format. >> >> + ima_flush_htable [IMA] >> + Flush the measurement list hash table when trim all >> + or a part of it for deletion. >> + >> ima_hash= [IMA] >> Format: { md5 | sha1 | rmd160 | sha256 | sha384 >> | sha512 | ... } >> diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h >> index e3d71d8d56e3..ab0e30ee25ea 100644 >> --- a/security/integrity/ima/ima.h >> +++ b/security/integrity/ima/ima.h >> @@ -246,8 +246,10 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key, >> >> #ifdef CONFIG_IMA_KEXEC >> void ima_measure_kexec_event(const char *event_name); >> +long ima_purge_event_log(long number_logs); >> #else >> static inline void ima_measure_kexec_event(const char *event_name) {} >> +static inline long ima_purge_event_log(long number_logs) { return 0; } >> #endif >> >> /* >> diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c >> index 87045b09f120..410f7d03c43f 100644 >> --- a/security/integrity/ima/ima_fs.c >> +++ b/security/integrity/ima/ima_fs.c >> @@ -21,6 +21,9 @@ >> #include <linux/rcupdate.h> >> #include <linux/parser.h> >> #include <linux/vmalloc.h> >> +#include <linux/ktime.h> >> +#include <linux/timekeeping.h> >> +#include <linux/ima.h> >> >> #include "ima.h" >> >> @@ -38,6 +41,14 @@ __setup("ima_canonical_fmt", default_canonical_fmt_setup); >> >> static int valid_policy = 1; >> >> +#define IMA_LOG_TRIM_REQ_LENGTH 11 >> +#define IMA_LOG_TRIM_EVENT_LEN 256 >> + >> +static long trimcount; >> +/* mutex protects atomicity of trimming measurement list requests */ >> +static DEFINE_MUTEX(ima_measure_lock); >> +static long ima_measure_users; >> + >> static ssize_t ima_show_htable_value(char __user *buf, size_t count, >> loff_t *ppos, atomic_long_t *val) >> { >> @@ -202,16 +213,65 @@ static const struct seq_operations ima_measurments_seqops = { >> .show = ima_measurements_show >> }; >> >> +static int _ima_measurements_open(struct inode *inode, struct file *file, >> + const struct seq_operations *seq_ops) >> +{ >> + bool write = !!(file->f_mode & FMODE_WRITE); >> + int ret; >> + >> + if (write && !capable(CAP_SYS_ADMIN)) >> + return -EPERM; >> + >> + mutex_lock(&ima_measure_lock); >> + if ((write && ima_measure_users != 0) || >> + (!write && ima_measure_users < 0)) { >> + mutex_unlock(&ima_measure_lock); >> + return -EBUSY; >> + } >> + >> + ret = seq_open(file, seq_ops); >> + if (ret < 0) { >> + mutex_unlock(&ima_measure_lock); >> + return ret; >> + } >> + >> + if (write) >> + ima_measure_users--; >> + else >> + ima_measure_users++; >> + >> + mutex_unlock(&ima_measure_lock); >> + return ret; >> +} >> + >> static int ima_measurements_open(struct inode *inode, struct file *file) >> { >> - return seq_open(file, &ima_measurments_seqops); >> + return _ima_measurements_open(inode, file, &ima_measurments_seqops); >> +} >> + >> +static int ima_measurements_release(struct inode *inode, struct file *file) >> +{ >> + bool write = !!(file->f_mode & FMODE_WRITE); >> + int ret; >> + >> + mutex_lock(&ima_measure_lock); >> + ret = seq_release(inode, file); >> + if (!ret) { >> + if (write) >> + ima_measure_users++; >> + else >> + ima_measure_users--; >> + } >> + >> + mutex_unlock(&ima_measure_lock); >> + return ret; >> } >> >> static const struct file_operations ima_measurements_ops = { >> .open = ima_measurements_open, >> .read = seq_read, >> .llseek = seq_lseek, >> - .release = seq_release, >> + .release = ima_measurements_release, >> }; >> >> void ima_print_digest(struct seq_file *m, u8 *digest, u32 size) >> @@ -279,14 +339,111 @@ static const struct seq_operations ima_ascii_measurements_seqops = { >> >> static int ima_ascii_measurements_open(struct inode *inode, struct file *file) >> { >> - return seq_open(file, &ima_ascii_measurements_seqops); >> + return _ima_measurements_open(inode, file, &ima_ascii_measurements_seqops); >> } >> >> static const struct file_operations ima_ascii_measurements_ops = { >> .open = ima_ascii_measurements_open, >> .read = seq_read, >> .llseek = seq_lseek, >> - .release = seq_release, >> + .release = ima_measurements_release, >> +}; >> + >> +static void ima_measure_trim_event(const long number_logs) >> +{ >> + char ima_log_trim_event[IMA_LOG_TRIM_EVENT_LEN]; >> + struct timespec64 ts; >> + u64 time_ns; >> + int n; >> + >> + ktime_get_real_ts64(&ts); >> + time_ns = (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec; >> + n = scnprintf(ima_log_trim_event, IMA_LOG_TRIM_EVENT_LEN, >> + "time=%llu; log trim this time=%lu;", >> + time_ns, number_logs); >> + >> + ima_measure_critical_data("ima_log_trim", "trim ima event logs", ima_log_trim_event, n, false, NULL, 0); >> +} >> + >> +static int ima_log_trim_open(struct inode *inode, struct file *file) >> +{ >> + bool write = !!(file->f_mode & FMODE_WRITE); >> + >> + if (!write && capable(CAP_SYS_ADMIN)) >> + return 0; >> + else if (!capable(CAP_SYS_ADMIN)) >> + return -EPERM; >> + >> + return _ima_measurements_open(inode, file, &ima_measurments_seqops); >> +} >> + >> +static ssize_t ima_log_trim_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) >> +{ >> + char tmpbuf[IMA_LOG_TRIM_REQ_LENGTH]; /* greater than largest 'long' string value */ >> + ssize_t len; >> + >> + len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", trimcount); >> + return simple_read_from_buffer(buf, size, ppos, tmpbuf, len); >> +} >> + >> +static ssize_t ima_log_trim_write(struct file *file, >> + const char __user *buf, size_t datalen, loff_t *ppos) >> +{ >> + unsigned char req[IMA_LOG_TRIM_REQ_LENGTH]; >> + long count, n; >> + int ret; >> + >> + if (*ppos > 0 || datalen > IMA_LOG_TRIM_REQ_LENGTH || datalen < 2) { >> + ret = -EINVAL; >> + goto out; >> + } >> + >> + n = (int)datalen; >> + >> + ret = copy_from_user(req, buf, datalen); >> + if (ret < 0) >> + goto out; >> + >> + count = 0; >> + for (int i = 0; i < n; ++i) { >> + if (req[i] < '0' || req[i] > '9') { >> + ret = -EINVAL; >> + goto out; >> + } >> + count = count * 10 + req[i] - '0'; >> + } >> + ret = ima_purge_event_log(count); >> + >> + if (ret < 0) >> + goto out; >> + >> + trimcount = ret; >> + >> + if (trimcount > 0) >> + ima_measure_trim_event(trimcount); >> + >> + ret = datalen; >> +out: >> + return ret; >> +} >> + >> +static int ima_log_trim_release(struct inode *inode, struct file *file) >> +{ >> + bool write = !!(file->f_mode & FMODE_WRITE); >> + if (!write && capable(CAP_SYS_ADMIN)) >> + return 0; >> + else if (!capable(CAP_SYS_ADMIN)) >> + return -EPERM; >> + >> + return ima_measurements_release(inode, file); >> +} >> + >> +static const struct file_operations ima_log_trim_ops = { >> + .open = ima_log_trim_open, >> + .read = ima_log_trim_read, >> + .write = ima_log_trim_write, >> + .llseek = generic_file_llseek, >> + .release = ima_log_trim_release >> }; >> >> static ssize_t ima_read_policy(char *path) >> @@ -528,6 +685,16 @@ int __init ima_fs_init(void) >> goto out; >> } >> >> + dentry = securityfs_create_file("ima_trim_log", >> + S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP, >> + ima_dir, NULL, &ima_log_trim_ops); >> + if (IS_ERR(dentry)) { >> + ret = PTR_ERR(dentry); >> + goto out; >> + } >> + >> + trimcount = 0; >> + >> dentry = securityfs_create_file("runtime_measurements_count", >> S_IRUSR | S_IRGRP, ima_dir, NULL, >> &ima_measurements_count_ops); >> diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c >> index 590637e81ad1..77ab52469727 100644 >> --- a/security/integrity/ima/ima_queue.c >> +++ b/security/integrity/ima/ima_queue.c >> @@ -22,6 +22,14 @@ >> >> #define AUDIT_CAUSE_LEN_MAX 32 >> >> +bool ima_flush_htable; >> +static int __init ima_flush_htable_setup(char *str) >> +{ >> + ima_flush_htable = true; >> + return 1; >> +} >> +__setup("ima_flush_htable", ima_flush_htable_setup); >> + >> /* pre-allocated array of tpm_digest structures to extend a PCR */ >> static struct tpm_digest *digests; >> >> @@ -220,6 +228,62 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation, >> return result; >> } >> >> +/* Delete the IMA event logs */ >> +long ima_purge_event_log(long number_logs) >> +{ >> + struct ima_queue_entry *qe, *qe_tmp; >> + LIST_HEAD(ima_measurements_staged); >> + unsigned int i; >> + long cur = number_logs; >> + >> + if (number_logs <= 0) >> + return number_logs; >> + >> + mutex_lock(&ima_extend_list_mutex); >> + >> + >> + list_for_each_entry(qe, &ima_measurements, later) { >> + if (--number_logs == 0) >> + break; >> + } >> + >> + if (number_logs > 0) { >> + mutex_unlock(&ima_extend_list_mutex); >> + return -ENOENT; >> + } >> + >> + __list_cut_position(&ima_measurements_staged, &ima_measurements, >> + &qe->later); >> + atomic_long_sub(cur, &ima_htable.len); >> + >> + if (!IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE) && ima_flush_htable) { >> + list_for_each_entry(qe, &ima_measurements_staged, later) >> + /* It can race with ima_lookup_digest_entry(). */ >> + hlist_del_rcu(&qe->hnext); >> + } >> + >> + mutex_unlock(&ima_extend_list_mutex); >> + >> + >> + list_for_each_entry_safe(qe, qe_tmp, &ima_measurements_staged, later) { >> + for (i = 0; i < qe->entry->template_desc->num_fields; i++) { >> + kfree(qe->entry->template_data[i].data); >> + qe->entry->template_data[i].data = NULL; >> + qe->entry->template_data[i].len = 0; >> + } >> + >> + list_del(&qe->later); >> + >> + if (ima_flush_htable) { >> + kfree(qe->entry->digests); >> + kfree(qe->entry); >> + kfree(qe); >> + } >> + } >> + >> + return cur; >> +} >> + >> int ima_restore_measurement_entry(struct ima_template_entry *entry) >> { >> int result = 0; ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH V2 1/1] IMA event log trimming 2025-12-10 23:53 ` [PATCH V2 1/1] IMA event log trimming steven chen 2025-12-11 10:14 ` Roberto Sassu @ 2025-12-11 18:41 ` steven chen 1 sibling, 0 replies; 5+ messages in thread From: steven chen @ 2025-12-11 18:41 UTC (permalink / raw) To: linux-integrity Cc: zohar, roberto.sassu, dmitry.kasatkin, eric.snowberg, corbet, serge, paul, jmorris, linux-security-module, anirudhve, gregorylumen, nramas, sushring, linux-doc, steven chen On 12/10/2025 3:53 PM, steven chen wrote: > This patch is for trimming N entries of the IMA event logs. It will also > cleaning the hash table if ima_flush_htable is set. > > It provides a userspace interface ima_trim_log that can be used to input > number N to let kernel to trim N entries of IMA event logs. When read > this interface, it returns number of entries trimmed last time. > > Signed-off-by: steven chen <chenste@linux.microsoft.com> > --- > .../admin-guide/kernel-parameters.txt | 4 + > security/integrity/ima/ima.h | 2 + > security/integrity/ima/ima_fs.c | 175 +++++++++++++++++- > security/integrity/ima/ima_queue.c | 64 +++++++ > 4 files changed, 241 insertions(+), 4 deletions(-) > > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt > index e92c0056e4e0..cd1a1d0bf0e2 100644 > --- a/Documentation/admin-guide/kernel-parameters.txt > +++ b/Documentation/admin-guide/kernel-parameters.txt > @@ -2197,6 +2197,10 @@ > Use the canonical format for the binary runtime > measurements, instead of host native format. > > + ima_flush_htable [IMA] > + Flush the measurement list hash table when trim all > + or a part of it for deletion. > + > ima_hash= [IMA] > Format: { md5 | sha1 | rmd160 | sha256 | sha384 > | sha512 | ... } > diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h > index e3d71d8d56e3..ab0e30ee25ea 100644 > --- a/security/integrity/ima/ima.h > +++ b/security/integrity/ima/ima.h > @@ -246,8 +246,10 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key, > > #ifdef CONFIG_IMA_KEXEC > void ima_measure_kexec_event(const char *event_name); > +long ima_purge_event_log(long number_logs); > #else > static inline void ima_measure_kexec_event(const char *event_name) {} > +static inline long ima_purge_event_log(long number_logs) { return 0; } > #endif > > /* > diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c > index 87045b09f120..410f7d03c43f 100644 > --- a/security/integrity/ima/ima_fs.c > +++ b/security/integrity/ima/ima_fs.c > @@ -21,6 +21,9 @@ > #include <linux/rcupdate.h> > #include <linux/parser.h> > #include <linux/vmalloc.h> > +#include <linux/ktime.h> > +#include <linux/timekeeping.h> > +#include <linux/ima.h> > > #include "ima.h" > > @@ -38,6 +41,14 @@ __setup("ima_canonical_fmt", default_canonical_fmt_setup); > > static int valid_policy = 1; > > +#define IMA_LOG_TRIM_REQ_LENGTH 11 > +#define IMA_LOG_TRIM_EVENT_LEN 256 > + > +static long trimcount; > +/* mutex protects atomicity of trimming measurement list requests */ > +static DEFINE_MUTEX(ima_measure_lock); > +static long ima_measure_users; > + > static ssize_t ima_show_htable_value(char __user *buf, size_t count, > loff_t *ppos, atomic_long_t *val) > { > @@ -202,16 +213,65 @@ static const struct seq_operations ima_measurments_seqops = { > .show = ima_measurements_show > }; > > +static int _ima_measurements_open(struct inode *inode, struct file *file, > + const struct seq_operations *seq_ops) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + int ret; > + > + if (write && !capable(CAP_SYS_ADMIN)) > + return -EPERM; > + > + mutex_lock(&ima_measure_lock); > + if ((write && ima_measure_users != 0) || > + (!write && ima_measure_users < 0)) { > + mutex_unlock(&ima_measure_lock); > + return -EBUSY; > + } > + > + ret = seq_open(file, seq_ops); > + if (ret < 0) { > + mutex_unlock(&ima_measure_lock); > + return ret; > + } > + > + if (write) > + ima_measure_users--; > + else > + ima_measure_users++; > + > + mutex_unlock(&ima_measure_lock); > + return ret; > +} > + > static int ima_measurements_open(struct inode *inode, struct file *file) > { > - return seq_open(file, &ima_measurments_seqops); > + return _ima_measurements_open(inode, file, &ima_measurments_seqops); > +} > + > +static int ima_measurements_release(struct inode *inode, struct file *file) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + int ret; > + > + mutex_lock(&ima_measure_lock); > + ret = seq_release(inode, file); > + if (!ret) { > + if (write) > + ima_measure_users++; > + else > + ima_measure_users--; > + } > + > + mutex_unlock(&ima_measure_lock); > + return ret; > } > > static const struct file_operations ima_measurements_ops = { > .open = ima_measurements_open, > .read = seq_read, > .llseek = seq_lseek, > - .release = seq_release, > + .release = ima_measurements_release, > }; > > void ima_print_digest(struct seq_file *m, u8 *digest, u32 size) > @@ -279,14 +339,111 @@ static const struct seq_operations ima_ascii_measurements_seqops = { > > static int ima_ascii_measurements_open(struct inode *inode, struct file *file) > { > - return seq_open(file, &ima_ascii_measurements_seqops); > + return _ima_measurements_open(inode, file, &ima_ascii_measurements_seqops); > } > > static const struct file_operations ima_ascii_measurements_ops = { > .open = ima_ascii_measurements_open, > .read = seq_read, > .llseek = seq_lseek, > - .release = seq_release, > + .release = ima_measurements_release, > +}; > + > +static void ima_measure_trim_event(const long number_logs) > +{ > + char ima_log_trim_event[IMA_LOG_TRIM_EVENT_LEN]; > + struct timespec64 ts; > + u64 time_ns; > + int n; > + > + ktime_get_real_ts64(&ts); > + time_ns = (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec; > + n = scnprintf(ima_log_trim_event, IMA_LOG_TRIM_EVENT_LEN, > + "time=%llu; log trim this time=%lu;", > + time_ns, number_logs); > + > + ima_measure_critical_data("ima_log_trim", "trim ima event logs", ima_log_trim_event, n, false, NULL, 0); > +} > + > +static int ima_log_trim_open(struct inode *inode, struct file *file) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + > + if (!write && capable(CAP_SYS_ADMIN)) > + return 0; > + else if (!capable(CAP_SYS_ADMIN)) > + return -EPERM; > + > + return _ima_measurements_open(inode, file, &ima_measurments_seqops); > +} > + > +static ssize_t ima_log_trim_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) > +{ > + char tmpbuf[IMA_LOG_TRIM_REQ_LENGTH]; /* greater than largest 'long' string value */ > + ssize_t len; > + > + len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", trimcount); > + return simple_read_from_buffer(buf, size, ppos, tmpbuf, len); > +} > + > +static ssize_t ima_log_trim_write(struct file *file, > + const char __user *buf, size_t datalen, loff_t *ppos) > +{ > + unsigned char req[IMA_LOG_TRIM_REQ_LENGTH]; > + long count, n; > + int ret; > + > + if (*ppos > 0 || datalen > IMA_LOG_TRIM_REQ_LENGTH || datalen < 2) { > + ret = -EINVAL; > + goto out; > + } > + > + n = (int)datalen; > + > + ret = copy_from_user(req, buf, datalen); > + if (ret < 0) > + goto out; > + > + count = 0; > + for (int i = 0; i < n; ++i) { > + if (req[i] < '0' || req[i] > '9') { > + ret = -EINVAL; > + goto out; > + } > + count = count * 10 + req[i] - '0'; > + } > + ret = ima_purge_event_log(count); > + > + if (ret < 0) > + goto out; > + > + trimcount = ret; > + > + if (trimcount > 0) > + ima_measure_trim_event(trimcount); > + > + ret = datalen; > +out: > + return ret; > +} > + > +static int ima_log_trim_release(struct inode *inode, struct file *file) > +{ > + bool write = !!(file->f_mode & FMODE_WRITE); > + if (!write && capable(CAP_SYS_ADMIN)) > + return 0; > + else if (!capable(CAP_SYS_ADMIN)) > + return -EPERM; > + > + return ima_measurements_release(inode, file); > +} > + > +static const struct file_operations ima_log_trim_ops = { > + .open = ima_log_trim_open, > + .read = ima_log_trim_read, > + .write = ima_log_trim_write, > + .llseek = generic_file_llseek, > + .release = ima_log_trim_release > }; > > static ssize_t ima_read_policy(char *path) > @@ -528,6 +685,16 @@ int __init ima_fs_init(void) > goto out; > } > > + dentry = securityfs_create_file("ima_trim_log", > + S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP, > + ima_dir, NULL, &ima_log_trim_ops); > + if (IS_ERR(dentry)) { > + ret = PTR_ERR(dentry); > + goto out; > + } > + > + trimcount = 0; > + > dentry = securityfs_create_file("runtime_measurements_count", > S_IRUSR | S_IRGRP, ima_dir, NULL, > &ima_measurements_count_ops); > diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c > index 590637e81ad1..77ab52469727 100644 > --- a/security/integrity/ima/ima_queue.c > +++ b/security/integrity/ima/ima_queue.c > @@ -22,6 +22,14 @@ > > #define AUDIT_CAUSE_LEN_MAX 32 > > +bool ima_flush_htable; > +static int __init ima_flush_htable_setup(char *str) > +{ > + ima_flush_htable = true; > + return 1; > +} > +__setup("ima_flush_htable", ima_flush_htable_setup); > + > /* pre-allocated array of tpm_digest structures to extend a PCR */ > static struct tpm_digest *digests; > > @@ -220,6 +228,62 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation, > return result; > } > > +/* Delete the IMA event logs */ > +long ima_purge_event_log(long number_logs) > +{ > + struct ima_queue_entry *qe, *qe_tmp; > + LIST_HEAD(ima_measurements_staged); > + unsigned int i; > + long cur = number_logs; > + > + if (number_logs <= 0) > + return number_logs; > + > + mutex_lock(&ima_extend_list_mutex); > + > + > + list_for_each_entry(qe, &ima_measurements, later) { > + if (--number_logs == 0) > + break; > + } > + > + if (number_logs > 0) { > + mutex_unlock(&ima_extend_list_mutex); > + return -ENOENT; > + } > + > + __list_cut_position(&ima_measurements_staged, &ima_measurements, > + &qe->later); > + atomic_long_sub(cur, &ima_htable.len); > + > + if (!IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE) && ima_flush_htable) { > + list_for_each_entry(qe, &ima_measurements_staged, later) > + /* It can race with ima_lookup_digest_entry(). */ > + hlist_del_rcu(&qe->hnext); > + } If the h table can be staged during the locking period and deleted after unlocking, the time the list is held will be reduced. I will work on this, and any suggestions are greatly appreciated. Thanks, Steven > + > + mutex_unlock(&ima_extend_list_mutex); > + > + > + list_for_each_entry_safe(qe, qe_tmp, &ima_measurements_staged, later) { > + for (i = 0; i < qe->entry->template_desc->num_fields; i++) { > + kfree(qe->entry->template_data[i].data); > + qe->entry->template_data[i].data = NULL; > + qe->entry->template_data[i].len = 0; > + } > + > + list_del(&qe->later); > + > + if (ima_flush_htable) { > + kfree(qe->entry->digests); > + kfree(qe->entry); > + kfree(qe); > + } > + } > + > + return cur; > +} > + > int ima_restore_measurement_entry(struct ima_template_entry *entry) > { > int result = 0; ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-12-11 18:41 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-12-10 23:53 [PATCH v2 0/1] Trim N entries of IMA event logs steven chen 2025-12-10 23:53 ` [PATCH V2 1/1] IMA event log trimming steven chen 2025-12-11 10:14 ` Roberto Sassu 2025-12-11 18:20 ` steven chen 2025-12-11 18:41 ` steven chen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).