[v2,13/25] smack: add hooks for fscaps operations

Message ID 20240221-idmap-fscap-refactor-v2-13-3039364623bd@kernel.org
State New
Headers
Series fs: use type-safe uid representation for filesystem capabilities |

Commit Message

Seth Forshee (DigitalOcean) Feb. 21, 2024, 9:24 p.m. UTC
  Add hooks for set/get/remove fscaps operations which perform the same
checks as the xattr hooks would have done for XATTR_NAME_CAPS.

Signed-off-by: Seth Forshee (DigitalOcean) <sforshee@kernel.org>
---
 security/smack/smack_lsm.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
  

Comments

Casey Schaufler Feb. 21, 2024, 10:52 p.m. UTC | #1
On 2/21/2024 1:24 PM, Seth Forshee (DigitalOcean) wrote:
> Add hooks for set/get/remove fscaps operations which perform the same
> checks as the xattr hooks would have done for XATTR_NAME_CAPS.
>
> Signed-off-by: Seth Forshee (DigitalOcean) <sforshee@kernel.org>
> ---
>  security/smack/smack_lsm.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 71 insertions(+)
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 0fdbf04cc258..1eaa89dede6b 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -1530,6 +1530,74 @@ static int smack_inode_remove_acl(struct mnt_idmap *idmap,
>  	return rc;
>  }
>  
> +/**
> + * smack_inode_set_fscaps - Smack check for setting file capabilities
> + * @mnt_userns: the userns attached to the source mnt for this request
> + * @detry: the object
> + * @caps: the file capabilities
> + * @flags: unused
> + *
> + * Returns 0 if the access is permitted, or an error code otherwise.
> + */
> +static int smack_inode_set_fscaps(struct mnt_idmap *idmap,
> +				  struct dentry *dentry,
> +				  const struct vfs_caps *caps, int flags)
> +{
> +	struct smk_audit_info ad;
> +	int rc;
> +
> +	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
> +	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
> +	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
> +	rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
> +	return rc;
> +}
> +
> +/**
> + * smack_inode_get_fscaps - Smack check for getting file capabilities
> + * @dentry: the object
> + *
> + * Returns 0 if access is permitted, an error code otherwise
> + */
> +static int smack_inode_get_fscaps(struct mnt_idmap *idmap,
> +				  struct dentry *dentry)
> +{
> +	struct smk_audit_info ad;
> +	int rc;
> +
> +	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
> +	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
> +
> +	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
> +	rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
> +	return rc;
> +}
> +
> +/**
> + * smack_inode_remove_acl - Smack check for removing file capabilities

s/smack_inode_remove_acl/smack_inode_remove_fscaps/

> + * @idmap: idmap of the mnt this request came from
> + * @dentry: the object
> + *
> + * Returns 0 if access is permitted, an error code otherwise
> + */
> +static int smack_inode_remove_fscaps(struct mnt_idmap *idmap,
> +				     struct dentry *dentry)
> +{
> +	struct smk_audit_info ad;
> +	int rc;
> +
> +	rc = cap_inode_removexattr(idmap, dentry, XATTR_NAME_CAPS);
> +	if (rc != 0)
> +		return rc;
> +
> +	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
> +	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
> +
> +	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
> +	rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
> +	return rc;
> +}
> +
>  /**
>   * smack_inode_getsecurity - get smack xattrs
>   * @idmap: idmap of the mount
> @@ -5045,6 +5113,9 @@ static struct security_hook_list smack_hooks[] __ro_after_init = {
>  	LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl),
>  	LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl),
>  	LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl),
> +	LSM_HOOK_INIT(inode_set_fscaps, smack_inode_set_fscaps),
> +	LSM_HOOK_INIT(inode_get_fscaps, smack_inode_get_fscaps),
> +	LSM_HOOK_INIT(inode_remove_fscaps, smack_inode_remove_fscaps),
>  	LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
>  	LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
>  	LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
>
  
Seth Forshee (DigitalOcean) Feb. 22, 2024, 12:11 a.m. UTC | #2
On Wed, Feb 21, 2024 at 02:52:50PM -0800, Casey Schaufler wrote:
> > +/**
> > + * smack_inode_remove_acl - Smack check for removing file capabilities
> 
> s/smack_inode_remove_acl/smack_inode_remove_fscaps/

Fixed, thanks! I guess you can see where I copied my work from :-)
  

Patch

diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 0fdbf04cc258..1eaa89dede6b 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -1530,6 +1530,74 @@  static int smack_inode_remove_acl(struct mnt_idmap *idmap,
 	return rc;
 }
 
+/**
+ * smack_inode_set_fscaps - Smack check for setting file capabilities
+ * @mnt_userns: the userns attached to the source mnt for this request
+ * @detry: the object
+ * @caps: the file capabilities
+ * @flags: unused
+ *
+ * Returns 0 if the access is permitted, or an error code otherwise.
+ */
+static int smack_inode_set_fscaps(struct mnt_idmap *idmap,
+				  struct dentry *dentry,
+				  const struct vfs_caps *caps, int flags)
+{
+	struct smk_audit_info ad;
+	int rc;
+
+	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
+	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
+	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
+	rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
+	return rc;
+}
+
+/**
+ * smack_inode_get_fscaps - Smack check for getting file capabilities
+ * @dentry: the object
+ *
+ * Returns 0 if access is permitted, an error code otherwise
+ */
+static int smack_inode_get_fscaps(struct mnt_idmap *idmap,
+				  struct dentry *dentry)
+{
+	struct smk_audit_info ad;
+	int rc;
+
+	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
+	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
+
+	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
+	rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
+	return rc;
+}
+
+/**
+ * smack_inode_remove_acl - Smack check for removing file capabilities
+ * @idmap: idmap of the mnt this request came from
+ * @dentry: the object
+ *
+ * Returns 0 if access is permitted, an error code otherwise
+ */
+static int smack_inode_remove_fscaps(struct mnt_idmap *idmap,
+				     struct dentry *dentry)
+{
+	struct smk_audit_info ad;
+	int rc;
+
+	rc = cap_inode_removexattr(idmap, dentry, XATTR_NAME_CAPS);
+	if (rc != 0)
+		return rc;
+
+	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
+	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
+
+	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
+	rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
+	return rc;
+}
+
 /**
  * smack_inode_getsecurity - get smack xattrs
  * @idmap: idmap of the mount
@@ -5045,6 +5113,9 @@  static struct security_hook_list smack_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl),
 	LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl),
 	LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl),
+	LSM_HOOK_INIT(inode_set_fscaps, smack_inode_set_fscaps),
+	LSM_HOOK_INIT(inode_get_fscaps, smack_inode_get_fscaps),
+	LSM_HOOK_INIT(inode_remove_fscaps, smack_inode_remove_fscaps),
 	LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
 	LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
 	LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),