Commit db1d1e8b9867 ("IMA: use vfs_getattr_nosec to get the i_version")
replaced detecting file change based on i_version with
STATX_CHANGE_COOKIE.

On filesystems without STATX_CHANGE_COOKIE enabled, revert back to
detecting file change based on i_version.

Fixes: db1d1e8b9867 ("IMA: use vfs_getattr_nosec to get the i_version")
Signed-off-by: Mimi Zohar <[email protected]>
---
 security/integrity/ima/ima_api.c  | 13 ++++++++----
 security/integrity/ima/ima_main.c | 34 +++++++++++++++++++++++++------
 2 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/ima_api.c
index c35ea613c9f8..3f0c73a8b459 100644
--- a/security/integrity/ima/ima_api.c
+++ b/security/integrity/ima/ima_api.c
@@ -267,15 +267,20 @@ int ima_collect_measurement(struct ima_iint_cache *iint, 
struct file *file,
                goto out;
 
        /*
-        * Detecting file change is based on i_version. On filesystems
-        * which do not support i_version, support was originally limited
-        * to an initial measurement/appraisal/audit, but was modified to
-        * assume the file changed.
+        * Detect file change based on STATX_CHANGE_COOKIE, when supported,
+        * and fallback to detecting file change based on i_version.
+        *
+        * On filesystems which did not support i_version, support was
+        * originally limited to an initial measurement/appraisal/audit,
+        * but was later modified to assume the file changed.
         */
        result = vfs_getattr_nosec(&file->f_path, &stat, STATX_CHANGE_COOKIE,
                                   AT_STATX_SYNC_AS_STAT);
        if (!result && (stat.result_mask & STATX_CHANGE_COOKIE))
                i_version = stat.change_cookie;
+       else if (IS_I_VERSION(inode))
+               i_version = inode_peek_iversion(inode);
+
        hash.hdr.algo = algo;
        hash.hdr.length = hash_digest_size[algo];
 
diff --git a/security/integrity/ima/ima_main.c 
b/security/integrity/ima/ima_main.c
index 1d6229b156fb..fc50f727c954 100644
--- a/security/integrity/ima/ima_main.c
+++ b/security/integrity/ima/ima_main.c
@@ -184,7 +184,9 @@ static void ima_check_last_writer(struct ima_iint_cache 
*iint,
                                  struct inode *inode, struct file *file)
 {
        fmode_t mode = file->f_mode;
+       int modified = 0;
        bool update;
+       int result;
 
        if (!(mode & FMODE_WRITE))
                return;
@@ -197,12 +199,32 @@ static void ima_check_last_writer(struct ima_iint_cache 
*iint,
 
                update = test_and_clear_bit(IMA_UPDATE_XATTR,
                                            &iint->atomic_flags);
-               if ((iint->flags & IMA_NEW_FILE) ||
-                   vfs_getattr_nosec(&file->f_path, &stat,
-                                     STATX_CHANGE_COOKIE,
-                                     AT_STATX_SYNC_AS_STAT) ||
-                   !(stat.result_mask & STATX_CHANGE_COOKIE) ||
-                   stat.change_cookie != iint->real_inode.version) {
+
+               if (iint->flags & IMA_NEW_FILE)
+                       modified = 1;
+
+               /*
+                * Detect file change based on STATX_CHANGE_COOKIE, when
+                * supported, and fallback to detecting file change based
+                * on i_version. On filesystems which do not support either,
+                * assume the file changed.
+                */
+                result = vfs_getattr_nosec(&file->f_path, &stat,
+                                           STATX_CHANGE_COOKIE,
+                                           AT_STATX_SYNC_AS_STAT);
+
+               if (!result && stat.result_mask & STATX_CHANGE_COOKIE &&
+                   stat.change_cookie != iint->real_inode.version)
+                       modified = 1;
+               else if (!(stat.result_mask & STATX_CHANGE_COOKIE) &&
+                        IS_I_VERSION(inode) &&
+                        !(inode_eq_iversion(inode, iint->real_inode.version)))
+                       modified = 1;
+               else if (!(stat.result_mask & STATX_CHANGE_COOKIE) &&
+                        !(IS_I_VERSION(inode)))
+                       modified = 1;
+
+               if (modified) {
                        iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
                        iint->measured_pcrs = 0;
                        if (update)
-- 
2.53.0


Reply via email to