Backports to support OpenBMC with IMA
Subsystems (5)
cover Backports to support OpenBMC with IMA
This series of patches are backports from recent extensions to core Linux filesystem code (support for STATX_CHANGE_COOKIE) and subsequent fixes to IMA and overlayfs so that IMA can be used by OpenBMC configured with overlayfs. Overlayfs is for example used by the p10bmc build.
The patches in this series have been cherry-picked from upstream Linux using the following commands:
git cherry-pick 131f4fd2c25a # 3 consecutive patches of longer series git cherry-pick a3bb710383cb
git cherry-pick a1175d6b1bda
git cherry-pick db1d1e8b9867 # IMA: use vfs_getattr_nosec ... git cherry-pick 18b44bc5a672 # ovl: Always reevaluate ...
Regards,
Stefan
Eric Snowberg (1):
ovl: Always reevaluate the file signature for IMA
Jeff Layton (4):
fs: uninline inode_query_iversion
fs: clarify when the i_version counter must be updated
vfs: plumb i_version handling into struct kstat
IMA: use vfs_getattr_nosec to get the i_version
fs/libfs.c | 36 +++++++++++++++++++ fs/overlayfs/super.c | 2 +- fs/stat.c | 17 +++++++-- include/linux/iversion.h | 60 ++++++++++++------------------- include/linux/stat.h | 9 +++++ security/integrity/ima/ima_api.c | 9 +++-- security/integrity/ima/ima_main.c | 12 ++++--- 7 files changed, 97 insertions(+), 48 deletions(-)
Signature · 1 line
2.40.1
PATCH 1/5 fs: uninline inode_query_iversion
From: Jeff Layton <[email protected]>
--- fs/libfs.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/iversion.h | 38 ++------------------------------------ 2 files changed, 38 insertions(+), 36 deletions(-)
fs/libfs.c +36 -0
@@ -1582,3 +1582,39 @@ bool inode_maybe_inc_iversion(struct inode *inode, bool force)return true; }EXPORT_SYMBOL (inode_maybe_inc_iversion); + +/** +* inode_query_iversion - read i_version for later use +* @inode: inode from which i_version should be read +* +* Read the inode i_version counter. This should be used by callers that wish +* to store the returned i_version for later comparison. This will guarantee +* that a later query of the i_version will result in a different value if +* anything has changed. +* +* In this implementation, we fetch the current value, set the QUERIED flag and +* then try to swap it into place with a cmpxchg, if it wasn't already set. If +* that fails, we try again with the newly fetched value from the cmpxchg. +*/ +u64 inode_query_iversion (struct inode *inode) +{ +u64 cur, new; + + cur =inode_peek_iversion_raw (inode); +do { +/* If flag is already set, then no need to swap */ +if (cur &I_VERSION_QUERIED ) { +/* +* This barrier (and the implicit barrier in the +* cmpxchg below) pairs with the barrier in +* inode_maybe_inc_iversion(). +*/ +smp_mb (); +break ; + } + + new = cur |I_VERSION_QUERIED ; + }while (!atomic64_try_cmpxchg (&inode->i_version, &cur, new)); +return cur >>I_VERSION_QUERIED_SHIFT ; +} +EXPORT_SYMBOL (inode_query_iversion);
include/linux/iversion.h +2 -36
@@ -234,42 +234,6 @@ inode_peek_iversion(const struct inode *inode)return inode_peek_iversion_raw (inode) >>I_VERSION_QUERIED_SHIFT ; } -/** -* inode_query_iversion - read i_version for later use -* @inode: inode from which i_version should be read -* -* Read the inode i_version counter. This should be used by callers that wish -* to store the returned i_version for later comparison. This will guarantee -* that a later query of the i_version will result in a different value if -* anything has changed. -* -* In this implementation, we fetch the current value, set the QUERIED flag and -* then try to swap it into place with a cmpxchg, if it wasn't already set. If -* that fails, we try again with the newly fetched value from the cmpxchg. -*/ -static inline u64 -inode_query_iversion (struct inode *inode) -{ -u64 cur, new; - - cur =inode_peek_iversion_raw (inode); -do { -/* If flag is already set, then no need to swap */ -if (cur &I_VERSION_QUERIED ) { -/* -* This barrier (and the implicit barrier in the -* cmpxchg below) pairs with the barrier in -* inode_maybe_inc_iversion(). -*/ -smp_mb (); -break ; - } - - new = cur |I_VERSION_QUERIED ; - }while (!atomic64_try_cmpxchg (&inode->i_version, &cur, new)); -return cur >>I_VERSION_QUERIED_SHIFT ; -} -/* * For filesystems without any sort of change attribute, the best we can * do is fake one up from the ctime:
@@ -283,6 +247,8 @@ static inline u64 time_to_chattr(struct timespec64 *t)return chattr; } +u64 inode_query_iversion (struct inode *inode); +/** * inode_eq_iversion_raw - check whether the raw i_version counter has changed * @inode: inode to check
Signature · 1 line
2.40.1
PATCH 2/5 fs: clarify when the i_version counter must be updated
In reply to Stefan BergerFrom: Jeff Layton <[email protected]>
The i_version field in the kernel has had different semantics over the decades, but NFSv4 has certain expectations. Update the comments in iversion.h to describe when the i_version must change.
--- include/linux/iversion.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
include/linux/iversion.h +20 -2
@@ -9,8 +9,26 @@* --------------------------- * The change attribute (i_version) is mandated by NFSv4 and is mostly for * knfsd, but is also used for other purposes (e.g. IMA). The i_version must -* appear different to observers if there was a change to the inode's data or -* metadata since it was last queried. +* appear larger to observers if there was an explicit change to the inode's +* data or metadata since it was last queried. +* +* An explicit change is one that would ordinarily result in a change to the +* inode status change time (aka ctime). i_version must appear to change, even +* if the ctime does not (since the whole point is to avoid missing updates due +* to timestamp granularity). If POSIX or other relevant spec mandates that the +* ctime must change due to an operation, then the i_version counter must be +* incremented as well. +* +* Making the i_version update completely atomic with the operation itself would +* be prohibitively expensive. Traditionally the kernel has updated the times on +* directories after an operation that changes its contents. For regular files, +* the ctime is usually updated before the data is copied into the cache for a +* write. This means that there is a window of time when an observer can +* associate a new timestamp with old file contents. Since the purpose of the +* i_version is to allow for better cache coherency, the i_version must always +* be updated after the results of the operation are visible. Updating it before +* and after a change is also permitted. (Note that no filesystems currently do +* this. Fixing that is a work-in-progress). * * Observers see the i_version as a 64-bit number that never decreases. If it * remains the same since it was last checked, then nothing has changed in the
Signature · 1 line
2.40.1
PATCH 3/5 vfs: plumb i_version handling into struct kstat
In reply to Stefan BergerFrom: Jeff Layton <[email protected]>
The NFS server has a lot of special handling for different types of change attribute access, depending on the underlying filesystem. In most cases, it's doing a getattr anyway and then fetching that value after the fact.
Rather that do that, add a new STATX_CHANGE_COOKIE flag that is a kernel-only symbol (for now). If requested and getattr can implement it, it can fill out this field. For IS_I_VERSION inodes, add a generic implementation in vfs_getattr_nosec. Take care to mask STATX_CHANGE_COOKIE off in requests from userland and in the result mask.
Since not all filesystems can give the same guarantees of monotonicity, claim a STATX_ATTR_CHANGE_MONOTONIC flag that filesystems can set to indicate that they offer an i_version value that can never go backward.
Eventually if we decide to make the i_version available to userland, we can just designate a field for it in struct statx, and move the STATX_CHANGE_COOKIE definition to the uapi header.
--- fs/stat.c | 17 +++++++++++++++-- include/linux/stat.h | 9 +++++++++ 2 files changed, 24 insertions(+), 2 deletions(-)
fs/stat.c +15 -2
@@ -18,6 +18,7 @@#include <linux/syscalls.h> #include <linux/pagemap.h> #include <linux/compat.h> +#include <linux/iversion.h> #include <linux/uaccess.h> #include <asm/unistd.h>
@@ -119,6 +120,11 @@ int vfs_getattr_nosec(const struct path *path, struct kstat *stat, stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |STATX_ATTR_DAX ); +if ((request_mask &STATX_CHANGE_COOKIE ) &&IS_I_VERSION (inode)) { + stat->result_mask |=STATX_CHANGE_COOKIE ; + stat->change_cookie =inode_query_iversion (inode); + } + mnt_userns =mnt_user_ns (path->mnt);if (inode->i_op->getattr) return inode->i_op->getattr(mnt_userns, path, stat,
@@ -599,9 +605,11 @@ cp_statx(const struct kstat *stat, struct statx __user *buffer)memset (&tmp,0 ,sizeof (tmp)); - tmp.stx_mask = stat->result_mask; +/* STATX_CHANGE_COOKIE is kernel-only for now */ + tmp.stx_mask = stat->result_mask & ~STATX_CHANGE_COOKIE ; tmp.stx_blksize = stat->blksize; - tmp.stx_attributes = stat->attributes; +/* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */ + tmp.stx_attributes = stat->attributes & ~STATX_ATTR_CHANGE_MONOTONIC ; tmp.stx_nlink = stat->nlink; tmp.stx_uid =from_kuid_munged (current_user_ns (), stat->uid); tmp.stx_gid =from_kgid_munged (current_user_ns (), stat->gid);
@@ -640,6 +648,11 @@ int do_statx(int dfd, struct filename *filename, unsigned int flags,if ((flags &AT_STATX_SYNC_TYPE ) ==AT_STATX_SYNC_TYPE )return -EINVAL ; +/* STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests +* from userland. +*/ + mask &= ~STATX_CHANGE_COOKIE ; + error =vfs_statx (dfd, filename, flags, &stat, mask);if (error)return error;
include/linux/stat.h +9 -0
@@ -52,6 +52,15 @@ struct kstat {u64 mnt_id;u32 dio_mem_align;u32 dio_offset_align; + u64 change_cookie; }; +/* These definitions are internal to the kernel for now. Mainly used by nfsd. */ + +/* mask values */ +#define STATX_CHANGE_COOKIE 0x40000000U/* Want/got stx_change_attr */ + +/* file attribute values */ +#define STATX_ATTR_CHANGE_MONOTONIC 0x8000000000000000ULL/* version monotonically increases */ +#endif
Signature · 1 line
2.40.1
PATCH 4/5 IMA: use vfs_getattr_nosec to get the i_version
In reply to Stefan BergerFrom: Jeff Layton <[email protected]>
IMA currently accesses the i_version out of the inode directly when it does a measurement. This is fine for most simple filesystems, but can be problematic with more complex setups (e.g. overlayfs).
Make IMA instead call vfs_getattr_nosec to get this info. This allows the filesystem to determine whether and how to report the i_version, and should allow IMA to work properly with a broader class of filesystems in the future.
--- security/integrity/ima/ima_api.c | 9 ++++++--- security/integrity/ima/ima_main.c | 12 ++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-)
security/integrity/ima/ima_api.c +6 -3
@@ -13,7 +13,6 @@#include <linux/fs.h> #include <linux/xattr.h> #include <linux/evm.h> -#include <linux/iversion.h> #include <linux/fsverity.h> #include "ima.h"
@@ -245,10 +244,11 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,struct inode *inode =file_inode (file);const char *filename = file->f_path.dentry->d_name.name;struct ima_max_digest_data hash; +struct kstat stat;int result =0 ;int length;void *tmpbuf; -u64 i_version; +u64 i_version =0 ;/* * Always collect the modsig, because IMA might have already collected
@@ -267,7 +267,10 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,* to an initial measurement/appraisal/audit, but was modified to * assume the file changed. */ - i_version =inode_query_iversion (inode); + 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; hash.hdr.algo = algo; hash.hdr.length = hash_digest_size[algo];
security/integrity/ima/ima_main.c +8 -4
@@ -24,7 +24,6 @@#include <linux/slab.h> #include <linux/xattr.h> #include <linux/ima.h> -#include <linux/iversion.h> #include <linux/fs.h> #include "ima.h"
@@ -163,11 +162,16 @@ static void ima_check_last_writer(struct integrity_iint_cache *iint,mutex_lock (&iint->mutex);if (atomic_read (&inode->i_writecount) ==1 ) { +struct kstat stat; + update =test_and_clear_bit (IMA_UPDATE_XATTR , &iint->atomic_flags); -if (!IS_I_VERSION (inode) || - !inode_eq_iversion (inode, iint->version) || - (iint->flags &IMA_NEW_FILE )) { +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->version) { iint->flags &= ~(IMA_DONE_MASK |IMA_NEW_FILE ); iint->measured_pcrs =0 ;if (update)
Signature · 1 line
2.40.1
PATCH 5/5 ovl: Always reevaluate the file signature for IMA
In reply to Stefan BergerFrom: Eric Snowberg <[email protected]>
Commit db1d1e8b9867 ("IMA: use vfs_getattr_nosec to get the i_version") partially closed an IMA integrity issue when directly modifying a file on the lower filesystem. If the overlay file is first opened by a user and later the lower backing file is modified by root, but the extended attribute is NOT updated, the signature validation succeeds with the old original signature.
Update the super_block s_iflags to SB_I_IMA_UNVERIFIABLE_SIGNATURE to force signature reevaluation on every file access until a fine grained solution can be found.
--- fs/overlayfs/super.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/super.c +1 -1
@@ -2155,7 +2155,7 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) ovl_trusted_xattr_handlers; sb->s_fs_info = ofs; sb->s_flags |=SB_POSIXACL ; - sb->s_iflags |=SB_I_SKIP_SYNC ; + sb->s_iflags |=SB_I_SKIP_SYNC |SB_I_IMA_UNVERIFIABLE_SIGNATURE ; err = -ENOMEM ; root_dentry =ovl_get_root (sb, upperpath.dentry, oe);
Signature · 1 line
2.40.1
Quoted 6 lines · Stefan Berger
This series of patches are backports from recent extensions to core Linux filesystem code (support for
STATX_CHANGE_COOKIE) and subsequent fixes to IMA and overlayfs so that IMA can be used by OpenBMC configured with overlayfs. Overlayfs is for example used by the p10bmc build.
Quoted 8 lines
The patches in this series have been cherry-picked from upstream Linux using the following commands:
git cherry-pick
131f4fd2c25a# 3 consecutive patches of longer series git cherry-picka3bb710383cb
git cherry-picka1175d6b1bda
git cherry-pickdb1d1e8b9867# IMA: usevfs_getattr_nosec... git cherry-pick18b44bc5a672# ovl: Always reevaluate ...
This is great. My only concern with backporting a bunch of upstream changes is we miss out on subsequent upstream fixes that modify this code.
The intent is to move to v6.5 shortly. Do you mind if we wait for that, and ensure the 6.5 tree has your changes (I assume the delta will be smaller)?
Cheers,
Joel
Quoted 25 lines
Regards,
StefanEric Snowberg (1):
ovl: Always reevaluate the file signature for IMAJeff Layton (4):
fs: uninlineinode_query_iversion
fs: clarify when thei_versioncounter must be updated
vfs: plumbi_versionhandling into struct kstat
IMA: usevfs_getattr_nosecto get thei_versionfs/libfs.c | 36 +++++++++++++++++++ fs/overlayfs/super.c | 2 +- fs/stat.c | 17 +++++++-- include/linux/iversion.h | 60 ++++++++++++------------------- include/linux/stat.h | 9 +++++ security/integrity/ima/ima_api.c | 9 +++-- security/integrity/ima/ima_main.c | 12 ++++--- 7 files changed, 97 insertions(+), 48 deletions(-)--
2.40.1
Quoted 14 lines · Joel Stanley
Quoted 5 lines · Stefan Berger
This series of patches are backports from recent extensions to core Linux filesystem code (support for
STATX_CHANGE_COOKIE) and subsequent fixes to IMA and overlayfs so that IMA can be used by OpenBMC configured with overlayfs. Overlayfs is for example used by the p10bmc build.Quoted 8 lines
The patches in this series have been cherry-picked from upstream Linux using the following commands:
git cherry-pick
131f4fd2c25a# 3 consecutive patches of longer series git cherry-picka3bb710383cb
git cherry-picka1175d6b1bda
git cherry-pickdb1d1e8b9867# IMA: usevfs_getattr_nosec... git cherry-pick18b44bc5a672# ovl: Always reevaluate ...
Quoted 7 lines
This is great. My only concern with backporting a bunch of upstream changes is we miss out on subsequent upstream fixes that modify this code.
The intent is to move to v6.5 shortly. Do you mind if we wait for that, and ensure the 6.5 tree has your changes (I assume the delta will be smaller)?
I can wait. 6.5.2 seems to have all these changes. (The first commit above seems wrong and should be c5bc1b3ff35ae321d018.)
Regards,
Stefan
Quoted 29 lines
Cheers,
Joel
Quoted 24 lines
Regards,
StefanEric Snowberg (1):
ovl: Always reevaluate the file signature for IMAJeff Layton (4):
fs: uninlineinode_query_iversion
fs: clarify when thei_versioncounter must be updated
vfs: plumbi_versionhandling into struct kstat
IMA: usevfs_getattr_nosecto get thei_versionfs/libfs.c | 36 +++++++++++++++++++ fs/overlayfs/super.c | 2 +- fs/stat.c | 17 +++++++-- include/linux/iversion.h | 60 ++++++++++++------------------- include/linux/stat.h | 9 +++++ security/integrity/ima/ima_api.c | 9 +++-- security/integrity/ima/ima_main.c | 12 ++++--- 7 files changed, 97 insertions(+), 48 deletions(-)--
2.40.1
Quoted 21 lines · Stefan Berger
Quoted 18 lines · Joel Stanley
Quoted 5 lines · Stefan Berger
This series of patches are backports from recent extensions to core Linux filesystem code (support for
STATX_CHANGE_COOKIE) and subsequent fixes to IMA and overlayfs so that IMA can be used by OpenBMC configured with overlayfs. Overlayfs is for example used by the p10bmc build.Quoted 8 lines
The patches in this series have been cherry-picked from upstream Linux using the following commands:
git cherry-pick
131f4fd2c25a# 3 consecutive patches of longer series git cherry-picka3bb710383cb
git cherry-picka1175d6b1bda
git cherry-pickdb1d1e8b9867# IMA: usevfs_getattr_nosec... git cherry-pick18b44bc5a672# ovl: Always reevaluate ...This is great. My only concern with backporting a bunch of upstream changes is we miss out on subsequent upstream fixes that modify this code.
Quoted 6 lines
Quoted 3 lines
The intent is to move to v6.5 shortly. Do you mind if we wait for that, and ensure the 6.5 tree has your changes (I assume the delta will be smaller)?
I can wait. 6.5.2 seems to have all these changes. (The first commit above seems wrong and should be
c5bc1b3ff35ae321d018.)
No worries. If the situation changes please reply to this mail and we can reconsider.
Cheers,
Joel