2023-06-07 18:21:23

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: [PATCH v4 00/14] ceph: support idmapped mounts

Dear friends,

This patchset was originally developed by Christian Brauner but I'll continue
to push it forward. Christian allowed me to do that :)

This feature is already actively used/tested with LXD/LXC project.

Git tree (based on https://github.com/ceph/ceph-client.git master):
https://github.com/mihalicyn/linux/tree/fs.idmapped.ceph

In the version 3 I've changed only two commits:
- fs: export mnt_idmap_get/mnt_idmap_put
- ceph: allow idmapped setattr inode op
and added a new one:
- ceph: pass idmap to __ceph_setattr

In the version 4 I've reworked the ("ceph: stash idmapping in mdsc request")
commit. Now we take idmap refcounter just in place where req->r_mnt_idmap
is filled. It's more safer approach and prevents possible refcounter underflow
on error paths where __register_request wasn't called but ceph_mdsc_release_request is
called.

I can confirm that this version passes xfstests.

Links to previous versions:
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/#t

Kind regards,
Alex

Original description from Christian:
========================================================================
This patch series enables cephfs to support idmapped mounts, i.e. the
ability to alter ownership information on a per-mount basis.

Container managers such as LXD support sharaing data via cephfs between
the host and unprivileged containers and between unprivileged containers.
They may all use different idmappings. Idmapped mounts can be used to
create mounts with the idmapping used for the container (or a different
one specific to the use-case).

There are in fact more use-cases such as remapping ownership for
mountpoints on the host itself to grant or restrict access to different
users or to make it possible to enforce that programs running as root
will write with a non-zero {g,u}id to disk.

The patch series is simple overall and few changes are needed to cephfs.
There is one cephfs specific issue that I would like to discuss and
solve which I explain in detail in:

[PATCH 02/12] ceph: handle idmapped mounts in create_request_message()

It has to do with how to handle mds serves which have id-based access
restrictions configured. I would ask you to please take a look at the
explanation in the aforementioned patch.

The patch series passes the vfs and idmapped mount testsuite as part of
xfstests. To run it you will need a config like:

[ceph]
export FSTYP=ceph
export TEST_DIR=/mnt/test
export TEST_DEV=10.103.182.10:6789:/
export TEST_FS_MOUNT_OPTS="-o name=admin,secret=$password

and then simply call

sudo ./check -g idmapped

========================================================================

Alexander Mikhalitsyn (2):
fs: export mnt_idmap_get/mnt_idmap_put
ceph: pass idmap to __ceph_setattr

Christian Brauner (12):
ceph: stash idmapping in mdsc request
ceph: handle idmapped mounts in create_request_message()
ceph: allow idmapped mknod inode op
ceph: allow idmapped symlink inode op
ceph: allow idmapped mkdir inode op
ceph: allow idmapped rename inode op
ceph: allow idmapped getattr inode op
ceph: allow idmapped permission inode op
ceph: allow idmapped setattr inode op
ceph/acl: allow idmapped set_acl inode op
ceph/file: allow idmapped atomic_open inode op
ceph: allow idmapped mounts

fs/ceph/acl.c | 6 +++---
fs/ceph/dir.c | 4 ++++
fs/ceph/file.c | 10 ++++++++--
fs/ceph/inode.c | 29 +++++++++++++++++------------
fs/ceph/mds_client.c | 27 +++++++++++++++++++++++----
fs/ceph/mds_client.h | 1 +
fs/ceph/super.c | 2 +-
fs/ceph/super.h | 3 ++-
fs/mnt_idmapping.c | 2 ++
include/linux/mnt_idmapping.h | 3 +++
10 files changed, 64 insertions(+), 23 deletions(-)

--
2.34.1



2023-06-07 18:21:39

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: [PATCH v4 03/14] ceph: handle idmapped mounts in create_request_message()

From: Christian Brauner <[email protected]>

Inode operations that create a new filesystem object such as ->mknod,
->create, ->mkdir() and others don't take a {g,u}id argument explicitly.
Instead the caller's fs{g,u}id is used for the {g,u}id of the new
filesystem object.

Cephfs mds creation request argument structures mirror this filesystem
behavior. They don't encode a {g,u}id explicitly. Instead the caller's
fs{g,u}id that is always sent as part of any mds request is used by the
servers to set the {g,u}id of the new filesystem object.

In order to ensure that the correct {g,u}id is used map the caller's
fs{g,u}id for creation requests. This doesn't require complex changes.
It suffices to pass in the relevant idmapping recorded in the request
message. If this request message was triggered from an inode operation
that creates filesystem objects it will have passed down the relevant
idmaping. If this is a request message that was triggered from an inode
operation that doens't need to take idmappings into account the initial
idmapping is passed down which is an identity mapping and thus is
guaranteed to leave the caller's fs{g,u}id unchanged.,u}id is sent.

The last few weeks before Christmas 2021 I have spent time not just
reading and poking the cephfs kernel code but also took a look at the
ceph mds server userspace to ensure I didn't miss some subtlety.

This made me aware of one complication to solve. All requests send the
caller's fs{g,u}id over the wire. The caller's fs{g,u}id matters for the
server in exactly two cases:

1. to set the ownership for creation requests
2. to determine whether this client is allowed access on this server

Case 1. we already covered and explained. Case 2. is only relevant for
servers where an explicit uid access restriction has been set. That is
to say the mds server restricts access to requests coming from a
specific uid. Servers without uid restrictions will grant access to
requests from any uid by setting MDS_AUTH_UID_ANY.

Case 2. introduces the complication because the caller's fs{g,u}id is
not just used to record ownership but also serves as the {g,u}id used
when checking access to the server.

Consider a user mounting a cephfs client and creating an idmapped mount
from it that maps files owned by uid 1000 to be owned uid 0:

mount -t cephfs -o [...] /unmapped
mount-idmapped --map-mount 1000:0:1 /idmapped

That is to say if the mounted cephfs filesystem contains a file "file1"
which is owned by uid 1000:

- looking at it via /unmapped/file1 will report it as owned by uid 1000
(One can think of this as the on-disk value.)
- looking at it via /idmapped/file1 will report it as owned by uid 0

Now, consider creating new files via the idmapped mount at /idmapped.
When a caller with fs{g,u}id 1000 creates a file "file2" by going
through the idmapped mount mounted at /idmapped it will create a file
that is owned by uid 1000 on-disk, i.e.:

- looking at it via /unmapped/file2 will report it as owned by uid 1000
- looking at it via /idmapped/file2 will report it as owned by uid 0

Now consider an mds server that has a uid access restriction set and
only grants access to requests from uid 0.

If the client sends a creation request for a file e.g. /idmapped/file2
it will send the caller's fs{g,u}id idmapped according to the idmapped
mount. So if the caller has fs{g,u}id 1000 it will be mapped to {g,u}id
0 in the idmapped mount and will be sent over the wire allowing the
caller access to the mds server.

However, if the caller is not issuing a creation request the caller's
fs{g,u}id will be send without the mount's idmapping applied. So if the
caller that just successfully created a new file on the restricted mds
server sends a request as fs{g,u}id 1000 access will be refused. This
however is inconsistent.

From my perspective the root of the problem lies in the fact that
creation requests implicitly infer the ownership from the {g,u}id that
gets sent along with every mds request.

I have thought of multiple ways of addressing this problem but the one I
prefer is to give all mds requests that create a filesystem object a
proper, separate {g,u}id field entry in the argument struct. This is,
for example how ->setattr mds requests work.

This way the caller's fs{g,u}id can be used consistenly for server
access checks and is separated from the ownership for new filesystem
objects.

Servers could then be updated to refuse creation requests whenever the
{g,u}id used for access checking doesn't match the {g,u}id used for
creating the filesystem object just as is done for setattr requests on a
uid restricted server. But I am, of course, open to other suggestions.

Cc: Xiubo Li <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: [email protected]
Signed-off-by: Christian Brauner <[email protected]>
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
---
fs/ceph/mds_client.c | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index 05a99a8eb292..8826be3c209f 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -2581,6 +2581,8 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session,
void *p, *end;
int ret;
bool legacy = !(session->s_con.peer_features & CEPH_FEATURE_FS_BTIME);
+ kuid_t caller_fsuid;
+ kgid_t caller_fsgid;

ret = set_request_path_attr(req->r_inode, req->r_dentry,
req->r_parent, req->r_path1, req->r_ino1.ino,
@@ -2649,10 +2651,22 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session,

head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
head->op = cpu_to_le32(req->r_op);
- head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns,
- req->r_cred->fsuid));
- head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns,
- req->r_cred->fsgid));
+ /*
+ * Inode operations that create filesystem objects based on the
+ * caller's fs{g,u}id like ->mknod(), ->create(), ->mkdir() etc. don't
+ * have separate {g,u}id fields in their respective structs in the
+ * ceph_mds_request_args union. Instead the caller_{g,u}id field is
+ * used to set ownership of the newly created inode by the mds server.
+ * For these inode operations we need to send the mapped fs{g,u}id over
+ * the wire. For other cases we simple set req->r_mnt_idmap to the
+ * initial idmapping meaning the unmapped fs{g,u}id is sent.
+ */
+ caller_fsuid = from_vfsuid(req->r_mnt_idmap, &init_user_ns,
+ VFSUIDT_INIT(req->r_cred->fsuid));
+ caller_fsgid = from_vfsgid(req->r_mnt_idmap, &init_user_ns,
+ VFSGIDT_INIT(req->r_cred->fsgid));
+ head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, caller_fsuid));
+ head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, caller_fsgid));
head->ino = cpu_to_le64(req->r_deleg_ino);
head->args = req->r_args;

--
2.34.1


2023-06-07 18:22:13

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: [PATCH v4 08/14] ceph: allow idmapped getattr inode op

From: Christian Brauner <[email protected]>

Enable ceph_getattr() to handle idmapped mounts. This is just a matter
of passing down the mount's idmapping.

Cc: Xiubo Li <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: [email protected]
Signed-off-by: Christian Brauner <[email protected]>
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
---
fs/ceph/inode.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
index 8e5f41d45283..2e988612ed6c 100644
--- a/fs/ceph/inode.c
+++ b/fs/ceph/inode.c
@@ -2465,7 +2465,7 @@ int ceph_getattr(struct mnt_idmap *idmap, const struct path *path,
return err;
}

- generic_fillattr(&nop_mnt_idmap, inode, stat);
+ generic_fillattr(idmap, inode, stat);
stat->ino = ceph_present_inode(inode);

/*
--
2.34.1


2023-06-07 18:23:21

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: [PATCH v4 04/14] ceph: allow idmapped mknod inode op

From: Christian Brauner <[email protected]>

Enable ceph_mknod() to handle idmapped mounts. This is just a matter of
passing down the mount's idmapping.

Cc: Xiubo Li <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: [email protected]
Signed-off-by: Christian Brauner <[email protected]>
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
---
v4:
- call mnt_idmap_get
---
fs/ceph/dir.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c
index cb67ac821f0e..aaae586de4de 100644
--- a/fs/ceph/dir.c
+++ b/fs/ceph/dir.c
@@ -884,6 +884,7 @@ static int ceph_mknod(struct mnt_idmap *idmap, struct inode *dir,
req->r_parent = dir;
ihold(dir);
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
+ req->r_mnt_idmap = mnt_idmap_get(idmap);
req->r_args.mknod.mode = cpu_to_le32(mode);
req->r_args.mknod.rdev = cpu_to_le32(rdev);
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
--
2.34.1


2023-06-07 18:23:40

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: [PATCH v4 06/14] ceph: allow idmapped mkdir inode op

From: Christian Brauner <[email protected]>

Enable ceph_mkdir() to handle idmapped mounts. This is just a matter of
passing down the mount's idmapping.

Cc: Xiubo Li <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: [email protected]
Signed-off-by: Christian Brauner <[email protected]>
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
---
v4:
- call mnt_idmap_get
---
fs/ceph/dir.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c
index 5025b570683d..5ef90a49b156 100644
--- a/fs/ceph/dir.c
+++ b/fs/ceph/dir.c
@@ -1024,6 +1024,7 @@ static int ceph_mkdir(struct mnt_idmap *idmap, struct inode *dir,
ihold(dir);
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
req->r_args.mkdir.mode = cpu_to_le32(mode);
+ req->r_mnt_idmap = mnt_idmap_get(idmap);
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
if (as_ctx.pagelist) {
--
2.34.1


2023-06-07 18:24:14

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: [PATCH v4 10/14] ceph: pass idmap to __ceph_setattr

Just pass down the mount's idmapping to __ceph_setattr,
because we will need it later.

Cc: Xiubo Li <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
---
fs/ceph/acl.c | 4 ++--
fs/ceph/inode.c | 5 +++--
fs/ceph/super.h | 3 ++-
3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/fs/ceph/acl.c b/fs/ceph/acl.c
index 6945a938d396..51ffef848429 100644
--- a/fs/ceph/acl.c
+++ b/fs/ceph/acl.c
@@ -140,7 +140,7 @@ int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
newattrs.ia_ctime = current_time(inode);
newattrs.ia_mode = new_mode;
newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
- ret = __ceph_setattr(inode, &newattrs);
+ ret = __ceph_setattr(idmap, inode, &newattrs);
if (ret)
goto out_free;
}
@@ -151,7 +151,7 @@ int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
newattrs.ia_ctime = old_ctime;
newattrs.ia_mode = old_mode;
newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
- __ceph_setattr(inode, &newattrs);
+ __ceph_setattr(idmap, inode, &newattrs);
}
goto out_free;
}
diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
index 37e1cbfc7c89..bcd9b506ec3b 100644
--- a/fs/ceph/inode.c
+++ b/fs/ceph/inode.c
@@ -2009,7 +2009,8 @@ static const struct inode_operations ceph_symlink_iops = {
.listxattr = ceph_listxattr,
};

-int __ceph_setattr(struct inode *inode, struct iattr *attr)
+int __ceph_setattr(struct mnt_idmap *idmap, struct inode *inode,
+ struct iattr *attr)
{
struct ceph_inode_info *ci = ceph_inode(inode);
unsigned int ia_valid = attr->ia_valid;
@@ -2252,7 +2253,7 @@ int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
return -EDQUOT;

- err = __ceph_setattr(inode, attr);
+ err = __ceph_setattr(idmap, inode, attr);

if (err >= 0 && (attr->ia_valid & ATTR_MODE))
err = posix_acl_chmod(&nop_mnt_idmap, dentry, attr->ia_mode);
diff --git a/fs/ceph/super.h b/fs/ceph/super.h
index d24bf0db5234..d9cc27307cb7 100644
--- a/fs/ceph/super.h
+++ b/fs/ceph/super.h
@@ -1052,7 +1052,8 @@ static inline int ceph_do_getattr(struct inode *inode, int mask, bool force)
}
extern int ceph_permission(struct mnt_idmap *idmap,
struct inode *inode, int mask);
-extern int __ceph_setattr(struct inode *inode, struct iattr *attr);
+extern int __ceph_setattr(struct mnt_idmap *idmap, struct inode *inode,
+ struct iattr *attr);
extern int ceph_setattr(struct mnt_idmap *idmap,
struct dentry *dentry, struct iattr *attr);
extern int ceph_getattr(struct mnt_idmap *idmap,
--
2.34.1


2023-06-07 18:24:28

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: [PATCH v4 05/14] ceph: allow idmapped symlink inode op

From: Christian Brauner <[email protected]>

Enable ceph_symlink() to handle idmapped mounts. This is just a matter
of passing down the mount's idmapping.

Cc: Xiubo Li <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: [email protected]
Signed-off-by: Christian Brauner <[email protected]>
Signed-off-by: Alexander Mikhalitsyn <[email protected]>
---
v4:
- call mnt_idmap_get
---
fs/ceph/dir.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c
index aaae586de4de..5025b570683d 100644
--- a/fs/ceph/dir.c
+++ b/fs/ceph/dir.c
@@ -956,6 +956,7 @@ static int ceph_symlink(struct mnt_idmap *idmap, struct inode *dir,
req->r_num_caps = 2;
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
+ req->r_mnt_idmap = mnt_idmap_get(idmap);
if (as_ctx.pagelist) {
req->r_pagelist = as_ctx.pagelist;
as_ctx.pagelist = NULL;
--
2.34.1


2023-06-08 03:15:39

by Xiubo Li

[permalink] [raw]
Subject: Re: [PATCH v4 00/14] ceph: support idmapped mounts

Hi Alexander,

As I mentioned in V2 thread
https://www.spinics.net/lists/kernel/msg4810994.html, we should use the
'idmap' for all the requests below, because MDS will do the
'check_access()' for all the requests by using the caller uid/gid,
please see
https://github.com/ceph/ceph/blob/main/src/mds/Server.cc#L3294-L3310.


Cscope tag: ceph_mdsc_do_request
   #   line  filename / context / line
   1    321  fs/ceph/addr.c <<ceph_netfs_issue_op_inline>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
   2    443  fs/ceph/dir.c <<ceph_readdir>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
   3    838  fs/ceph/dir.c <<ceph_lookup>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
   4    933  fs/ceph/dir.c <<ceph_mknod>>
             err = ceph_mdsc_do_request(mdsc, dir, req);
   5   1045  fs/ceph/dir.c <<ceph_symlink>>
             err = ceph_mdsc_do_request(mdsc, dir, req);
   6   1120  fs/ceph/dir.c <<ceph_mkdir>>
             err = ceph_mdsc_do_request(mdsc, dir, req);
   7   1180  fs/ceph/dir.c <<ceph_link>>
             err = ceph_mdsc_do_request(mdsc, dir, req);
   8   1365  fs/ceph/dir.c <<ceph_unlink>>
             err = ceph_mdsc_do_request(mdsc, dir, req);
   9   1431  fs/ceph/dir.c <<ceph_rename>>
             err = ceph_mdsc_do_request(mdsc, old_dir, req);
  10   1927  fs/ceph/dir.c <<ceph_d_revalidate>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  11    154  fs/ceph/export.c <<__lookup_inode>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  12    262  fs/ceph/export.c <<__snapfh_to_dentry>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  13    347  fs/ceph/export.c <<__get_parent>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  14    490  fs/ceph/export.c <<__get_snap_name>>
             err = ceph_mdsc_do_request(fsc->mdsc, NULL, req);
  15    561  fs/ceph/export.c <<ceph_get_name>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  16    339  fs/ceph/file.c <<ceph_renew_caps>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  17    434  fs/ceph/file.c <<ceph_open>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  18    855  fs/ceph/file.c <<ceph_atomic_open>>
             err = ceph_mdsc_do_request(mdsc, (flags & O_CREAT) ? dir :
NULL, req);
  19   2715  fs/ceph/inode.c <<__ceph_setattr>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  20   2839  fs/ceph/inode.c <<__ceph_do_getattr>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  21   2883  fs/ceph/inode.c <<ceph_do_getvxattr>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  22    126  fs/ceph/ioctl.c <<ceph_ioctl_set_layout>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  23    171  fs/ceph/ioctl.c <<ceph_ioctl_set_layout_policy>>
             err = ceph_mdsc_do_request(mdsc, inode, req);
  24    216  fs/ceph/locks.c <<ceph_lock_wait_for_completion>>
             err = ceph_mdsc_do_request(mdsc, inode, intr_req);
  25   1091  fs/ceph/super.c <<open_root_dentry>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);
  26   1151  fs/ceph/xattr.c <<ceph_sync_setxattr>>
             err = ceph_mdsc_do_request(mdsc, NULL, req);


And also could you squash the similar commit into one ?


Thanks

- Xiubo


On 6/8/23 02:09, Alexander Mikhalitsyn wrote:
> Dear friends,
>
> This patchset was originally developed by Christian Brauner but I'll continue
> to push it forward. Christian allowed me to do that :)
>
> This feature is already actively used/tested with LXD/LXC project.
>
> Git tree (based on https://github.com/ceph/ceph-client.git master):
> https://github.com/mihalicyn/linux/tree/fs.idmapped.ceph
>
> In the version 3 I've changed only two commits:
> - fs: export mnt_idmap_get/mnt_idmap_put
> - ceph: allow idmapped setattr inode op
> and added a new one:
> - ceph: pass idmap to __ceph_setattr
>
> In the version 4 I've reworked the ("ceph: stash idmapping in mdsc request")
> commit. Now we take idmap refcounter just in place where req->r_mnt_idmap
> is filled. It's more safer approach and prevents possible refcounter underflow
> on error paths where __register_request wasn't called but ceph_mdsc_release_request is
> called.
>
> I can confirm that this version passes xfstests.
>
> Links to previous versions:
> v1: https://lore.kernel.org/all/[email protected]/
> v2: https://lore.kernel.org/lkml/[email protected]/
> v3: https://lore.kernel.org/lkml/[email protected]/#t
>
> Kind regards,
> Alex
>
> Original description from Christian:
> ========================================================================
> This patch series enables cephfs to support idmapped mounts, i.e. the
> ability to alter ownership information on a per-mount basis.
>
> Container managers such as LXD support sharaing data via cephfs between
> the host and unprivileged containers and between unprivileged containers.
> They may all use different idmappings. Idmapped mounts can be used to
> create mounts with the idmapping used for the container (or a different
> one specific to the use-case).
>
> There are in fact more use-cases such as remapping ownership for
> mountpoints on the host itself to grant or restrict access to different
> users or to make it possible to enforce that programs running as root
> will write with a non-zero {g,u}id to disk.
>
> The patch series is simple overall and few changes are needed to cephfs.
> There is one cephfs specific issue that I would like to discuss and
> solve which I explain in detail in:
>
> [PATCH 02/12] ceph: handle idmapped mounts in create_request_message()
>
> It has to do with how to handle mds serves which have id-based access
> restrictions configured. I would ask you to please take a look at the
> explanation in the aforementioned patch.
>
> The patch series passes the vfs and idmapped mount testsuite as part of
> xfstests. To run it you will need a config like:
>
> [ceph]
> export FSTYP=ceph
> export TEST_DIR=/mnt/test
> export TEST_DEV=10.103.182.10:6789:/
> export TEST_FS_MOUNT_OPTS="-o name=admin,secret=$password
>
> and then simply call
>
> sudo ./check -g idmapped
>
> ========================================================================
>
> Alexander Mikhalitsyn (2):
> fs: export mnt_idmap_get/mnt_idmap_put
> ceph: pass idmap to __ceph_setattr
>
> Christian Brauner (12):
> ceph: stash idmapping in mdsc request
> ceph: handle idmapped mounts in create_request_message()
> ceph: allow idmapped mknod inode op
> ceph: allow idmapped symlink inode op
> ceph: allow idmapped mkdir inode op
> ceph: allow idmapped rename inode op
> ceph: allow idmapped getattr inode op
> ceph: allow idmapped permission inode op
> ceph: allow idmapped setattr inode op
> ceph/acl: allow idmapped set_acl inode op
> ceph/file: allow idmapped atomic_open inode op
> ceph: allow idmapped mounts
>
> fs/ceph/acl.c | 6 +++---
> fs/ceph/dir.c | 4 ++++
> fs/ceph/file.c | 10 ++++++++--
> fs/ceph/inode.c | 29 +++++++++++++++++------------
> fs/ceph/mds_client.c | 27 +++++++++++++++++++++++----
> fs/ceph/mds_client.h | 1 +
> fs/ceph/super.c | 2 +-
> fs/ceph/super.h | 3 ++-
> fs/mnt_idmapping.c | 2 ++
> include/linux/mnt_idmapping.h | 3 +++
> 10 files changed, 64 insertions(+), 23 deletions(-)
>


2023-06-08 07:25:25

by Aleksandr Mikhalitsyn

[permalink] [raw]
Subject: Re: [PATCH v4 00/14] ceph: support idmapped mounts

On Thu, Jun 8, 2023 at 5:01 AM Xiubo Li <[email protected]> wrote:
>
> Hi Alexander,

Dear Xiubo,

>
> As I mentioned in V2 thread
> https://www.spinics.net/lists/kernel/msg4810994.html, we should use the
> 'idmap' for all the requests below, because MDS will do the
> 'check_access()' for all the requests by using the caller uid/gid,
> please see
> https://github.com/ceph/ceph/blob/main/src/mds/Server.cc#L3294-L3310.
>
>
> Cscope tag: ceph_mdsc_do_request
> # line filename / context / line
> 1 321 fs/ceph/addr.c <<ceph_netfs_issue_op_inline>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 2 443 fs/ceph/dir.c <<ceph_readdir>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 3 838 fs/ceph/dir.c <<ceph_lookup>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 4 933 fs/ceph/dir.c <<ceph_mknod>>
> err = ceph_mdsc_do_request(mdsc, dir, req);
> 5 1045 fs/ceph/dir.c <<ceph_symlink>>
> err = ceph_mdsc_do_request(mdsc, dir, req);
> 6 1120 fs/ceph/dir.c <<ceph_mkdir>>
> err = ceph_mdsc_do_request(mdsc, dir, req);
> 7 1180 fs/ceph/dir.c <<ceph_link>>
> err = ceph_mdsc_do_request(mdsc, dir, req);
> 8 1365 fs/ceph/dir.c <<ceph_unlink>>
> err = ceph_mdsc_do_request(mdsc, dir, req);
> 9 1431 fs/ceph/dir.c <<ceph_rename>>
> err = ceph_mdsc_do_request(mdsc, old_dir, req);
> 10 1927 fs/ceph/dir.c <<ceph_d_revalidate>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 11 154 fs/ceph/export.c <<__lookup_inode>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 12 262 fs/ceph/export.c <<__snapfh_to_dentry>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 13 347 fs/ceph/export.c <<__get_parent>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 14 490 fs/ceph/export.c <<__get_snap_name>>
> err = ceph_mdsc_do_request(fsc->mdsc, NULL, req);
> 15 561 fs/ceph/export.c <<ceph_get_name>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 16 339 fs/ceph/file.c <<ceph_renew_caps>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 17 434 fs/ceph/file.c <<ceph_open>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 18 855 fs/ceph/file.c <<ceph_atomic_open>>
> err = ceph_mdsc_do_request(mdsc, (flags & O_CREAT) ? dir :
> NULL, req);
> 19 2715 fs/ceph/inode.c <<__ceph_setattr>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 20 2839 fs/ceph/inode.c <<__ceph_do_getattr>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 21 2883 fs/ceph/inode.c <<ceph_do_getvxattr>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 22 126 fs/ceph/ioctl.c <<ceph_ioctl_set_layout>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 23 171 fs/ceph/ioctl.c <<ceph_ioctl_set_layout_policy>>
> err = ceph_mdsc_do_request(mdsc, inode, req);
> 24 216 fs/ceph/locks.c <<ceph_lock_wait_for_completion>>
> err = ceph_mdsc_do_request(mdsc, inode, intr_req);
> 25 1091 fs/ceph/super.c <<open_root_dentry>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);
> 26 1151 fs/ceph/xattr.c <<ceph_sync_setxattr>>
> err = ceph_mdsc_do_request(mdsc, NULL, req);

Sure, I remember about this point and as far as I mentioned earlier
https://lore.kernel.org/all/[email protected]/
It is a discussional thing, because not all the inode_operations
allows us to get a mount idmapping.

For instance,
lookup, get_link, get_inode_acl, readlink, link, unlink, rmdir,
listxattr, fiemap, update_time, fileattr_get
inode_operations are not provided with an idmapping.

atomic_open also lacks the mnt_idmap argument, but we have a struct
file so we can get an idmapping through it.

As far as I can see from the code:
https://raw.githubusercontent.com/ceph/ceph/main/src/mds/Server.cc
We have Server::check_access calls for all inode_operations, including
the lookup.

It means that with the current VFS we are not able to support MDS
UID/GID-based path restriction with idmapped mounts.
But we can return to it later if someone really wants it.

If I understand your idea right, you want me to set req->r_mnt_idmap
to an actual idmapping everywhere, where it is possible,
and ignore inode_operations where we have no idmapping passed?

Christian's approach was more conservative here, his idea was to pass
an idmapping only to the operations that are creating
some nodes on the filesystem, but pass a "nop_mnt_idmap" to everyone else.

So, I'll try to set up MDS UID/GID-based path restriction on my local
environment and reproduce the issue with it,
but as I mentioned earlier we can't support it right now anyway. But
as we already have an idmappings supported for most
of existing filesystems, having it supported for cephfs would be great
(even with this limitation about MDS UID/GID-based path restriction),
because we already have a real world use cases for cephfs idmapped
mounts and this particular patchset is used by LXD/LXC for more that a
year.
We can extend this later, if someone really wants to use this
combination and once we extend the VFS layer.

>
>
> And also could you squash the similar commit into one ?

Sure, you mean commits that do `req->r_mnt_idmap =
mnt_idmap_get(idmap)`? Will do.

Big thanks for the fast reaction/review on this series, Xiubo!

>

Kind regards,
Alex

>
> Thanks
>
> - Xiubo
>
>
> On 6/8/23 02:09, Alexander Mikhalitsyn wrote:
> > Dear friends,
> >
> > This patchset was originally developed by Christian Brauner but I'll continue
> > to push it forward. Christian allowed me to do that :)
> >
> > This feature is already actively used/tested with LXD/LXC project.
> >
> > Git tree (based on https://github.com/ceph/ceph-client.git master):
> > https://github.com/mihalicyn/linux/tree/fs.idmapped.ceph
> >
> > In the version 3 I've changed only two commits:
> > - fs: export mnt_idmap_get/mnt_idmap_put
> > - ceph: allow idmapped setattr inode op
> > and added a new one:
> > - ceph: pass idmap to __ceph_setattr
> >
> > In the version 4 I've reworked the ("ceph: stash idmapping in mdsc request")
> > commit. Now we take idmap refcounter just in place where req->r_mnt_idmap
> > is filled. It's more safer approach and prevents possible refcounter underflow
> > on error paths where __register_request wasn't called but ceph_mdsc_release_request is
> > called.
> >
> > I can confirm that this version passes xfstests.
> >
> > Links to previous versions:
> > v1: https://lore.kernel.org/all/[email protected]/
> > v2: https://lore.kernel.org/lkml/[email protected]/
> > v3: https://lore.kernel.org/lkml/[email protected]/#t
> >
> > Kind regards,
> > Alex
> >
> > Original description from Christian:
> > ========================================================================
> > This patch series enables cephfs to support idmapped mounts, i.e. the
> > ability to alter ownership information on a per-mount basis.
> >
> > Container managers such as LXD support sharaing data via cephfs between
> > the host and unprivileged containers and between unprivileged containers.
> > They may all use different idmappings. Idmapped mounts can be used to
> > create mounts with the idmapping used for the container (or a different
> > one specific to the use-case).
> >
> > There are in fact more use-cases such as remapping ownership for
> > mountpoints on the host itself to grant or restrict access to different
> > users or to make it possible to enforce that programs running as root
> > will write with a non-zero {g,u}id to disk.
> >
> > The patch series is simple overall and few changes are needed to cephfs.
> > There is one cephfs specific issue that I would like to discuss and
> > solve which I explain in detail in:
> >
> > [PATCH 02/12] ceph: handle idmapped mounts in create_request_message()
> >
> > It has to do with how to handle mds serves which have id-based access
> > restrictions configured. I would ask you to please take a look at the
> > explanation in the aforementioned patch.
> >
> > The patch series passes the vfs and idmapped mount testsuite as part of
> > xfstests. To run it you will need a config like:
> >
> > [ceph]
> > export FSTYP=ceph
> > export TEST_DIR=/mnt/test
> > export TEST_DEV=10.103.182.10:6789:/
> > export TEST_FS_MOUNT_OPTS="-o name=admin,secret=$password
> >
> > and then simply call
> >
> > sudo ./check -g idmapped
> >
> > ========================================================================
> >
> > Alexander Mikhalitsyn (2):
> > fs: export mnt_idmap_get/mnt_idmap_put
> > ceph: pass idmap to __ceph_setattr
> >
> > Christian Brauner (12):
> > ceph: stash idmapping in mdsc request
> > ceph: handle idmapped mounts in create_request_message()
> > ceph: allow idmapped mknod inode op
> > ceph: allow idmapped symlink inode op
> > ceph: allow idmapped mkdir inode op
> > ceph: allow idmapped rename inode op
> > ceph: allow idmapped getattr inode op
> > ceph: allow idmapped permission inode op
> > ceph: allow idmapped setattr inode op
> > ceph/acl: allow idmapped set_acl inode op
> > ceph/file: allow idmapped atomic_open inode op
> > ceph: allow idmapped mounts
> >
> > fs/ceph/acl.c | 6 +++---
> > fs/ceph/dir.c | 4 ++++
> > fs/ceph/file.c | 10 ++++++++--
> > fs/ceph/inode.c | 29 +++++++++++++++++------------
> > fs/ceph/mds_client.c | 27 +++++++++++++++++++++++----
> > fs/ceph/mds_client.h | 1 +
> > fs/ceph/super.c | 2 +-
> > fs/ceph/super.h | 3 ++-
> > fs/mnt_idmapping.c | 2 ++
> > include/linux/mnt_idmapping.h | 3 +++
> > 10 files changed, 64 insertions(+), 23 deletions(-)
> >
>