Skip to content

Commit 01baa39

Browse files
committed
ima: fallback to using i_version to detect file change
Commit db1d1e8 ("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. On filesystems which do not support either, assume the file changed. Reported-by: Frederick Lawler <fred@cloudflare.com> Fixes: db1d1e8 ("IMA: use vfs_getattr_nosec to get the i_version") Cc: stable@vger.kernel.org Reviewed-by: Frederick Lawler <fred@cloudflare.com> Tested-by: Frederick Lawler <fred@cloudflare.com> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 0ec959c commit 01baa39

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

security/integrity/ima/ima_api.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,20 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
269269
goto out;
270270

271271
/*
272-
* Detecting file change is based on i_version. On filesystems
273-
* which do not support i_version, support was originally limited
274-
* to an initial measurement/appraisal/audit, but was modified to
275-
* assume the file changed.
272+
* Detect file change based on STATX_CHANGE_COOKIE, when supported,
273+
* and fallback to detecting file change based on i_version.
274+
*
275+
* On filesystems which did not support i_version, support was
276+
* originally limited to an initial measurement/appraisal/audit,
277+
* but was later modified to assume the file changed.
276278
*/
277279
result = vfs_getattr_nosec(&file->f_path, &stat, STATX_CHANGE_COOKIE,
278280
AT_STATX_SYNC_AS_STAT);
279281
if (!result && (stat.result_mask & STATX_CHANGE_COOKIE))
280282
i_version = stat.change_cookie;
283+
else if (IS_I_VERSION(real_inode))
284+
i_version = inode_peek_iversion(real_inode);
285+
281286
hash.hdr.algo = algo;
282287
hash.hdr.length = hash_digest_size[algo];
283288

security/integrity/ima/ima_main.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ static void ima_rdwr_violation_check(struct file *file,
180180
"invalid_pcr", "open_writers");
181181
}
182182

183+
/*
184+
* Detect file change based on STATX_CHANGE_COOKIE, when supported, and
185+
* fallback to detecting file change based on i_version. On filesystems
186+
* which do not support either, assume the file changed.
187+
*/
188+
static bool ima_detect_file_change(struct ima_iint_cache *iint,
189+
struct inode *inode, struct file *file)
190+
{
191+
struct kstat stat;
192+
int result;
193+
194+
result = vfs_getattr_nosec(&file->f_path, &stat, STATX_CHANGE_COOKIE,
195+
AT_STATX_SYNC_AS_STAT);
196+
197+
if (!result && stat.result_mask & STATX_CHANGE_COOKIE)
198+
return stat.change_cookie != iint->real_inode.version;
199+
200+
if (IS_I_VERSION(inode))
201+
return !inode_eq_iversion(inode, iint->real_inode.version);
202+
203+
return true;
204+
}
205+
183206
static void ima_check_last_writer(struct ima_iint_cache *iint,
184207
struct inode *inode, struct file *file)
185208
{
@@ -191,18 +214,13 @@ static void ima_check_last_writer(struct ima_iint_cache *iint,
191214

192215
mutex_lock(&iint->mutex);
193216
if (atomic_read(&inode->i_writecount) == 1) {
194-
struct kstat stat;
195-
196217
clear_bit(IMA_EMITTED_OPENWRITERS, &iint->atomic_flags);
197218

198219
update = test_and_clear_bit(IMA_UPDATE_XATTR,
199220
&iint->atomic_flags);
200-
if ((iint->flags & IMA_NEW_FILE) ||
201-
vfs_getattr_nosec(&file->f_path, &stat,
202-
STATX_CHANGE_COOKIE,
203-
AT_STATX_SYNC_AS_STAT) ||
204-
!(stat.result_mask & STATX_CHANGE_COOKIE) ||
205-
stat.change_cookie != iint->real_inode.version) {
221+
222+
if (iint->flags & IMA_NEW_FILE ||
223+
ima_detect_file_change(iint, inode, file)) {
206224
iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
207225
iint->measured_pcrs = 0;
208226
if (update)

0 commit comments

Comments
 (0)