History

ceph: handle idmapped mounts in create_request_message()

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. 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. This change uses a new cephfs protocol extension CEPHFS_FEATURE_HAS_OWNER_UIDGID which adds two new fields (owner_{u,g}id) to the request head structure. So, we need to ensure that MDS supports it otherwise we need to fail any IO that comes through an idmapped mount because we can't process it in a proper way. MDS server without such an extension will use caller_{u,g}id fields to set a new inode owner UID/GID which is incorrect because caller_{u,g}id values are unmapped. At the same time we can't map these fields with an idmapping as it can break UID/GID-based permission checks logic on the MDS side. This problem was described with a lot of details at [1], [2]. [1] https://lore.kernel.org/lkml/CAEivzxfw1fHO2TFA4dx3u23ZKK6Q+EThfzuibrhA3RKM=ZOYLg@mail.gmail.com/ [2] https://lore.kernel.org/all/20220104140414.155198-3-brauner@kernel.org/

Christian Brauner committed 3 years ago

Commit 5ccd8531 parent 9c2df223 files · +65 −6Browse files

Unified · Split

3 files
  1. fs/ceph/mds_client.c +52 −4
  2. fs/ceph/mds_client.h +4 −1
  3. include/linux/ceph/ceph_fs.h +9 −1
fs/ceph/mds_client.c+52−4
@@ -2881,6 +2881,17 @@ static void encode_mclientrequest_tail(void **p, · 2880 unmodified lines
28812881 }
28822882 }
28832883
2884+static inline u16 mds_supported_head_version(struct ceph_mds_session *session)
2885+{
2886+ if (!test_bit(CEPHFS_FEATURE_32BITS_RETRY_FWD, &session->s_features))
2887+ return 1;
2888+
2889+ if (!test_bit(CEPHFS_FEATURE_HAS_OWNER_UIDGID, &session->s_features))
2890+ return 2;
2891+
2892+ return CEPH_MDS_REQUEST_HEAD_VERSION;
2893+}
2894+
28842895 static struct ceph_mds_request_head_legacy *
28852896 find_legacy_request_head(void *p, u64 features)
28862897 {
@@ -2902,6 +2913,7 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session, · 15 unmodified lines
29022913 {
29032914 int mds = session->s_mds;
29042915 struct ceph_mds_client *mdsc = session->s_mdsc;
2916+ struct ceph_client *cl = mdsc->fsc->client;
29052917 struct ceph_msg *msg;
29062918 struct ceph_mds_request_head_legacy *lhead;
29072919 const char *path1 = NULL;
@@ -2915,8 +2927,7 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session, · 7 unmodified lines
29152927 void *p, *end;
29162928 int ret;
29172929 bool legacy = !(session->s_con.peer_features & CEPH_FEATURE_FS_BTIME);
2918− bool old_version = !test_bit(CEPHFS_FEATURE_32BITS_RETRY_FWD,
2919− &session->s_features);
2930+ u16 request_head_version = mds_supported_head_version(session);
29202931
29212932 ret = set_request_path_attr(mdsc, req->r_inode, req->r_dentry,
29222933 req->r_parent, req->r_path1, req->r_ino1.ino,
@@ -2957,8 +2968,10 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session, · 34 unmodified lines
29572968 */
29582969 if (legacy)
29592970 len = sizeof(struct ceph_mds_request_head_legacy);
2960− else if (old_version)
2971+ else if (request_head_version == 1)
29612972 len = sizeof(struct ceph_mds_request_head_old);
2973+ else if (request_head_version == 2)
2974+ len = offsetofend(struct ceph_mds_request_head, ext_num_fwd);
29622975 else
29632976 len = sizeof(struct ceph_mds_request_head);
29642977
@@ -3008,6 +3021,18 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session, · 43 unmodified lines
30083021 lhead = find_legacy_request_head(msg->front.iov_base,
30093022 session->s_con.peer_features);
30103023
3024+ if ((req->r_mnt_idmap != &nop_mnt_idmap) &&
3025+ !test_bit(CEPHFS_FEATURE_HAS_OWNER_UIDGID, &session->s_features)) {
3026+ WARN_ON_ONCE(!IS_CEPH_MDS_OP_NEWINODE(req->r_op));
3027+
3028+ pr_err_ratelimited_client(cl,
3029+ "idmapped mount is used and CEPHFS_FEATURE_HAS_OWNER_UIDGID"
3030+ " is not supported by MDS. Fail request with -EIO.\n");
3031+
3032+ ret = -EIO;
3033+ goto out_err;
3034+ }
3035+
30113036 /*
30123037 * The ceph_mds_request_head_legacy didn't contain a version field, and
30133038 * one was added when we moved the message version from 3->4.
@@ -3015,17 +3040,40 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session, · 1 unmodified line
30153040 if (legacy) {
30163041 msg->hdr.version = cpu_to_le16(3);
30173042 p = msg->front.iov_base + sizeof(*lhead);
3018− } else if (old_version) {
3043+ } else if (request_head_version == 1) {
30193044 struct ceph_mds_request_head_old *ohead = msg->front.iov_base;
30203045
30213046 msg->hdr.version = cpu_to_le16(4);
30223047 ohead->version = cpu_to_le16(1);
30233048 p = msg->front.iov_base + sizeof(*ohead);
3049+ } else if (request_head_version == 2) {
3050+ struct ceph_mds_request_head *nhead = msg->front.iov_base;
3051+
3052+ msg->hdr.version = cpu_to_le16(6);
3053+ nhead->version = cpu_to_le16(2);
3054+
3055+ p = msg->front.iov_base + offsetofend(struct ceph_mds_request_head, ext_num_fwd);
30243056 } else {
30253057 struct ceph_mds_request_head *nhead = msg->front.iov_base;
3058+ kuid_t owner_fsuid;
3059+ kgid_t owner_fsgid;
30263060
30273061 msg->hdr.version = cpu_to_le16(6);
30283062 nhead->version = cpu_to_le16(CEPH_MDS_REQUEST_HEAD_VERSION);
3063+ nhead->struct_len = cpu_to_le32(sizeof(struct ceph_mds_request_head));
3064+
3065+ if (IS_CEPH_MDS_OP_NEWINODE(req->r_op)) {
3066+ owner_fsuid = from_vfsuid(req->r_mnt_idmap, &init_user_ns,
3067+ VFSUIDT_INIT(req->r_cred->fsuid));
3068+ owner_fsgid = from_vfsgid(req->r_mnt_idmap, &init_user_ns,
3069+ VFSGIDT_INIT(req->r_cred->fsgid));
3070+ nhead->owner_uid = cpu_to_le32(from_kuid(&init_user_ns, owner_fsuid));
3071+ nhead->owner_gid = cpu_to_le32(from_kgid(&init_user_ns, owner_fsgid));
3072+ } else {
3073+ nhead->owner_uid = cpu_to_le32(-1);
3074+ nhead->owner_gid = cpu_to_le32(-1);
3075+ }
3076+
30293077 p = msg->front.iov_base + sizeof(*nhead);
30303078 }
30313079
fs/ceph/mds_client.h+4−1
@@ -33,8 +33,10 @@ enum ceph_feature_type { · 32 unmodified lines
3333 CEPHFS_FEATURE_NOTIFY_SESSION_STATE,
3434 CEPHFS_FEATURE_OP_GETVXATTR,
3535 CEPHFS_FEATURE_32BITS_RETRY_FWD,
36+ CEPHFS_FEATURE_NEW_SNAPREALM_INFO,
37+ CEPHFS_FEATURE_HAS_OWNER_UIDGID,
3638
37− CEPHFS_FEATURE_MAX = CEPHFS_FEATURE_32BITS_RETRY_FWD,
39+ CEPHFS_FEATURE_MAX = CEPHFS_FEATURE_HAS_OWNER_UIDGID,
3840 };
3941
4042 #define CEPHFS_FEATURES_CLIENT_SUPPORTED { \
@@ -49,6 +51,7 @@ enum ceph_feature_type { · 8 unmodified lines
4951 CEPHFS_FEATURE_NOTIFY_SESSION_STATE, \
5052 CEPHFS_FEATURE_OP_GETVXATTR, \
5153 CEPHFS_FEATURE_32BITS_RETRY_FWD, \
54+ CEPHFS_FEATURE_HAS_OWNER_UIDGID, \
5255 }
5356
5457 /*
include/linux/ceph/ceph_fs.h+9−1
@@ -357,6 +357,11 @@ enum { · 356 unmodified lines
357357 CEPH_MDS_OP_RENAMESNAP = 0x01403,
358358 };
359359
360+#define IS_CEPH_MDS_OP_NEWINODE(op) (op == CEPH_MDS_OP_CREATE || \
361+ op == CEPH_MDS_OP_MKNOD || \
362+ op == CEPH_MDS_OP_MKDIR || \
363+ op == CEPH_MDS_OP_SYMLINK)
364+
360365 extern const char *ceph_mds_op_name(int op);
361366
362367 #define CEPH_SETATTR_MODE (1 << 0)
@@ -497,7 +502,7 @@ struct ceph_mds_request_head_legacy { · 134 unmodified lines
497502 union ceph_mds_request_args args;
498503 } __attribute__ ((packed));
499504
500−#define CEPH_MDS_REQUEST_HEAD_VERSION 2
505+#define CEPH_MDS_REQUEST_HEAD_VERSION 3
501506
502507 struct ceph_mds_request_head_old {
503508 __le16 version; /* struct version */
@@ -528,6 +533,9 @@ struct ceph_mds_request_head { · 24 unmodified lines
528533
529534 __le32 ext_num_retry; /* new count retry attempts */
530535 __le32 ext_num_fwd; /* new count fwd attempts */
536+
537+ __le32 struct_len; /* to store size of struct ceph_mds_request_head */
538+ __le32 owner_uid, owner_gid; /* used for OPs which create inodes */
531539 } __attribute__ ((packed));
532540
533541 /* cap/lease release record */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t