ima: keep the integrity state of open files up to date
Subsystems (7)
PATCH 1/3 ima: keep the integrity state of open files up to date
When a file is open for writing, kernel crash or power outage is guaranteed to corrupt the inode integrity state leading to file appraisal failure on the subsequent boot. Add some basic infrastructure to keep the integrity measurements up to date as the files are written to.
Core file operations (open, close, sync, msync, truncate) are now allowed to update the measurement immediately. In order to maintain sufficient write performance for writes, add a latency tunable delayed work workqueue for computing the measurements.
Changelog v2:
- Make write measurements optional
- Add support for MMIO measurements
- Handle all writes via page flush
- Add work cancellation support, files are properly closed after last_writer checks out. This fixes a userspace break where the file was still open for writing after closing it.
- Fix/unify IMA_UPDATE_XATTR usage
--- include/linux/ima.h | 14 +++ security/integrity/ima/Kconfig | 30 ++++++ security/integrity/ima/ima.h | 13 +++ security/integrity/ima/ima_appraise.c | 13 ++- security/integrity/ima/ima_main.c | 128 +++++++++++++++++++++++++- security/integrity/integrity.h | 18 ++++ 6 files changed, 213 insertions(+), 3 deletions(-)
include/linux/ima.h +14 -0
@@ -93,6 +93,20 @@ static inline void ima_post_path_mknod(struct dentry *dentry)static inline void ima_kexec_cmdline (const void *buf,int size) {}#endif /* CONFIG_IMA */ +#if ((definedCONFIG_IMA ) && defined(CONFIG_IMA_MEASURE_WRITES )) +void ima_file_update (struct file *file); +void ima_file_delayed_update (struct file *file); +#else +static inline void ima_file_update (struct file *file) +{ +return ; +} +static inline void ima_file_delayed_update (struct file *file) +{ +return ; +} +#endif +#ifndef CONFIG_IMA_KEXEC struct kimage ;
security/integrity/ima/Kconfig +30 -0
@@ -310,3 +310,33 @@ config IMA_APPRAISE_SIGNED_INIT default n help This option requires user-space init to be signed. + +config IMA_MEASURE_WRITES + bool "Measure file writes (EXPERIMENTAL)" + depends on IMA + depends on EVM + default n + help + By default IMA measures files only when they close or sync. + Choose this option if you want the integrity measurements on + the disk to update when the files are still open for writing. + +config IMA_MEASUREMENT_LATENCY + int + depends on IMA + range 0 60000 + default 50 + help + This value defines the measurement interval when files are + being written. Value is in milliseconds. + +config IMA_MEASUREMENT_LATENCY_CEILING + int + depends on IMA + range 100 60000 + default 5000 + help + In order to maintain high write performance for large files, + IMA increases the measurement interval as the file size grows. + This value defines the ceiling for the measurement delay in + milliseconds.
security/integrity/ima/ima.h +13 -0
@@ -160,6 +160,19 @@ void ima_init_template_list(void);int __init ima_init_digests (void );int ima_lsm_policy_change (struct notifier_block *nb,unsigned long event,void *lsm_data); +#if ((definedCONFIG_IMA ) && defined(CONFIG_IMA_MEASURE_WRITES )) +void ima_cancel_measurement (struct integrity_iint_cache *iint); +#else +static inline void ima_cancel_measurement (struct integrity_iint_cache *iint) +{ +return ; +} +static inline void ima_init_measurement (struct integrity_iint_cache *iint, +struct dentry *dentry) +{ +return ; +} +#endif /* * used to protect h_table and sha_table
security/integrity/ima/ima_appraise.c +12 -1
@@ -78,6 +78,15 @@ static int ima_fix_xattr(struct dentry *dentry,return rc; } +#ifdef CONFIG_IMA_MEASURE_WRITES +inline void ima_init_measurement (struct integrity_iint_cache *iint, +struct dentry *dentry) +{ +if (test_bit (IMA_UPDATE_XATTR , &iint->atomic_flags)) +ima_fix_xattr (dentry, iint); +} +#endif +/* Return specific func appraised cached result */ enum integrity_status ima_get_cache_status (struct integrity_iint_cache *iint,enum ima_hooks func)
@@ -341,8 +350,10 @@ int ima_appraise_measurement(enum ima_hooks func, iint->flags |=IMA_NEW_FILE ;if ((iint->flags &IMA_NEW_FILE ) && (!(iint->flags &IMA_DIGSIG_REQUIRED ) || - (inode->i_size ==0 ))) + (inode->i_size ==0 ))) { +ima_init_measurement (iint, dentry); status =INTEGRITY_PASS ; + } goto out; }
security/integrity/ima/ima_main.c +126 -2
166 lines not on this page · raw message
security/integrity/integrity.h +18 -0
@@ -64,6 +64,11 @@#define IMA_DIGSIG 3#define IMA_MUST_MEASURE 4 +/* delayed measurement job state */ +#define IMA_WORK_INACTIVE 0 +#define IMA_WORK_ACTIVE 1 +#define IMA_WORK_CANCELLED 2 +enum evm_ima_xattr_type {IMA_XATTR_DIGEST =0x01 ,EVM_XATTR_HMAC ,
@@ -115,6 +120,18 @@ struct signature_v2_hdr {uint8_t sig[0 ];/* signature payload */ } __packed; +#if CONFIG_IMA_MEASUREMENT_LATENCY ==0 +#define IMA_LATENCY_INCREMENT 100 +#else +#define IMA_LATENCY_INCREMENT CONFIG_IMA_MEASUREMENT_LATENCY +#endif + +struct ima_work_entry { +struct delayed_work work; +struct file *file; +uint8_t state; +}; +/* integrity data associated with an inode */ struct integrity_iint_cache {struct rb_node rb_node;/* rooted in integrity_iint_tree */
@@ -131,6 +148,7 @@ struct integrity_iint_cache { enum integrity_status ima_creds_status:4; enum integrity_status evm_status:4;struct ima_digest_data *ima_hash; +struct ima_work_entry ima_work; };/* rbtree tree calls to lookup, insert, delete
Signature · 1 line
2.17.1
PATCH 2/3 ima: update the file measurement on truncate
Let IMA know when a file is being opened with truncate or truncated directly.
Depends on commit 72649b7862a7 ("ima: keep the integrity state of open files up to date")'
--- fs/namei.c | 5 ++++- fs/open.c | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-)
fs/namei.c +4 -1
@@ -3418,8 +3418,11 @@ static int do_last(struct nameidata *nd, goto out; opened: error =ima_file_check (file, op->acc_mode); -if (!error && will_truncate) +if (!error && will_truncate) { error =handle_truncate (file); +if (!error) +ima_file_update (file); + } out:if (unlikely (error >0 )) {WARN_ON (1 );
fs/open.c +3 -0
@@ -63,6 +63,9 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,/* Note any delegations or leases have already been broken: */ ret =notify_change (dentry, &newattrs,NULL );inode_unlock (dentry->d_inode); + +if (filp) +ima_file_update (filp);return ret; }
Signature · 1 line
2.17.1
Mimi Zohar
maintainerRe: [PATCH 2/3] ima: update the file measurement on truncate
Quoted 17 lines · Janne Karhunen
Let IMA know when a file is being opened with truncate or truncated directly.
Depends on commit
72649b7862a7("ima: keep the integrity state of open files up to date")'Signed-off-by Janne Karhunen <[email protected]>Signed-off-by Konsta Karsisto <[email protected]>--- fs/namei.c | 5 ++++- fs/open.c | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-)fs/namei.c +0
-0
fs/namei.c static int do_last(struct nameidata *nd, · line 3424
Quoted 8 lines
fs/namei.c +3
-1@@ -3418,8 +3418,11 @@ static int do_last(struct nameidata *nd, goto out; opened: error =ima_file_check (file, op->acc_mode); -if (!error && will_truncate) +if (!error && will_truncate) { error =handle_truncate (file); +if (!error) +ima_file_update (file);
Security and IMA hooks are normally named after the function. For example, there's a security hook named security_path_truncate() in handle_truncate(). The new hook after the truncate would either be named security_post_path_truncate() or ima_post_path_truncate().
Quoted 9 lines
fs/namei.c +1
-0+ } out:if (unlikely (error >0 )) {WARN_ON (1 );fs/open.c +0
-0
fs/open.c int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, · line 71
Quoted 9 lines
fs/open.c +3
-0@@ -63,6 +63,9 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,/* Note any delegations or leases have already been broken: */ ret =notify_change (dentry, &newattrs,NULL );inode_unlock (dentry->d_inode); + +if (filp) +ima_file_update (filp);return ret; }
do_truncate() is called from a number of places. Are you sure that the call to IMA should be in all of them? security_path_truncate() isn't in all of the callers.
Mimi
PATCH 3/3 ima: update the file measurement on writes
In reply to Janne KarhunenFrom: Konsta Karsisto <[email protected]>
Hook do_writepages() in order to do IMA measurement of an inode that is written out.
Depends on commit 72649b7862a7 ("ima: keep the integrity state of open files up to date")'
--- include/linux/ima.h | 10 +++ mm/page-writeback.c | 7 ++ security/integrity/iint.c | 3 + security/integrity/ima/ima.h | 14 ++++ security/integrity/ima/ima_main.c | 115 ++++++++++++++++++++++++++++-- security/integrity/integrity.h | 6 ++ 6 files changed, 150 insertions(+), 5 deletions(-)
include/linux/ima.h +10 -0
@@ -96,6 +96,8 @@ static inline void ima_kexec_cmdline(const void *buf, int size) {}#if ((definedCONFIG_IMA ) && defined(CONFIG_IMA_MEASURE_WRITES ))void ima_file_update (struct file *file);void ima_file_delayed_update (struct file *file); +void ima_inode_update (struct inode *inode); +void ima_inode_delayed_update (struct inode *inode);#else static inline void ima_file_update (struct file *file) {
@@ -105,6 +107,14 @@ static inline void ima_file_delayed_update(struct file *file) {return ; } +static inline void ima_inode_update (struct inode *inode) +{ +return ; +} +static inline void ima_inode_delayed_update (struct inode *inode) +{ +return ; +}#endif #ifndef CONFIG_IMA_KEXEC
mm/page-writeback.c +7 -0
@@ -38,6 +38,7 @@#include <linux/sched/rt.h> #include <linux/sched/signal.h> #include <linux/mm_inline.h> +#include <linux/ima.h> #include <trace/events/writeback.h> #include "internal.h"
@@ -2347,6 +2348,12 @@ int do_writepages(struct address_space *mapping, struct writeback_control *wbc)cond_resched ();congestion_wait (BLK_RW_ASYNC ,HZ /50 ); } +if (ret ==0 ) { +if (wbc->sync_mode ==WB_SYNC_ALL ) +ima_inode_update (mapping->host); +else +ima_inode_delayed_update (mapping->host); + }return ret; }
security/integrity/iint.c +3 -0
@@ -82,6 +82,8 @@ static void iint_free(struct integrity_iint_cache *iint) iint->ima_creds_status =INTEGRITY_UNKNOWN ; iint->evm_status =INTEGRITY_UNKNOWN ; iint->measured_pcrs =0 ; +WARN_ON (iint->ima_work.file); +WARN_ON (!list_empty (&iint->file_list));kmem_cache_free (iint_cache, iint); }
@@ -161,6 +163,7 @@ static void init_once(void *foo) iint->ima_read_status =INTEGRITY_UNKNOWN ; iint->ima_creds_status =INTEGRITY_UNKNOWN ; iint->evm_status =INTEGRITY_UNKNOWN ; +INIT_LIST_HEAD (&iint->file_list);mutex_init (&iint->mutex); }
security/integrity/ima/ima.h +14 -0
@@ -162,6 +162,10 @@ int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, void *lsm_data);#if ((definedCONFIG_IMA ) && defined(CONFIG_IMA_MEASURE_WRITES ))void ima_cancel_measurement (struct integrity_iint_cache *iint); +void ima_get_file (struct integrity_iint_cache *iint, +struct file *file); +void ima_put_file (struct integrity_iint_cache *iint, +struct file *file);#else static inline void ima_cancel_measurement (struct integrity_iint_cache *iint) {
@@ -172,6 +176,16 @@ static inline void ima_init_measurement(struct integrity_iint_cache *iint, {return ; } +static inline void ima_get_file (struct integrity_iint_cache *iint, +struct file *file) +{ +return ; +} +static inline void ima_put_file (struct integrity_iint_cache *iint, +struct file *file) +{ +return ; +}#endif /*
security/integrity/ima/ima_main.c +110 -5
159 lines not on this page · raw message
security/integrity/integrity.h +6 -0
@@ -132,6 +132,11 @@ struct ima_work_entry {uint8_t state; }; +struct ima_fl_entry { +struct list_head list; +struct file *file; +}; +/* integrity data associated with an inode */ struct integrity_iint_cache {struct rb_node rb_node;/* rooted in integrity_iint_tree */
@@ -149,6 +154,7 @@ struct integrity_iint_cache { enum integrity_status evm_status:4;struct ima_digest_data *ima_hash;struct ima_work_entry ima_work; +struct list_head file_list; };/* rbtree tree calls to lookup, insert, delete
Signature · 1 line
2.17.1
Mimi Zohar
maintainerRe: [PATCH 3/3] ima: update the file measurement on writes
Quoted 4 lines · Janne Karhunen
From: Konsta Karsisto <[email protected]>
Hook
do_writepages()in order to do IMA measurement of an inode that is written out.
With this explanation I would expect to see a security/ima hook in do_writepages(), nothing else. There's a lot more going on here than that. The memory management maintainer(s) doesn't really care about IMA. Please break this patch up so that it is easier to review and upstream.
My comments on the first patch in this patch set (e.g. function names, ifdefs in C code, workqueue belonging in a separate patch) are also applicable to this patch.
Quoted 6 lines
Depends on commit
72649b7862a7("ima: keep the integrity state of open files up to date")'Signed-off-by Konsta Karsisto <[email protected]>Signed-off-by Janne Karhunen <[email protected]>---
[snip]
Quoted 13 lines
mm/page-writeback.c +1
-0@@ -38,6 +38,7 @@#include <linux/sched/rt.h> #include <linux/sched/signal.h> #include <linux/mm_inline.h> +#include <linux/ima.h> #include <trace/events/writeback.h> #include "internal.h"
mm/page-writeback.c int do_writepages(struct address_space *mapping, struct writeback_control *wbc) · line 2355
Quoted 8 lines
mm/page-writeback.c +5
-0@@ -2347,6 +2348,12 @@ int do_writepages(struct address_space *mapping, struct writeback_control *wbc)cond_resched ();congestion_wait (BLK_RW_ASYNC ,HZ /50 ); } +if (ret ==0 ) { +if (wbc->sync_mode ==WB_SYNC_ALL ) +ima_inode_update (mapping->host); +else +ima_inode_delayed_update (mapping->host);
It's hard enough upstreaming a single security or IMA hook. There's no need for two. security/IMA hooks are normally based on function names (eg. ima_do_writepages). The sync_mode should be an argument.
Quoted 39 lines
mm/page-writeback.c +1
-0+ }return ret; }security/integrity/ima/ima_main.c +26
-0@@ -425,6 +430,42 @@ int ima_bprm_check(struct linux_binprm *bprm) }#ifdef CONFIG_IMA_MEASURE_WRITES +void ima_get_file (struct integrity_iint_cache *iint, +struct file *file) +{ +struct ima_fl_entry *e; + +if (!iint || !file) +return ; +if (!(file->f_mode &FMODE_WRITE ) || + !test_bit (IMA_UPDATE_XATTR , &iint->atomic_flags)) +return ; + +list_for_each_entry (e, &iint->file_list, list) { +if (e->file == file) +return ; + } + e =kmalloc (sizeof (*e),GFP_KERNEL ); +if (!e) +return ; + e->file = file; +list_add (&e->list, &iint->file_list); +} + +void ima_put_file (struct integrity_iint_cache *iint, +struct file *file) +{ +struct ima_fl_entry *e;
security/integrity/ima/ima_main.c int ima_bprm_check(struct linux_binprm *bprm) · line 468
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+ +list_for_each_entry (e, &iint->file_list, list) { +if (e->file == file) { +list_del (&e->list); +kfree (e); +break ; + } + } +} +
Functions with "get" or "put" in their name increment/decrement a reference count. No reference count is being updated here.
Mimi
kbuild test robot
build botThank you for the patch! Perhaps something to improve:
Hi Janne,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on linus/master]
[cannot apply to v5.3-rc6 next-20190830]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Janne-Karhunen/ima-keep-the-integrity-state-of-open-files-up-to-date/20190902-182420
config: i386-defconfig (attached as .config)
compiler: gcc-7 (Debian 7.4.0-11) 7.4.0 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
In file included from security/integrity/iint.c:22:0:
>> security/integrity/integrity.h:122:5: warning: "CONFIG_IMA_MEASUREMENT_LATENCY" is not defined, evaluates to 0 [-Wundef]
#if CONFIG_IMA_MEASUREMENT_LATENCY == 0
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~vim +/CONFIG_IMA_MEASUREMENT_LATENCY +122 security/integrity/integrity.h
121
> 122 #if CONFIG_IMA_MEASUREMENT_LATENCY == 0
123 #define IMA_LATENCY_INCREMENT 100
124 #else
125 #define IMA_LATENCY_INCREMENT CONFIG_IMA_MEASUREMENT_LATENCY
126 #endif
127
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botThank you for the patch! Perhaps something to improve:
Hi Janne,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on linus/master]
[cannot apply to v5.3-rc6 next-20190830]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Janne-Karhunen/ima-keep-the-integrity-state-of-open-files-up-to-date/20190902-182420
config: x86_64-randconfig-s0-09021304 (attached as .config)
compiler: gcc-7 (Debian 7.4.0-11) 7.4.0
reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
In file included from security/integrity/evm/evm.h:18:0,
from security/integrity/evm/evm_main.c:27:
>> security/integrity/evm/../integrity.h:122:5: warning: "CONFIG_IMA_MEASUREMENT_LATENCY" is not defined, evaluates to 0 [-Wundef]
#if CONFIG_IMA_MEASUREMENT_LATENCY == 0
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~vim +/CONFIG_IMA_MEASUREMENT_LATENCY +122 security/integrity/evm/../integrity.h
121
> 122 #if CONFIG_IMA_MEASUREMENT_LATENCY == 0
123 #define IMA_LATENCY_INCREMENT 100
124 #else
125 #define IMA_LATENCY_INCREMENT CONFIG_IMA_MEASUREMENT_LATENCY
126 #endif
127
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Mimi Zohar
maintainerQuoted 5 lines · Janne Karhunen
When a file is open for writing, kernel crash or power outage is guaranteed to corrupt the inode integrity state leading to file appraisal failure on the subsequent boot. Add some basic infrastructure to keep the integrity measurements up to date as the files are written to.
The term "measurement" refers to the file hash stored in the IMA measurement list and used to extend the TPM. IMA-appraisal verifies the file hash against a known good value stored as an extended attribute(xattr). For immutable files, the known good value should be a file signature. For mutable files, the known good value is a file hash. The purpose of this patch set is to increase the frequency in which the file hash, stored as an xattr, is updated.
Throughout this patch set the term "measurement" or "measure" is inappropriately used.
Quoted 57 lines
Core file operations (open, close, sync, msync, truncate) are now allowed to update the measurement immediately. In order to maintain sufficient write performance for writes, add a latency tunable delayed work workqueue for computing the measurements.
Changelog v2:
- Make write measurements optional
- Add support for MMIO measurements
- Handle all writes via page flush
- Add work cancellation support, files are properly closed afterlast_writerchecks out. This fixes a userspace break where the file was still open for writing after closing it.
- Fix/unifyIMA_UPDATE_XATTRusageSigned-off-by Janne Karhunen <[email protected]>Signed-off-by Konsta Karsisto <[email protected]>--- include/linux/ima.h | 14 +++ security/integrity/ima/Kconfig | 30 ++++++ security/integrity/ima/ima.h | 13 +++ security/integrity/ima/ima_appraise.c | 13 ++- security/integrity/ima/ima_main.c | 128 +++++++++++++++++++++++++- security/integrity/integrity.h | 18 ++++ 6 files changed, 213 insertions(+), 3 deletions(-)include/linux/ima.h +14
-0@@ -93,6 +93,20 @@ static inline void ima_post_path_mknod(struct dentry *dentry)static inline void ima_kexec_cmdline (const void *buf,int size) {}#endif /* CONFIG_IMA */ +#if ((definedCONFIG_IMA ) && defined(CONFIG_IMA_MEASURE_WRITES )) +void ima_file_update (struct file *file); +void ima_file_delayed_update (struct file *file); +#else +static inline void ima_file_update (struct file *file) +{ +return ; +} +static inline void ima_file_delayed_update (struct file *file) +{ +return ; +} +#endif +#ifndef CONFIG_IMA_KEXEC struct kimage ;security/integrity/ima/Kconfig +0
-0
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 315
Quoted 6 lines
security/integrity/ima/Kconfig +3
-0@@ -310,3 +310,33 @@ config IMA_APPRAISE_SIGNED_INIT default n help This option requires user-space init to be signed. + +config IMA_MEASURE_WRITES + bool "Measure file writes (EXPERIMENTAL)"
"MEASURE" is the wrong term.
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 316
Quoted 1 line
security/integrity/ima/Kconfig +1
-0+ depends on IMA
Only IMA_APPRAISE updates the security.ima. This should be "depends on IMA_APPRAISE".
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 317
Quoted 1 line
security/integrity/ima/Kconfig +1
-0+ depends on EVM
Anyone relying on file hashes to verify a file's integrity should enable EVM, but as much as possible IMA and EVM are defined independently of each other. Please remove this dependency.
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 320
Quoted 3 lines
security/integrity/ima/Kconfig +3
-0+ default n + help + By default IMA measures files only when they close or sync.
By default IMA-appraisal updates the file hash, stored as an xattr,
...
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 326
Quoted 6 lines
security/integrity/ima/Kconfig +6
-0+ Choose this option if you want the integrity measurements on + the disk to update when the files are still open for writing. + +config IMA_MEASUREMENT_LATENCY + int + depends on IMA
More specific dependency is required.
I'd like to see smaller patches that build upon each other. For example, the initial design should be in the first patch. Performance improvements, like the latency and latency_ceiling, could be subsequent patches.
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 335
Quoted 9 lines
security/integrity/ima/Kconfig +9
-0+ range 0 60000 + default 50 + help + This value defines the measurement interval when files are + being written. Value is in milliseconds. + +config IMA_MEASUREMENT_LATENCY_CEILING + int + depends on IMA
More specific dependency required.
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 336
Quoted 1 line
security/integrity/ima/Kconfig +1
-0+ range 100 60000
The "ceiling" needs to be greater than the "latency". If it isn't possible to implement in the Kconfig, then at least check in the code.
Quoted 11 lines
security/integrity/ima/Kconfig +6
-0+ default 5000 + help + In order to maintain high write performance for large files, + IMA increases the measurement interval as the file size grows. + This value defines the ceiling for the measurement delay in + milliseconds.security/integrity/ima/ima.h +0
-0
security/integrity/ima/ima.h void ima_init_template_list(void); · line 163
Quoted 4 lines
security/integrity/ima/ima.h +1
-0@@ -160,6 +160,19 @@ void ima_init_template_list(void);int __init ima_init_digests (void );int ima_lsm_policy_change (struct notifier_block *nb,unsigned long event,void *lsm_data); +#if ((definedCONFIG_IMA ) && defined(CONFIG_IMA_MEASURE_WRITES ))
Both Kconfig options shouldn't be required. Use the more specific one.
security/integrity/ima/ima.h void ima_init_template_list(void); · line 164
Quoted 1 line
security/integrity/ima/ima.h +1
-0+void ima_cancel_measurement (struct integrity_iint_cache *iint);
(The function needs to be renamed to something without the word "measurement".)
Currently ima_cancel_measurement() is defined and called from ima_main.c.
security/integrity/ima/ima.h void ima_init_template_list(void); · line 175
Quoted 11 lines
security/integrity/ima/ima.h +11
-0+#else +static inline void ima_cancel_measurement (struct integrity_iint_cache *iint) +{ +return ; +} +static inline void ima_init_measurement (struct integrity_iint_cache *iint, +struct dentry *dentry) +{ +return ; +} +#endif
If the function definition and usage are in the same file, the function should be defined as static. There shouldn't be a need for these the function declarations or stub functions.
security/integrity/ima/ima_appraise.c static int ima_fix_xattr(struct dentry *dentry, · line 81
Quoted 12 lines
security/integrity/ima/ima.h +0
-0/* * used to protect h_table and sha_table security/integrity/ima/ima_appraise.c +1
-0@@ -78,6 +78,15 @@ static int ima_fix_xattr(struct dentry *dentry,return rc; } +#ifdef CONFIG_IMA_MEASURE_WRITES
ifdef's don't belong in C code. Refer to section 21 "Conditional Compilation" in Documentation/process/coding-style.rst.
security/integrity/ima/ima_appraise.c static int ima_fix_xattr(struct dentry *dentry, · line 83
Quoted 2 lines
security/integrity/ima/ima_appraise.c +2
-0+inline void ima_init_measurement (struct integrity_iint_cache *iint, +struct dentry *dentry)
(
(The function needs to be renamed to something without the word "measurement".)
Writing xattrs requires the i_rwsem. Please add a comment indicating that callers must take the i_rwsem.
Quoted 10 lines
security/integrity/ima/ima_appraise.c +6
-0+{ +if (test_bit (IMA_UPDATE_XATTR , &iint->atomic_flags)) +ima_fix_xattr (dentry, iint); +} +#endif +/* Return specific func appraised cached result */ enum integrity_status ima_get_cache_status (struct integrity_iint_cache *iint,enum ima_hooks func)
security/integrity/ima/ima_appraise.c int ima_appraise_measurement(enum ima_hooks func, · line 354
Quoted 6 lines
security/integrity/ima/ima_appraise.c +2
-1@@ -341,8 +350,10 @@ int ima_appraise_measurement(enum ima_hooks func, iint->flags |=IMA_NEW_FILE ;if ((iint->flags &IMA_NEW_FILE ) && (!(iint->flags &IMA_DIGSIG_REQUIRED ) || - (inode->i_size ==0 ))) + (inode->i_size ==0 ))) { +ima_init_measurement (iint, dentry);
Do we really need to write the file hashes for 0 length files?
Quoted 28 lines
security/integrity/ima/ima_appraise.c +1
-0status =INTEGRITY_PASS ; + } goto out; }security/integrity/ima/ima_main.c +3
-1@@ -12,7 +12,7 @@* * File: ima_main.c * implements the IMA hooks: ima_bprm_check, ima_file_mmap, -* and ima_file_check. +* ima_file_delayed_update, ima_file_update and ima_file_check. */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt@@ -26,6 +26,8 @@#include <linux/xattr.h> #include <linux/ima.h> #include <linux/iversion.h> +#include <linux/workqueue.h> +#include <linux/sizes.h> #include <linux/fs.h> #include "ima.h"
security/integrity/ima/ima_main.c static int hash_setup_done; · line 47
Quoted 4 lines
security/integrity/ima/ima_main.c +1
-0@@ -42,6 +44,7 @@ static int hash_setup_done;static struct notifier_block ima_lsm_policy_notifier = { .notifier_call = ima_lsm_policy_change, }; +static struct workqueue_struct *ima_update_wq;
All of the workqueue support should probably be defined in a separate file, not here in ima_main.c.
Quoted 12 lines
security/integrity/ima/ima_main.c +1
-0static int __init hash_setup (char *str) {@@ -151,6 +154,7 @@ static void ima_check_last_writer(struct integrity_iint_cache *iint,if (!(mode &FMODE_WRITE ))return ; +ima_cancel_measurement (iint);mutex_lock (&iint->mutex);if (atomic_read (&inode->i_writecount) ==1 ) {
security/integrity/ima/ima_main.c int ima_bprm_check(struct linux_binprm *bprm) · line 427
Quoted 4 lines
security/integrity/ima/ima_main.c +1
-0@@ -420,6 +424,117 @@ int ima_bprm_check(struct linux_binprm *bprm) MAY_EXEC, CREDS_CHECK); } +#ifdef CONFIG_IMA_MEASURE_WRITES
ifdef's don't belong in C code.
Quoted 155 lines
security/integrity/ima/ima_main.c +120
-1+static unsigned long ima_inode_update_delay (struct inode *inode) +{ +unsigned long blocks, msecs; + + blocks =i_size_read (inode) /SZ_1M +1 ; + msecs = blocks *IMA_LATENCY_INCREMENT ; +if (msecs >CONFIG_IMA_MEASUREMENT_LATENCY_CEILING ) + msecs =CONFIG_IMA_MEASUREMENT_LATENCY_CEILING ; + +return msecs; +} + +static void ima_delayed_update_handler (struct work_struct *work) +{ +struct ima_work_entry *entry; + + entry =container_of (work,typeof (*entry), work.work); + +ima_file_update (entry->file); + entry->file =NULL ; + entry->state =IMA_WORK_INACTIVE ; +} + +void ima_cancel_measurement (struct integrity_iint_cache *iint) +{ +if (iint->ima_work.state !=IMA_WORK_ACTIVE ) +return ; + +cancel_delayed_work_sync (&iint->ima_work.work); + iint->ima_work.state =IMA_WORK_CANCELLED ; +} + +/** +* ima_file_delayed_update +* @file: pointer to file structure being updated +*/ +void ima_file_delayed_update (struct file *file) +{ +struct inode *inode =file_inode (file); +struct integrity_iint_cache *iint; +unsigned long msecs; +bool creq; + +if (!ima_policy_flag || !S_ISREG (inode->i_mode)) +return ; + + iint =integrity_iint_find (inode); +if (!iint) +return ; + +if (!test_bit (IMA_UPDATE_XATTR , &iint->atomic_flags)) +return ; + +mutex_lock (&iint->mutex); +if (iint->ima_work.state ==IMA_WORK_ACTIVE ) + goto out; + + msecs =ima_inode_update_delay (inode); + iint->ima_work.file = file; + iint->ima_work.state =IMA_WORK_ACTIVE ; +INIT_DELAYED_WORK (&iint->ima_work.work, ima_delayed_update_handler); + + creq =queue_delayed_work (ima_update_wq, + &iint->ima_work.work, +msecs_to_jiffies (msecs)); +if (creq == false) { + iint->ima_work.file =NULL ; + iint->ima_work.state =IMA_WORK_INACTIVE ; + } +out: +mutex_unlock (&iint->mutex); +} +EXPORT_SYMBOL_GPL (ima_file_delayed_update); + +/** +* ima_file_update - update the file measurement +* @file: pointer to file structure being updated +*/ +void ima_file_update (struct file *file) +{ +struct inode *inode =file_inode (file); +struct integrity_iint_cache *iint; +bool should_measure = true; +u64 i_version; + +if (!ima_policy_flag || !S_ISREG (inode->i_mode)) +return ; + + iint =integrity_iint_find (inode); +if (!iint) +return ; + +if (!test_bit (IMA_UPDATE_XATTR , &iint->atomic_flags)) +return ; + +mutex_lock (&iint->mutex); +if (IS_I_VERSION (inode)) { + i_version =inode_query_iversion (inode); +if (i_version == iint->version) + should_measure = false; + } +if (should_measure) { + iint->flags &= ~IMA_COLLECTED ; +ima_update_xattr (iint, file); + } +mutex_unlock (&iint->mutex); +} +EXPORT_SYMBOL_GPL (ima_file_update); +#endif /* CONFIG_IMA_MEASURE_WRITES */ +/** * ima_path_check - based on policy, collect/store measurement. * @file: pointer to the file to be measured @@ -716,9 +831,18 @@ static int __init init_ima(void)if (error)pr_warn ("Couldn't register LSM notifier, error %d\n" , error); -if (!error) +if (!error) {ima_update_policy_flag (); + ima_update_wq =alloc_workqueue ("ima-update-wq" , +WQ_MEM_RECLAIM | +WQ_CPU_INTENSIVE , +0 ); +if (!ima_update_wq) { +pr_err ("Failed to allocate write measurement workqueue\n" ); + error = -ENOMEM ; + } + }return error; }security/integrity/integrity.h +7
-0@@ -64,6 +64,11 @@#define IMA_DIGSIG 3#define IMA_MUST_MEASURE 4 +/* delayed measurement job state */ +#define IMA_WORK_INACTIVE 0 +#define IMA_WORK_ACTIVE 1 +#define IMA_WORK_CANCELLED 2 +enum evm_ima_xattr_type {IMA_XATTR_DIGEST =0x01 ,EVM_XATTR_HMAC ,@@ -115,6 +120,18 @@ struct signature_v2_hdr {uint8_t sig[0 ];/* signature payload */ } __packed; +#if CONFIG_IMA_MEASUREMENT_LATENCY ==0 +#define IMA_LATENCY_INCREMENT 100
security/integrity/integrity.h struct signature_v2_hdr { · line 134
Quoted 10 lines
security/integrity/integrity.h +10
-0+#else +#define IMA_LATENCY_INCREMENT CONFIG_IMA_MEASUREMENT_LATENCY +#endif + +struct ima_work_entry { +struct delayed_work work; +struct file *file; +uint8_t state; +}; +
Please add a comment indicating the type of work or maybe update the struct name.
Mimi
Quoted 11 lines
security/integrity/integrity.h +1
-0/* integrity data associated with an inode */ struct integrity_iint_cache {struct rb_node rb_node;/* rooted in integrity_iint_tree */ @@ -131,6 +148,7 @@ struct integrity_iint_cache { enum integrity_status ima_creds_status:4; enum integrity_status evm_status:4;struct ima_digest_data *ima_hash; +struct ima_work_entry ima_work; };/* rbtree tree calls to lookup, insert, delete
Quoted 6 lines · Janne Karhunen
When a file is open for writing, kernel crash or power outage is guaranteed to corrupt the inode integrity state leading to file appraisal failure on the subsequent boot. Add some basic infrastructure to keep the integrity measurements up to date as the files are written to.
Quoted 6 lines
Core file operations (open, close, sync, msync, truncate) are now allowed to update the measurement immediately. In order to maintain sufficient write performance for writes, add a latency tunable delayed work workqueue for computing the measurements.
This still doesn't make it crash-safe. So why is it okay?
- Eric
Quoted 7 lines · Eric Biggers
Quoted 5 lines
Core file operations (open, close, sync, msync, truncate) are now allowed to update the measurement immediately. In order to maintain sufficient write performance for writes, add a latency tunable delayed work workqueue for computing the measurements.
This still doesn't make it crash-safe. So why is it okay?
If Android is the load, this makes it crash safe 99% of the time and that is considerably better than 0% of the time.
That said, we have now a patch draft forming up that pushes the update to the ext4 journal. With this patch on top we should reach the magical 100% given data=journal mount. One step at a time.
--
Janne
Quoted 7 lines · Janne Karhunen
Quoted 6 lines · Eric Biggers
Quoted 5 lines
Core file operations (open, close, sync, msync, truncate) are now allowed to update the measurement immediately. In order to maintain sufficient write performance for writes, add a latency tunable delayed work workqueue for computing the measurements.
Quoted 5 lines
Quoted 1 line
This still doesn't make it crash-safe. So why is it okay?
If Android is the load, this makes it crash safe 99% of the time and that is considerably better than 0% of the time.
Who will use it if it isn't 100% safe?
- Eric
Quoted 7 lines · Eric Biggers
Quoted 5 lines
Quoted 1 line
This still doesn't make it crash-safe. So why is it okay?
If Android is the load, this makes it crash safe 99% of the time and that is considerably better than 0% of the time.
Who will use it if it isn't 100% safe?
I suppose anyone using mutable data with IMA appraise should, unless they have a redundant power supply and a kernel that never crashes. In a way this is like asking if the ima-appraise should be there for mutable data at all. All this is doing is that it improves the crash recovery reliability without taking anything away.
Anyway, I think I'm getting along with my understanding of the page writeback slowly and the journal support will eventually be there at least as an add-on patch for those that want to use it and really need the last 0.n% reliability. Note that even without that patch you can build ima-appraise based systems that are 99.999% reliable just by having the patch we're discussing here. Without it you would be orders of magnitude worse off. All we are doing is that we give it a fairly good chance to recover instead of giving up without even trying.
That said, I'm not sure the 100% crash recovery is ever guaranteed in any Linux system. We just have to do what we can, no?
--
Janne
Quoted 8 lines · Janne Karhunen
Quoted 6 lines · Eric Biggers
Quoted 5 lines
Quoted 1 line
This still doesn't make it crash-safe. So why is it okay?
If Android is the load, this makes it crash safe 99% of the time and that is considerably better than 0% of the time.
Quoted 7 lines
Quoted 1 line
Who will use it if it isn't 100% safe?
I suppose anyone using mutable data with IMA appraise should, unless they have a redundant power supply and a kernel that never crashes. In a way this is like asking if the ima-appraise should be there for mutable data at all. All this is doing is that it improves the crash recovery reliability without taking anything away.
Okay, so why would anyone use mutable data with IMA appraise if it corrupts your files by design, both with and without this patchset?
Quoted 6 lines
Anyway, I think I'm getting along with my understanding of the page writeback slowly and the journal support will eventually be there at least as an add-on patch for those that want to use it and really need the last 0.n% reliability. Note that even without that patch you can build ima-appraise based systems that are 99.999% reliable just by
On what storage devices, workloads, and filesystems is this number for?
Quoted 7 lines
having the patch we're discussing here. Without it you would be orders of magnitude worse off. All we are doing is that we give it a fairly good chance to recover instead of giving up without even trying.
That said, I'm not sure the 100% crash recovery is ever guaranteed in any Linux system. We just have to do what we can, no?
Filesystems implement consistency mechanisms, e.g. journalling or copy-on-write, to recover from crashes by design. This patchset doesn't implement or use any such mechanism, so it's not crash-safe. It's not clear that it's even a step in the right direction, as no patches have been proposed for a correct solution so we can see what it actually involves.
- Eric
Quoted 10 lines · Eric Biggers
Quoted 7 lines
Quoted 1 line
Who will use it if it isn't 100% safe?
I suppose anyone using mutable data with IMA appraise should, unless they have a redundant power supply and a kernel that never crashes. In a way this is like asking if the ima-appraise should be there for mutable data at all. All this is doing is that it improves the crash recovery reliability without taking anything away.
Okay, so why would anyone use mutable data with IMA appraise if it corrupts your files by design, both with and without this patchset?
Now you are exaggerating heavily: it does not corrupt your files by design. A crash in any security related system is supposed to be pretty rare occurrence.
Quoted 7 lines
Quoted 5 lines
Anyway, I think I'm getting along with my understanding of the page writeback slowly and the journal support will eventually be there at least as an add-on patch for those that want to use it and really need the last 0.n% reliability. Note that even without that patch you can build ima-appraise based systems that are 99.999% reliable just by
On what storage devices, workloads, and filesystems is this number for?
I reached 99.2% recovery rate with the AOSP without touching the android on top by crashing the kernel with a test case while the device was in use. 80% if I crash it while the device is in the busiest write cycle (the first boot, I guess we would suck quite royally if we never made past this point without dying).
99.95+% of course requires a high-availability system that probably crashes once per year at best and recovers in seconds. In that case this will recover it with pretty high odds, so reliability is not all that much reduced from it's normal reliability statistics. So, the ima-appraise for the mutable data could be in use even in a high-availability system. 99% recovery probability for the crash that occurs once per year would be OK; 0% would not be. I suppose it all depends on your requirements.
Quoted 4 lines
Quoted 4 lines
having the patch we're discussing here. Without it you would be orders of magnitude worse off. All we are doing is that we give it a fairly good chance to recover instead of giving up without even trying.
Quoted 8 lines
Quoted 2 lines
That said, I'm not sure the 100% crash recovery is ever guaranteed in any Linux system. We just have to do what we can, no?
Filesystems implement consistency mechanisms, e.g. journalling or copy-on-write, to recover from crashes by design. This patchset doesn't implement or use any such mechanism, so it's not crash-safe. It's not clear that it's even a step in the right direction, as no patches have been proposed for a correct solution so we can see what it actually involves.
Great, what would be the better alternative? I guess the suggestion cannot be that 'don't use it' since the code is there?
As for the 'step to the right direction': before we could talk about any of this journaling stuff we had to make sure that we have the plumbing where the measurements are accurate. These patches do that and the journaling is the next step. All the journaling add-on does now is that it binds the page write and the xattr update into one transaction, so both of those run as sub-transactions of one master. Now, only when the master ends the data is moved out of the journal in one bundle. All this is so ridiculously simple I doubt my own eyes, but it seems to work fine apart from some slowdown on shutdown when processes call sync() like there is no tomorrow. Nevertheless, understanding the related code (the page writeback and the ext4) is pretty nasty and there are lots of things I need to understand about that still. The thing I'm currently trying to get my head around is that whether or not it is possible that we have a measurement over a page that was not eligible for the writeback. I'm also no ext4 expert so all help in that regard is highly appreciated if this type of thing is interesting to others.
Anyway, all this is good info. If this code is not needed upstream, I'm happy to stop working with it and will maintain this for my use only. Let me know,
--
Janne