linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes
@ 2023-03-14  8:17 Roberto Sassu
  2023-03-14  8:17 ` [PATCH v8 1/6] reiserfs: Switch to security_inode_init_security() Roberto Sassu
                   ` (6 more replies)
  0 siblings, 7 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:17 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

One of the major goals of LSM stacking is to run multiple LSMs side by side
without interfering with each other. The ultimate decision will depend on
individual LSM decision.

Several changes need to be made to the LSM infrastructure to be able to
support that. This patch set tackles one of them: gives to each LSM the
ability to specify one or multiple xattrs to be set at inode creation
time and, at the same time, gives to EVM the ability to access all those
xattrs and calculate the HMAC on them.

The first problem that this patch set addresses is to make the
inode_init_security hook definition suitable to use with EVM which, unlike
other LSMs, needs to have visibility of all xattrs and not only the one
that the LSM infrastructure passes to the LSM to be set.

The solution is to replace in the inode_init_security definition the
name/value/len parameters with the beginning of the array containing all
xattrs set by LSMs. Due to security_old_inode_init_security() API
limitation of setting only one xattr, it has been dropped and the remaining
users, ocfs2 and reiserfs, switch to security_inode_init_security().
However, due to the complexity of the changes required to fully exploit the
ability of security_inode_init_security() to set multiple xattrs, those
users can still set only one xattr (the first set in the xattr array) where
previously they called security_old_inode_init_security().

Furthermore, while EVM is invoked unlike before, its xattr will not be set
as it would not be the first set in the xattr array, or if it is the first,
there would not be protected xattrs to calculate the HMAC on.

The second problem this patch set addresses is the limitation of the
call_int_hook() of stopping the loop when the return value from a hook
implementation is not zero. Unfortunately, for the inode_init_security hook
it is a legitimate case to return -EOPNOTSUPP, but this would not
necessarily mean that there is an error to report to the LSM infrastructure
but just that an LSM does not will to set an xattr. Other LSMs should be
still consulted as well.

The solution for this specific case is to replace the call_int_hook() with
the loop itself, so that -EOPNOTSUPP can be ignored.

Next, this patch set removes the limitation of creating only two xattrs,
one by an active LSM and another by EVM. This patch set extends the
reservation mechanism of the LSM infrastructure, to allow each LSM to
request one or multiple xattrs. While this could potentially lead to
reaching the filesystem limits of number/size of the xattrs, it seems not
an issue that need to be solved by the LSM infrastructure but by the
filesystems themselves. Currently, if the limit is reached, the only
workaround would be to use fewer LSMs.

The reservation mechanism concept makes it very easy for LSMs to position
themselves correctly in the xattr array, as the LSM infrastructure at
initialization time changes the number of xattrs requested by each LSM with
an offset. LSMs can just take that offset as the starting index in the
xattr array and fill the next slots depending on how many xattrs they
requested.

However, while this concept is intuitive, it needs extra care. While for
security blobs (the main reason of the reservation mechanism) it is not
relevant for an LSM if other LSMs filled their portion, it matters for
xattrs, as both EVM and initxattrs() callbacks scan the entire array until
a terminator (xattr with NULL name). If an LSM did not provide an xattr,
which could happen if it is loaded but not initialized, consumers of the
xattr array would stop prematurely.

This patch set avoids this problem by compacting the xattr array each time
after an LSM executed its implementation of the inode_init_security hook.
It needs to be done after each LSM, and not after all, since there might be
LSMs scanning that xattr array too. Compacting the array after all LSMs
would be too late.

Finally, this patch set modifies the evm_inode_init_security() definition
to be compatible with the inode_init_security hook definition and adds
support for scanning the whole xattr array and for calculating the HMAC
on all xattrs provided by LSMs.

This patch set has been tested by introducing several instances of a
TestLSM (some providing an xattr, some not, one with a wrong implementation
to see how the LSM infrastructure handles it, one providing multiple xattrs
and another providing an xattr but in a disabled state). The patch is not
included in this set but it is available here:

https://github.com/robertosassu/linux/commit/311b83a98757915cc3ccb363f545578e7c38df54

The test, added to ima-evm-utils, is available here:

https://github.com/robertosassu/ima-evm-utils/blob/evm-multiple-lsms-v8-devel-v2/tests/evm_multiple_lsms.test

The test takes a UML kernel built by Github Actions and launches it several
times, each time with a different combination of LSMs and filesystems (ext4,
reiserfs, ocfs2). After boot, it first checks that there is an xattr for each
LSM providing it (for reiserfs and ocfs2 just the first LSM), and then (for
ext4) calculates the HMAC in user space and compares it with the HMAC
calculated by EVM in kernel space.

A test report can be obtained here:

https://github.com/robertosassu/ima-evm-utils/actions/runs/4403223954/jobs/7722101662

The patch set has been tested with both the SElinux and Smack test suites.
Below, there is the summary of the test results:

SELinux Test Suite result (without patches):
All tests successful.
Files=76, Tests=1343, 255 wallclock secs ( 0.51 usr  0.19 sys +  6.30 cusr 58.15 csys = 65.15 CPU)
Result: PASS

SELinux Test Suite result (with patches):
All tests successful.
Files=76, Tests=1343, 258 wallclock secs ( 0.49 usr  0.21 sys +  6.46 cusr 59.27 csys = 66.43 CPU)
Result: PASS

Smack Test Suite result (without patches):
95 Passed, 0 Failed, 100% Success rate

Smack Test Suite result (with patches):
95 Passed, 0 Failed, 100% Success rate

Changelog

v7:
- Add a patch dependency comment in patch 1 (suggested by Mimi)
- Restore check of -EOPNOTSUPP status in ocfs2_mknod() and ocfs2_symlink()
  (reported by Mimi)
- Add explanation in evm_inode_init_security() why walking through the
  xattrs array is safe (suggested by Mimi)
- Document the lbs_xattr field of struct lsm_blob_sizes (suggested by
  Casey)
- Move documentation changes of the inode_init_security hook to security.c,
  after LSM documentation reorganization by Paul
- Use attributes in plural form in the description of the xattrs parameter
  of smack_inode_init_security()
- Check xattr name instead of xattr value in evm_inode_init_security(),
  for consistency with evm_init_hmac(); equivalent, since
  security_check_compact_filled_xattrs() rejects xattrs with xattr name
  NULL and value not NULL, and viceversa

v6:
- Add a comment in Smack to introduce its xattrs (suggested by Casey)
- Document the overloaded meaning of -EOPNOTSUPP in
  security_inode_init_security() (suggested by Mimi)

v5:
- Modify the cover letter to explain that the goal of this patch set is
  supporting multiple per LSM xattrs in EVM, and not moving IMA and EVM to
  the LSM infrastructure (suggested by Mimi)
- Remove references in the patches description about moving IMA and EVM
  to the LSM infrastructure (suggested by Mimi)
- Explain that the additional EVM invocation due to the switch to
  security_inode_init_security() will not cause the EVM xattr to be added
  (suggested by Mimi)

v4:
- Remove patch to call reiserfs_security_free(), already queued
- Switch ocfs2 and reiserfs to security_inode_init_security() (suggested by
  Mimi)
- Remove security_old_inode_init_security() (suggested by Paul)
- Rename security_check_compact_xattrs() to
  security_check_compact_filled_xattrs() and add function description
  (suggested by Mimi)
- Rename checked_xattrs parameter of security_check_compact_filled_xattrs()
  to num_filled_xattrs (suggested by Mimi)
- Rename cur_xattrs variable in security_inode_init_security() to
  num_filled_xattrs (suggested by Mimi)

v3:
- Don't free the xattr name in reiserfs_security_free()
- Don't include fs_data parameter in inode_init_security hook
- Don't change evm_inode_init_security(), as it will be removed if EVM is
  stacked
- Fix inode_init_security hook documentation
- Drop lsm_find_xattr_slot(), use simple xattr reservation mechanism and
  introduce security_check_compact_xattrs() to compact the xattr array
- Don't allocate xattr array if LSMs didn't reserve any xattr
- Return zero if initxattrs() is not provided to
  security_inode_init_security(), -EOPNOTSUPP if value is not provided to
  security_old_inode_init_security()
- Request LSMs to fill xattrs if only value (not the triple) is provided to
  security_old_inode_init_security(), to avoid unnecessary memory
  allocation

v2:
- rewrite selinux_old_inode_init_security() to use
  security_inode_init_security()
- add lbs_xattr field to lsm_blob_sizes structure, to give the ability to
  LSMs to reserve slots in the xattr array (suggested by Casey)
- add new parameter base_slot to inode_init_security hook definition

v1:
- add calls to reiserfs_security_free() and initialize sec->value to NULL
  (suggested by Tetsuo and Mimi)
- change definition of inode_init_security hook, replace the name, value
  and len triple with the xattr array (suggested by Casey)
- introduce lsm_find_xattr_slot() helper for LSMs to find an unused slot in
  the passed xattr array

Roberto Sassu (6):
  reiserfs: Switch to security_inode_init_security()
  ocfs2: Switch to security_inode_init_security()
  security: Remove security_old_inode_init_security()
  security: Allow all LSMs to provide xattrs for inode_init_security
    hook
  evm: Align evm_inode_init_security() definition with LSM
    infrastructure
  evm: Support multiple LSMs providing an xattr

 fs/ocfs2/namei.c                    |   2 +
 fs/ocfs2/xattr.c                    |  30 ++++++-
 fs/reiserfs/xattr_security.c        |  23 +++--
 include/linux/evm.h                 |  12 +--
 include/linux/lsm_hook_defs.h       |   3 +-
 include/linux/lsm_hooks.h           |   1 +
 include/linux/security.h            |  12 ---
 security/integrity/evm/evm.h        |   2 +
 security/integrity/evm/evm_crypto.c |   9 +-
 security/integrity/evm/evm_main.c   |  33 +++++--
 security/security.c                 | 131 +++++++++++++++++++++-------
 security/selinux/hooks.c            |  19 ++--
 security/smack/smack_lsm.c          |  33 ++++---
 13 files changed, 224 insertions(+), 86 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PATCH v8 1/6] reiserfs: Switch to security_inode_init_security()
  2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
@ 2023-03-14  8:17 ` Roberto Sassu
  2023-03-23 23:33   ` Paul Moore
  2023-03-14  8:17 ` [PATCH v8 2/6] ocfs2: " Roberto Sassu
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:17 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

In preparation for removing security_old_inode_init_security(), switch to
security_inode_init_security(). Commit 572302af1258 ("reiserfs: Add missing
calls to reiserfs_security_free()") fixed possible memory leaks and another
issue related to adding an xattr at inode creation time.

Define the initxattrs callback reiserfs_initxattrs(), to populate the
name/value/len triple in the reiserfs_security_handle() with the first
xattr provided by LSMs. Make a copy of the xattr value, as
security_inode_init_security() frees it.

After the call to security_inode_init_security(), remove the check for
returning -EOPNOTSUPP, as security_inode_init_security() changes it to
zero.

Multiple xattrs are currently not supported, as the
reiserfs_security_handle structure is exported to user space. As a
consequence, even if EVM is invoked, it will not provide an xattr (if it
is not the first to set it, its xattr will be discarded; if it is the
first, it does not have xattrs to calculate the HMAC on).

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 fs/reiserfs/xattr_security.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/fs/reiserfs/xattr_security.c b/fs/reiserfs/xattr_security.c
index 41c0ea84fbf..6bffdf9a4fd 100644
--- a/fs/reiserfs/xattr_security.c
+++ b/fs/reiserfs/xattr_security.c
@@ -39,6 +39,22 @@ static bool security_list(struct dentry *dentry)
 	return !IS_PRIVATE(d_inode(dentry));
 }
 
+static int
+reiserfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
+		    void *fs_info)
+{
+	struct reiserfs_security_handle *sec = fs_info;
+
+	sec->value = kmemdup(xattr_array->value, xattr_array->value_len,
+			     GFP_KERNEL);
+	if (!sec->value)
+		return -ENOMEM;
+
+	sec->name = xattr_array->name;
+	sec->length = xattr_array->value_len;
+	return 0;
+}
+
 /* Initializes the security context for a new inode and returns the number
  * of blocks needed for the transaction. If successful, reiserfs_security
  * must be released using reiserfs_security_free when the caller is done. */
@@ -56,12 +72,9 @@ int reiserfs_security_init(struct inode *dir, struct inode *inode,
 	if (IS_PRIVATE(dir))
 		return 0;
 
-	error = security_old_inode_init_security(inode, dir, qstr, &sec->name,
-						 &sec->value, &sec->length);
+	error = security_inode_init_security(inode, dir, qstr,
+					     &reiserfs_initxattrs, sec);
 	if (error) {
-		if (error == -EOPNOTSUPP)
-			error = 0;
-
 		sec->name = NULL;
 		sec->value = NULL;
 		sec->length = 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v8 2/6] ocfs2: Switch to security_inode_init_security()
  2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
  2023-03-14  8:17 ` [PATCH v8 1/6] reiserfs: Switch to security_inode_init_security() Roberto Sassu
@ 2023-03-14  8:17 ` Roberto Sassu
  2023-03-17 19:39   ` Mimi Zohar
  2023-03-23 23:37   ` Paul Moore
  2023-03-14  8:17 ` [PATCH v8 3/6] security: Remove security_old_inode_init_security() Roberto Sassu
                   ` (4 subsequent siblings)
  6 siblings, 2 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:17 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

In preparation for removing security_old_inode_init_security(), switch to
security_inode_init_security().

Extend the existing ocfs2_initxattrs() to take the
ocfs2_security_xattr_info structure from fs_info, and populate the
name/value/len triple with the first xattr provided by LSMs.

As fs_info was not used before, ocfs2_initxattrs() can now handle the case
of replicating the behavior of security_old_inode_init_security(), i.e.
just obtaining the xattr, in addition to setting all xattrs provided by
LSMs.

Supporting multiple xattrs is not currently supported where
security_old_inode_init_security() was called (mknod, symlink), as it
requires non-trivial changes that can be done at a later time. Like for
reiserfs, even if EVM is invoked, it will not provide an xattr (if it is
not the first to set it, its xattr will be discarded; if it is the first,
it does not have xattrs to calculate the HMAC on).

Finally, since security_inode_init_security(), unlike
security_old_inode_init_security(), returns zero instead of -EOPNOTSUPP if
no xattrs were provided by LSMs or if inodes are private, additionally
check in ocfs2_init_security_get() if the xattr name is set.

If not, act as if security_old_inode_init_security() returned -EOPNOTSUPP,
and set si->enable to zero to notify to the functions following
ocfs2_init_security_get() that no xattrs are available.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Acked-by: Joseph Qi <joseph.qi@linux.alibaba.com>
---
 fs/ocfs2/namei.c |  2 ++
 fs/ocfs2/xattr.c | 30 ++++++++++++++++++++++++++----
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/fs/ocfs2/namei.c b/fs/ocfs2/namei.c
index 9175dbc4720..17c52225b87 100644
--- a/fs/ocfs2/namei.c
+++ b/fs/ocfs2/namei.c
@@ -242,6 +242,7 @@ static int ocfs2_mknod(struct mnt_idmap *idmap,
 	int want_meta = 0;
 	int xattr_credits = 0;
 	struct ocfs2_security_xattr_info si = {
+		.name = NULL,
 		.enable = 1,
 	};
 	int did_quota_inode = 0;
@@ -1805,6 +1806,7 @@ static int ocfs2_symlink(struct mnt_idmap *idmap,
 	int want_clusters = 0;
 	int xattr_credits = 0;
 	struct ocfs2_security_xattr_info si = {
+		.name = NULL,
 		.enable = 1,
 	};
 	int did_quota = 0, did_quota_inode = 0;
diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
index 389308efe85..469ec45baee 100644
--- a/fs/ocfs2/xattr.c
+++ b/fs/ocfs2/xattr.c
@@ -7259,9 +7259,21 @@ static int ocfs2_xattr_security_set(const struct xattr_handler *handler,
 static int ocfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
 		     void *fs_info)
 {
+	struct ocfs2_security_xattr_info *si = fs_info;
 	const struct xattr *xattr;
 	int err = 0;
 
+	if (si) {
+		si->value = kmemdup(xattr_array->value, xattr_array->value_len,
+				    GFP_KERNEL);
+		if (!si->value)
+			return -ENOMEM;
+
+		si->name = xattr_array->name;
+		si->value_len = xattr_array->value_len;
+		return 0;
+	}
+
 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
 		err = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
 				      xattr->name, xattr->value,
@@ -7277,13 +7289,23 @@ int ocfs2_init_security_get(struct inode *inode,
 			    const struct qstr *qstr,
 			    struct ocfs2_security_xattr_info *si)
 {
+	int ret;
+
 	/* check whether ocfs2 support feature xattr */
 	if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
 		return -EOPNOTSUPP;
-	if (si)
-		return security_old_inode_init_security(inode, dir, qstr,
-							&si->name, &si->value,
-							&si->value_len);
+	if (si) {
+		ret = security_inode_init_security(inode, dir, qstr,
+						   &ocfs2_initxattrs, si);
+		/*
+		 * security_inode_init_security() does not return -EOPNOTSUPP,
+		 * we have to check the xattr ourselves.
+		 */
+		if (!ret && !si->name)
+			si->enable = 0;
+
+		return ret;
+	}
 
 	return security_inode_init_security(inode, dir, qstr,
 					    &ocfs2_initxattrs, NULL);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v8 3/6] security: Remove security_old_inode_init_security()
  2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
  2023-03-14  8:17 ` [PATCH v8 1/6] reiserfs: Switch to security_inode_init_security() Roberto Sassu
  2023-03-14  8:17 ` [PATCH v8 2/6] ocfs2: " Roberto Sassu
@ 2023-03-14  8:17 ` Roberto Sassu
  2023-03-23 23:41   ` Paul Moore
  2023-03-14  8:17 ` [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook Roberto Sassu
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:17 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

As the remaining two users reiserfs and ocfs2 switched to
security_inode_init_security(), security_old_inode_init_security() can be
now removed.

Out-of-tree kernel modules should switch to security_inode_init_security()
too.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 include/linux/security.h | 12 ------------
 security/security.c      | 11 -----------
 2 files changed, 23 deletions(-)

diff --git a/include/linux/security.h b/include/linux/security.h
index 5984d0d550b..cd23221ce9e 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -336,9 +336,6 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
 int security_inode_init_security_anon(struct inode *inode,
 				      const struct qstr *name,
 				      const struct inode *context_inode);
-int security_old_inode_init_security(struct inode *inode, struct inode *dir,
-				     const struct qstr *qstr, const char **name,
-				     void **value, size_t *len);
 int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode);
 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
 			 struct dentry *new_dentry);
@@ -778,15 +775,6 @@ static inline int security_inode_init_security_anon(struct inode *inode,
 	return 0;
 }
 
-static inline int security_old_inode_init_security(struct inode *inode,
-						   struct inode *dir,
-						   const struct qstr *qstr,
-						   const char **name,
-						   void **value, size_t *len)
-{
-	return -EOPNOTSUPP;
-}
-
 static inline int security_inode_create(struct inode *dir,
 					 struct dentry *dentry,
 					 umode_t mode)
diff --git a/security/security.c b/security/security.c
index b808e1b8655..f4170efcddd 100644
--- a/security/security.c
+++ b/security/security.c
@@ -1655,17 +1655,6 @@ int security_inode_init_security_anon(struct inode *inode,
 			     context_inode);
 }
 
-int security_old_inode_init_security(struct inode *inode, struct inode *dir,
-				     const struct qstr *qstr, const char **name,
-				     void **value, size_t *len)
-{
-	if (unlikely(IS_PRIVATE(inode)))
-		return -EOPNOTSUPP;
-	return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
-			     qstr, name, value, len);
-}
-EXPORT_SYMBOL(security_old_inode_init_security);
-
 #ifdef CONFIG_SECURITY_PATH
 /**
  * security_path_mknod() - Check if creating a special file is allowed
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
                   ` (2 preceding siblings ...)
  2023-03-14  8:17 ` [PATCH v8 3/6] security: Remove security_old_inode_init_security() Roberto Sassu
@ 2023-03-14  8:17 ` Roberto Sassu
  2023-03-24  0:09   ` Paul Moore
  2023-03-14  8:17 ` [PATCH v8 5/6] evm: Align evm_inode_init_security() definition with LSM infrastructure Roberto Sassu
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:17 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Currently, security_inode_init_security() supports only one LSM providing
an xattr and EVM calculating the HMAC on that xattr, plus other inode
metadata.

Allow all LSMs to provide one or multiple xattrs, by extending the security
blob reservation mechanism. Introduce the new lbs_xattr field of the
lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
needs, and the LSM infrastructure knows how many xattr slots it should
allocate.

Dynamically allocate the xattrs array to be populated by LSMs with the
inode_init_security hook, and pass it to the latter instead of the
name/value/len triple. Update the documentation accordingly, and fix the
description of the xattr name, as it is not allocated anymore.

Since the LSM infrastructure, at initialization time, updates the number of
the requested xattrs provided by each LSM with a corresponding offset in
the security blob (in this case the xattr array), it makes straightforward
for an LSM to access the right position in the xattr array.

There is still the issue that an LSM might not fill the xattr, even if it
requests it (legitimate case, for example it might have been loaded but not
initialized with a policy). Since users of the xattr array (e.g. the
initxattrs() callbacks) detect the end of the xattr array by checking if
the xattr name is NULL, not filling an xattr would cause those users to
stop scanning xattrs prematurely.

Solve that issue by introducing security_check_compact_filled_xattrs(),
which does a basic check of the xattr array (if the xattr name is filled,
the xattr value should be too, and viceversa), and compacts the xattr array
by removing the holes.

An alternative solution would be to let users of the xattr array know the
number of elements of that array, so that they don't have to check the
termination. However, this seems more invasive, compared to a simple move
of few array elements.

security_check_compact_filled_xattrs() also determines how many xattrs in
the xattr array have been filled. If there is none, skip
evm_inode_init_security() and initxattrs(). Skipping the former also avoids
EVM to crash the kernel, as it is expecting a filled xattr.

Finally, adapt both SELinux and Smack to use the new definition of the
inode_init_security hook, and to correctly fill the designated slots in the
xattr array. For Smack, reserve space for the other defined xattrs although
they are not set yet in smack_inode_init_security().

Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 include/linux/lsm_hook_defs.h |   3 +-
 include/linux/lsm_hooks.h     |   1 +
 security/security.c           | 119 +++++++++++++++++++++++++++++-----
 security/selinux/hooks.c      |  19 ++++--
 security/smack/smack_lsm.c    |  33 ++++++----
 5 files changed, 137 insertions(+), 38 deletions(-)

diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index 6bb55e61e8e..b814955ae70 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -112,8 +112,7 @@ LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask,
 LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode)
 LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode)
 LSM_HOOK(int, 0, inode_init_security, struct inode *inode,
-	 struct inode *dir, const struct qstr *qstr, const char **name,
-	 void **value, size_t *len)
+	 struct inode *dir, const struct qstr *qstr, struct xattr *xattrs)
 LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode,
 	 const struct qstr *name, const struct inode *context_inode)
 LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index c2be66c669a..75a2f85b49d 100644
--- a/include/linux/lsm_hooks.h
+++ b/include/linux/lsm_hooks.h
@@ -63,6 +63,7 @@ struct lsm_blob_sizes {
 	int	lbs_ipc;
 	int	lbs_msg_msg;
 	int	lbs_task;
+	int	lbs_xattr; /* number of xattr slots in new_xattrs array */
 };
 
 /*
diff --git a/security/security.c b/security/security.c
index f4170efcddd..f1f5f62f7fa 100644
--- a/security/security.c
+++ b/security/security.c
@@ -31,8 +31,6 @@
 #include <linux/msg.h>
 #include <net/flow.h>
 
-#define MAX_LSM_EVM_XATTR	2
-
 /* How many LSMs were built into the kernel? */
 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
 
@@ -212,6 +210,7 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
 	lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
 	lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
 	lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
+	lsm_set_blob_size(&needed->lbs_xattr, &blob_sizes.lbs_xattr);
 }
 
 /* Prepare LSM for initialization. */
@@ -378,6 +377,7 @@ static void __init ordered_lsm_init(void)
 	init_debug("msg_msg blob size    = %d\n", blob_sizes.lbs_msg_msg);
 	init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
 	init_debug("task blob size       = %d\n", blob_sizes.lbs_task);
+	init_debug("xattr slots          = %d\n", blob_sizes.lbs_xattr);
 
 	/*
 	 * Create any kmem_caches needed for blobs
@@ -1579,6 +1579,52 @@ int security_dentry_create_files_as(struct dentry *dentry, int mode,
 }
 EXPORT_SYMBOL(security_dentry_create_files_as);
 
+/**
+ * security_check_compact_filled_xattrs - check xattrs and make array contiguous
+ * @xattrs: xattr array filled by LSMs
+ * @num_xattrs: length of xattr array
+ * @num_filled_xattrs: number of already processed xattrs
+ *
+ * Ensure that each xattr slot is correctly filled and close the gaps in the
+ * xattr array if an LSM didn't provide an xattr for which it asked space
+ * (legitimate case, it might have been loaded but not initialized). An LSM
+ * might request space in the xattr array for one or multiple xattrs. The LSM
+ * infrastructure ensures that all requests by LSMs are satisfied.
+ *
+ * Track the number of filled xattrs in @num_filled_xattrs, so that it is easy
+ * to determine whether the currently processed xattr is fine in its position
+ * (if all previous xattrs were filled) or it should be moved after the last
+ * filled xattr.
+ *
+ * Return: zero if all xattrs are valid, -EINVAL otherwise.
+ */
+static int security_check_compact_filled_xattrs(struct xattr *xattrs,
+						int num_xattrs,
+						int *num_filled_xattrs)
+{
+	int i;
+
+	for (i = *num_filled_xattrs; i < num_xattrs; i++) {
+		if ((!xattrs[i].name && xattrs[i].value) ||
+		    (xattrs[i].name && !xattrs[i].value))
+			return -EINVAL;
+
+		if (!xattrs[i].name)
+			continue;
+
+		if (i == *num_filled_xattrs) {
+			(*num_filled_xattrs)++;
+			continue;
+		}
+
+		memcpy(xattrs + (*num_filled_xattrs)++, xattrs + i,
+		       sizeof(*xattrs));
+		memset(xattrs + i, 0, sizeof(*xattrs));
+	}
+
+	return 0;
+}
+
 /**
  * security_inode_init_security() - Initialize an inode's LSM context
  * @inode: the inode
@@ -1591,9 +1637,13 @@ EXPORT_SYMBOL(security_dentry_create_files_as);
  * created inode and set up the incore security field for the new inode.  This
  * hook is called by the fs code as part of the inode creation transaction and
  * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
- * hooks called by the VFS.  The hook function is expected to allocate the name
- * and value via kmalloc, with the caller being responsible for calling kfree
- * after using them.  If the security module does not use security attributes
+ * hooks called by the VFS.  The hook function is expected to populate the
+ * @xattrs array, depending on how many xattrs have been specified by the
+ * security module in the lbs_xattr field of the lsm_blob_sizes structure.  For
+ * each array element, the hook function is expected to set ->name to the
+ * attribute name suffix (e.g. selinux), to allocate ->value (will be freed by
+ * the caller) and set it to the attribute value, to set ->value_len to the
+ * length of the value.  If the security module does not use security attributes
  * or does not wish to put a security attribute on this particular inode, then
  * it should return -EOPNOTSUPP to skip this processing.
  *
@@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
 				 const struct qstr *qstr,
 				 const initxattrs initxattrs, void *fs_data)
 {
-	struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
-	struct xattr *lsm_xattr, *evm_xattr, *xattr;
-	int ret;
+	struct security_hook_list *P;
+	struct xattr *new_xattrs;
+	struct xattr *xattr;
+	int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
 
 	if (unlikely(IS_PRIVATE(inode)))
 		return 0;
 
+	if (!blob_sizes.lbs_xattr)
+		return 0;
+
 	if (!initxattrs)
 		return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
-				     dir, qstr, NULL, NULL, NULL);
-	memset(new_xattrs, 0, sizeof(new_xattrs));
-	lsm_xattr = new_xattrs;
-	ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
-			    &lsm_xattr->name,
-			    &lsm_xattr->value,
-			    &lsm_xattr->value_len);
-	if (ret)
+				    dir, qstr, NULL);
+	/* Allocate +1 for EVM and +1 as terminator. */
+	new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
+			     GFP_NOFS);
+	if (!new_xattrs)
+		return -ENOMEM;
+
+	hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
+			     list) {
+		ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
+		if (ret && ret != -EOPNOTSUPP)
+			goto out;
+		/*
+		 * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
+		 * means that the LSM is not willing to provide an xattr, not
+		 * that it wants to signal an error. Thus, continue to invoke
+		 * the remaining LSMs.
+		 */
+		if (ret == -EOPNOTSUPP)
+			continue;
+		/*
+		 * As the number of xattrs reserved by LSMs is not directly
+		 * available, directly use the total number blob_sizes.lbs_xattr
+		 * to keep the code simple, while being not the most efficient
+		 * way.
+		 */
+		ret = security_check_compact_filled_xattrs(new_xattrs,
+							   blob_sizes.lbs_xattr,
+							   &num_filled_xattrs);
+		if (ret < 0) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
+
+	if (!num_filled_xattrs)
 		goto out;
 
-	evm_xattr = lsm_xattr + 1;
-	ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
+	ret = evm_inode_init_security(inode, new_xattrs,
+				      new_xattrs + num_filled_xattrs);
 	if (ret)
 		goto out;
 	ret = initxattrs(inode, new_xattrs, fs_data);
 out:
 	for (xattr = new_xattrs; xattr->value != NULL; xattr++)
 		kfree(xattr->value);
+	kfree(new_xattrs);
 	return (ret == -EOPNOTSUPP) ? 0 : ret;
 }
 EXPORT_SYMBOL(security_inode_init_security);
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 9a5bdfc2131..3e4308dd336 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -104,6 +104,8 @@
 #include "audit.h"
 #include "avc_ss.h"
 
+#define SELINUX_INODE_INIT_XATTRS 1
+
 struct selinux_state selinux_state;
 
 /* SECMARK reference count */
@@ -2868,11 +2870,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
 
 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 				       const struct qstr *qstr,
-				       const char **name,
-				       void **value, size_t *len)
+				       struct xattr *xattrs)
 {
 	const struct task_security_struct *tsec = selinux_cred(current_cred());
 	struct superblock_security_struct *sbsec;
+	struct xattr *xattr = NULL;
 	u32 newsid, clen;
 	int rc;
 	char *context;
@@ -2899,16 +2901,18 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 	    !(sbsec->flags & SBLABEL_MNT))
 		return -EOPNOTSUPP;
 
-	if (name)
-		*name = XATTR_SELINUX_SUFFIX;
+	if (xattrs)
+		xattr = xattrs + selinux_blob_sizes.lbs_xattr;
+
+	if (xattr) {
+		xattr->name = XATTR_SELINUX_SUFFIX;
 
-	if (value && len) {
 		rc = security_sid_to_context_force(&selinux_state, newsid,
 						   &context, &clen);
 		if (rc)
 			return rc;
-		*value = context;
-		*len = clen;
+		xattr->value = context;
+		xattr->value_len = clen;
 	}
 
 	return 0;
@@ -6918,6 +6922,7 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
 	.lbs_ipc = sizeof(struct ipc_security_struct),
 	.lbs_msg_msg = sizeof(struct msg_security_struct),
 	.lbs_superblock = sizeof(struct superblock_security_struct),
+	.lbs_xattr = SELINUX_INODE_INIT_XATTRS,
 };
 
 #ifdef CONFIG_PERF_EVENTS
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index cfcbb748da2..c8cf8df268b 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -52,6 +52,15 @@
 #define SMK_RECEIVING	1
 #define SMK_SENDING	2
 
+/*
+ * Smack uses multiple xattrs.
+ * SMACK64 - for access control, SMACK64EXEC - label for the program,
+ * SMACK64MMAP - controls library loading,
+ * SMACK64TRANSMUTE - label initialization,
+ * Not saved on files - SMACK64IPIN and SMACK64IPOUT
+ */
+#define SMACK_INODE_INIT_XATTRS 4
+
 #ifdef SMACK_IPV6_PORT_LABELING
 static DEFINE_MUTEX(smack_ipv6_lock);
 static LIST_HEAD(smk_ipv6_port_list);
@@ -939,26 +948,27 @@ static int smack_inode_alloc_security(struct inode *inode)
  * @inode: the newly created inode
  * @dir: containing directory object
  * @qstr: unused
- * @name: where to put the attribute name
- * @value: where to put the attribute value
- * @len: where to put the length of the attribute
+ * @xattrs: where to put the attributes
  *
  * Returns 0 if it all works out, -ENOMEM if there's no memory
  */
 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
-				     const struct qstr *qstr, const char **name,
-				     void **value, size_t *len)
+				     const struct qstr *qstr,
+				     struct xattr *xattrs)
 {
 	struct inode_smack *issp = smack_inode(inode);
 	struct smack_known *skp = smk_of_current();
 	struct smack_known *isp = smk_of_inode(inode);
 	struct smack_known *dsp = smk_of_inode(dir);
+	struct xattr *xattr = NULL;
 	int may;
 
-	if (name)
-		*name = XATTR_SMACK_SUFFIX;
+	if (xattrs)
+		xattr = xattrs + smack_blob_sizes.lbs_xattr;
+
+	if (xattr) {
+		xattr->name = XATTR_SMACK_SUFFIX;
 
-	if (value && len) {
 		rcu_read_lock();
 		may = smk_access_entry(skp->smk_known, dsp->smk_known,
 				       &skp->smk_rules);
@@ -976,11 +986,11 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir,
 			issp->smk_flags |= SMK_INODE_CHANGED;
 		}
 
-		*value = kstrdup(isp->smk_known, GFP_NOFS);
-		if (*value == NULL)
+		xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
+		if (xattr->value == NULL)
 			return -ENOMEM;
 
-		*len = strlen(isp->smk_known);
+		xattr->value_len = strlen(isp->smk_known);
 	}
 
 	return 0;
@@ -4854,6 +4864,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
 	.lbs_ipc = sizeof(struct smack_known *),
 	.lbs_msg_msg = sizeof(struct smack_known *),
 	.lbs_superblock = sizeof(struct superblock_smack),
+	.lbs_xattr = SMACK_INODE_INIT_XATTRS,
 };
 
 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v8 5/6] evm: Align evm_inode_init_security() definition with LSM infrastructure
  2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
                   ` (3 preceding siblings ...)
  2023-03-14  8:17 ` [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook Roberto Sassu
@ 2023-03-14  8:17 ` Roberto Sassu
  2023-03-14  8:17 ` [PATCH v8 6/6] evm: Support multiple LSMs providing an xattr Roberto Sassu
  2023-03-14  8:21 ` [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
  6 siblings, 0 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:17 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Change the evm_inode_init_security() definition to align with the LSM
infrastructure. Keep the existing behavior of including in the HMAC
calculation only the first xattr provided by LSMs.

Changing the evm_inode_init_security() definition requires passing only the
xattr array allocated by security_inode_init_security(), instead of the
first LSM xattr and the place where the EVM xattr should be filled. In lieu
of passing the EVM xattr, EVM must position itself after the last filled
xattr (by checking the xattr name), since only the beginning of the xattr
array is given.

Finally, make evm_inode_init_security() return value compatible with the
inode_init_security hook conventions, i.e. return -EOPNOTSUPP if it is not
setting an xattr.

EVM is a bit tricky, because xattrs is both an input and an output. If it
was just output, EVM should have returned zero if xattrs is NULL. But,
since xattrs is also input, EVM is unable to do its calculations, so return
-EOPNOTSUPP and handle this error in security_inode_init_security().

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 include/linux/evm.h               | 12 ++++++------
 security/integrity/evm/evm_main.c | 25 ++++++++++++++++++-------
 security/security.c               |  5 ++---
 3 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/include/linux/evm.h b/include/linux/evm.h
index 7dc1ee74169..cc64cea354e 100644
--- a/include/linux/evm.h
+++ b/include/linux/evm.h
@@ -56,9 +56,9 @@ static inline void evm_inode_post_set_acl(struct dentry *dentry,
 {
 	return evm_inode_post_setxattr(dentry, acl_name, NULL, 0);
 }
-extern int evm_inode_init_security(struct inode *inode,
-				   const struct xattr *xattr_array,
-				   struct xattr *evm);
+extern int evm_inode_init_security(struct inode *inode, struct inode *dir,
+				   const struct qstr *qstr,
+				   struct xattr *xattrs);
 extern bool evm_revalidate_status(const char *xattr_name);
 extern int evm_protected_xattr_if_enabled(const char *req_xattr_name);
 extern int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,
@@ -157,9 +157,9 @@ static inline void evm_inode_post_set_acl(struct dentry *dentry,
 	return;
 }
 
-static inline int evm_inode_init_security(struct inode *inode,
-					  const struct xattr *xattr_array,
-					  struct xattr *evm)
+static inline int evm_inode_init_security(struct inode *inode, struct inode *dir,
+					  const struct qstr *qstr,
+					  struct xattr *xattrs)
 {
 	return 0;
 }
diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c
index cf24c525558..7d20ce83915 100644
--- a/security/integrity/evm/evm_main.c
+++ b/security/integrity/evm/evm_main.c
@@ -864,23 +864,34 @@ void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
 /*
  * evm_inode_init_security - initializes security.evm HMAC value
  */
-int evm_inode_init_security(struct inode *inode,
-				 const struct xattr *lsm_xattr,
-				 struct xattr *evm_xattr)
+int evm_inode_init_security(struct inode *inode, struct inode *dir,
+			    const struct qstr *qstr,
+			    struct xattr *xattrs)
 {
 	struct evm_xattr *xattr_data;
+	struct xattr *xattr, *evm_xattr;
 	int rc;
 
-	if (!(evm_initialized & EVM_INIT_HMAC) ||
-	    !evm_protected_xattr(lsm_xattr->name))
-		return 0;
+	if (!(evm_initialized & EVM_INIT_HMAC) || !xattrs ||
+	    !evm_protected_xattr(xattrs->name))
+		return -EOPNOTSUPP;
+
+	/*
+	 * security_inode_init_security() makes sure that the xattrs array is
+	 * contiguous, there is enough space for security.evm, and that there is
+	 * a terminator at the end of the array.
+	 */
+	for (xattr = xattrs; xattr->name != NULL; xattr++)
+		;
+
+	evm_xattr = xattr;
 
 	xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
 	if (!xattr_data)
 		return -ENOMEM;
 
 	xattr_data->data.type = EVM_XATTR_HMAC;
-	rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
+	rc = evm_init_hmac(inode, xattrs, xattr_data->digest);
 	if (rc < 0)
 		goto out;
 
diff --git a/security/security.c b/security/security.c
index f1f5f62f7fa..d0e20b26b6c 100644
--- a/security/security.c
+++ b/security/security.c
@@ -1705,9 +1705,8 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
 	if (!num_filled_xattrs)
 		goto out;
 
-	ret = evm_inode_init_security(inode, new_xattrs,
-				      new_xattrs + num_filled_xattrs);
-	if (ret)
+	ret = evm_inode_init_security(inode, dir, qstr, new_xattrs);
+	if (ret && ret != -EOPNOTSUPP)
 		goto out;
 	ret = initxattrs(inode, new_xattrs, fs_data);
 out:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH v8 6/6] evm: Support multiple LSMs providing an xattr
  2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
                   ` (4 preceding siblings ...)
  2023-03-14  8:17 ` [PATCH v8 5/6] evm: Align evm_inode_init_security() definition with LSM infrastructure Roberto Sassu
@ 2023-03-14  8:17 ` Roberto Sassu
  2023-03-14  8:21 ` [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
  6 siblings, 0 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:17 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Currently, evm_inode_init_security() processes a single LSM xattr from
the array passed by security_inode_init_security(), and calculates the
HMAC on it and other inode metadata.

Given that initxattrs() callbacks, called by
security_inode_init_security(), expect that this array is terminated when
the xattr name is set to NULL, reuse the same assumption to scan all xattrs
and to calculate the HMAC on all of them.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 security/integrity/evm/evm.h        |  2 ++
 security/integrity/evm/evm_crypto.c |  9 ++++++++-
 security/integrity/evm/evm_main.c   | 16 +++++++++++-----
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/security/integrity/evm/evm.h b/security/integrity/evm/evm.h
index f8b8c5004fc..f799d72a59f 100644
--- a/security/integrity/evm/evm.h
+++ b/security/integrity/evm/evm.h
@@ -46,6 +46,8 @@ struct evm_digest {
 	char digest[IMA_MAX_DIGEST_SIZE];
 } __packed;
 
+int evm_protected_xattr(const char *req_xattr_name);
+
 int evm_init_key(void);
 int evm_update_evmxattr(struct dentry *dentry,
 			const char *req_xattr_name,
diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
index 033804f5a5f..5d8b5ecf594 100644
--- a/security/integrity/evm/evm_crypto.c
+++ b/security/integrity/evm/evm_crypto.c
@@ -389,6 +389,7 @@ int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
 		  char *hmac_val)
 {
 	struct shash_desc *desc;
+	const struct xattr *xattr;
 
 	desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
 	if (IS_ERR(desc)) {
@@ -396,7 +397,13 @@ int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
 		return PTR_ERR(desc);
 	}
 
-	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
+	for (xattr = lsm_xattr; xattr->name != NULL; xattr++) {
+		if (!evm_protected_xattr(xattr->name))
+			continue;
+
+		crypto_shash_update(desc, xattr->value, xattr->value_len);
+	}
+
 	hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
 	kfree(desc);
 	return 0;
diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c
index 7d20ce83915..013eb220cc5 100644
--- a/security/integrity/evm/evm_main.c
+++ b/security/integrity/evm/evm_main.c
@@ -305,7 +305,7 @@ static int evm_protected_xattr_common(const char *req_xattr_name,
 	return found;
 }
 
-static int evm_protected_xattr(const char *req_xattr_name)
+int evm_protected_xattr(const char *req_xattr_name)
 {
 	return evm_protected_xattr_common(req_xattr_name, false);
 }
@@ -870,10 +870,10 @@ int evm_inode_init_security(struct inode *inode, struct inode *dir,
 {
 	struct evm_xattr *xattr_data;
 	struct xattr *xattr, *evm_xattr;
+	bool evm_protected_xattrs = false;
 	int rc;
 
-	if (!(evm_initialized & EVM_INIT_HMAC) || !xattrs ||
-	    !evm_protected_xattr(xattrs->name))
+	if (!(evm_initialized & EVM_INIT_HMAC) || !xattrs)
 		return -EOPNOTSUPP;
 
 	/*
@@ -881,8 +881,14 @@ int evm_inode_init_security(struct inode *inode, struct inode *dir,
 	 * contiguous, there is enough space for security.evm, and that there is
 	 * a terminator at the end of the array.
 	 */
-	for (xattr = xattrs; xattr->name != NULL; xattr++)
-		;
+	for (xattr = xattrs; xattr->name != NULL; xattr++) {
+		if (evm_protected_xattr(xattr->name))
+			evm_protected_xattrs = true;
+	}
+
+	/* EVM xattr not needed. */
+	if (!evm_protected_xattrs)
+		return -EOPNOTSUPP;
 
 	evm_xattr = xattr;
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes
  2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
                   ` (5 preceding siblings ...)
  2023-03-14  8:17 ` [PATCH v8 6/6] evm: Support multiple LSMs providing an xattr Roberto Sassu
@ 2023-03-14  8:21 ` Roberto Sassu
  6 siblings, 0 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-14  8:21 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, paul, jmorris,
	serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

On Tue, 2023-03-14 at 09:17 +0100, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> One of the major goals of LSM stacking is to run multiple LSMs side by side
> without interfering with each other. The ultimate decision will depend on
> individual LSM decision.
> 
> Several changes need to be made to the LSM infrastructure to be able to
> support that. This patch set tackles one of them: gives to each LSM the
> ability to specify one or multiple xattrs to be set at inode creation
> time and, at the same time, gives to EVM the ability to access all those
> xattrs and calculate the HMAC on them.
> 
> The first problem that this patch set addresses is to make the
> inode_init_security hook definition suitable to use with EVM which, unlike
> other LSMs, needs to have visibility of all xattrs and not only the one
> that the LSM infrastructure passes to the LSM to be set.
> 
> The solution is to replace in the inode_init_security definition the
> name/value/len parameters with the beginning of the array containing all
> xattrs set by LSMs. Due to security_old_inode_init_security() API
> limitation of setting only one xattr, it has been dropped and the remaining
> users, ocfs2 and reiserfs, switch to security_inode_init_security().
> However, due to the complexity of the changes required to fully exploit the
> ability of security_inode_init_security() to set multiple xattrs, those
> users can still set only one xattr (the first set in the xattr array) where
> previously they called security_old_inode_init_security().
> 
> Furthermore, while EVM is invoked unlike before, its xattr will not be set
> as it would not be the first set in the xattr array, or if it is the first,
> there would not be protected xattrs to calculate the HMAC on.
> 
> The second problem this patch set addresses is the limitation of the
> call_int_hook() of stopping the loop when the return value from a hook
> implementation is not zero. Unfortunately, for the inode_init_security hook
> it is a legitimate case to return -EOPNOTSUPP, but this would not
> necessarily mean that there is an error to report to the LSM infrastructure
> but just that an LSM does not will to set an xattr. Other LSMs should be
> still consulted as well.
> 
> The solution for this specific case is to replace the call_int_hook() with
> the loop itself, so that -EOPNOTSUPP can be ignored.
> 
> Next, this patch set removes the limitation of creating only two xattrs,
> one by an active LSM and another by EVM. This patch set extends the
> reservation mechanism of the LSM infrastructure, to allow each LSM to
> request one or multiple xattrs. While this could potentially lead to
> reaching the filesystem limits of number/size of the xattrs, it seems not
> an issue that need to be solved by the LSM infrastructure but by the
> filesystems themselves. Currently, if the limit is reached, the only
> workaround would be to use fewer LSMs.
> 
> The reservation mechanism concept makes it very easy for LSMs to position
> themselves correctly in the xattr array, as the LSM infrastructure at
> initialization time changes the number of xattrs requested by each LSM with
> an offset. LSMs can just take that offset as the starting index in the
> xattr array and fill the next slots depending on how many xattrs they
> requested.
> 
> However, while this concept is intuitive, it needs extra care. While for
> security blobs (the main reason of the reservation mechanism) it is not
> relevant for an LSM if other LSMs filled their portion, it matters for
> xattrs, as both EVM and initxattrs() callbacks scan the entire array until
> a terminator (xattr with NULL name). If an LSM did not provide an xattr,
> which could happen if it is loaded but not initialized, consumers of the
> xattr array would stop prematurely.
> 
> This patch set avoids this problem by compacting the xattr array each time
> after an LSM executed its implementation of the inode_init_security hook.
> It needs to be done after each LSM, and not after all, since there might be
> LSMs scanning that xattr array too. Compacting the array after all LSMs
> would be too late.
> 
> Finally, this patch set modifies the evm_inode_init_security() definition
> to be compatible with the inode_init_security hook definition and adds
> support for scanning the whole xattr array and for calculating the HMAC
> on all xattrs provided by LSMs.
> 
> This patch set has been tested by introducing several instances of a
> TestLSM (some providing an xattr, some not, one with a wrong implementation
> to see how the LSM infrastructure handles it, one providing multiple xattrs
> and another providing an xattr but in a disabled state). The patch is not
> included in this set but it is available here:
> 
> https://github.com/robertosassu/linux/commit/311b83a98757915cc3ccb363f545578e7c38df54
> 
> The test, added to ima-evm-utils, is available here:
> 
> https://github.com/robertosassu/ima-evm-utils/blob/evm-multiple-lsms-v8-devel-v2/tests/evm_multiple_lsms.test
> 
> The test takes a UML kernel built by Github Actions and launches it several
> times, each time with a different combination of LSMs and filesystems (ext4,
> reiserfs, ocfs2). After boot, it first checks that there is an xattr for each
> LSM providing it (for reiserfs and ocfs2 just the first LSM), and then (for
> ext4) calculates the HMAC in user space and compares it with the HMAC
> calculated by EVM in kernel space.
> 
> A test report can be obtained here:
> 
> https://github.com/robertosassu/ima-evm-utils/actions/runs/4403223954/jobs/7722101662
> 
> The patch set has been tested with both the SElinux and Smack test suites.
> Below, there is the summary of the test results:
> 
> SELinux Test Suite result (without patches):
> All tests successful.
> Files=76, Tests=1343, 255 wallclock secs ( 0.51 usr  0.19 sys +  6.30 cusr 58.15 csys = 65.15 CPU)
> Result: PASS
> 
> SELinux Test Suite result (with patches):
> All tests successful.
> Files=76, Tests=1343, 258 wallclock secs ( 0.49 usr  0.21 sys +  6.46 cusr 59.27 csys = 66.43 CPU)
> Result: PASS

Side note:

https://github.com/SELinuxProject/selinux-testsuite/blob/master/tests/mmap/test#L51

This test fails if CONFIG_LSM_MMAP_MIN_ADDR=0

Roberto

> Smack Test Suite result (without patches):
> 95 Passed, 0 Failed, 100% Success rate
> 
> Smack Test Suite result (with patches):
> 95 Passed, 0 Failed, 100% Success rate
> 
> Changelog
> 
> v7:
> - Add a patch dependency comment in patch 1 (suggested by Mimi)
> - Restore check of -EOPNOTSUPP status in ocfs2_mknod() and ocfs2_symlink()
>   (reported by Mimi)
> - Add explanation in evm_inode_init_security() why walking through the
>   xattrs array is safe (suggested by Mimi)
> - Document the lbs_xattr field of struct lsm_blob_sizes (suggested by
>   Casey)
> - Move documentation changes of the inode_init_security hook to security.c,
>   after LSM documentation reorganization by Paul
> - Use attributes in plural form in the description of the xattrs parameter
>   of smack_inode_init_security()
> - Check xattr name instead of xattr value in evm_inode_init_security(),
>   for consistency with evm_init_hmac(); equivalent, since
>   security_check_compact_filled_xattrs() rejects xattrs with xattr name
>   NULL and value not NULL, and viceversa
> 
> v6:
> - Add a comment in Smack to introduce its xattrs (suggested by Casey)
> - Document the overloaded meaning of -EOPNOTSUPP in
>   security_inode_init_security() (suggested by Mimi)
> 
> v5:
> - Modify the cover letter to explain that the goal of this patch set is
>   supporting multiple per LSM xattrs in EVM, and not moving IMA and EVM to
>   the LSM infrastructure (suggested by Mimi)
> - Remove references in the patches description about moving IMA and EVM
>   to the LSM infrastructure (suggested by Mimi)
> - Explain that the additional EVM invocation due to the switch to
>   security_inode_init_security() will not cause the EVM xattr to be added
>   (suggested by Mimi)
> 
> v4:
> - Remove patch to call reiserfs_security_free(), already queued
> - Switch ocfs2 and reiserfs to security_inode_init_security() (suggested by
>   Mimi)
> - Remove security_old_inode_init_security() (suggested by Paul)
> - Rename security_check_compact_xattrs() to
>   security_check_compact_filled_xattrs() and add function description
>   (suggested by Mimi)
> - Rename checked_xattrs parameter of security_check_compact_filled_xattrs()
>   to num_filled_xattrs (suggested by Mimi)
> - Rename cur_xattrs variable in security_inode_init_security() to
>   num_filled_xattrs (suggested by Mimi)
> 
> v3:
> - Don't free the xattr name in reiserfs_security_free()
> - Don't include fs_data parameter in inode_init_security hook
> - Don't change evm_inode_init_security(), as it will be removed if EVM is
>   stacked
> - Fix inode_init_security hook documentation
> - Drop lsm_find_xattr_slot(), use simple xattr reservation mechanism and
>   introduce security_check_compact_xattrs() to compact the xattr array
> - Don't allocate xattr array if LSMs didn't reserve any xattr
> - Return zero if initxattrs() is not provided to
>   security_inode_init_security(), -EOPNOTSUPP if value is not provided to
>   security_old_inode_init_security()
> - Request LSMs to fill xattrs if only value (not the triple) is provided to
>   security_old_inode_init_security(), to avoid unnecessary memory
>   allocation
> 
> v2:
> - rewrite selinux_old_inode_init_security() to use
>   security_inode_init_security()
> - add lbs_xattr field to lsm_blob_sizes structure, to give the ability to
>   LSMs to reserve slots in the xattr array (suggested by Casey)
> - add new parameter base_slot to inode_init_security hook definition
> 
> v1:
> - add calls to reiserfs_security_free() and initialize sec->value to NULL
>   (suggested by Tetsuo and Mimi)
> - change definition of inode_init_security hook, replace the name, value
>   and len triple with the xattr array (suggested by Casey)
> - introduce lsm_find_xattr_slot() helper for LSMs to find an unused slot in
>   the passed xattr array
> 
> Roberto Sassu (6):
>   reiserfs: Switch to security_inode_init_security()
>   ocfs2: Switch to security_inode_init_security()
>   security: Remove security_old_inode_init_security()
>   security: Allow all LSMs to provide xattrs for inode_init_security
>     hook
>   evm: Align evm_inode_init_security() definition with LSM
>     infrastructure
>   evm: Support multiple LSMs providing an xattr
> 
>  fs/ocfs2/namei.c                    |   2 +
>  fs/ocfs2/xattr.c                    |  30 ++++++-
>  fs/reiserfs/xattr_security.c        |  23 +++--
>  include/linux/evm.h                 |  12 +--
>  include/linux/lsm_hook_defs.h       |   3 +-
>  include/linux/lsm_hooks.h           |   1 +
>  include/linux/security.h            |  12 ---
>  security/integrity/evm/evm.h        |   2 +
>  security/integrity/evm/evm_crypto.c |   9 +-
>  security/integrity/evm/evm_main.c   |  33 +++++--
>  security/security.c                 | 131 +++++++++++++++++++++-------
>  security/selinux/hooks.c            |  19 ++--
>  security/smack/smack_lsm.c          |  33 ++++---
>  13 files changed, 224 insertions(+), 86 deletions(-)
> 


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 2/6] ocfs2: Switch to security_inode_init_security()
  2023-03-14  8:17 ` [PATCH v8 2/6] ocfs2: " Roberto Sassu
@ 2023-03-17 19:39   ` Mimi Zohar
  2023-03-23 23:37   ` Paul Moore
  1 sibling, 0 replies; 25+ messages in thread
From: Mimi Zohar @ 2023-03-17 19:39 UTC (permalink / raw)
  To: Roberto Sassu, mark, jlbec, joseph.qi, dmitry.kasatkin, paul,
	jmorris, serge, stephen.smalley.work, eparis, casey
  Cc: ocfs2-devel, reiserfs-devel, linux-integrity,
	linux-security-module, selinux, linux-kernel, keescook,
	nicolas.bouchinet, Roberto Sassu

On Tue, 2023-03-14 at 09:17 +0100, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> In preparation for removing security_old_inode_init_security(), switch to
> security_inode_init_security().
> 
> Extend the existing ocfs2_initxattrs() to take the
> ocfs2_security_xattr_info structure from fs_info, and populate the
> name/value/len triple with the first xattr provided by LSMs.
> 
> As fs_info was not used before, ocfs2_initxattrs() can now handle the case
> of replicating the behavior of security_old_inode_init_security(), i.e.
> just obtaining the xattr, in addition to setting all xattrs provided by
> LSMs.
> 
> Supporting multiple xattrs is not currently supported where
> security_old_inode_init_security() was called (mknod, symlink), as it
> requires non-trivial changes that can be done at a later time. Like for
> reiserfs, even if EVM is invoked, it will not provide an xattr (if it is
> not the first to set it, its xattr will be discarded; if it is the first,
> it does not have xattrs to calculate the HMAC on).
> 
> Finally, since security_inode_init_security(), unlike
> security_old_inode_init_security(), returns zero instead of -EOPNOTSUPP if
> no xattrs were provided by LSMs or if inodes are private, additionally
> check in ocfs2_init_security_get() if the xattr name is set.
> 
> If not, act as if security_old_inode_init_security() returned -EOPNOTSUPP,
> and set si->enable to zero to notify to the functions following
> ocfs2_init_security_get() that no xattrs are available.
> 
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> Acked-by: Joseph Qi <joseph.qi@linux.alibaba.com>

Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 1/6] reiserfs: Switch to security_inode_init_security()
  2023-03-14  8:17 ` [PATCH v8 1/6] reiserfs: Switch to security_inode_init_security() Roberto Sassu
@ 2023-03-23 23:33   ` Paul Moore
  0 siblings, 0 replies; 25+ messages in thread
From: Paul Moore @ 2023-03-23 23:33 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Tue, Mar 14, 2023 at 4:18 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
>
> From: Roberto Sassu <roberto.sassu@huawei.com>
>
> In preparation for removing security_old_inode_init_security(), switch to
> security_inode_init_security(). Commit 572302af1258 ("reiserfs: Add missing
> calls to reiserfs_security_free()") fixed possible memory leaks and another
> issue related to adding an xattr at inode creation time.
>
> Define the initxattrs callback reiserfs_initxattrs(), to populate the
> name/value/len triple in the reiserfs_security_handle() with the first
> xattr provided by LSMs. Make a copy of the xattr value, as
> security_inode_init_security() frees it.
>
> After the call to security_inode_init_security(), remove the check for
> returning -EOPNOTSUPP, as security_inode_init_security() changes it to
> zero.
>
> Multiple xattrs are currently not supported, as the
> reiserfs_security_handle structure is exported to user space. As a
> consequence, even if EVM is invoked, it will not provide an xattr (if it
> is not the first to set it, its xattr will be discarded; if it is the
> first, it does not have xattrs to calculate the HMAC on).
>
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> ---
>  fs/reiserfs/xattr_security.c | 23 ++++++++++++++++++-----
>  1 file changed, 18 insertions(+), 5 deletions(-)

Merged into lsm/next, thanks.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 2/6] ocfs2: Switch to security_inode_init_security()
  2023-03-14  8:17 ` [PATCH v8 2/6] ocfs2: " Roberto Sassu
  2023-03-17 19:39   ` Mimi Zohar
@ 2023-03-23 23:37   ` Paul Moore
  1 sibling, 0 replies; 25+ messages in thread
From: Paul Moore @ 2023-03-23 23:37 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Tue, Mar 14, 2023 at 4:18 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
>
> From: Roberto Sassu <roberto.sassu@huawei.com>
>
> In preparation for removing security_old_inode_init_security(), switch to
> security_inode_init_security().
>
> Extend the existing ocfs2_initxattrs() to take the
> ocfs2_security_xattr_info structure from fs_info, and populate the
> name/value/len triple with the first xattr provided by LSMs.
>
> As fs_info was not used before, ocfs2_initxattrs() can now handle the case
> of replicating the behavior of security_old_inode_init_security(), i.e.
> just obtaining the xattr, in addition to setting all xattrs provided by
> LSMs.
>
> Supporting multiple xattrs is not currently supported where
> security_old_inode_init_security() was called (mknod, symlink), as it
> requires non-trivial changes that can be done at a later time. Like for
> reiserfs, even if EVM is invoked, it will not provide an xattr (if it is
> not the first to set it, its xattr will be discarded; if it is the first,
> it does not have xattrs to calculate the HMAC on).
>
> Finally, since security_inode_init_security(), unlike
> security_old_inode_init_security(), returns zero instead of -EOPNOTSUPP if
> no xattrs were provided by LSMs or if inodes are private, additionally
> check in ocfs2_init_security_get() if the xattr name is set.
>
> If not, act as if security_old_inode_init_security() returned -EOPNOTSUPP,
> and set si->enable to zero to notify to the functions following
> ocfs2_init_security_get() that no xattrs are available.
>
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> Acked-by: Joseph Qi <joseph.qi@linux.alibaba.com>
> ---
>  fs/ocfs2/namei.c |  2 ++
>  fs/ocfs2/xattr.c | 30 ++++++++++++++++++++++++++----
>  2 files changed, 28 insertions(+), 4 deletions(-)

Merged into lsm/next, thanks.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 3/6] security: Remove security_old_inode_init_security()
  2023-03-14  8:17 ` [PATCH v8 3/6] security: Remove security_old_inode_init_security() Roberto Sassu
@ 2023-03-23 23:41   ` Paul Moore
  0 siblings, 0 replies; 25+ messages in thread
From: Paul Moore @ 2023-03-23 23:41 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Tue, Mar 14, 2023 at 4:18 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
>
> From: Roberto Sassu <roberto.sassu@huawei.com>
>
> As the remaining two users reiserfs and ocfs2 switched to
> security_inode_init_security(), security_old_inode_init_security() can be
> now removed.
>
> Out-of-tree kernel modules should switch to security_inode_init_security()
> too.
>
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> ---
>  include/linux/security.h | 12 ------------
>  security/security.c      | 11 -----------
>  2 files changed, 23 deletions(-)

Merged into lsm/next, thanks.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-14  8:17 ` [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook Roberto Sassu
@ 2023-03-24  0:09   ` Paul Moore
  2023-03-24  1:01     ` Casey Schaufler
  2023-03-24 10:18     ` Roberto Sassu
  0 siblings, 2 replies; 25+ messages in thread
From: Paul Moore @ 2023-03-24  0:09 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
>
> From: Roberto Sassu <roberto.sassu@huawei.com>
>
> Currently, security_inode_init_security() supports only one LSM providing
> an xattr and EVM calculating the HMAC on that xattr, plus other inode
> metadata.
>
> Allow all LSMs to provide one or multiple xattrs, by extending the security
> blob reservation mechanism. Introduce the new lbs_xattr field of the
> lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> needs, and the LSM infrastructure knows how many xattr slots it should
> allocate.
>
> Dynamically allocate the xattrs array to be populated by LSMs with the
> inode_init_security hook, and pass it to the latter instead of the
> name/value/len triple. Update the documentation accordingly, and fix the
> description of the xattr name, as it is not allocated anymore.
>
> Since the LSM infrastructure, at initialization time, updates the number of
> the requested xattrs provided by each LSM with a corresponding offset in
> the security blob (in this case the xattr array), it makes straightforward
> for an LSM to access the right position in the xattr array.
>
> There is still the issue that an LSM might not fill the xattr, even if it
> requests it (legitimate case, for example it might have been loaded but not
> initialized with a policy). Since users of the xattr array (e.g. the
> initxattrs() callbacks) detect the end of the xattr array by checking if
> the xattr name is NULL, not filling an xattr would cause those users to
> stop scanning xattrs prematurely.
>
> Solve that issue by introducing security_check_compact_filled_xattrs(),
> which does a basic check of the xattr array (if the xattr name is filled,
> the xattr value should be too, and viceversa), and compacts the xattr array
> by removing the holes.
>
> An alternative solution would be to let users of the xattr array know the
> number of elements of that array, so that they don't have to check the
> termination. However, this seems more invasive, compared to a simple move
> of few array elements.
>
> security_check_compact_filled_xattrs() also determines how many xattrs in
> the xattr array have been filled. If there is none, skip
> evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> EVM to crash the kernel, as it is expecting a filled xattr.
>
> Finally, adapt both SELinux and Smack to use the new definition of the
> inode_init_security hook, and to correctly fill the designated slots in the
> xattr array. For Smack, reserve space for the other defined xattrs although
> they are not set yet in smack_inode_init_security().
>
> Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> ---
>  include/linux/lsm_hook_defs.h |   3 +-
>  include/linux/lsm_hooks.h     |   1 +
>  security/security.c           | 119 +++++++++++++++++++++++++++++-----
>  security/selinux/hooks.c      |  19 ++++--
>  security/smack/smack_lsm.c    |  33 ++++++----
>  5 files changed, 137 insertions(+), 38 deletions(-)
>
> diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
> index 6bb55e61e8e..b814955ae70 100644
> --- a/include/linux/lsm_hook_defs.h
> +++ b/include/linux/lsm_hook_defs.h
> @@ -112,8 +112,7 @@ LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask,
>  LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode)
>  LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode)
>  LSM_HOOK(int, 0, inode_init_security, struct inode *inode,
> -        struct inode *dir, const struct qstr *qstr, const char **name,
> -        void **value, size_t *len)
> +        struct inode *dir, const struct qstr *qstr, struct xattr *xattrs)
>  LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode,
>          const struct qstr *name, const struct inode *context_inode)
>  LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
> diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
> index c2be66c669a..75a2f85b49d 100644
> --- a/include/linux/lsm_hooks.h
> +++ b/include/linux/lsm_hooks.h
> @@ -63,6 +63,7 @@ struct lsm_blob_sizes {
>         int     lbs_ipc;
>         int     lbs_msg_msg;
>         int     lbs_task;
> +       int     lbs_xattr; /* number of xattr slots in new_xattrs array */

No need for the comment, we don't do it for the other fields.

>  };
>
>  /*
> diff --git a/security/security.c b/security/security.c
> index f4170efcddd..f1f5f62f7fa 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -1579,6 +1579,52 @@ int security_dentry_create_files_as(struct dentry *dentry, int mode,
>  }
>  EXPORT_SYMBOL(security_dentry_create_files_as);
>
> +/**
> + * security_check_compact_filled_xattrs - check xattrs and make array contiguous
> + * @xattrs: xattr array filled by LSMs
> + * @num_xattrs: length of xattr array
> + * @num_filled_xattrs: number of already processed xattrs
> + *
> + * Ensure that each xattr slot is correctly filled and close the gaps in the
> + * xattr array if an LSM didn't provide an xattr for which it asked space
> + * (legitimate case, it might have been loaded but not initialized). An LSM
> + * might request space in the xattr array for one or multiple xattrs. The LSM
> + * infrastructure ensures that all requests by LSMs are satisfied.
> + *
> + * Track the number of filled xattrs in @num_filled_xattrs, so that it is easy
> + * to determine whether the currently processed xattr is fine in its position
> + * (if all previous xattrs were filled) or it should be moved after the last
> + * filled xattr.
> + *
> + * Return: zero if all xattrs are valid, -EINVAL otherwise.
> + */
> +static int security_check_compact_filled_xattrs(struct xattr *xattrs,
> +                                               int num_xattrs,
> +                                               int *num_filled_xattrs)

That is one long name :)

Since you're making some other changes to this patch, can you rename
this to security_xattr_compact() or something like that?

> +{
> +       int i;
> +
> +       for (i = *num_filled_xattrs; i < num_xattrs; i++) {
> +               if ((!xattrs[i].name && xattrs[i].value) ||
> +                   (xattrs[i].name && !xattrs[i].value))
> +                       return -EINVAL;
> +
> +               if (!xattrs[i].name)
> +                       continue;
> +
> +               if (i == *num_filled_xattrs) {
> +                       (*num_filled_xattrs)++;
> +                       continue;
> +               }
> +
> +               memcpy(xattrs + (*num_filled_xattrs)++, xattrs + i,
> +                      sizeof(*xattrs));
> +               memset(xattrs + i, 0, sizeof(*xattrs));
> +       }
> +
> +       return 0;
> +}
> +
>  /**
>   * security_inode_init_security() - Initialize an inode's LSM context
>   * @inode: the inode
> @@ -1591,9 +1637,13 @@ EXPORT_SYMBOL(security_dentry_create_files_as);
>   * created inode and set up the incore security field for the new inode.  This
>   * hook is called by the fs code as part of the inode creation transaction and
>   * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
> - * hooks called by the VFS.  The hook function is expected to allocate the name
> - * and value via kmalloc, with the caller being responsible for calling kfree
> - * after using them.  If the security module does not use security attributes
> + * hooks called by the VFS.  The hook function is expected to populate the
> + * @xattrs array, depending on how many xattrs have been specified by the
> + * security module in the lbs_xattr field of the lsm_blob_sizes structure.  For
> + * each array element, the hook function is expected to set ->name to the
> + * attribute name suffix (e.g. selinux), to allocate ->value (will be freed by
> + * the caller) and set it to the attribute value, to set ->value_len to the
> + * length of the value.  If the security module does not use security attributes
>   * or does not wish to put a security attribute on this particular inode, then
>   * it should return -EOPNOTSUPP to skip this processing.
>   *
> @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
>                                  const struct qstr *qstr,
>                                  const initxattrs initxattrs, void *fs_data)
>  {
> -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> -       int ret;
> +       struct security_hook_list *P;
> +       struct xattr *new_xattrs;
> +       struct xattr *xattr;
> +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
>
>         if (unlikely(IS_PRIVATE(inode)))
>                 return 0;
>
> +       if (!blob_sizes.lbs_xattr)
> +               return 0;
> +
>         if (!initxattrs)
>                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> -                                    dir, qstr, NULL, NULL, NULL);
> -       memset(new_xattrs, 0, sizeof(new_xattrs));
> -       lsm_xattr = new_xattrs;
> -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> -                           &lsm_xattr->name,
> -                           &lsm_xattr->value,
> -                           &lsm_xattr->value_len);
> -       if (ret)
> +                                   dir, qstr, NULL);
> +       /* Allocate +1 for EVM and +1 as terminator. */
> +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> +                            GFP_NOFS);
> +       if (!new_xattrs)
> +               return -ENOMEM;
> +
> +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> +                            list) {
> +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> +               if (ret && ret != -EOPNOTSUPP)
> +                       goto out;
> +               /*
> +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> +                * means that the LSM is not willing to provide an xattr, not
> +                * that it wants to signal an error. Thus, continue to invoke
> +                * the remaining LSMs.
> +                */
> +               if (ret == -EOPNOTSUPP)
> +                       continue;
> +               /*
> +                * As the number of xattrs reserved by LSMs is not directly
> +                * available, directly use the total number blob_sizes.lbs_xattr
> +                * to keep the code simple, while being not the most efficient
> +                * way.
> +                */

Is there a good reason why the LSM can't return the number of xattrs
it is adding to the xattr array?  It seems like it should be fairly
trivial for the individual LSMs to determine and it could save a lot
of work.  However, given we're at v8 on this patchset I'm sure I'm
missing something obvious, can you help me understand why the idea
above is crazy stupid? ;)

> +               ret = security_check_compact_filled_xattrs(new_xattrs,
> +                                                          blob_sizes.lbs_xattr,
> +                                                          &num_filled_xattrs);
> +               if (ret < 0) {
> +                       ret = -ENOMEM;
> +                       goto out;
> +               }
> +       }
> +
> +       if (!num_filled_xattrs)
>                 goto out;
>
> -       evm_xattr = lsm_xattr + 1;
> -       ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
> +       ret = evm_inode_init_security(inode, new_xattrs,
> +                                     new_xattrs + num_filled_xattrs);
>         if (ret)
>                 goto out;
>         ret = initxattrs(inode, new_xattrs, fs_data);
>  out:
>         for (xattr = new_xattrs; xattr->value != NULL; xattr++)
>                 kfree(xattr->value);
> +       kfree(new_xattrs);
>         return (ret == -EOPNOTSUPP) ? 0 : ret;
>  }
>  EXPORT_SYMBOL(security_inode_init_security);
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 9a5bdfc2131..3e4308dd336 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -104,6 +104,8 @@
>  #include "audit.h"
>  #include "avc_ss.h"
>
> +#define SELINUX_INODE_INIT_XATTRS 1
> +
>  struct selinux_state selinux_state;
>
>  /* SECMARK reference count */
> @@ -2868,11 +2870,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
>
>  static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
>                                        const struct qstr *qstr,
> -                                      const char **name,
> -                                      void **value, size_t *len)
> +                                      struct xattr *xattrs)
>  {
>         const struct task_security_struct *tsec = selinux_cred(current_cred());
>         struct superblock_security_struct *sbsec;
> +       struct xattr *xattr = NULL;
>         u32 newsid, clen;
>         int rc;
>         char *context;
> @@ -2899,16 +2901,18 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
>             !(sbsec->flags & SBLABEL_MNT))
>                 return -EOPNOTSUPP;
>
> -       if (name)
> -               *name = XATTR_SELINUX_SUFFIX;
> +       if (xattrs)
> +               xattr = xattrs + selinux_blob_sizes.lbs_xattr;

Please abstract that away to an inline function similar to
selinux_cred(), selinux_file(), selinux_inode(), etc.

> +       if (xattr) {
> +               xattr->name = XATTR_SELINUX_SUFFIX;

I'm guessing the xattr->name assignment is always done, regardless of
if security_sid_to_context_force() is successful, due to the -EINVAL
check in security_check_compact_filled_xattrs()?  If yes, it would be
good to make note of that here in the code.  If not, it would be nice
to move this down the function to go with the other xattr->XXX
assignments, unless there is another reason for its placement that I'm
missing.

> -       if (value && len) {
>                 rc = security_sid_to_context_force(&selinux_state, newsid,
>                                                    &context, &clen);
>                 if (rc)
>                         return rc;
> -               *value = context;
> -               *len = clen;
> +               xattr->value = context;
> +               xattr->value_len = clen;
>         }
>
>         return 0;
> @@ -6918,6 +6922,7 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
>         .lbs_ipc = sizeof(struct ipc_security_struct),
>         .lbs_msg_msg = sizeof(struct msg_security_struct),
>         .lbs_superblock = sizeof(struct superblock_security_struct),
> +       .lbs_xattr = SELINUX_INODE_INIT_XATTRS,
>  };
>
>  #ifdef CONFIG_PERF_EVENTS
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index cfcbb748da2..c8cf8df268b 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -52,6 +52,15 @@
>  #define SMK_RECEIVING  1
>  #define SMK_SENDING    2
>
> +/*
> + * Smack uses multiple xattrs.
> + * SMACK64 - for access control, SMACK64EXEC - label for the program,
> + * SMACK64MMAP - controls library loading,
> + * SMACK64TRANSMUTE - label initialization,
> + * Not saved on files - SMACK64IPIN and SMACK64IPOUT
> + */
> +#define SMACK_INODE_INIT_XATTRS 4
> +
>  #ifdef SMACK_IPV6_PORT_LABELING
>  static DEFINE_MUTEX(smack_ipv6_lock);
>  static LIST_HEAD(smk_ipv6_port_list);
> @@ -939,26 +948,27 @@ static int smack_inode_alloc_security(struct inode *inode)
>   * @inode: the newly created inode
>   * @dir: containing directory object
>   * @qstr: unused
> - * @name: where to put the attribute name
> - * @value: where to put the attribute value
> - * @len: where to put the length of the attribute
> + * @xattrs: where to put the attributes
>   *
>   * Returns 0 if it all works out, -ENOMEM if there's no memory
>   */
>  static int smack_inode_init_security(struct inode *inode, struct inode *dir,
> -                                    const struct qstr *qstr, const char **name,
> -                                    void **value, size_t *len)
> +                                    const struct qstr *qstr,
> +                                    struct xattr *xattrs)
>  {
>         struct inode_smack *issp = smack_inode(inode);
>         struct smack_known *skp = smk_of_current();
>         struct smack_known *isp = smk_of_inode(inode);
>         struct smack_known *dsp = smk_of_inode(dir);
> +       struct xattr *xattr = NULL;
>         int may;
>
> -       if (name)
> -               *name = XATTR_SMACK_SUFFIX;
> +       if (xattrs)
> +               xattr = xattrs + smack_blob_sizes.lbs_xattr;
> +
> +       if (xattr) {
> +               xattr->name = XATTR_SMACK_SUFFIX;
>
> -       if (value && len) {
>                 rcu_read_lock();
>                 may = smk_access_entry(skp->smk_known, dsp->smk_known,
>                                        &skp->smk_rules);
> @@ -976,11 +986,11 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir,
>                         issp->smk_flags |= SMK_INODE_CHANGED;
>                 }
>
> -               *value = kstrdup(isp->smk_known, GFP_NOFS);
> -               if (*value == NULL)
> +               xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
> +               if (xattr->value == NULL)
>                         return -ENOMEM;
>
> -               *len = strlen(isp->smk_known);
> +               xattr->value_len = strlen(isp->smk_known);
>         }
>
>         return 0;
> @@ -4854,6 +4864,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
>         .lbs_ipc = sizeof(struct smack_known *),
>         .lbs_msg_msg = sizeof(struct smack_known *),
>         .lbs_superblock = sizeof(struct superblock_smack),
> +       .lbs_xattr = SMACK_INODE_INIT_XATTRS,
>  };
>
>  static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
> --
> 2.25.1

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24  0:09   ` Paul Moore
@ 2023-03-24  1:01     ` Casey Schaufler
  2023-03-24 14:17       ` Paul Moore
  2023-03-24 10:18     ` Roberto Sassu
  1 sibling, 1 reply; 25+ messages in thread
From: Casey Schaufler @ 2023-03-24  1:01 UTC (permalink / raw)
  To: Paul Moore, Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu, Casey Schaufler

On 3/23/2023 5:09 PM, Paul Moore wrote:
> On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> <roberto.sassu@huaweicloud.com> wrote:
>> From: Roberto Sassu <roberto.sassu@huawei.com>
>>
>> Currently, security_inode_init_security() supports only one LSM providing
>> an xattr and EVM calculating the HMAC on that xattr, plus other inode
>> metadata.
>>
>> Allow all LSMs to provide one or multiple xattrs, by extending the security
>> blob reservation mechanism. Introduce the new lbs_xattr field of the
>> lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
>> needs, and the LSM infrastructure knows how many xattr slots it should
>> allocate.
>>
>> Dynamically allocate the xattrs array to be populated by LSMs with the
>> inode_init_security hook, and pass it to the latter instead of the
>> name/value/len triple. Update the documentation accordingly, and fix the
>> description of the xattr name, as it is not allocated anymore.
>>
>> Since the LSM infrastructure, at initialization time, updates the number of
>> the requested xattrs provided by each LSM with a corresponding offset in
>> the security blob (in this case the xattr array), it makes straightforward
>> for an LSM to access the right position in the xattr array.
>>
>> There is still the issue that an LSM might not fill the xattr, even if it
>> requests it (legitimate case, for example it might have been loaded but not
>> initialized with a policy). Since users of the xattr array (e.g. the
>> initxattrs() callbacks) detect the end of the xattr array by checking if
>> the xattr name is NULL, not filling an xattr would cause those users to
>> stop scanning xattrs prematurely.
>>
>> Solve that issue by introducing security_check_compact_filled_xattrs(),
>> which does a basic check of the xattr array (if the xattr name is filled,
>> the xattr value should be too, and viceversa), and compacts the xattr array
>> by removing the holes.
>>
>> An alternative solution would be to let users of the xattr array know the
>> number of elements of that array, so that they don't have to check the
>> termination. However, this seems more invasive, compared to a simple move
>> of few array elements.
>>
>> security_check_compact_filled_xattrs() also determines how many xattrs in
>> the xattr array have been filled. If there is none, skip
>> evm_inode_init_security() and initxattrs(). Skipping the former also avoids
>> EVM to crash the kernel, as it is expecting a filled xattr.
>>
>> Finally, adapt both SELinux and Smack to use the new definition of the
>> inode_init_security hook, and to correctly fill the designated slots in the
>> xattr array. For Smack, reserve space for the other defined xattrs although
>> they are not set yet in smack_inode_init_security().
>>
>> Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
>> Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
>> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
>> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
>> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
>> ---
>>  include/linux/lsm_hook_defs.h |   3 +-
>>  include/linux/lsm_hooks.h     |   1 +
>>  security/security.c           | 119 +++++++++++++++++++++++++++++-----
>>  security/selinux/hooks.c      |  19 ++++--
>>  security/smack/smack_lsm.c    |  33 ++++++----
>>  5 files changed, 137 insertions(+), 38 deletions(-)
>>
>> diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
>> index 6bb55e61e8e..b814955ae70 100644
>> --- a/include/linux/lsm_hook_defs.h
>> +++ b/include/linux/lsm_hook_defs.h
>> @@ -112,8 +112,7 @@ LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask,
>>  LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode)
>>  LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode)
>>  LSM_HOOK(int, 0, inode_init_security, struct inode *inode,
>> -        struct inode *dir, const struct qstr *qstr, const char **name,
>> -        void **value, size_t *len)
>> +        struct inode *dir, const struct qstr *qstr, struct xattr *xattrs)
>>  LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode,
>>          const struct qstr *name, const struct inode *context_inode)
>>  LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
>> diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
>> index c2be66c669a..75a2f85b49d 100644
>> --- a/include/linux/lsm_hooks.h
>> +++ b/include/linux/lsm_hooks.h
>> @@ -63,6 +63,7 @@ struct lsm_blob_sizes {
>>         int     lbs_ipc;
>>         int     lbs_msg_msg;
>>         int     lbs_task;
>> +       int     lbs_xattr; /* number of xattr slots in new_xattrs array */
> No need for the comment, we don't do it for the other fields.

I asked for the comment. lbs_xattr is the number of entries, which is
different from the other fields. The other fields contain blob sizes in
bytes. Inconsistent behavior should be noted.

>
>>  };
>>
>>  /*
>> diff --git a/security/security.c b/security/security.c
>> index f4170efcddd..f1f5f62f7fa 100644
>> --- a/security/security.c
>> +++ b/security/security.c
>> @@ -1579,6 +1579,52 @@ int security_dentry_create_files_as(struct dentry *dentry, int mode,
>>  }
>>  EXPORT_SYMBOL(security_dentry_create_files_as);
>>
>> +/**
>> + * security_check_compact_filled_xattrs - check xattrs and make array contiguous
>> + * @xattrs: xattr array filled by LSMs
>> + * @num_xattrs: length of xattr array
>> + * @num_filled_xattrs: number of already processed xattrs
>> + *
>> + * Ensure that each xattr slot is correctly filled and close the gaps in the
>> + * xattr array if an LSM didn't provide an xattr for which it asked space
>> + * (legitimate case, it might have been loaded but not initialized). An LSM
>> + * might request space in the xattr array for one or multiple xattrs. The LSM
>> + * infrastructure ensures that all requests by LSMs are satisfied.
>> + *
>> + * Track the number of filled xattrs in @num_filled_xattrs, so that it is easy
>> + * to determine whether the currently processed xattr is fine in its position
>> + * (if all previous xattrs were filled) or it should be moved after the last
>> + * filled xattr.
>> + *
>> + * Return: zero if all xattrs are valid, -EINVAL otherwise.
>> + */
>> +static int security_check_compact_filled_xattrs(struct xattr *xattrs,
>> +                                               int num_xattrs,
>> +                                               int *num_filled_xattrs)
> That is one long name :)
>
> Since you're making some other changes to this patch, can you rename
> this to security_xattr_compact() or something like that?
>
>> +{
>> +       int i;
>> +
>> +       for (i = *num_filled_xattrs; i < num_xattrs; i++) {
>> +               if ((!xattrs[i].name && xattrs[i].value) ||
>> +                   (xattrs[i].name && !xattrs[i].value))
>> +                       return -EINVAL;
>> +
>> +               if (!xattrs[i].name)
>> +                       continue;
>> +
>> +               if (i == *num_filled_xattrs) {
>> +                       (*num_filled_xattrs)++;
>> +                       continue;
>> +               }
>> +
>> +               memcpy(xattrs + (*num_filled_xattrs)++, xattrs + i,
>> +                      sizeof(*xattrs));
>> +               memset(xattrs + i, 0, sizeof(*xattrs));
>> +       }
>> +
>> +       return 0;
>> +}
>> +
>>  /**
>>   * security_inode_init_security() - Initialize an inode's LSM context
>>   * @inode: the inode
>> @@ -1591,9 +1637,13 @@ EXPORT_SYMBOL(security_dentry_create_files_as);
>>   * created inode and set up the incore security field for the new inode.  This
>>   * hook is called by the fs code as part of the inode creation transaction and
>>   * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
>> - * hooks called by the VFS.  The hook function is expected to allocate the name
>> - * and value via kmalloc, with the caller being responsible for calling kfree
>> - * after using them.  If the security module does not use security attributes
>> + * hooks called by the VFS.  The hook function is expected to populate the
>> + * @xattrs array, depending on how many xattrs have been specified by the
>> + * security module in the lbs_xattr field of the lsm_blob_sizes structure.  For
>> + * each array element, the hook function is expected to set ->name to the
>> + * attribute name suffix (e.g. selinux), to allocate ->value (will be freed by
>> + * the caller) and set it to the attribute value, to set ->value_len to the
>> + * length of the value.  If the security module does not use security attributes
>>   * or does not wish to put a security attribute on this particular inode, then
>>   * it should return -EOPNOTSUPP to skip this processing.
>>   *
>> @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
>>                                  const struct qstr *qstr,
>>                                  const initxattrs initxattrs, void *fs_data)
>>  {
>> -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
>> -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
>> -       int ret;
>> +       struct security_hook_list *P;
>> +       struct xattr *new_xattrs;
>> +       struct xattr *xattr;
>> +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
>>
>>         if (unlikely(IS_PRIVATE(inode)))
>>                 return 0;
>>
>> +       if (!blob_sizes.lbs_xattr)
>> +               return 0;
>> +
>>         if (!initxattrs)
>>                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
>> -                                    dir, qstr, NULL, NULL, NULL);
>> -       memset(new_xattrs, 0, sizeof(new_xattrs));
>> -       lsm_xattr = new_xattrs;
>> -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
>> -                           &lsm_xattr->name,
>> -                           &lsm_xattr->value,
>> -                           &lsm_xattr->value_len);
>> -       if (ret)
>> +                                   dir, qstr, NULL);
>> +       /* Allocate +1 for EVM and +1 as terminator. */
>> +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
>> +                            GFP_NOFS);
>> +       if (!new_xattrs)
>> +               return -ENOMEM;
>> +
>> +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
>> +                            list) {
>> +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
>> +               if (ret && ret != -EOPNOTSUPP)
>> +                       goto out;
>> +               /*
>> +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
>> +                * means that the LSM is not willing to provide an xattr, not
>> +                * that it wants to signal an error. Thus, continue to invoke
>> +                * the remaining LSMs.
>> +                */
>> +               if (ret == -EOPNOTSUPP)
>> +                       continue;
>> +               /*
>> +                * As the number of xattrs reserved by LSMs is not directly
>> +                * available, directly use the total number blob_sizes.lbs_xattr
>> +                * to keep the code simple, while being not the most efficient
>> +                * way.
>> +                */
> Is there a good reason why the LSM can't return the number of xattrs
> it is adding to the xattr array?  It seems like it should be fairly
> trivial for the individual LSMs to determine and it could save a lot
> of work.  However, given we're at v8 on this patchset I'm sure I'm
> missing something obvious, can you help me understand why the idea
> above is crazy stupid? ;)
>
>> +               ret = security_check_compact_filled_xattrs(new_xattrs,
>> +                                                          blob_sizes.lbs_xattr,
>> +                                                          &num_filled_xattrs);
>> +               if (ret < 0) {
>> +                       ret = -ENOMEM;
>> +                       goto out;
>> +               }
>> +       }
>> +
>> +       if (!num_filled_xattrs)
>>                 goto out;
>>
>> -       evm_xattr = lsm_xattr + 1;
>> -       ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
>> +       ret = evm_inode_init_security(inode, new_xattrs,
>> +                                     new_xattrs + num_filled_xattrs);
>>         if (ret)
>>                 goto out;
>>         ret = initxattrs(inode, new_xattrs, fs_data);
>>  out:
>>         for (xattr = new_xattrs; xattr->value != NULL; xattr++)
>>                 kfree(xattr->value);
>> +       kfree(new_xattrs);
>>         return (ret == -EOPNOTSUPP) ? 0 : ret;
>>  }
>>  EXPORT_SYMBOL(security_inode_init_security);
>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>> index 9a5bdfc2131..3e4308dd336 100644
>> --- a/security/selinux/hooks.c
>> +++ b/security/selinux/hooks.c
>> @@ -104,6 +104,8 @@
>>  #include "audit.h"
>>  #include "avc_ss.h"
>>
>> +#define SELINUX_INODE_INIT_XATTRS 1
>> +
>>  struct selinux_state selinux_state;
>>
>>  /* SECMARK reference count */
>> @@ -2868,11 +2870,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
>>
>>  static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
>>                                        const struct qstr *qstr,
>> -                                      const char **name,
>> -                                      void **value, size_t *len)
>> +                                      struct xattr *xattrs)
>>  {
>>         const struct task_security_struct *tsec = selinux_cred(current_cred());
>>         struct superblock_security_struct *sbsec;
>> +       struct xattr *xattr = NULL;
>>         u32 newsid, clen;
>>         int rc;
>>         char *context;
>> @@ -2899,16 +2901,18 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
>>             !(sbsec->flags & SBLABEL_MNT))
>>                 return -EOPNOTSUPP;
>>
>> -       if (name)
>> -               *name = XATTR_SELINUX_SUFFIX;
>> +       if (xattrs)
>> +               xattr = xattrs + selinux_blob_sizes.lbs_xattr;
> Please abstract that away to an inline function similar to
> selinux_cred(), selinux_file(), selinux_inode(), etc.
>
>> +       if (xattr) {
>> +               xattr->name = XATTR_SELINUX_SUFFIX;
> I'm guessing the xattr->name assignment is always done, regardless of
> if security_sid_to_context_force() is successful, due to the -EINVAL
> check in security_check_compact_filled_xattrs()?  If yes, it would be
> good to make note of that here in the code.  If not, it would be nice
> to move this down the function to go with the other xattr->XXX
> assignments, unless there is another reason for its placement that I'm
> missing.
>
>> -       if (value && len) {
>>                 rc = security_sid_to_context_force(&selinux_state, newsid,
>>                                                    &context, &clen);
>>                 if (rc)
>>                         return rc;
>> -               *value = context;
>> -               *len = clen;
>> +               xattr->value = context;
>> +               xattr->value_len = clen;
>>         }
>>
>>         return 0;
>> @@ -6918,6 +6922,7 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
>>         .lbs_ipc = sizeof(struct ipc_security_struct),
>>         .lbs_msg_msg = sizeof(struct msg_security_struct),
>>         .lbs_superblock = sizeof(struct superblock_security_struct),
>> +       .lbs_xattr = SELINUX_INODE_INIT_XATTRS,
>>  };
>>
>>  #ifdef CONFIG_PERF_EVENTS
>> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
>> index cfcbb748da2..c8cf8df268b 100644
>> --- a/security/smack/smack_lsm.c
>> +++ b/security/smack/smack_lsm.c
>> @@ -52,6 +52,15 @@
>>  #define SMK_RECEIVING  1
>>  #define SMK_SENDING    2
>>
>> +/*
>> + * Smack uses multiple xattrs.
>> + * SMACK64 - for access control, SMACK64EXEC - label for the program,
>> + * SMACK64MMAP - controls library loading,
>> + * SMACK64TRANSMUTE - label initialization,
>> + * Not saved on files - SMACK64IPIN and SMACK64IPOUT
>> + */
>> +#define SMACK_INODE_INIT_XATTRS 4
>> +
>>  #ifdef SMACK_IPV6_PORT_LABELING
>>  static DEFINE_MUTEX(smack_ipv6_lock);
>>  static LIST_HEAD(smk_ipv6_port_list);
>> @@ -939,26 +948,27 @@ static int smack_inode_alloc_security(struct inode *inode)
>>   * @inode: the newly created inode
>>   * @dir: containing directory object
>>   * @qstr: unused
>> - * @name: where to put the attribute name
>> - * @value: where to put the attribute value
>> - * @len: where to put the length of the attribute
>> + * @xattrs: where to put the attributes
>>   *
>>   * Returns 0 if it all works out, -ENOMEM if there's no memory
>>   */
>>  static int smack_inode_init_security(struct inode *inode, struct inode *dir,
>> -                                    const struct qstr *qstr, const char **name,
>> -                                    void **value, size_t *len)
>> +                                    const struct qstr *qstr,
>> +                                    struct xattr *xattrs)
>>  {
>>         struct inode_smack *issp = smack_inode(inode);
>>         struct smack_known *skp = smk_of_current();
>>         struct smack_known *isp = smk_of_inode(inode);
>>         struct smack_known *dsp = smk_of_inode(dir);
>> +       struct xattr *xattr = NULL;
>>         int may;
>>
>> -       if (name)
>> -               *name = XATTR_SMACK_SUFFIX;
>> +       if (xattrs)
>> +               xattr = xattrs + smack_blob_sizes.lbs_xattr;
>> +
>> +       if (xattr) {
>> +               xattr->name = XATTR_SMACK_SUFFIX;
>>
>> -       if (value && len) {
>>                 rcu_read_lock();
>>                 may = smk_access_entry(skp->smk_known, dsp->smk_known,
>>                                        &skp->smk_rules);
>> @@ -976,11 +986,11 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir,
>>                         issp->smk_flags |= SMK_INODE_CHANGED;
>>                 }
>>
>> -               *value = kstrdup(isp->smk_known, GFP_NOFS);
>> -               if (*value == NULL)
>> +               xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
>> +               if (xattr->value == NULL)
>>                         return -ENOMEM;
>>
>> -               *len = strlen(isp->smk_known);
>> +               xattr->value_len = strlen(isp->smk_known);
>>         }
>>
>>         return 0;
>> @@ -4854,6 +4864,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
>>         .lbs_ipc = sizeof(struct smack_known *),
>>         .lbs_msg_msg = sizeof(struct smack_known *),
>>         .lbs_superblock = sizeof(struct superblock_smack),
>> +       .lbs_xattr = SMACK_INODE_INIT_XATTRS,
>>  };
>>
>>  static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
>> --
>> 2.25.1

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24  0:09   ` Paul Moore
  2023-03-24  1:01     ` Casey Schaufler
@ 2023-03-24 10:18     ` Roberto Sassu
  2023-03-24 13:25       ` Roberto Sassu
  2023-03-24 21:19       ` Paul Moore
  1 sibling, 2 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-24 10:18 UTC (permalink / raw)
  To: Paul Moore
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> <roberto.sassu@huaweicloud.com> wrote:
> > From: Roberto Sassu <roberto.sassu@huawei.com>
> > 
> > Currently, security_inode_init_security() supports only one LSM providing
> > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > metadata.
> > 
> > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > needs, and the LSM infrastructure knows how many xattr slots it should
> > allocate.
> > 
> > Dynamically allocate the xattrs array to be populated by LSMs with the
> > inode_init_security hook, and pass it to the latter instead of the
> > name/value/len triple. Update the documentation accordingly, and fix the
> > description of the xattr name, as it is not allocated anymore.
> > 
> > Since the LSM infrastructure, at initialization time, updates the number of
> > the requested xattrs provided by each LSM with a corresponding offset in
> > the security blob (in this case the xattr array), it makes straightforward
> > for an LSM to access the right position in the xattr array.
> > 
> > There is still the issue that an LSM might not fill the xattr, even if it
> > requests it (legitimate case, for example it might have been loaded but not
> > initialized with a policy). Since users of the xattr array (e.g. the
> > initxattrs() callbacks) detect the end of the xattr array by checking if
> > the xattr name is NULL, not filling an xattr would cause those users to
> > stop scanning xattrs prematurely.
> > 
> > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > which does a basic check of the xattr array (if the xattr name is filled,
> > the xattr value should be too, and viceversa), and compacts the xattr array
> > by removing the holes.
> > 
> > An alternative solution would be to let users of the xattr array know the
> > number of elements of that array, so that they don't have to check the
> > termination. However, this seems more invasive, compared to a simple move
> > of few array elements.
> > 
> > security_check_compact_filled_xattrs() also determines how many xattrs in
> > the xattr array have been filled. If there is none, skip
> > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > EVM to crash the kernel, as it is expecting a filled xattr.
> > 
> > Finally, adapt both SELinux and Smack to use the new definition of the
> > inode_init_security hook, and to correctly fill the designated slots in the
> > xattr array. For Smack, reserve space for the other defined xattrs although
> > they are not set yet in smack_inode_init_security().
> > 
> > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > ---
> >  include/linux/lsm_hook_defs.h |   3 +-
> >  include/linux/lsm_hooks.h     |   1 +
> >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> >  security/selinux/hooks.c      |  19 ++++--
> >  security/smack/smack_lsm.c    |  33 ++++++----
> >  5 files changed, 137 insertions(+), 38 deletions(-)
> > 
> > diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
> > index 6bb55e61e8e..b814955ae70 100644
> > --- a/include/linux/lsm_hook_defs.h
> > +++ b/include/linux/lsm_hook_defs.h
> > @@ -112,8 +112,7 @@ LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask,
> >  LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode)
> >  LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode)
> >  LSM_HOOK(int, 0, inode_init_security, struct inode *inode,
> > -        struct inode *dir, const struct qstr *qstr, const char **name,
> > -        void **value, size_t *len)
> > +        struct inode *dir, const struct qstr *qstr, struct xattr *xattrs)
> >  LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode,
> >          const struct qstr *name, const struct inode *context_inode)
> >  LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
> > diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
> > index c2be66c669a..75a2f85b49d 100644
> > --- a/include/linux/lsm_hooks.h
> > +++ b/include/linux/lsm_hooks.h
> > @@ -63,6 +63,7 @@ struct lsm_blob_sizes {
> >         int     lbs_ipc;
> >         int     lbs_msg_msg;
> >         int     lbs_task;
> > +       int     lbs_xattr; /* number of xattr slots in new_xattrs array */
> 
> No need for the comment, we don't do it for the other fields.
> 
> >  };
> > 
> >  /*
> > diff --git a/security/security.c b/security/security.c
> > index f4170efcddd..f1f5f62f7fa 100644
> > --- a/security/security.c
> > +++ b/security/security.c
> > @@ -1579,6 +1579,52 @@ int security_dentry_create_files_as(struct dentry *dentry, int mode,
> >  }
> >  EXPORT_SYMBOL(security_dentry_create_files_as);
> > 
> > +/**
> > + * security_check_compact_filled_xattrs - check xattrs and make array contiguous
> > + * @xattrs: xattr array filled by LSMs
> > + * @num_xattrs: length of xattr array
> > + * @num_filled_xattrs: number of already processed xattrs
> > + *
> > + * Ensure that each xattr slot is correctly filled and close the gaps in the
> > + * xattr array if an LSM didn't provide an xattr for which it asked space
> > + * (legitimate case, it might have been loaded but not initialized). An LSM
> > + * might request space in the xattr array for one or multiple xattrs. The LSM
> > + * infrastructure ensures that all requests by LSMs are satisfied.
> > + *
> > + * Track the number of filled xattrs in @num_filled_xattrs, so that it is easy
> > + * to determine whether the currently processed xattr is fine in its position
> > + * (if all previous xattrs were filled) or it should be moved after the last
> > + * filled xattr.
> > + *
> > + * Return: zero if all xattrs are valid, -EINVAL otherwise.
> > + */
> > +static int security_check_compact_filled_xattrs(struct xattr *xattrs,
> > +                                               int num_xattrs,
> > +                                               int *num_filled_xattrs)
> 
> That is one long name :)
> 
> Since you're making some other changes to this patch, can you rename
> this to security_xattr_compact() or something like that?

Yes, definitely!

> > +{
> > +       int i;
> > +
> > +       for (i = *num_filled_xattrs; i < num_xattrs; i++) {
> > +               if ((!xattrs[i].name && xattrs[i].value) ||
> > +                   (xattrs[i].name && !xattrs[i].value))
> > +                       return -EINVAL;
> > +
> > +               if (!xattrs[i].name)
> > +                       continue;
> > +
> > +               if (i == *num_filled_xattrs) {
> > +                       (*num_filled_xattrs)++;
> > +                       continue;
> > +               }
> > +
> > +               memcpy(xattrs + (*num_filled_xattrs)++, xattrs + i,
> > +                      sizeof(*xattrs));
> > +               memset(xattrs + i, 0, sizeof(*xattrs));
> > +       }
> > +
> > +       return 0;
> > +}
> > +
> >  /**
> >   * security_inode_init_security() - Initialize an inode's LSM context
> >   * @inode: the inode
> > @@ -1591,9 +1637,13 @@ EXPORT_SYMBOL(security_dentry_create_files_as);
> >   * created inode and set up the incore security field for the new inode.  This
> >   * hook is called by the fs code as part of the inode creation transaction and
> >   * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
> > - * hooks called by the VFS.  The hook function is expected to allocate the name
> > - * and value via kmalloc, with the caller being responsible for calling kfree
> > - * after using them.  If the security module does not use security attributes
> > + * hooks called by the VFS.  The hook function is expected to populate the
> > + * @xattrs array, depending on how many xattrs have been specified by the
> > + * security module in the lbs_xattr field of the lsm_blob_sizes structure.  For
> > + * each array element, the hook function is expected to set ->name to the
> > + * attribute name suffix (e.g. selinux), to allocate ->value (will be freed by
> > + * the caller) and set it to the attribute value, to set ->value_len to the
> > + * length of the value.  If the security module does not use security attributes
> >   * or does not wish to put a security attribute on this particular inode, then
> >   * it should return -EOPNOTSUPP to skip this processing.
> >   *
> > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> >                                  const struct qstr *qstr,
> >                                  const initxattrs initxattrs, void *fs_data)
> >  {
> > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > -       int ret;
> > +       struct security_hook_list *P;
> > +       struct xattr *new_xattrs;
> > +       struct xattr *xattr;
> > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > 
> >         if (unlikely(IS_PRIVATE(inode)))
> >                 return 0;
> > 
> > +       if (!blob_sizes.lbs_xattr)
> > +               return 0;
> > +
> >         if (!initxattrs)
> >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > -                                    dir, qstr, NULL, NULL, NULL);
> > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > -       lsm_xattr = new_xattrs;
> > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > -                           &lsm_xattr->name,
> > -                           &lsm_xattr->value,
> > -                           &lsm_xattr->value_len);
> > -       if (ret)
> > +                                   dir, qstr, NULL);
> > +       /* Allocate +1 for EVM and +1 as terminator. */
> > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > +                            GFP_NOFS);
> > +       if (!new_xattrs)
> > +               return -ENOMEM;
> > +
> > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > +                            list) {
> > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > +               if (ret && ret != -EOPNOTSUPP)
> > +                       goto out;
> > +               /*
> > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > +                * means that the LSM is not willing to provide an xattr, not
> > +                * that it wants to signal an error. Thus, continue to invoke
> > +                * the remaining LSMs.
> > +                */
> > +               if (ret == -EOPNOTSUPP)
> > +                       continue;
> > +               /*
> > +                * As the number of xattrs reserved by LSMs is not directly
> > +                * available, directly use the total number blob_sizes.lbs_xattr
> > +                * to keep the code simple, while being not the most efficient
> > +                * way.
> > +                */
> 
> Is there a good reason why the LSM can't return the number of xattrs
> it is adding to the xattr array?  It seems like it should be fairly
> trivial for the individual LSMs to determine and it could save a lot
> of work.  However, given we're at v8 on this patchset I'm sure I'm
> missing something obvious, can you help me understand why the idea
> above is crazy stupid? ;)

Ok, I looked back at what I did for v3.

Moving from v3 to v4, I decided to put less burden on LSMs, and to make
all the processing from the LSM infrastructure side.

v3 had some safeguards to prevent some programming mistakes by LSMs,
which maybe made the code less understandable.

However, if we say we keep things as simple as possible and assume that
LSMs implement this correctly, we can just pass num_filled_xattrs to
them and they simply increment it.

The EVM bug should not arise (accessing xattr->name = NULL), even if
BPF LSM alone returns zero, due to the check of num_filled_xattrs
before calling evm_inode_init_security().

Patch 6 (at the end) will prevent the bug from arising when EVM is
moved to the LSM infrastructure (no num_filled_xattrs check anymore).
There is a loop that stops if xattr->name is NULL, so
evm_protected_xattr() will not be called.

Or, like you suggested, we just return a positive value from LSMs and
we keep num_filled_xattrs in security_inode_init_security().

Ok, I'll go for your proposal.

> > +               ret = security_check_compact_filled_xattrs(new_xattrs,
> > +                                                          blob_sizes.lbs_xattr,
> > +                                                          &num_filled_xattrs);
> > +               if (ret < 0) {
> > +                       ret = -ENOMEM;
> > +                       goto out;
> > +               }
> > +       }
> > +
> > +       if (!num_filled_xattrs)
> >                 goto out;
> > 
> > -       evm_xattr = lsm_xattr + 1;
> > -       ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
> > +       ret = evm_inode_init_security(inode, new_xattrs,
> > +                                     new_xattrs + num_filled_xattrs);
> >         if (ret)
> >                 goto out;
> >         ret = initxattrs(inode, new_xattrs, fs_data);
> >  out:
> >         for (xattr = new_xattrs; xattr->value != NULL; xattr++)
> >                 kfree(xattr->value);
> > +       kfree(new_xattrs);
> >         return (ret == -EOPNOTSUPP) ? 0 : ret;
> >  }
> >  EXPORT_SYMBOL(security_inode_init_security);
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index 9a5bdfc2131..3e4308dd336 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -104,6 +104,8 @@
> >  #include "audit.h"
> >  #include "avc_ss.h"
> > 
> > +#define SELINUX_INODE_INIT_XATTRS 1
> > +
> >  struct selinux_state selinux_state;
> > 
> >  /* SECMARK reference count */
> > @@ -2868,11 +2870,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
> > 
> >  static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> >                                        const struct qstr *qstr,
> > -                                      const char **name,
> > -                                      void **value, size_t *len)
> > +                                      struct xattr *xattrs)
> >  {
> >         const struct task_security_struct *tsec = selinux_cred(current_cred());
> >         struct superblock_security_struct *sbsec;
> > +       struct xattr *xattr = NULL;
> >         u32 newsid, clen;
> >         int rc;
> >         char *context;
> > @@ -2899,16 +2901,18 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> >             !(sbsec->flags & SBLABEL_MNT))
> >                 return -EOPNOTSUPP;
> > 
> > -       if (name)
> > -               *name = XATTR_SELINUX_SUFFIX;
> > +       if (xattrs)
> > +               xattr = xattrs + selinux_blob_sizes.lbs_xattr;
> 
> Please abstract that away to an inline function similar to
> selinux_cred(), selinux_file(), selinux_inode(), etc.

Ok.

> > +       if (xattr) {
> > +               xattr->name = XATTR_SELINUX_SUFFIX;
> 
> I'm guessing the xattr->name assignment is always done, regardless of
> if security_sid_to_context_force() is successful, due to the -EINVAL
> check in security_check_compact_filled_xattrs()?  If yes, it would be
> good to make note of that here in the code.  If not, it would be nice
> to move this down the function to go with the other xattr->XXX
> assignments, unless there is another reason for its placement that I'm
> missing.

Uhm, if an LSM returns an error, security_inode_init_security() stops
and does the cleanup. It should not matter if xattr->name was set.

Thanks

Roberto

> > -       if (value && len) {
> >                 rc = security_sid_to_context_force(&selinux_state, newsid,
> >                                                    &context, &clen);
> >                 if (rc)
> >                         return rc;
> > -               *value = context;
> > -               *len = clen;
> > +               xattr->value = context;
> > +               xattr->value_len = clen;
> >         }
> > 
> >         return 0;
> > @@ -6918,6 +6922,7 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
> >         .lbs_ipc = sizeof(struct ipc_security_struct),
> >         .lbs_msg_msg = sizeof(struct msg_security_struct),
> >         .lbs_superblock = sizeof(struct superblock_security_struct),
> > +       .lbs_xattr = SELINUX_INODE_INIT_XATTRS,
> >  };
> > 
> >  #ifdef CONFIG_PERF_EVENTS
> > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> > index cfcbb748da2..c8cf8df268b 100644
> > --- a/security/smack/smack_lsm.c
> > +++ b/security/smack/smack_lsm.c
> > @@ -52,6 +52,15 @@
> >  #define SMK_RECEIVING  1
> >  #define SMK_SENDING    2
> > 
> > +/*
> > + * Smack uses multiple xattrs.
> > + * SMACK64 - for access control, SMACK64EXEC - label for the program,
> > + * SMACK64MMAP - controls library loading,
> > + * SMACK64TRANSMUTE - label initialization,
> > + * Not saved on files - SMACK64IPIN and SMACK64IPOUT
> > + */
> > +#define SMACK_INODE_INIT_XATTRS 4
> > +
> >  #ifdef SMACK_IPV6_PORT_LABELING
> >  static DEFINE_MUTEX(smack_ipv6_lock);
> >  static LIST_HEAD(smk_ipv6_port_list);
> > @@ -939,26 +948,27 @@ static int smack_inode_alloc_security(struct inode *inode)
> >   * @inode: the newly created inode
> >   * @dir: containing directory object
> >   * @qstr: unused
> > - * @name: where to put the attribute name
> > - * @value: where to put the attribute value
> > - * @len: where to put the length of the attribute
> > + * @xattrs: where to put the attributes
> >   *
> >   * Returns 0 if it all works out, -ENOMEM if there's no memory
> >   */
> >  static int smack_inode_init_security(struct inode *inode, struct inode *dir,
> > -                                    const struct qstr *qstr, const char **name,
> > -                                    void **value, size_t *len)
> > +                                    const struct qstr *qstr,
> > +                                    struct xattr *xattrs)
> >  {
> >         struct inode_smack *issp = smack_inode(inode);
> >         struct smack_known *skp = smk_of_current();
> >         struct smack_known *isp = smk_of_inode(inode);
> >         struct smack_known *dsp = smk_of_inode(dir);
> > +       struct xattr *xattr = NULL;
> >         int may;
> > 
> > -       if (name)
> > -               *name = XATTR_SMACK_SUFFIX;
> > +       if (xattrs)
> > +               xattr = xattrs + smack_blob_sizes.lbs_xattr;
> > +
> > +       if (xattr) {
> > +               xattr->name = XATTR_SMACK_SUFFIX;
> > 
> > -       if (value && len) {
> >                 rcu_read_lock();
> >                 may = smk_access_entry(skp->smk_known, dsp->smk_known,
> >                                        &skp->smk_rules);
> > @@ -976,11 +986,11 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir,
> >                         issp->smk_flags |= SMK_INODE_CHANGED;
> >                 }
> > 
> > -               *value = kstrdup(isp->smk_known, GFP_NOFS);
> > -               if (*value == NULL)
> > +               xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
> > +               if (xattr->value == NULL)
> >                         return -ENOMEM;
> > 
> > -               *len = strlen(isp->smk_known);
> > +               xattr->value_len = strlen(isp->smk_known);
> >         }
> > 
> >         return 0;
> > @@ -4854,6 +4864,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
> >         .lbs_ipc = sizeof(struct smack_known *),
> >         .lbs_msg_msg = sizeof(struct smack_known *),
> >         .lbs_superblock = sizeof(struct superblock_smack),
> > +       .lbs_xattr = SMACK_INODE_INIT_XATTRS,
> >  };
> > 
> >  static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
> > --
> > 2.25.1


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24 10:18     ` Roberto Sassu
@ 2023-03-24 13:25       ` Roberto Sassu
  2023-03-24 21:39         ` Paul Moore
  2023-03-24 21:19       ` Paul Moore
  1 sibling, 1 reply; 25+ messages in thread
From: Roberto Sassu @ 2023-03-24 13:25 UTC (permalink / raw)
  To: Paul Moore
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Fri, 2023-03-24 at 11:18 +0100, Roberto Sassu wrote:
> On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > <roberto.sassu@huaweicloud.com> wrote:
> > > From: Roberto Sassu <roberto.sassu@huawei.com>
> > > 
> > > Currently, security_inode_init_security() supports only one LSM providing
> > > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > > metadata.
> > > 
> > > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > > needs, and the LSM infrastructure knows how many xattr slots it should
> > > allocate.
> > > 
> > > Dynamically allocate the xattrs array to be populated by LSMs with the
> > > inode_init_security hook, and pass it to the latter instead of the
> > > name/value/len triple. Update the documentation accordingly, and fix the
> > > description of the xattr name, as it is not allocated anymore.
> > > 
> > > Since the LSM infrastructure, at initialization time, updates the number of
> > > the requested xattrs provided by each LSM with a corresponding offset in
> > > the security blob (in this case the xattr array), it makes straightforward
> > > for an LSM to access the right position in the xattr array.
> > > 
> > > There is still the issue that an LSM might not fill the xattr, even if it
> > > requests it (legitimate case, for example it might have been loaded but not
> > > initialized with a policy). Since users of the xattr array (e.g. the
> > > initxattrs() callbacks) detect the end of the xattr array by checking if
> > > the xattr name is NULL, not filling an xattr would cause those users to
> > > stop scanning xattrs prematurely.
> > > 
> > > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > > which does a basic check of the xattr array (if the xattr name is filled,
> > > the xattr value should be too, and viceversa), and compacts the xattr array
> > > by removing the holes.
> > > 
> > > An alternative solution would be to let users of the xattr array know the
> > > number of elements of that array, so that they don't have to check the
> > > termination. However, this seems more invasive, compared to a simple move
> > > of few array elements.
> > > 
> > > security_check_compact_filled_xattrs() also determines how many xattrs in
> > > the xattr array have been filled. If there is none, skip
> > > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > > EVM to crash the kernel, as it is expecting a filled xattr.
> > > 
> > > Finally, adapt both SELinux and Smack to use the new definition of the
> > > inode_init_security hook, and to correctly fill the designated slots in the
> > > xattr array. For Smack, reserve space for the other defined xattrs although
> > > they are not set yet in smack_inode_init_security().
> > > 
> > > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > > ---
> > >  include/linux/lsm_hook_defs.h |   3 +-
> > >  include/linux/lsm_hooks.h     |   1 +
> > >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> > >  security/selinux/hooks.c      |  19 ++++--
> > >  security/smack/smack_lsm.c    |  33 ++++++----
> > >  5 files changed, 137 insertions(+), 38 deletions(-)
> > > 
> > > diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
> > > index 6bb55e61e8e..b814955ae70 100644
> > > --- a/include/linux/lsm_hook_defs.h
> > > +++ b/include/linux/lsm_hook_defs.h
> > > @@ -112,8 +112,7 @@ LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask,
> > >  LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode)
> > >  LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode)
> > >  LSM_HOOK(int, 0, inode_init_security, struct inode *inode,
> > > -        struct inode *dir, const struct qstr *qstr, const char **name,
> > > -        void **value, size_t *len)
> > > +        struct inode *dir, const struct qstr *qstr, struct xattr *xattrs)
> > >  LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode,
> > >          const struct qstr *name, const struct inode *context_inode)
> > >  LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
> > > diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
> > > index c2be66c669a..75a2f85b49d 100644
> > > --- a/include/linux/lsm_hooks.h
> > > +++ b/include/linux/lsm_hooks.h
> > > @@ -63,6 +63,7 @@ struct lsm_blob_sizes {
> > >         int     lbs_ipc;
> > >         int     lbs_msg_msg;
> > >         int     lbs_task;
> > > +       int     lbs_xattr; /* number of xattr slots in new_xattrs array */
> > 
> > No need for the comment, we don't do it for the other fields.
> > 
> > >  };
> > > 
> > >  /*
> > > diff --git a/security/security.c b/security/security.c
> > > index f4170efcddd..f1f5f62f7fa 100644
> > > --- a/security/security.c
> > > +++ b/security/security.c
> > > @@ -1579,6 +1579,52 @@ int security_dentry_create_files_as(struct dentry *dentry, int mode,
> > >  }
> > >  EXPORT_SYMBOL(security_dentry_create_files_as);
> > > 
> > > +/**
> > > + * security_check_compact_filled_xattrs - check xattrs and make array contiguous
> > > + * @xattrs: xattr array filled by LSMs
> > > + * @num_xattrs: length of xattr array
> > > + * @num_filled_xattrs: number of already processed xattrs
> > > + *
> > > + * Ensure that each xattr slot is correctly filled and close the gaps in the
> > > + * xattr array if an LSM didn't provide an xattr for which it asked space
> > > + * (legitimate case, it might have been loaded but not initialized). An LSM
> > > + * might request space in the xattr array for one or multiple xattrs. The LSM
> > > + * infrastructure ensures that all requests by LSMs are satisfied.
> > > + *
> > > + * Track the number of filled xattrs in @num_filled_xattrs, so that it is easy
> > > + * to determine whether the currently processed xattr is fine in its position
> > > + * (if all previous xattrs were filled) or it should be moved after the last
> > > + * filled xattr.
> > > + *
> > > + * Return: zero if all xattrs are valid, -EINVAL otherwise.
> > > + */
> > > +static int security_check_compact_filled_xattrs(struct xattr *xattrs,
> > > +                                               int num_xattrs,
> > > +                                               int *num_filled_xattrs)
> > 
> > That is one long name :)
> > 
> > Since you're making some other changes to this patch, can you rename
> > this to security_xattr_compact() or something like that?
> 
> Yes, definitely!
> 
> > > +{
> > > +       int i;
> > > +
> > > +       for (i = *num_filled_xattrs; i < num_xattrs; i++) {
> > > +               if ((!xattrs[i].name && xattrs[i].value) ||
> > > +                   (xattrs[i].name && !xattrs[i].value))
> > > +                       return -EINVAL;
> > > +
> > > +               if (!xattrs[i].name)
> > > +                       continue;
> > > +
> > > +               if (i == *num_filled_xattrs) {
> > > +                       (*num_filled_xattrs)++;
> > > +                       continue;
> > > +               }
> > > +
> > > +               memcpy(xattrs + (*num_filled_xattrs)++, xattrs + i,
> > > +                      sizeof(*xattrs));
> > > +               memset(xattrs + i, 0, sizeof(*xattrs));
> > > +       }
> > > +
> > > +       return 0;
> > > +}
> > > +
> > >  /**
> > >   * security_inode_init_security() - Initialize an inode's LSM context
> > >   * @inode: the inode
> > > @@ -1591,9 +1637,13 @@ EXPORT_SYMBOL(security_dentry_create_files_as);
> > >   * created inode and set up the incore security field for the new inode.  This
> > >   * hook is called by the fs code as part of the inode creation transaction and
> > >   * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
> > > - * hooks called by the VFS.  The hook function is expected to allocate the name
> > > - * and value via kmalloc, with the caller being responsible for calling kfree
> > > - * after using them.  If the security module does not use security attributes
> > > + * hooks called by the VFS.  The hook function is expected to populate the
> > > + * @xattrs array, depending on how many xattrs have been specified by the
> > > + * security module in the lbs_xattr field of the lsm_blob_sizes structure.  For
> > > + * each array element, the hook function is expected to set ->name to the
> > > + * attribute name suffix (e.g. selinux), to allocate ->value (will be freed by
> > > + * the caller) and set it to the attribute value, to set ->value_len to the
> > > + * length of the value.  If the security module does not use security attributes
> > >   * or does not wish to put a security attribute on this particular inode, then
> > >   * it should return -EOPNOTSUPP to skip this processing.
> > >   *
> > > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> > >                                  const struct qstr *qstr,
> > >                                  const initxattrs initxattrs, void *fs_data)
> > >  {
> > > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > > -       int ret;
> > > +       struct security_hook_list *P;
> > > +       struct xattr *new_xattrs;
> > > +       struct xattr *xattr;
> > > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > > 
> > >         if (unlikely(IS_PRIVATE(inode)))
> > >                 return 0;
> > > 
> > > +       if (!blob_sizes.lbs_xattr)
> > > +               return 0;
> > > +
> > >         if (!initxattrs)
> > >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > > -                                    dir, qstr, NULL, NULL, NULL);
> > > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > > -       lsm_xattr = new_xattrs;
> > > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > > -                           &lsm_xattr->name,
> > > -                           &lsm_xattr->value,
> > > -                           &lsm_xattr->value_len);
> > > -       if (ret)
> > > +                                   dir, qstr, NULL);
> > > +       /* Allocate +1 for EVM and +1 as terminator. */
> > > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > > +                            GFP_NOFS);
> > > +       if (!new_xattrs)
> > > +               return -ENOMEM;
> > > +
> > > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > > +                            list) {
> > > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > > +               if (ret && ret != -EOPNOTSUPP)
> > > +                       goto out;
> > > +               /*
> > > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > > +                * means that the LSM is not willing to provide an xattr, not
> > > +                * that it wants to signal an error. Thus, continue to invoke
> > > +                * the remaining LSMs.
> > > +                */
> > > +               if (ret == -EOPNOTSUPP)
> > > +                       continue;
> > > +               /*
> > > +                * As the number of xattrs reserved by LSMs is not directly
> > > +                * available, directly use the total number blob_sizes.lbs_xattr
> > > +                * to keep the code simple, while being not the most efficient
> > > +                * way.
> > > +                */
> > 
> > Is there a good reason why the LSM can't return the number of xattrs
> > it is adding to the xattr array?  It seems like it should be fairly
> > trivial for the individual LSMs to determine and it could save a lot
> > of work.  However, given we're at v8 on this patchset I'm sure I'm
> > missing something obvious, can you help me understand why the idea
> > above is crazy stupid? ;)

Much simple answer. Yes, LSMs could return the number of xattrs set,
but security_check_compact_filled_xattrs() also needs to know from
which offset (the lbs_xattr of each LSM) it should start compacting.

Example: suppose that you have three LSMs with:

LSM#1: lbs_xattr 1
LSM#2: lbs_xattr 2 (disabled)
LSM#3: lbs_xattr 1

The current compaction interval is: already compacted xattrs - end of
new_xattr array.

When the security_inode_init_security() loop calls LSM#3, the
compaction interval is: 1 - 2 (LSM#2 returns 0), which clearly isn't
right. The correct compaction interval should be: 3 - 4.

Going to the end of new_xattrs is an approximation, but it ensures
that security_check_compact_filled_xattrs() reaches the xattr set by
LSM#3.

The alternative I was mentioning of passing num_filled_xattrs to LSMs
goes again in the direction of doing on-the-fly compaction, while LSMs
are more familiar with using the lbs_* fields.

I suggest to keep this part as it is, if you agree.

Thanks

Roberto

> Ok, I looked back at what I did for v3.
> 
> Moving from v3 to v4, I decided to put less burden on LSMs, and to make
> all the processing from the LSM infrastructure side.
> 
> v3 had some safeguards to prevent some programming mistakes by LSMs,
> which maybe made the code less understandable.
> 
> However, if we say we keep things as simple as possible and assume that
> LSMs implement this correctly, we can just pass num_filled_xattrs to
> them and they simply increment it.
> 
> The EVM bug should not arise (accessing xattr->name = NULL), even if
> BPF LSM alone returns zero, due to the check of num_filled_xattrs
> before calling evm_inode_init_security().
> 
> Patch 6 (at the end) will prevent the bug from arising when EVM is
> moved to the LSM infrastructure (no num_filled_xattrs check anymore).
> There is a loop that stops if xattr->name is NULL, so
> evm_protected_xattr() will not be called.
> 
> Or, like you suggested, we just return a positive value from LSMs and
> we keep num_filled_xattrs in security_inode_init_security().
> 
> Ok, I'll go for your proposal.
> 
> > > +               ret = security_check_compact_filled_xattrs(new_xattrs,
> > > +                                                          blob_sizes.lbs_xattr,
> > > +                                                          &num_filled_xattrs);
> > > +               if (ret < 0) {
> > > +                       ret = -ENOMEM;
> > > +                       goto out;
> > > +               }
> > > +       }
> > > +
> > > +       if (!num_filled_xattrs)
> > >                 goto out;
> > > 
> > > -       evm_xattr = lsm_xattr + 1;
> > > -       ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
> > > +       ret = evm_inode_init_security(inode, new_xattrs,
> > > +                                     new_xattrs + num_filled_xattrs);
> > >         if (ret)
> > >                 goto out;
> > >         ret = initxattrs(inode, new_xattrs, fs_data);
> > >  out:
> > >         for (xattr = new_xattrs; xattr->value != NULL; xattr++)
> > >                 kfree(xattr->value);
> > > +       kfree(new_xattrs);
> > >         return (ret == -EOPNOTSUPP) ? 0 : ret;
> > >  }
> > >  EXPORT_SYMBOL(security_inode_init_security);
> > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > > index 9a5bdfc2131..3e4308dd336 100644
> > > --- a/security/selinux/hooks.c
> > > +++ b/security/selinux/hooks.c
> > > @@ -104,6 +104,8 @@
> > >  #include "audit.h"
> > >  #include "avc_ss.h"
> > > 
> > > +#define SELINUX_INODE_INIT_XATTRS 1
> > > +
> > >  struct selinux_state selinux_state;
> > > 
> > >  /* SECMARK reference count */
> > > @@ -2868,11 +2870,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
> > > 
> > >  static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> > >                                        const struct qstr *qstr,
> > > -                                      const char **name,
> > > -                                      void **value, size_t *len)
> > > +                                      struct xattr *xattrs)
> > >  {
> > >         const struct task_security_struct *tsec = selinux_cred(current_cred());
> > >         struct superblock_security_struct *sbsec;
> > > +       struct xattr *xattr = NULL;
> > >         u32 newsid, clen;
> > >         int rc;
> > >         char *context;
> > > @@ -2899,16 +2901,18 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> > >             !(sbsec->flags & SBLABEL_MNT))
> > >                 return -EOPNOTSUPP;
> > > 
> > > -       if (name)
> > > -               *name = XATTR_SELINUX_SUFFIX;
> > > +       if (xattrs)
> > > +               xattr = xattrs + selinux_blob_sizes.lbs_xattr;
> > 
> > Please abstract that away to an inline function similar to
> > selinux_cred(), selinux_file(), selinux_inode(), etc.
> 
> Ok.
> 
> > > +       if (xattr) {
> > > +               xattr->name = XATTR_SELINUX_SUFFIX;
> > 
> > I'm guessing the xattr->name assignment is always done, regardless of
> > if security_sid_to_context_force() is successful, due to the -EINVAL
> > check in security_check_compact_filled_xattrs()?  If yes, it would be
> > good to make note of that here in the code.  If not, it would be nice
> > to move this down the function to go with the other xattr->XXX
> > assignments, unless there is another reason for its placement that I'm
> > missing.
> 
> Uhm, if an LSM returns an error, security_inode_init_security() stops
> and does the cleanup. It should not matter if xattr->name was set.
> 
> Thanks
> 
> Roberto
> 
> > > -       if (value && len) {
> > >                 rc = security_sid_to_context_force(&selinux_state, newsid,
> > >                                                    &context, &clen);
> > >                 if (rc)
> > >                         return rc;
> > > -               *value = context;
> > > -               *len = clen;
> > > +               xattr->value = context;
> > > +               xattr->value_len = clen;
> > >         }
> > > 
> > >         return 0;
> > > @@ -6918,6 +6922,7 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
> > >         .lbs_ipc = sizeof(struct ipc_security_struct),
> > >         .lbs_msg_msg = sizeof(struct msg_security_struct),
> > >         .lbs_superblock = sizeof(struct superblock_security_struct),
> > > +       .lbs_xattr = SELINUX_INODE_INIT_XATTRS,
> > >  };
> > > 
> > >  #ifdef CONFIG_PERF_EVENTS
> > > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> > > index cfcbb748da2..c8cf8df268b 100644
> > > --- a/security/smack/smack_lsm.c
> > > +++ b/security/smack/smack_lsm.c
> > > @@ -52,6 +52,15 @@
> > >  #define SMK_RECEIVING  1
> > >  #define SMK_SENDING    2
> > > 
> > > +/*
> > > + * Smack uses multiple xattrs.
> > > + * SMACK64 - for access control, SMACK64EXEC - label for the program,
> > > + * SMACK64MMAP - controls library loading,
> > > + * SMACK64TRANSMUTE - label initialization,
> > > + * Not saved on files - SMACK64IPIN and SMACK64IPOUT
> > > + */
> > > +#define SMACK_INODE_INIT_XATTRS 4
> > > +
> > >  #ifdef SMACK_IPV6_PORT_LABELING
> > >  static DEFINE_MUTEX(smack_ipv6_lock);
> > >  static LIST_HEAD(smk_ipv6_port_list);
> > > @@ -939,26 +948,27 @@ static int smack_inode_alloc_security(struct inode *inode)
> > >   * @inode: the newly created inode
> > >   * @dir: containing directory object
> > >   * @qstr: unused
> > > - * @name: where to put the attribute name
> > > - * @value: where to put the attribute value
> > > - * @len: where to put the length of the attribute
> > > + * @xattrs: where to put the attributes
> > >   *
> > >   * Returns 0 if it all works out, -ENOMEM if there's no memory
> > >   */
> > >  static int smack_inode_init_security(struct inode *inode, struct inode *dir,
> > > -                                    const struct qstr *qstr, const char **name,
> > > -                                    void **value, size_t *len)
> > > +                                    const struct qstr *qstr,
> > > +                                    struct xattr *xattrs)
> > >  {
> > >         struct inode_smack *issp = smack_inode(inode);
> > >         struct smack_known *skp = smk_of_current();
> > >         struct smack_known *isp = smk_of_inode(inode);
> > >         struct smack_known *dsp = smk_of_inode(dir);
> > > +       struct xattr *xattr = NULL;
> > >         int may;
> > > 
> > > -       if (name)
> > > -               *name = XATTR_SMACK_SUFFIX;
> > > +       if (xattrs)
> > > +               xattr = xattrs + smack_blob_sizes.lbs_xattr;
> > > +
> > > +       if (xattr) {
> > > +               xattr->name = XATTR_SMACK_SUFFIX;
> > > 
> > > -       if (value && len) {
> > >                 rcu_read_lock();
> > >                 may = smk_access_entry(skp->smk_known, dsp->smk_known,
> > >                                        &skp->smk_rules);
> > > @@ -976,11 +986,11 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir,
> > >                         issp->smk_flags |= SMK_INODE_CHANGED;
> > >                 }
> > > 
> > > -               *value = kstrdup(isp->smk_known, GFP_NOFS);
> > > -               if (*value == NULL)
> > > +               xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
> > > +               if (xattr->value == NULL)
> > >                         return -ENOMEM;
> > > 
> > > -               *len = strlen(isp->smk_known);
> > > +               xattr->value_len = strlen(isp->smk_known);
> > >         }
> > > 
> > >         return 0;
> > > @@ -4854,6 +4864,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
> > >         .lbs_ipc = sizeof(struct smack_known *),
> > >         .lbs_msg_msg = sizeof(struct smack_known *),
> > >         .lbs_superblock = sizeof(struct superblock_smack),
> > > +       .lbs_xattr = SMACK_INODE_INIT_XATTRS,
> > >  };
> > > 
> > >  static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
> > > --
> > > 2.25.1


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24  1:01     ` Casey Schaufler
@ 2023-03-24 14:17       ` Paul Moore
  0 siblings, 0 replies; 25+ messages in thread
From: Paul Moore @ 2023-03-24 14:17 UTC (permalink / raw)
  To: Casey Schaufler
  Cc: Roberto Sassu, mark, jlbec, joseph.qi, zohar, dmitry.kasatkin,
	jmorris, serge, stephen.smalley.work, eparis, ocfs2-devel,
	reiserfs-devel, linux-integrity, linux-security-module, selinux,
	linux-kernel, keescook, nicolas.bouchinet, Roberto Sassu

On Thu, Mar 23, 2023 at 9:01 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
> On 3/23/2023 5:09 PM, Paul Moore wrote:
> > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > <roberto.sassu@huaweicloud.com> wrote:

...

> >> diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
> >> index c2be66c669a..75a2f85b49d 100644
> >> --- a/include/linux/lsm_hooks.h
> >> +++ b/include/linux/lsm_hooks.h
> >> @@ -63,6 +63,7 @@ struct lsm_blob_sizes {
> >>         int     lbs_ipc;
> >>         int     lbs_msg_msg;
> >>         int     lbs_task;
> >> +       int     lbs_xattr; /* number of xattr slots in new_xattrs array */
> >
> > No need for the comment, we don't do it for the other fields.
>
> I asked for the comment. lbs_xattr is the number of entries, which is
> different from the other fields. The other fields contain blob sizes in
> bytes. Inconsistent behavior should be noted.

Fair enough.  Since that's the case, let's encode something in the
field name itself so that every user has a slight reminder that it is
a count and not a size.  How about 'lbs_xattr_count' or similar?

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24 10:18     ` Roberto Sassu
  2023-03-24 13:25       ` Roberto Sassu
@ 2023-03-24 21:19       ` Paul Moore
  2023-03-27  7:35         ` Roberto Sassu
  1 sibling, 1 reply; 25+ messages in thread
From: Paul Moore @ 2023-03-24 21:19 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Fri, Mar 24, 2023 at 6:18 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
> On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > <roberto.sassu@huaweicloud.com> wrote:
> > > From: Roberto Sassu <roberto.sassu@huawei.com>
> > >
> > > Currently, security_inode_init_security() supports only one LSM providing
> > > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > > metadata.
> > >
> > > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > > needs, and the LSM infrastructure knows how many xattr slots it should
> > > allocate.
> > >
> > > Dynamically allocate the xattrs array to be populated by LSMs with the
> > > inode_init_security hook, and pass it to the latter instead of the
> > > name/value/len triple. Update the documentation accordingly, and fix the
> > > description of the xattr name, as it is not allocated anymore.
> > >
> > > Since the LSM infrastructure, at initialization time, updates the number of
> > > the requested xattrs provided by each LSM with a corresponding offset in
> > > the security blob (in this case the xattr array), it makes straightforward
> > > for an LSM to access the right position in the xattr array.
> > >
> > > There is still the issue that an LSM might not fill the xattr, even if it
> > > requests it (legitimate case, for example it might have been loaded but not
> > > initialized with a policy). Since users of the xattr array (e.g. the
> > > initxattrs() callbacks) detect the end of the xattr array by checking if
> > > the xattr name is NULL, not filling an xattr would cause those users to
> > > stop scanning xattrs prematurely.
> > >
> > > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > > which does a basic check of the xattr array (if the xattr name is filled,
> > > the xattr value should be too, and viceversa), and compacts the xattr array
> > > by removing the holes.
> > >
> > > An alternative solution would be to let users of the xattr array know the
> > > number of elements of that array, so that they don't have to check the
> > > termination. However, this seems more invasive, compared to a simple move
> > > of few array elements.
> > >
> > > security_check_compact_filled_xattrs() also determines how many xattrs in
> > > the xattr array have been filled. If there is none, skip
> > > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > > EVM to crash the kernel, as it is expecting a filled xattr.
> > >
> > > Finally, adapt both SELinux and Smack to use the new definition of the
> > > inode_init_security hook, and to correctly fill the designated slots in the
> > > xattr array. For Smack, reserve space for the other defined xattrs although
> > > they are not set yet in smack_inode_init_security().
> > >
> > > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > > ---
> > >  include/linux/lsm_hook_defs.h |   3 +-
> > >  include/linux/lsm_hooks.h     |   1 +
> > >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> > >  security/selinux/hooks.c      |  19 ++++--
> > >  security/smack/smack_lsm.c    |  33 ++++++----
> > >  5 files changed, 137 insertions(+), 38 deletions(-)

...

> > > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> > >                                  const struct qstr *qstr,
> > >                                  const initxattrs initxattrs, void *fs_data)
> > >  {
> > > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > > -       int ret;
> > > +       struct security_hook_list *P;
> > > +       struct xattr *new_xattrs;
> > > +       struct xattr *xattr;
> > > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > >
> > >         if (unlikely(IS_PRIVATE(inode)))
> > >                 return 0;
> > >
> > > +       if (!blob_sizes.lbs_xattr)
> > > +               return 0;
> > > +
> > >         if (!initxattrs)
> > >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > > -                                    dir, qstr, NULL, NULL, NULL);
> > > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > > -       lsm_xattr = new_xattrs;
> > > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > > -                           &lsm_xattr->name,
> > > -                           &lsm_xattr->value,
> > > -                           &lsm_xattr->value_len);
> > > -       if (ret)
> > > +                                   dir, qstr, NULL);
> > > +       /* Allocate +1 for EVM and +1 as terminator. */
> > > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > > +                            GFP_NOFS);
> > > +       if (!new_xattrs)
> > > +               return -ENOMEM;
> > > +
> > > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > > +                            list) {
> > > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > > +               if (ret && ret != -EOPNOTSUPP)
> > > +                       goto out;
> > > +               /*
> > > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > > +                * means that the LSM is not willing to provide an xattr, not
> > > +                * that it wants to signal an error. Thus, continue to invoke
> > > +                * the remaining LSMs.
> > > +                */
> > > +               if (ret == -EOPNOTSUPP)
> > > +                       continue;
> > > +               /*
> > > +                * As the number of xattrs reserved by LSMs is not directly
> > > +                * available, directly use the total number blob_sizes.lbs_xattr
> > > +                * to keep the code simple, while being not the most efficient
> > > +                * way.
> > > +                */
> >
> > Is there a good reason why the LSM can't return the number of xattrs
> > it is adding to the xattr array?  It seems like it should be fairly
> > trivial for the individual LSMs to determine and it could save a lot
> > of work.  However, given we're at v8 on this patchset I'm sure I'm
> > missing something obvious, can you help me understand why the idea
> > above is crazy stupid? ;)
>
> Ok, I looked back at what I did for v3.
>
> Moving from v3 to v4, I decided to put less burden on LSMs, and to make
> all the processing from the LSM infrastructure side.

As a general rule I think it's a good goal to keep the LSM layer as
small as possible; I believe it allows us to be more flexible with the
LSMs and it keeps the LSM as simple as possible.  I mean less code,
less bugs, amirite? ... ;)

> v3 had some safeguards to prevent some programming mistakes by LSMs,
> which maybe made the code less understandable.
>
> However, if we say we keep things as simple as possible and assume that
> LSMs implement this correctly, we can just pass num_filled_xattrs to
> them and they simply increment it.
>
> The EVM bug should not arise (accessing xattr->name = NULL), even if
> BPF LSM alone returns zero, due to the check of num_filled_xattrs
> before calling evm_inode_init_security().
>
> Patch 6 (at the end) will prevent the bug from arising when EVM is
> moved to the LSM infrastructure (no num_filled_xattrs check anymore).
> There is a loop that stops if xattr->name is NULL, so
> evm_protected_xattr() will not be called.
>
> Or, like you suggested, we just return a positive value from LSMs and
> we keep num_filled_xattrs in security_inode_init_security().

I like the idea of individual LSMs simply reporting the number of
xattrs they've generated instead of incrementing the num_filled_xattrs
variable.

It seems like returning the xattr count as a positive return value
should work just fine, leaving negative values for errors, but if you
run into problems you can always pass the value back in a new
parameter pointer if needed.

> > > @@ -2868,11 +2870,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
> > >
> > >  static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> > >                                        const struct qstr *qstr,
> > > -                                      const char **name,
> > > -                                      void **value, size_t *len)
> > > +                                      struct xattr *xattrs)
> > >  {
> > >         const struct task_security_struct *tsec = selinux_cred(current_cred());
> > >         struct superblock_security_struct *sbsec;
> > > +       struct xattr *xattr = NULL;
> > >         u32 newsid, clen;
> > >         int rc;
> > >         char *context;
> > > @@ -2899,16 +2901,18 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> > >             !(sbsec->flags & SBLABEL_MNT))
> > >                 return -EOPNOTSUPP;
> > >
> > > -       if (name)
> > > -               *name = XATTR_SELINUX_SUFFIX;
> > > +       if (xattrs)
> > > +               xattr = xattrs + selinux_blob_sizes.lbs_xattr;
> >
> > Please abstract that away to an inline function similar to
> > selinux_cred(), selinux_file(), selinux_inode(), etc.
>
> Ok.
>
> > > +       if (xattr) {
> > > +               xattr->name = XATTR_SELINUX_SUFFIX;
> >
> > I'm guessing the xattr->name assignment is always done, regardless of
> > if security_sid_to_context_force() is successful, due to the -EINVAL
> > check in security_check_compact_filled_xattrs()?  If yes, it would be
> > good to make note of that here in the code.  If not, it would be nice
> > to move this down the function to go with the other xattr->XXX
> > assignments, unless there is another reason for its placement that I'm
> > missing.
>
> Uhm, if an LSM returns an error, security_inode_init_security() stops
> and does the cleanup. It should not matter if xattr->name was set.

Okay, I thought I might be missing something during the review.  Since
there is no special reason for putting the xattr->name assignment up
there, please move it down below with the other xattr->XXX
assignments.

Thanks.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24 13:25       ` Roberto Sassu
@ 2023-03-24 21:39         ` Paul Moore
  2023-03-27  7:29           ` Roberto Sassu
  0 siblings, 1 reply; 25+ messages in thread
From: Paul Moore @ 2023-03-24 21:39 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Fri, Mar 24, 2023 at 9:26 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
>
> On Fri, 2023-03-24 at 11:18 +0100, Roberto Sassu wrote:
> > On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > > <roberto.sassu@huaweicloud.com> wrote:
> > > > From: Roberto Sassu <roberto.sassu@huawei.com>
> > > >
> > > > Currently, security_inode_init_security() supports only one LSM providing
> > > > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > > > metadata.
> > > >
> > > > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > > > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > > > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > > > needs, and the LSM infrastructure knows how many xattr slots it should
> > > > allocate.
> > > >
> > > > Dynamically allocate the xattrs array to be populated by LSMs with the
> > > > inode_init_security hook, and pass it to the latter instead of the
> > > > name/value/len triple. Update the documentation accordingly, and fix the
> > > > description of the xattr name, as it is not allocated anymore.
> > > >
> > > > Since the LSM infrastructure, at initialization time, updates the number of
> > > > the requested xattrs provided by each LSM with a corresponding offset in
> > > > the security blob (in this case the xattr array), it makes straightforward
> > > > for an LSM to access the right position in the xattr array.
> > > >
> > > > There is still the issue that an LSM might not fill the xattr, even if it
> > > > requests it (legitimate case, for example it might have been loaded but not
> > > > initialized with a policy). Since users of the xattr array (e.g. the
> > > > initxattrs() callbacks) detect the end of the xattr array by checking if
> > > > the xattr name is NULL, not filling an xattr would cause those users to
> > > > stop scanning xattrs prematurely.
> > > >
> > > > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > > > which does a basic check of the xattr array (if the xattr name is filled,
> > > > the xattr value should be too, and viceversa), and compacts the xattr array
> > > > by removing the holes.
> > > >
> > > > An alternative solution would be to let users of the xattr array know the
> > > > number of elements of that array, so that they don't have to check the
> > > > termination. However, this seems more invasive, compared to a simple move
> > > > of few array elements.
> > > >
> > > > security_check_compact_filled_xattrs() also determines how many xattrs in
> > > > the xattr array have been filled. If there is none, skip
> > > > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > > > EVM to crash the kernel, as it is expecting a filled xattr.
> > > >
> > > > Finally, adapt both SELinux and Smack to use the new definition of the
> > > > inode_init_security hook, and to correctly fill the designated slots in the
> > > > xattr array. For Smack, reserve space for the other defined xattrs although
> > > > they are not set yet in smack_inode_init_security().
> > > >
> > > > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > > > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > > > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > > > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > > > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > > > ---
> > > >  include/linux/lsm_hook_defs.h |   3 +-
> > > >  include/linux/lsm_hooks.h     |   1 +
> > > >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> > > >  security/selinux/hooks.c      |  19 ++++--
> > > >  security/smack/smack_lsm.c    |  33 ++++++----
> > > >  5 files changed, 137 insertions(+), 38 deletions(-)

...

> > > > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> > > >                                  const struct qstr *qstr,
> > > >                                  const initxattrs initxattrs, void *fs_data)
> > > >  {
> > > > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > > > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > > > -       int ret;
> > > > +       struct security_hook_list *P;
> > > > +       struct xattr *new_xattrs;
> > > > +       struct xattr *xattr;
> > > > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > > >
> > > >         if (unlikely(IS_PRIVATE(inode)))
> > > >                 return 0;
> > > >
> > > > +       if (!blob_sizes.lbs_xattr)
> > > > +               return 0;
> > > > +
> > > >         if (!initxattrs)
> > > >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > > > -                                    dir, qstr, NULL, NULL, NULL);
> > > > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > > > -       lsm_xattr = new_xattrs;
> > > > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > > > -                           &lsm_xattr->name,
> > > > -                           &lsm_xattr->value,
> > > > -                           &lsm_xattr->value_len);
> > > > -       if (ret)
> > > > +                                   dir, qstr, NULL);
> > > > +       /* Allocate +1 for EVM and +1 as terminator. */
> > > > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > > > +                            GFP_NOFS);
> > > > +       if (!new_xattrs)
> > > > +               return -ENOMEM;
> > > > +
> > > > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > > > +                            list) {
> > > > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > > > +               if (ret && ret != -EOPNOTSUPP)
> > > > +                       goto out;
> > > > +               /*
> > > > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > > > +                * means that the LSM is not willing to provide an xattr, not
> > > > +                * that it wants to signal an error. Thus, continue to invoke
> > > > +                * the remaining LSMs.
> > > > +                */
> > > > +               if (ret == -EOPNOTSUPP)
> > > > +                       continue;
> > > > +               /*
> > > > +                * As the number of xattrs reserved by LSMs is not directly
> > > > +                * available, directly use the total number blob_sizes.lbs_xattr
> > > > +                * to keep the code simple, while being not the most efficient
> > > > +                * way.
> > > > +                */
> > >
> > > Is there a good reason why the LSM can't return the number of xattrs
> > > it is adding to the xattr array?  It seems like it should be fairly
> > > trivial for the individual LSMs to determine and it could save a lot
> > > of work.  However, given we're at v8 on this patchset I'm sure I'm
> > > missing something obvious, can you help me understand why the idea
> > > above is crazy stupid? ;)
>
> Much simple answer. Yes, LSMs could return the number of xattrs set,
> but security_check_compact_filled_xattrs() also needs to know from
> which offset (the lbs_xattr of each LSM) it should start compacting.
>
> Example: suppose that you have three LSMs with:
>
> LSM#1: lbs_xattr 1
> LSM#2: lbs_xattr 2 (disabled)
> LSM#3: lbs_xattr 1
>
> The current compaction interval is: already compacted xattrs - end of
> new_xattr array.
>
> When the security_inode_init_security() loop calls LSM#3, the
> compaction interval is: 1 - 2 (LSM#2 returns 0), which clearly isn't
> right. The correct compaction interval should be: 3 - 4.
>
> Going to the end of new_xattrs is an approximation, but it ensures
> that security_check_compact_filled_xattrs() reaches the xattr set by
> LSM#3.
>
> The alternative I was mentioning of passing num_filled_xattrs to LSMs
> goes again in the direction of doing on-the-fly compaction, while LSMs
> are more familiar with using the lbs_* fields.

I guess I was thinking of the case where the LSM layer, i.e.
security_inode_init_security(), allocates an xattr array like it does
now based on the maximum number of xattrs possible using the
lsm_blob_sizes values and passes a pointer to the individual LSMs
which is incremented based on how many xattrs are created by the
individual LSMs.  Here is some *very* rough pseudo code:

int security_inode_init_security(...)
{

  /* allocate an xattr array */
  xattrs = kcalloc(blob_sizes, sizeof(*xattrs), GFP_BLAH);

  /* loop on the lsms */
  xa_cnt = 0;
  while (lsm_hooks) {
    rc = call_hook(lsm_hook, &xattrs[xa_cnt]);
    if (rc > 0)
      xa_cnt += rc;
  }

  /* evm magic */
  evm_inode_init_security(...)
}

Does that work?  Am I missing something?

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24 21:39         ` Paul Moore
@ 2023-03-27  7:29           ` Roberto Sassu
  2023-03-27 21:02             ` Paul Moore
  0 siblings, 1 reply; 25+ messages in thread
From: Roberto Sassu @ 2023-03-27  7:29 UTC (permalink / raw)
  To: Paul Moore
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Fri, 2023-03-24 at 17:39 -0400, Paul Moore wrote:
> On Fri, Mar 24, 2023 at 9:26 AM Roberto Sassu
> <roberto.sassu@huaweicloud.com> wrote:
> > On Fri, 2023-03-24 at 11:18 +0100, Roberto Sassu wrote:
> > > On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > > > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > > > <roberto.sassu@huaweicloud.com> wrote:
> > > > > From: Roberto Sassu <roberto.sassu@huawei.com>
> > > > > 
> > > > > Currently, security_inode_init_security() supports only one LSM providing
> > > > > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > > > > metadata.
> > > > > 
> > > > > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > > > > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > > > > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > > > > needs, and the LSM infrastructure knows how many xattr slots it should
> > > > > allocate.
> > > > > 
> > > > > Dynamically allocate the xattrs array to be populated by LSMs with the
> > > > > inode_init_security hook, and pass it to the latter instead of the
> > > > > name/value/len triple. Update the documentation accordingly, and fix the
> > > > > description of the xattr name, as it is not allocated anymore.
> > > > > 
> > > > > Since the LSM infrastructure, at initialization time, updates the number of
> > > > > the requested xattrs provided by each LSM with a corresponding offset in
> > > > > the security blob (in this case the xattr array), it makes straightforward
> > > > > for an LSM to access the right position in the xattr array.
> > > > > 
> > > > > There is still the issue that an LSM might not fill the xattr, even if it
> > > > > requests it (legitimate case, for example it might have been loaded but not
> > > > > initialized with a policy). Since users of the xattr array (e.g. the
> > > > > initxattrs() callbacks) detect the end of the xattr array by checking if
> > > > > the xattr name is NULL, not filling an xattr would cause those users to
> > > > > stop scanning xattrs prematurely.
> > > > > 
> > > > > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > > > > which does a basic check of the xattr array (if the xattr name is filled,
> > > > > the xattr value should be too, and viceversa), and compacts the xattr array
> > > > > by removing the holes.
> > > > > 
> > > > > An alternative solution would be to let users of the xattr array know the
> > > > > number of elements of that array, so that they don't have to check the
> > > > > termination. However, this seems more invasive, compared to a simple move
> > > > > of few array elements.
> > > > > 
> > > > > security_check_compact_filled_xattrs() also determines how many xattrs in
> > > > > the xattr array have been filled. If there is none, skip
> > > > > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > > > > EVM to crash the kernel, as it is expecting a filled xattr.
> > > > > 
> > > > > Finally, adapt both SELinux and Smack to use the new definition of the
> > > > > inode_init_security hook, and to correctly fill the designated slots in the
> > > > > xattr array. For Smack, reserve space for the other defined xattrs although
> > > > > they are not set yet in smack_inode_init_security().
> > > > > 
> > > > > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > > > > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > > > > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > > > > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > > > > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > > > > ---
> > > > >  include/linux/lsm_hook_defs.h |   3 +-
> > > > >  include/linux/lsm_hooks.h     |   1 +
> > > > >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> > > > >  security/selinux/hooks.c      |  19 ++++--
> > > > >  security/smack/smack_lsm.c    |  33 ++++++----
> > > > >  5 files changed, 137 insertions(+), 38 deletions(-)
> 
> ...
> 
> > > > > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> > > > >                                  const struct qstr *qstr,
> > > > >                                  const initxattrs initxattrs, void *fs_data)
> > > > >  {
> > > > > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > > > > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > > > > -       int ret;
> > > > > +       struct security_hook_list *P;
> > > > > +       struct xattr *new_xattrs;
> > > > > +       struct xattr *xattr;
> > > > > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > > > > 
> > > > >         if (unlikely(IS_PRIVATE(inode)))
> > > > >                 return 0;
> > > > > 
> > > > > +       if (!blob_sizes.lbs_xattr)
> > > > > +               return 0;
> > > > > +
> > > > >         if (!initxattrs)
> > > > >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > > > > -                                    dir, qstr, NULL, NULL, NULL);
> > > > > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > > > > -       lsm_xattr = new_xattrs;
> > > > > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > > > > -                           &lsm_xattr->name,
> > > > > -                           &lsm_xattr->value,
> > > > > -                           &lsm_xattr->value_len);
> > > > > -       if (ret)
> > > > > +                                   dir, qstr, NULL);
> > > > > +       /* Allocate +1 for EVM and +1 as terminator. */
> > > > > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > > > > +                            GFP_NOFS);
> > > > > +       if (!new_xattrs)
> > > > > +               return -ENOMEM;
> > > > > +
> > > > > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > > > > +                            list) {
> > > > > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > > > > +               if (ret && ret != -EOPNOTSUPP)
> > > > > +                       goto out;
> > > > > +               /*
> > > > > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > > > > +                * means that the LSM is not willing to provide an xattr, not
> > > > > +                * that it wants to signal an error. Thus, continue to invoke
> > > > > +                * the remaining LSMs.
> > > > > +                */
> > > > > +               if (ret == -EOPNOTSUPP)
> > > > > +                       continue;
> > > > > +               /*
> > > > > +                * As the number of xattrs reserved by LSMs is not directly
> > > > > +                * available, directly use the total number blob_sizes.lbs_xattr
> > > > > +                * to keep the code simple, while being not the most efficient
> > > > > +                * way.
> > > > > +                */
> > > > 
> > > > Is there a good reason why the LSM can't return the number of xattrs
> > > > it is adding to the xattr array?  It seems like it should be fairly
> > > > trivial for the individual LSMs to determine and it could save a lot
> > > > of work.  However, given we're at v8 on this patchset I'm sure I'm
> > > > missing something obvious, can you help me understand why the idea
> > > > above is crazy stupid? ;)
> > 
> > Much simple answer. Yes, LSMs could return the number of xattrs set,
> > but security_check_compact_filled_xattrs() also needs to know from
> > which offset (the lbs_xattr of each LSM) it should start compacting.
> > 
> > Example: suppose that you have three LSMs with:
> > 
> > LSM#1: lbs_xattr 1
> > LSM#2: lbs_xattr 2 (disabled)
> > LSM#3: lbs_xattr 1
> > 
> > The current compaction interval is: already compacted xattrs - end of
> > new_xattr array.
> > 
> > When the security_inode_init_security() loop calls LSM#3, the
> > compaction interval is: 1 - 2 (LSM#2 returns 0), which clearly isn't
> > right. The correct compaction interval should be: 3 - 4.
> > 
> > Going to the end of new_xattrs is an approximation, but it ensures
> > that security_check_compact_filled_xattrs() reaches the xattr set by
> > LSM#3.
> > 
> > The alternative I was mentioning of passing num_filled_xattrs to LSMs
> > goes again in the direction of doing on-the-fly compaction, while LSMs
> > are more familiar with using the lbs_* fields.
> 
> I guess I was thinking of the case where the LSM layer, i.e.
> security_inode_init_security(), allocates an xattr array like it does
> now based on the maximum number of xattrs possible using the
> lsm_blob_sizes values and passes a pointer to the individual LSMs
> which is incremented based on how many xattrs are created by the
> individual LSMs.  Here is some *very* rough pseudo code:
> 
> int security_inode_init_security(...)
> {
> 
>   /* allocate an xattr array */
>   xattrs = kcalloc(blob_sizes, sizeof(*xattrs), GFP_BLAH);
> 
>   /* loop on the lsms */
>   xa_cnt = 0;
>   while (lsm_hooks) {
>     rc = call_hook(lsm_hook, &xattrs[xa_cnt]);
>     if (rc > 0)
>       xa_cnt += rc;
>   }
> 
>   /* evm magic */
>   evm_inode_init_security(...)
> }
> 
> Does that work?  Am I missing something?

Oh, unfortunately not. EVM needs to see all xattrs (when it is moved to
the LSM infrastructure).

Thanks

Roberto


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-24 21:19       ` Paul Moore
@ 2023-03-27  7:35         ` Roberto Sassu
  0 siblings, 0 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-27  7:35 UTC (permalink / raw)
  To: Paul Moore
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Fri, 2023-03-24 at 17:19 -0400, Paul Moore wrote:
> On Fri, Mar 24, 2023 at 6:18 AM Roberto Sassu
> <roberto.sassu@huaweicloud.com> wrote:
> > On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > > <roberto.sassu@huaweicloud.com> wrote:
> > > > From: Roberto Sassu <roberto.sassu@huawei.com>
> > > > 
> > > > Currently, security_inode_init_security() supports only one LSM providing
> > > > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > > > metadata.
> > > > 
> > > > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > > > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > > > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > > > needs, and the LSM infrastructure knows how many xattr slots it should
> > > > allocate.
> > > > 
> > > > Dynamically allocate the xattrs array to be populated by LSMs with the
> > > > inode_init_security hook, and pass it to the latter instead of the
> > > > name/value/len triple. Update the documentation accordingly, and fix the
> > > > description of the xattr name, as it is not allocated anymore.
> > > > 
> > > > Since the LSM infrastructure, at initialization time, updates the number of
> > > > the requested xattrs provided by each LSM with a corresponding offset in
> > > > the security blob (in this case the xattr array), it makes straightforward
> > > > for an LSM to access the right position in the xattr array.
> > > > 
> > > > There is still the issue that an LSM might not fill the xattr, even if it
> > > > requests it (legitimate case, for example it might have been loaded but not
> > > > initialized with a policy). Since users of the xattr array (e.g. the
> > > > initxattrs() callbacks) detect the end of the xattr array by checking if
> > > > the xattr name is NULL, not filling an xattr would cause those users to
> > > > stop scanning xattrs prematurely.
> > > > 
> > > > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > > > which does a basic check of the xattr array (if the xattr name is filled,
> > > > the xattr value should be too, and viceversa), and compacts the xattr array
> > > > by removing the holes.
> > > > 
> > > > An alternative solution would be to let users of the xattr array know the
> > > > number of elements of that array, so that they don't have to check the
> > > > termination. However, this seems more invasive, compared to a simple move
> > > > of few array elements.
> > > > 
> > > > security_check_compact_filled_xattrs() also determines how many xattrs in
> > > > the xattr array have been filled. If there is none, skip
> > > > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > > > EVM to crash the kernel, as it is expecting a filled xattr.
> > > > 
> > > > Finally, adapt both SELinux and Smack to use the new definition of the
> > > > inode_init_security hook, and to correctly fill the designated slots in the
> > > > xattr array. For Smack, reserve space for the other defined xattrs although
> > > > they are not set yet in smack_inode_init_security().
> > > > 
> > > > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > > > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > > > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > > > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > > > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > > > ---
> > > >  include/linux/lsm_hook_defs.h |   3 +-
> > > >  include/linux/lsm_hooks.h     |   1 +
> > > >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> > > >  security/selinux/hooks.c      |  19 ++++--
> > > >  security/smack/smack_lsm.c    |  33 ++++++----
> > > >  5 files changed, 137 insertions(+), 38 deletions(-)
> 
> ...
> 
> > > > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> > > >                                  const struct qstr *qstr,
> > > >                                  const initxattrs initxattrs, void *fs_data)
> > > >  {
> > > > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > > > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > > > -       int ret;
> > > > +       struct security_hook_list *P;
> > > > +       struct xattr *new_xattrs;
> > > > +       struct xattr *xattr;
> > > > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > > > 
> > > >         if (unlikely(IS_PRIVATE(inode)))
> > > >                 return 0;
> > > > 
> > > > +       if (!blob_sizes.lbs_xattr)
> > > > +               return 0;
> > > > +
> > > >         if (!initxattrs)
> > > >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > > > -                                    dir, qstr, NULL, NULL, NULL);
> > > > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > > > -       lsm_xattr = new_xattrs;
> > > > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > > > -                           &lsm_xattr->name,
> > > > -                           &lsm_xattr->value,
> > > > -                           &lsm_xattr->value_len);
> > > > -       if (ret)
> > > > +                                   dir, qstr, NULL);
> > > > +       /* Allocate +1 for EVM and +1 as terminator. */
> > > > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > > > +                            GFP_NOFS);
> > > > +       if (!new_xattrs)
> > > > +               return -ENOMEM;
> > > > +
> > > > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > > > +                            list) {
> > > > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > > > +               if (ret && ret != -EOPNOTSUPP)
> > > > +                       goto out;
> > > > +               /*
> > > > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > > > +                * means that the LSM is not willing to provide an xattr, not
> > > > +                * that it wants to signal an error. Thus, continue to invoke
> > > > +                * the remaining LSMs.
> > > > +                */
> > > > +               if (ret == -EOPNOTSUPP)
> > > > +                       continue;
> > > > +               /*
> > > > +                * As the number of xattrs reserved by LSMs is not directly
> > > > +                * available, directly use the total number blob_sizes.lbs_xattr
> > > > +                * to keep the code simple, while being not the most efficient
> > > > +                * way.
> > > > +                */
> > > 
> > > Is there a good reason why the LSM can't return the number of xattrs
> > > it is adding to the xattr array?  It seems like it should be fairly
> > > trivial for the individual LSMs to determine and it could save a lot
> > > of work.  However, given we're at v8 on this patchset I'm sure I'm
> > > missing something obvious, can you help me understand why the idea
> > > above is crazy stupid? ;)
> > 
> > Ok, I looked back at what I did for v3.
> > 
> > Moving from v3 to v4, I decided to put less burden on LSMs, and to make
> > all the processing from the LSM infrastructure side.
> 
> As a general rule I think it's a good goal to keep the LSM layer as
> small as possible; I believe it allows us to be more flexible with the
> LSMs and it keeps the LSM as simple as possible.  I mean less code,
> less bugs, amirite? ... ;)
> 
> > v3 had some safeguards to prevent some programming mistakes by LSMs,
> > which maybe made the code less understandable.
> > 
> > However, if we say we keep things as simple as possible and assume that
> > LSMs implement this correctly, we can just pass num_filled_xattrs to
> > them and they simply increment it.
> > 
> > The EVM bug should not arise (accessing xattr->name = NULL), even if
> > BPF LSM alone returns zero, due to the check of num_filled_xattrs
> > before calling evm_inode_init_security().
> > 
> > Patch 6 (at the end) will prevent the bug from arising when EVM is
> > moved to the LSM infrastructure (no num_filled_xattrs check anymore).
> > There is a loop that stops if xattr->name is NULL, so
> > evm_protected_xattr() will not be called.
> > 
> > Or, like you suggested, we just return a positive value from LSMs and
> > we keep num_filled_xattrs in security_inode_init_security().
> 
> I like the idea of individual LSMs simply reporting the number of
> xattrs they've generated instead of incrementing the num_filled_xattrs
> variable.
> 
> It seems like returning the xattr count as a positive return value
> should work just fine, leaving negative values for errors, but if you
> run into problems you can always pass the value back in a new
> parameter pointer if needed.
> 
> > > > @@ -2868,11 +2870,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
> > > > 
> > > >  static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> > > >                                        const struct qstr *qstr,
> > > > -                                      const char **name,
> > > > -                                      void **value, size_t *len)
> > > > +                                      struct xattr *xattrs)
> > > >  {
> > > >         const struct task_security_struct *tsec = selinux_cred(current_cred());
> > > >         struct superblock_security_struct *sbsec;
> > > > +       struct xattr *xattr = NULL;
> > > >         u32 newsid, clen;
> > > >         int rc;
> > > >         char *context;
> > > > @@ -2899,16 +2901,18 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
> > > >             !(sbsec->flags & SBLABEL_MNT))
> > > >                 return -EOPNOTSUPP;
> > > > 
> > > > -       if (name)
> > > > -               *name = XATTR_SELINUX_SUFFIX;
> > > > +       if (xattrs)
> > > > +               xattr = xattrs + selinux_blob_sizes.lbs_xattr;
> > > 
> > > Please abstract that away to an inline function similar to
> > > selinux_cred(), selinux_file(), selinux_inode(), etc.
> > 
> > Ok.
> > 
> > > > +       if (xattr) {
> > > > +               xattr->name = XATTR_SELINUX_SUFFIX;
> > > 
> > > I'm guessing the xattr->name assignment is always done, regardless of
> > > if security_sid_to_context_force() is successful, due to the -EINVAL
> > > check in security_check_compact_filled_xattrs()?  If yes, it would be
> > > good to make note of that here in the code.  If not, it would be nice
> > > to move this down the function to go with the other xattr->XXX
> > > assignments, unless there is another reason for its placement that I'm
> > > missing.
> > 
> > Uhm, if an LSM returns an error, security_inode_init_security() stops
> > and does the cleanup. It should not matter if xattr->name was set.
> 
> Okay, I thought I might be missing something during the review.  Since
> there is no special reason for putting the xattr->name assignment up
> there, please move it down below with the other xattr->XXX
> assignments.

Ok, will do.

Thanks

Roberto


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-27  7:29           ` Roberto Sassu
@ 2023-03-27 21:02             ` Paul Moore
  2023-03-28  7:46               ` Roberto Sassu
  0 siblings, 1 reply; 25+ messages in thread
From: Paul Moore @ 2023-03-27 21:02 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Mon, Mar 27, 2023 at 3:30 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
> On Fri, 2023-03-24 at 17:39 -0400, Paul Moore wrote:
> > On Fri, Mar 24, 2023 at 9:26 AM Roberto Sassu
> > <roberto.sassu@huaweicloud.com> wrote:
> > > On Fri, 2023-03-24 at 11:18 +0100, Roberto Sassu wrote:
> > > > On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > > > > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > > > > <roberto.sassu@huaweicloud.com> wrote:
> > > > > > From: Roberto Sassu <roberto.sassu@huawei.com>
> > > > > >
> > > > > > Currently, security_inode_init_security() supports only one LSM providing
> > > > > > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > > > > > metadata.
> > > > > >
> > > > > > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > > > > > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > > > > > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > > > > > needs, and the LSM infrastructure knows how many xattr slots it should
> > > > > > allocate.
> > > > > >
> > > > > > Dynamically allocate the xattrs array to be populated by LSMs with the
> > > > > > inode_init_security hook, and pass it to the latter instead of the
> > > > > > name/value/len triple. Update the documentation accordingly, and fix the
> > > > > > description of the xattr name, as it is not allocated anymore.
> > > > > >
> > > > > > Since the LSM infrastructure, at initialization time, updates the number of
> > > > > > the requested xattrs provided by each LSM with a corresponding offset in
> > > > > > the security blob (in this case the xattr array), it makes straightforward
> > > > > > for an LSM to access the right position in the xattr array.
> > > > > >
> > > > > > There is still the issue that an LSM might not fill the xattr, even if it
> > > > > > requests it (legitimate case, for example it might have been loaded but not
> > > > > > initialized with a policy). Since users of the xattr array (e.g. the
> > > > > > initxattrs() callbacks) detect the end of the xattr array by checking if
> > > > > > the xattr name is NULL, not filling an xattr would cause those users to
> > > > > > stop scanning xattrs prematurely.
> > > > > >
> > > > > > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > > > > > which does a basic check of the xattr array (if the xattr name is filled,
> > > > > > the xattr value should be too, and viceversa), and compacts the xattr array
> > > > > > by removing the holes.
> > > > > >
> > > > > > An alternative solution would be to let users of the xattr array know the
> > > > > > number of elements of that array, so that they don't have to check the
> > > > > > termination. However, this seems more invasive, compared to a simple move
> > > > > > of few array elements.
> > > > > >
> > > > > > security_check_compact_filled_xattrs() also determines how many xattrs in
> > > > > > the xattr array have been filled. If there is none, skip
> > > > > > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > > > > > EVM to crash the kernel, as it is expecting a filled xattr.
> > > > > >
> > > > > > Finally, adapt both SELinux and Smack to use the new definition of the
> > > > > > inode_init_security hook, and to correctly fill the designated slots in the
> > > > > > xattr array. For Smack, reserve space for the other defined xattrs although
> > > > > > they are not set yet in smack_inode_init_security().
> > > > > >
> > > > > > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > > > > > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > > > > > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > > > > > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > > > > > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > > > > > ---
> > > > > >  include/linux/lsm_hook_defs.h |   3 +-
> > > > > >  include/linux/lsm_hooks.h     |   1 +
> > > > > >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> > > > > >  security/selinux/hooks.c      |  19 ++++--
> > > > > >  security/smack/smack_lsm.c    |  33 ++++++----
> > > > > >  5 files changed, 137 insertions(+), 38 deletions(-)
> >
> > ...
> >
> > > > > > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> > > > > >                                  const struct qstr *qstr,
> > > > > >                                  const initxattrs initxattrs, void *fs_data)
> > > > > >  {
> > > > > > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > > > > > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > > > > > -       int ret;
> > > > > > +       struct security_hook_list *P;
> > > > > > +       struct xattr *new_xattrs;
> > > > > > +       struct xattr *xattr;
> > > > > > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > > > > >
> > > > > >         if (unlikely(IS_PRIVATE(inode)))
> > > > > >                 return 0;
> > > > > >
> > > > > > +       if (!blob_sizes.lbs_xattr)
> > > > > > +               return 0;
> > > > > > +
> > > > > >         if (!initxattrs)
> > > > > >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > > > > > -                                    dir, qstr, NULL, NULL, NULL);
> > > > > > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > > > > > -       lsm_xattr = new_xattrs;
> > > > > > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > > > > > -                           &lsm_xattr->name,
> > > > > > -                           &lsm_xattr->value,
> > > > > > -                           &lsm_xattr->value_len);
> > > > > > -       if (ret)
> > > > > > +                                   dir, qstr, NULL);
> > > > > > +       /* Allocate +1 for EVM and +1 as terminator. */
> > > > > > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > > > > > +                            GFP_NOFS);
> > > > > > +       if (!new_xattrs)
> > > > > > +               return -ENOMEM;
> > > > > > +
> > > > > > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > > > > > +                            list) {
> > > > > > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > > > > > +               if (ret && ret != -EOPNOTSUPP)
> > > > > > +                       goto out;
> > > > > > +               /*
> > > > > > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > > > > > +                * means that the LSM is not willing to provide an xattr, not
> > > > > > +                * that it wants to signal an error. Thus, continue to invoke
> > > > > > +                * the remaining LSMs.
> > > > > > +                */
> > > > > > +               if (ret == -EOPNOTSUPP)
> > > > > > +                       continue;
> > > > > > +               /*
> > > > > > +                * As the number of xattrs reserved by LSMs is not directly
> > > > > > +                * available, directly use the total number blob_sizes.lbs_xattr
> > > > > > +                * to keep the code simple, while being not the most efficient
> > > > > > +                * way.
> > > > > > +                */
> > > > >
> > > > > Is there a good reason why the LSM can't return the number of xattrs
> > > > > it is adding to the xattr array?  It seems like it should be fairly
> > > > > trivial for the individual LSMs to determine and it could save a lot
> > > > > of work.  However, given we're at v8 on this patchset I'm sure I'm
> > > > > missing something obvious, can you help me understand why the idea
> > > > > above is crazy stupid? ;)
> > >
> > > Much simple answer. Yes, LSMs could return the number of xattrs set,
> > > but security_check_compact_filled_xattrs() also needs to know from
> > > which offset (the lbs_xattr of each LSM) it should start compacting.
> > >
> > > Example: suppose that you have three LSMs with:
> > >
> > > LSM#1: lbs_xattr 1
> > > LSM#2: lbs_xattr 2 (disabled)
> > > LSM#3: lbs_xattr 1
> > >
> > > The current compaction interval is: already compacted xattrs - end of
> > > new_xattr array.
> > >
> > > When the security_inode_init_security() loop calls LSM#3, the
> > > compaction interval is: 1 - 2 (LSM#2 returns 0), which clearly isn't
> > > right. The correct compaction interval should be: 3 - 4.
> > >
> > > Going to the end of new_xattrs is an approximation, but it ensures
> > > that security_check_compact_filled_xattrs() reaches the xattr set by
> > > LSM#3.
> > >
> > > The alternative I was mentioning of passing num_filled_xattrs to LSMs
> > > goes again in the direction of doing on-the-fly compaction, while LSMs
> > > are more familiar with using the lbs_* fields.
> >
> > I guess I was thinking of the case where the LSM layer, i.e.
> > security_inode_init_security(), allocates an xattr array like it does
> > now based on the maximum number of xattrs possible using the
> > lsm_blob_sizes values and passes a pointer to the individual LSMs
> > which is incremented based on how many xattrs are created by the
> > individual LSMs.  Here is some *very* rough pseudo code:
> >
> > int security_inode_init_security(...)
> > {
> >
> >   /* allocate an xattr array */
> >   xattrs = kcalloc(blob_sizes, sizeof(*xattrs), GFP_BLAH);
> >
> >   /* loop on the lsms */
> >   xa_cnt = 0;
> >   while (lsm_hooks) {
> >     rc = call_hook(lsm_hook, &xattrs[xa_cnt]);
> >     if (rc > 0)
> >       xa_cnt += rc;
> >   }
> >
> >   /* evm magic */
> >   evm_inode_init_security(...)
> > }
> >
> > Does that work?  Am I missing something?
>
> Oh, unfortunately not. EVM needs to see all xattrs (when it is moved to
> the LSM infrastructure).

Okay, that's fair, but we could still pass the full xattrs array and a
reference to the current count which could be both read and updated by
the individual LSMs, right?

The issue is that the separate compaction stage is not something we
want to have to do if we can avoid it.  Maybe we're stuck with it, but
I'm not yet convinced that we can't make some minor changes to the
LSMs to avoid the compaction step.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-27 21:02             ` Paul Moore
@ 2023-03-28  7:46               ` Roberto Sassu
  2023-03-28 20:19                 ` Paul Moore
  0 siblings, 1 reply; 25+ messages in thread
From: Roberto Sassu @ 2023-03-28  7:46 UTC (permalink / raw)
  To: Paul Moore
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Mon, 2023-03-27 at 17:02 -0400, Paul Moore wrote:
> On Mon, Mar 27, 2023 at 3:30 AM Roberto Sassu
> <roberto.sassu@huaweicloud.com> wrote:
> > On Fri, 2023-03-24 at 17:39 -0400, Paul Moore wrote:
> > > On Fri, Mar 24, 2023 at 9:26 AM Roberto Sassu
> > > <roberto.sassu@huaweicloud.com> wrote:
> > > > On Fri, 2023-03-24 at 11:18 +0100, Roberto Sassu wrote:
> > > > > On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > > > > > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > > > > > <roberto.sassu@huaweicloud.com> wrote:
> > > > > > > From: Roberto Sassu <roberto.sassu@huawei.com>
> > > > > > > 
> > > > > > > Currently, security_inode_init_security() supports only one LSM providing
> > > > > > > an xattr and EVM calculating the HMAC on that xattr, plus other inode
> > > > > > > metadata.
> > > > > > > 
> > > > > > > Allow all LSMs to provide one or multiple xattrs, by extending the security
> > > > > > > blob reservation mechanism. Introduce the new lbs_xattr field of the
> > > > > > > lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
> > > > > > > needs, and the LSM infrastructure knows how many xattr slots it should
> > > > > > > allocate.
> > > > > > > 
> > > > > > > Dynamically allocate the xattrs array to be populated by LSMs with the
> > > > > > > inode_init_security hook, and pass it to the latter instead of the
> > > > > > > name/value/len triple. Update the documentation accordingly, and fix the
> > > > > > > description of the xattr name, as it is not allocated anymore.
> > > > > > > 
> > > > > > > Since the LSM infrastructure, at initialization time, updates the number of
> > > > > > > the requested xattrs provided by each LSM with a corresponding offset in
> > > > > > > the security blob (in this case the xattr array), it makes straightforward
> > > > > > > for an LSM to access the right position in the xattr array.
> > > > > > > 
> > > > > > > There is still the issue that an LSM might not fill the xattr, even if it
> > > > > > > requests it (legitimate case, for example it might have been loaded but not
> > > > > > > initialized with a policy). Since users of the xattr array (e.g. the
> > > > > > > initxattrs() callbacks) detect the end of the xattr array by checking if
> > > > > > > the xattr name is NULL, not filling an xattr would cause those users to
> > > > > > > stop scanning xattrs prematurely.
> > > > > > > 
> > > > > > > Solve that issue by introducing security_check_compact_filled_xattrs(),
> > > > > > > which does a basic check of the xattr array (if the xattr name is filled,
> > > > > > > the xattr value should be too, and viceversa), and compacts the xattr array
> > > > > > > by removing the holes.
> > > > > > > 
> > > > > > > An alternative solution would be to let users of the xattr array know the
> > > > > > > number of elements of that array, so that they don't have to check the
> > > > > > > termination. However, this seems more invasive, compared to a simple move
> > > > > > > of few array elements.
> > > > > > > 
> > > > > > > security_check_compact_filled_xattrs() also determines how many xattrs in
> > > > > > > the xattr array have been filled. If there is none, skip
> > > > > > > evm_inode_init_security() and initxattrs(). Skipping the former also avoids
> > > > > > > EVM to crash the kernel, as it is expecting a filled xattr.
> > > > > > > 
> > > > > > > Finally, adapt both SELinux and Smack to use the new definition of the
> > > > > > > inode_init_security hook, and to correctly fill the designated slots in the
> > > > > > > xattr array. For Smack, reserve space for the other defined xattrs although
> > > > > > > they are not set yet in smack_inode_init_security().
> > > > > > > 
> > > > > > > Reported-by: Nicolas Bouchinet <nicolas.bouchinet@clip-os.org> (EVM crash)
> > > > > > > Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/
> > > > > > > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > > > > > > Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
> > > > > > > Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
> > > > > > > ---
> > > > > > >  include/linux/lsm_hook_defs.h |   3 +-
> > > > > > >  include/linux/lsm_hooks.h     |   1 +
> > > > > > >  security/security.c           | 119 +++++++++++++++++++++++++++++-----
> > > > > > >  security/selinux/hooks.c      |  19 ++++--
> > > > > > >  security/smack/smack_lsm.c    |  33 ++++++----
> > > > > > >  5 files changed, 137 insertions(+), 38 deletions(-)
> > > 
> > > ...
> > > 
> > > > > > > @@ -1604,33 +1654,66 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
> > > > > > >                                  const struct qstr *qstr,
> > > > > > >                                  const initxattrs initxattrs, void *fs_data)
> > > > > > >  {
> > > > > > > -       struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
> > > > > > > -       struct xattr *lsm_xattr, *evm_xattr, *xattr;
> > > > > > > -       int ret;
> > > > > > > +       struct security_hook_list *P;
> > > > > > > +       struct xattr *new_xattrs;
> > > > > > > +       struct xattr *xattr;
> > > > > > > +       int ret = -EOPNOTSUPP, num_filled_xattrs = 0;
> > > > > > > 
> > > > > > >         if (unlikely(IS_PRIVATE(inode)))
> > > > > > >                 return 0;
> > > > > > > 
> > > > > > > +       if (!blob_sizes.lbs_xattr)
> > > > > > > +               return 0;
> > > > > > > +
> > > > > > >         if (!initxattrs)
> > > > > > >                 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
> > > > > > > -                                    dir, qstr, NULL, NULL, NULL);
> > > > > > > -       memset(new_xattrs, 0, sizeof(new_xattrs));
> > > > > > > -       lsm_xattr = new_xattrs;
> > > > > > > -       ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
> > > > > > > -                           &lsm_xattr->name,
> > > > > > > -                           &lsm_xattr->value,
> > > > > > > -                           &lsm_xattr->value_len);
> > > > > > > -       if (ret)
> > > > > > > +                                   dir, qstr, NULL);
> > > > > > > +       /* Allocate +1 for EVM and +1 as terminator. */
> > > > > > > +       new_xattrs = kcalloc(blob_sizes.lbs_xattr + 2, sizeof(*new_xattrs),
> > > > > > > +                            GFP_NOFS);
> > > > > > > +       if (!new_xattrs)
> > > > > > > +               return -ENOMEM;
> > > > > > > +
> > > > > > > +       hlist_for_each_entry(P, &security_hook_heads.inode_init_security,
> > > > > > > +                            list) {
> > > > > > > +               ret = P->hook.inode_init_security(inode, dir, qstr, new_xattrs);
> > > > > > > +               if (ret && ret != -EOPNOTSUPP)
> > > > > > > +                       goto out;
> > > > > > > +               /*
> > > > > > > +                * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
> > > > > > > +                * means that the LSM is not willing to provide an xattr, not
> > > > > > > +                * that it wants to signal an error. Thus, continue to invoke
> > > > > > > +                * the remaining LSMs.
> > > > > > > +                */
> > > > > > > +               if (ret == -EOPNOTSUPP)
> > > > > > > +                       continue;
> > > > > > > +               /*
> > > > > > > +                * As the number of xattrs reserved by LSMs is not directly
> > > > > > > +                * available, directly use the total number blob_sizes.lbs_xattr
> > > > > > > +                * to keep the code simple, while being not the most efficient
> > > > > > > +                * way.
> > > > > > > +                */
> > > > > > 
> > > > > > Is there a good reason why the LSM can't return the number of xattrs
> > > > > > it is adding to the xattr array?  It seems like it should be fairly
> > > > > > trivial for the individual LSMs to determine and it could save a lot
> > > > > > of work.  However, given we're at v8 on this patchset I'm sure I'm
> > > > > > missing something obvious, can you help me understand why the idea
> > > > > > above is crazy stupid? ;)
> > > > 
> > > > Much simple answer. Yes, LSMs could return the number of xattrs set,
> > > > but security_check_compact_filled_xattrs() also needs to know from
> > > > which offset (the lbs_xattr of each LSM) it should start compacting.
> > > > 
> > > > Example: suppose that you have three LSMs with:
> > > > 
> > > > LSM#1: lbs_xattr 1
> > > > LSM#2: lbs_xattr 2 (disabled)
> > > > LSM#3: lbs_xattr 1
> > > > 
> > > > The current compaction interval is: already compacted xattrs - end of
> > > > new_xattr array.
> > > > 
> > > > When the security_inode_init_security() loop calls LSM#3, the
> > > > compaction interval is: 1 - 2 (LSM#2 returns 0), which clearly isn't
> > > > right. The correct compaction interval should be: 3 - 4.
> > > > 
> > > > Going to the end of new_xattrs is an approximation, but it ensures
> > > > that security_check_compact_filled_xattrs() reaches the xattr set by
> > > > LSM#3.
> > > > 
> > > > The alternative I was mentioning of passing num_filled_xattrs to LSMs
> > > > goes again in the direction of doing on-the-fly compaction, while LSMs
> > > > are more familiar with using the lbs_* fields.
> > > 
> > > I guess I was thinking of the case where the LSM layer, i.e.
> > > security_inode_init_security(), allocates an xattr array like it does
> > > now based on the maximum number of xattrs possible using the
> > > lsm_blob_sizes values and passes a pointer to the individual LSMs
> > > which is incremented based on how many xattrs are created by the
> > > individual LSMs.  Here is some *very* rough pseudo code:
> > > 
> > > int security_inode_init_security(...)
> > > {
> > > 
> > >   /* allocate an xattr array */
> > >   xattrs = kcalloc(blob_sizes, sizeof(*xattrs), GFP_BLAH);
> > > 
> > >   /* loop on the lsms */
> > >   xa_cnt = 0;
> > >   while (lsm_hooks) {
> > >     rc = call_hook(lsm_hook, &xattrs[xa_cnt]);
> > >     if (rc > 0)
> > >       xa_cnt += rc;
> > >   }
> > > 
> > >   /* evm magic */
> > >   evm_inode_init_security(...)
> > > }
> > > 
> > > Does that work?  Am I missing something?
> > 
> > Oh, unfortunately not. EVM needs to see all xattrs (when it is moved to
> > the LSM infrastructure).
> 
> Okay, that's fair, but we could still pass the full xattrs array and a
> reference to the current count which could be both read and updated by
> the individual LSMs, right?

Yes, we could do.

> The issue is that the separate compaction stage is not something we
> want to have to do if we can avoid it.  Maybe we're stuck with it, but
> I'm not yet convinced that we can't make some minor changes to the
> LSMs to avoid the compaction step.

I liked more the idea that LSMs do what they are most familiar with,
get an offset in a security blob or, in this case, a starting slot in
the new_xattrs array, and write there.

v3 had the lsm_find_xattr_slot() helper, to get the starting slot, but
somehow I find it less intuitive.

Ok, if you prefer to avoid the compaction stage, I will rewrite this
patch.

Thanks

Roberto


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-28  7:46               ` Roberto Sassu
@ 2023-03-28 20:19                 ` Paul Moore
  2023-03-29  7:11                   ` Roberto Sassu
  0 siblings, 1 reply; 25+ messages in thread
From: Paul Moore @ 2023-03-28 20:19 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Tue, Mar 28, 2023 at 3:47 AM Roberto Sassu
<roberto.sassu@huaweicloud.com> wrote:
>
> On Mon, 2023-03-27 at 17:02 -0400, Paul Moore wrote:
> > On Mon, Mar 27, 2023 at 3:30 AM Roberto Sassu
> > <roberto.sassu@huaweicloud.com> wrote:
> > > On Fri, 2023-03-24 at 17:39 -0400, Paul Moore wrote:
> > > > On Fri, Mar 24, 2023 at 9:26 AM Roberto Sassu
> > > > <roberto.sassu@huaweicloud.com> wrote:
> > > > > On Fri, 2023-03-24 at 11:18 +0100, Roberto Sassu wrote:
> > > > > > On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > > > > > > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > > > > > > <roberto.sassu@huaweicloud.com> wrote:
> > > > > > > > From: Roberto Sassu <roberto.sassu@huawei.com>

...

> > Okay, that's fair, but we could still pass the full xattrs array and a
> > reference to the current count which could be both read and updated by
> > the individual LSMs, right?
>
> Yes, we could do.
>
> > The issue is that the separate compaction stage is not something we
> > want to have to do if we can avoid it.  Maybe we're stuck with it, but
> > I'm not yet convinced that we can't make some minor changes to the
> > LSMs to avoid the compaction step.
>
> I liked more the idea that LSMs do what they are most familiar with,
> get an offset in a security blob or, in this case, a starting slot in
> the new_xattrs array, and write there.
>
> v3 had the lsm_find_xattr_slot() helper, to get the starting slot, but
> somehow I find it less intuitive.
>
> Ok, if you prefer to avoid the compaction stage, I will rewrite this
> patch.

My concern is having to look through the xattr array after each LSM
has been run and in at least one case having to then do a memcpy() to
keep the array packed.  There are some cases where there is no way to
avoid all that extra work, but here I think we have the LSMs do the
Right Thing with respect to packing the xattr array without overly
burdening the individual LSMs.

Does that make sense?  It basically comes down to being smart about
our abstractions and both selectively, and carefully, breaking them
when there is a reasonable performance gain to be had.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook
  2023-03-28 20:19                 ` Paul Moore
@ 2023-03-29  7:11                   ` Roberto Sassu
  0 siblings, 0 replies; 25+ messages in thread
From: Roberto Sassu @ 2023-03-29  7:11 UTC (permalink / raw)
  To: Paul Moore
  Cc: mark, jlbec, joseph.qi, zohar, dmitry.kasatkin, jmorris, serge,
	stephen.smalley.work, eparis, casey, ocfs2-devel, reiserfs-devel,
	linux-integrity, linux-security-module, selinux, linux-kernel,
	keescook, nicolas.bouchinet, Roberto Sassu

On Tue, 2023-03-28 at 16:19 -0400, Paul Moore wrote:
> On Tue, Mar 28, 2023 at 3:47 AM Roberto Sassu
> <roberto.sassu@huaweicloud.com> wrote:
> > On Mon, 2023-03-27 at 17:02 -0400, Paul Moore wrote:
> > > On Mon, Mar 27, 2023 at 3:30 AM Roberto Sassu
> > > <roberto.sassu@huaweicloud.com> wrote:
> > > > On Fri, 2023-03-24 at 17:39 -0400, Paul Moore wrote:
> > > > > On Fri, Mar 24, 2023 at 9:26 AM Roberto Sassu
> > > > > <roberto.sassu@huaweicloud.com> wrote:
> > > > > > On Fri, 2023-03-24 at 11:18 +0100, Roberto Sassu wrote:
> > > > > > > On Thu, 2023-03-23 at 20:09 -0400, Paul Moore wrote:
> > > > > > > > On Tue, Mar 14, 2023 at 4:19 AM Roberto Sassu
> > > > > > > > <roberto.sassu@huaweicloud.com> wrote:
> > > > > > > > > From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> ...
> 
> > > Okay, that's fair, but we could still pass the full xattrs array and a
> > > reference to the current count which could be both read and updated by
> > > the individual LSMs, right?
> > 
> > Yes, we could do.
> > 
> > > The issue is that the separate compaction stage is not something we
> > > want to have to do if we can avoid it.  Maybe we're stuck with it, but
> > > I'm not yet convinced that we can't make some minor changes to the
> > > LSMs to avoid the compaction step.
> > 
> > I liked more the idea that LSMs do what they are most familiar with,
> > get an offset in a security blob or, in this case, a starting slot in
> > the new_xattrs array, and write there.
> > 
> > v3 had the lsm_find_xattr_slot() helper, to get the starting slot, but
> > somehow I find it less intuitive.
> > 
> > Ok, if you prefer to avoid the compaction stage, I will rewrite this
> > patch.
> 
> My concern is having to look through the xattr array after each LSM
> has been run and in at least one case having to then do a memcpy() to
> keep the array packed.  There are some cases where there is no way to
> avoid all that extra work, but here I think we have the LSMs do the
> Right Thing with respect to packing the xattr array without overly
> burdening the individual LSMs.
> 
> Does that make sense?  It basically comes down to being smart about
> our abstractions and both selectively, and carefully, breaking them
> when there is a reasonable performance gain to be had.

Yes, ok, it is a good approach.

Thanks

Roberto


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2023-03-29  7:12 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14  8:17 [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu
2023-03-14  8:17 ` [PATCH v8 1/6] reiserfs: Switch to security_inode_init_security() Roberto Sassu
2023-03-23 23:33   ` Paul Moore
2023-03-14  8:17 ` [PATCH v8 2/6] ocfs2: " Roberto Sassu
2023-03-17 19:39   ` Mimi Zohar
2023-03-23 23:37   ` Paul Moore
2023-03-14  8:17 ` [PATCH v8 3/6] security: Remove security_old_inode_init_security() Roberto Sassu
2023-03-23 23:41   ` Paul Moore
2023-03-14  8:17 ` [PATCH v8 4/6] security: Allow all LSMs to provide xattrs for inode_init_security hook Roberto Sassu
2023-03-24  0:09   ` Paul Moore
2023-03-24  1:01     ` Casey Schaufler
2023-03-24 14:17       ` Paul Moore
2023-03-24 10:18     ` Roberto Sassu
2023-03-24 13:25       ` Roberto Sassu
2023-03-24 21:39         ` Paul Moore
2023-03-27  7:29           ` Roberto Sassu
2023-03-27 21:02             ` Paul Moore
2023-03-28  7:46               ` Roberto Sassu
2023-03-28 20:19                 ` Paul Moore
2023-03-29  7:11                   ` Roberto Sassu
2023-03-24 21:19       ` Paul Moore
2023-03-27  7:35         ` Roberto Sassu
2023-03-14  8:17 ` [PATCH v8 5/6] evm: Align evm_inode_init_security() definition with LSM infrastructure Roberto Sassu
2023-03-14  8:17 ` [PATCH v8 6/6] evm: Support multiple LSMs providing an xattr Roberto Sassu
2023-03-14  8:21 ` [PATCH v8 0/6] evm: Do HMAC of multiple per LSM xattrs for new inodes Roberto Sassu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).