ima: detect changes to the backing overlay file
Subsystems (5)
Mimi Zohar
maintainerPATCH 1/1 ima: detect changes to the backing overlay file
Commit 18b44bc5a672 ("ovl: Always reevaluate the file signature for IMA") forced signature re-evaulation on every file access.
IMA does not detect changes made to the backing overlay file. Instead of always re-evaluating the file's integrity, detect a change to the backing overlay file, by comparing the i_version, as stored in the iint, with the backing file's i_version.
--- fs/overlayfs/super.c | 2 +- security/integrity/ima/ima_api.c | 1 + security/integrity/ima/ima_main.c | 21 ++++++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-)
fs/overlayfs/super.c +1 -1
@@ -1482,7 +1482,7 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc) ovl_trusted_xattr_handlers; sb->s_fs_info = ofs; sb->s_flags |=SB_POSIXACL ; - sb->s_iflags |=SB_I_SKIP_SYNC |SB_I_IMA_UNVERIFIABLE_SIGNATURE ; + sb->s_iflags |=SB_I_SKIP_SYNC ; err = -ENOMEM ; root_dentry =ovl_get_root (sb, ctx->upper.dentry, oe);
security/integrity/ima/ima_api.c +1 -0
@@ -272,6 +272,7 @@ int ima_collect_measurement(struct integrity_iint_cache *iint, AT_STATX_SYNC_AS_STAT);if (!result && (stat.result_mask &STATX_CHANGE_COOKIE )) i_version = stat.change_cookie; + hash.hdr.algo = algo; hash.hdr.length = hash_digest_size[algo];
security/integrity/ima/ima_main.c +20 -1
@@ -25,6 +25,7 @@#include <linux/xattr.h> #include <linux/ima.h> #include <linux/fs.h> +#include <linux/iversion.h> #include "ima.h"
@@ -207,7 +208,7 @@ static int process_measurement(struct file *file, const struct cred *cred, u32 secid, char *buf, loff_t size, int mask, enum ima_hooks func) { - struct inode *inode = file_inode(file); + struct inode *backing_inode, *inode = file_inode(file);struct integrity_iint_cache *iint =NULL ;struct ima_template_desc *template_desc =NULL ;char *pathbuf =NULL ;
@@ -284,6 +285,24 @@ static int process_measurement(struct file *file, const struct cred *cred, iint->measured_pcrs =0 ; } +/* +* IMA does not detect changes made to the backing overlay file. +* If the backing file's i_version is greater than the overlay +* file's i_version or the backing file doesn't support iversion, +* clear the cache to force the file's integrity to be re-evaluated. +*/ +if (inode->i_sb->s_magic ==0x794c7630 && + (action &IMA_DO_MASK ) && (iint->flags &IMA_DONE_MASK )) { + backing_inode =d_real_inode (file_dentry (file)); +if (backing_inode != inode) { +if (!IS_I_VERSION (backing_inode) || +inode_peek_iversion (backing_inode) > iint->version) { + iint->flags &= ~IMA_DONE_MASK ; + iint->measured_pcrs =0 ; + } + } + } +/* Determine if already appraised/measured based on bitmask * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED, * IMA_AUDIT, IMA_AUDITED)
Signature · 1 line
2.39.3
Amir Goldstein
maintainerQuoted 63 lines · Mimi Zohar
Commit
18b44bc5a672("ovl: Always reevaluate the file signature for IMA") forced signature re-evaulation on every file access.IMA does not detect changes made to the backing overlay file. Instead of always re-evaluating the file's integrity, detect a change to the backing overlay file, by comparing the
i_version, as stored in the iint, with the backing file'si_version.Signed-off-by Mimi Zohar <[email protected]>--- fs/overlayfs/super.c | 2 +- security/integrity/ima/ima_api.c | 1 + security/integrity/ima/ima_main.c | 21 ++++++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-)fs/overlayfs/super.c +1
-1@@ -1482,7 +1482,7 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc) ovl_trusted_xattr_handlers; sb->s_fs_info = ofs; sb->s_flags |=SB_POSIXACL ; - sb->s_iflags |=SB_I_SKIP_SYNC |SB_I_IMA_UNVERIFIABLE_SIGNATURE ; + sb->s_iflags |=SB_I_SKIP_SYNC ; err = -ENOMEM ; root_dentry =ovl_get_root (sb, ctx->upper.dentry, oe);security/integrity/ima/ima_api.c +1
-0@@ -272,6 +272,7 @@ int ima_collect_measurement(struct integrity_iint_cache *iint, AT_STATX_SYNC_AS_STAT);if (!result && (stat.result_mask &STATX_CHANGE_COOKIE )) i_version = stat.change_cookie; + hash.hdr.algo = algo; hash.hdr.length = hash_digest_size[algo];security/integrity/ima/ima_main.c +2
-1@@ -25,6 +25,7 @@#include <linux/xattr.h> #include <linux/ima.h> #include <linux/fs.h> +#include <linux/iversion.h> #include "ima.h" @@ -207,7 +208,7 @@ static int process_measurement(struct file *file, const struct cred *cred, u32 secid, char *buf, loff_t size, int mask, enum ima_hooks func) { - struct inode *inode = file_inode(file); + struct inode *backing_inode, *inode = file_inode(file);struct integrity_iint_cache *iint =NULL ;struct ima_template_desc *template_desc =NULL ;char *pathbuf =NULL ;
security/integrity/ima/ima_main.c static int process_measurement(struct file *file, const struct cred *cred, · line 294
Quoted 10 lines
security/integrity/ima/ima_main.c +7
-0@@ -284,6 +285,24 @@ static int process_measurement(struct file *file, const struct cred *cred, iint->measured_pcrs =0 ; } +/* +* IMA does not detect changes made to the backing overlay file. +* If the backing file's i_version is greater than the overlay +* file's i_version or the backing file doesn't support iversion, +* clear the cache to force the file's integrity to be re-evaluated. +*/ + if (inode->i_sb->s_magic == 0x794c7630 &&
Please don't use magic to detect overlayfs. Overlayfs is not the only fs that can have a backing file and this test is not needed.
security/integrity/ima/ima_main.c static int process_measurement(struct file *file, const struct cred *cred, · line 297
Quoted 3 lines
security/integrity/ima/ima_main.c +3
-0+ (action & IMA_DO_MASK) && (iint->flags & IMA_DONE_MASK)) { + backing_inode =d_real_inode (file_dentry (file)); +if (backing_inode != inode) {
This test is sufficient, because for regular files of filesystems without ->d_real(),backing_inode will always be == inode.
security/integrity/ima/ima_main.c static int process_measurement(struct file *file, const struct cred *cred, · line 299
Quoted 2 lines
security/integrity/ima/ima_main.c +2
-0+if (!IS_I_VERSION (backing_inode) || +inode_peek_iversion (backing_inode) > iint->version) {
IIUC, this checking > iint->version is not enough.
The reason is that you could have recorded iint->version from i_version of a lower file, after open for write, lower file gets copied up to upper file, which is a completely different backing_inode, so there is no guarantee that the copy up will not result in a smaller or even equal i_version.
For a complete solution, you will need to store the backing_inode pointer or backing dev/ino in iint to make sure that backing_inode did not change.
Thanks,
Amir.