diff mbox series

[07/29] 9p: implement set acl method

Message ID 20220922151728.1557914-8-brauner@kernel.org (mailing list archive)
State New, archived
Headers show
Series acl: add vfs posix acl api | expand

Commit Message

Christian Brauner Sept. 22, 2022, 3:17 p.m. UTC
The current way of setting and getting posix acls through the generic
xattr interface is error prone and type unsafe. The vfs needs to
interpret and fixup posix acls before storing or reporting it to
userspace. Various hacks exist to make this work. The code is hard to
understand and difficult to maintain in it's current form. Instead of
making this work by hacking posix acls through xattr handlers we are
building a dedicated posix acl api around the get and set inode
operations. This removes a lot of hackiness and makes the codepaths
easier to maintain. A lot of background can be found in [1].

In order to build a type safe posix api around get and set acl we need
all filesystem to implement get and set acl.

So far 9p implemented a ->get_inode_acl() operation that didn't require
access to the dentry in order to allow (limited) permission checking via
posix acls in the vfs. Now that we have get and set acl inode operations
that take a dentry argument we can give 9p get and set acl inode
operations.

This is mostly a light refactoring of the codepaths currently used in 9p
posix acl xattr handler. After we have fully implemented the posix acl
api and switched the vfs over to it, the 9p specific posix acl xattr
handler and associated code will be removed.

Note, until the vfs has been switched to the new posix acl api this
patch is a non-functional change.

Link: https://lore.kernel.org/all/20220801145520.1532837-1-brauner@kernel.org [1]
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
---
 fs/9p/acl.c            | 89 ++++++++++++++++++++++++++++++++++++++++++
 fs/9p/acl.h            |  8 ++++
 fs/9p/vfs_inode_dotl.c |  2 +
 3 files changed, 99 insertions(+)

Comments

Al Viro Sept. 24, 2022, 6:22 p.m. UTC | #1
On Thu, Sep 22, 2022 at 05:17:05PM +0200, Christian Brauner wrote:

> +int v9fs_iop_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
> +		     struct posix_acl *acl, int type)
> +{
> +	int retval;
> +	void *value = NULL;
> +	size_t size = 0;
> +	struct v9fs_session_info *v9ses;
> +	struct inode *inode = d_inode(dentry);
> +
> +	v9ses = v9fs_dentry2v9ses(dentry);
> +
> +	if (acl) {
> +		retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
> +		if (retval)
> +			goto err_out;
> +
> +		size = posix_acl_xattr_size(acl->a_count);
> +
> +		value = kzalloc(size, GFP_NOFS);
> +		if (!value) {
> +			retval = -ENOMEM;
> +			goto err_out;
> +		}
> +
> +		retval = posix_acl_to_xattr(&init_user_ns, acl, value, size);
> +		if (retval < 0)
> +			goto err_out;
> +	}
> +
> +	/*
> +	 * set the attribute on the remote. Without even looking at the
> +	 * xattr value. We leave it to the server to validate
> +	 */
> +	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
> +		retval = v9fs_xattr_set(dentry, posix_acl_xattr_name(type),
> +					value, size, 0);
> +		goto err_out;
> +	}

> +	if (S_ISLNK(inode->i_mode))
> +		return -EOPNOTSUPP;
> +	if (!inode_owner_or_capable(&init_user_ns, inode))
> +		return -EPERM;

Shouldn't that chunk have been in the very beginning?  As it is, you've
got a leak here...
Christian Brauner Sept. 26, 2022, 7:51 a.m. UTC | #2
On Sat, Sep 24, 2022 at 07:22:50PM +0100, Al Viro wrote:
> On Thu, Sep 22, 2022 at 05:17:05PM +0200, Christian Brauner wrote:
> 
> > +int v9fs_iop_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
> > +		     struct posix_acl *acl, int type)
> > +{
> > +	int retval;
> > +	void *value = NULL;
> > +	size_t size = 0;
> > +	struct v9fs_session_info *v9ses;
> > +	struct inode *inode = d_inode(dentry);
> > +
> > +	v9ses = v9fs_dentry2v9ses(dentry);
> > +
> > +	if (acl) {
> > +		retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
> > +		if (retval)
> > +			goto err_out;
> > +
> > +		size = posix_acl_xattr_size(acl->a_count);
> > +
> > +		value = kzalloc(size, GFP_NOFS);
> > +		if (!value) {
> > +			retval = -ENOMEM;
> > +			goto err_out;
> > +		}
> > +
> > +		retval = posix_acl_to_xattr(&init_user_ns, acl, value, size);
> > +		if (retval < 0)
> > +			goto err_out;
> > +	}
> > +
> > +	/*
> > +	 * set the attribute on the remote. Without even looking at the
> > +	 * xattr value. We leave it to the server to validate
> > +	 */
> > +	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
> > +		retval = v9fs_xattr_set(dentry, posix_acl_xattr_name(type),
> > +					value, size, 0);
> > +		goto err_out;
> > +	}
> 
> > +	if (S_ISLNK(inode->i_mode))
> > +		return -EOPNOTSUPP;
> > +	if (!inode_owner_or_capable(&init_user_ns, inode))
> > +		return -EPERM;
> 
> Shouldn't that chunk have been in the very beginning?  As it is, you've
> got a leak here...

Good catch, I probably messed up the merge conflict resolution in my
last rebase... Thanks for spotting!
diff mbox series

Patch

diff --git a/fs/9p/acl.c b/fs/9p/acl.c
index 0ef3386cf61a..ad54f8ce0804 100644
--- a/fs/9p/acl.c
+++ b/fs/9p/acl.c
@@ -170,6 +170,95 @@  struct posix_acl *v9fs_iop_get_acl(struct user_namespace *mnt_userns,
 	return acl;
 }
 
+int v9fs_iop_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
+		     struct posix_acl *acl, int type)
+{
+	int retval;
+	void *value = NULL;
+	size_t size = 0;
+	struct v9fs_session_info *v9ses;
+	struct inode *inode = d_inode(dentry);
+
+	v9ses = v9fs_dentry2v9ses(dentry);
+
+	if (acl) {
+		retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
+		if (retval)
+			goto err_out;
+
+		size = posix_acl_xattr_size(acl->a_count);
+
+		value = kzalloc(size, GFP_NOFS);
+		if (!value) {
+			retval = -ENOMEM;
+			goto err_out;
+		}
+
+		retval = posix_acl_to_xattr(&init_user_ns, acl, value, size);
+		if (retval < 0)
+			goto err_out;
+	}
+
+	/*
+	 * set the attribute on the remote. Without even looking at the
+	 * xattr value. We leave it to the server to validate
+	 */
+	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
+		retval = v9fs_xattr_set(dentry, posix_acl_xattr_name(type),
+					value, size, 0);
+		goto err_out;
+	}
+
+	if (S_ISLNK(inode->i_mode))
+		return -EOPNOTSUPP;
+	if (!inode_owner_or_capable(&init_user_ns, inode))
+		return -EPERM;
+
+	switch (type) {
+	case ACL_TYPE_ACCESS:
+		if (acl) {
+			struct iattr iattr = { 0 };
+			struct posix_acl *old_acl = acl;
+
+			retval = posix_acl_update_mode(&init_user_ns, inode,
+						       &iattr.ia_mode, &acl);
+			if (retval)
+				goto err_out;
+			if (!acl) {
+				/*
+				 * ACL can be represented
+				 * by the mode bits. So don't
+				 * update ACL.
+				 */
+				posix_acl_release(old_acl);
+				value = NULL;
+				size = 0;
+			}
+			iattr.ia_valid = ATTR_MODE;
+			/* FIXME should we update ctime ?
+			 * What is the following setxattr update the
+			 * mode ?
+			 */
+			v9fs_vfs_setattr_dotl(&init_user_ns, dentry, &iattr);
+		}
+		break;
+	case ACL_TYPE_DEFAULT:
+		if (!S_ISDIR(inode->i_mode)) {
+			retval = acl ? -EINVAL : 0;
+			goto err_out;
+		}
+		break;
+	default:
+		BUG();
+	}
+	retval = v9fs_xattr_set(dentry, posix_acl_xattr_name(type), value, size, 0);
+	if (!retval)
+		set_cached_acl(inode, type, acl);
+err_out:
+	kfree(value);
+	return retval;
+}
+
 static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
 {
 	int retval;
diff --git a/fs/9p/acl.h b/fs/9p/acl.h
index 11d3f8ea4ce4..23ef70227d1f 100644
--- a/fs/9p/acl.h
+++ b/fs/9p/acl.h
@@ -12,6 +12,8 @@  struct posix_acl *v9fs_iop_get_inode_acl(struct inode *inode, int type,
 				   bool rcu);
 struct posix_acl *v9fs_iop_get_acl(struct user_namespace *mnt_userns,
 					  struct dentry *dentry, int type);
+int v9fs_iop_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
+		     struct posix_acl *acl, int type);
 int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid);
 int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
 			struct posix_acl *dacl, struct posix_acl *acl);
@@ -31,6 +33,12 @@  v9fs_iop_get_acl(struct user_namespace *mnt_userns,
 {
 	return NULL;
 }
+static inline int v9fs_iop_set_acl(struct user_namespace *mnt_userns,
+				   struct dentry *dentry, struct posix_acl *acl,
+				   int type)
+{
+	return 0;
+}
 static inline int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
 {
 	return 0;
diff --git a/fs/9p/vfs_inode_dotl.c b/fs/9p/vfs_inode_dotl.c
index a4211fcb9168..03c1743c4aff 100644
--- a/fs/9p/vfs_inode_dotl.c
+++ b/fs/9p/vfs_inode_dotl.c
@@ -985,6 +985,7 @@  const struct inode_operations v9fs_dir_inode_operations_dotl = {
 	.listxattr = v9fs_listxattr,
 	.get_inode_acl = v9fs_iop_get_inode_acl,
 	.get_acl = v9fs_iop_get_acl,
+	.set_acl = v9fs_iop_set_acl,
 };
 
 const struct inode_operations v9fs_file_inode_operations_dotl = {
@@ -993,6 +994,7 @@  const struct inode_operations v9fs_file_inode_operations_dotl = {
 	.listxattr = v9fs_listxattr,
 	.get_inode_acl = v9fs_iop_get_inode_acl,
 	.get_acl = v9fs_iop_get_acl,
+	.set_acl = v9fs_iop_set_acl,
 };
 
 const struct inode_operations v9fs_symlink_inode_operations_dotl = {