Skip to content

Commit

Permalink
ksmbd: fs: port i_{g,u}id_into_vfs{g,u}id() to mnt_idmap
Browse files Browse the repository at this point in the history
Convert to struct mnt_idmap.
Remove legacy file_mnt_user_ns() and mnt_user_ns().

Last cycle we merged the necessary infrastructure in
256c8aed2b42 ("fs: introduce dedicated idmap type for mounts").
This is just the conversion to struct mnt_idmap.

Currently we still pass around the plain namespace that was attached to a
mount. This is in general pretty convenient but it makes it easy to
conflate namespaces that are relevant on the filesystem with namespaces
that are relevent on the mount level. Especially for non-vfs developers
without detailed knowledge in this area this can be a potential source for
bugs.

Once the conversion to struct mnt_idmap is done all helpers down to the
really low-level helpers will take a struct mnt_idmap argument instead of
two namespace arguments. This way it becomes impossible to conflate the two
eliminating the possibility of any bugs. All of the vfs and all filesystems
only operate on struct mnt_idmap.

Acked-by: Dave Chinner <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Signed-off-by: Christian Brauner (Microsoft) <[email protected]>
Signed-off-by: Namjae Jeon <[email protected]>
  • Loading branch information
brauner authored and namjaejeon committed May 8, 2023
1 parent 7ca1856 commit 4352920
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
12 changes: 12 additions & 0 deletions ndr.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ static int ndr_encode_posix_acl_entry(struct ndr *n, struct xattr_smb_acl *acl)
}

int ndr_encode_posix_acl(struct ndr *n,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap,
#else
struct user_namespace *user_ns,
#endif
struct inode *inode,
struct xattr_smb_acl *acl,
struct xattr_smb_acl *def_acl)
Expand Down Expand Up @@ -377,7 +381,11 @@ int ndr_encode_posix_acl(struct ndr *n,

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vfsuid = i_uid_into_vfsuid(idmap, inode);
#else
vfsuid = i_uid_into_vfsuid(user_ns, inode);
#endif
ret = ndr_write_int64(n, from_kuid(&init_user_ns, vfsuid_into_kuid(vfsuid)));
#else
ret = ndr_write_int64(n, from_kuid(&init_user_ns, i_uid_into_mnt(user_ns, inode)));
Expand All @@ -386,7 +394,11 @@ int ndr_encode_posix_acl(struct ndr *n,
return ret;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vfsgid = i_gid_into_vfsgid(idmap, inode);
#else
vfsgid = i_gid_into_vfsgid(user_ns, inode);
#endif
ret = ndr_write_int64(n, from_kgid(&init_user_ns, vfsgid_into_kgid(vfsgid)));
#else
ret = ndr_write_int64(n, from_kgid(&init_user_ns, i_gid_into_mnt(user_ns, inode)));
Expand Down
4 changes: 4 additions & 0 deletions ndr.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ struct ndr {

int ndr_encode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da);
int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
int ndr_encode_posix_acl(struct ndr *n, struct mnt_idmap *idmap,
#else
int ndr_encode_posix_acl(struct ndr *n, struct user_namespace *user_ns,
#endif
struct inode *inode, struct xattr_smb_acl *acl,
struct xattr_smb_acl *def_acl);
int ndr_encode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl);
Expand Down
6 changes: 6 additions & 0 deletions oplock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1863,12 +1863,18 @@ void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp)
{
struct create_posix_rsp *buf;
struct inode *inode = file_inode(fp->filp);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
vfsuid_t vfsuid = i_uid_into_vfsuid(user_ns, inode);
vfsgid_t vfsgid = i_gid_into_vfsgid(user_ns, inode);
#endif
#endif

buf = (struct create_posix_rsp *)cc;
Expand Down
25 changes: 24 additions & 1 deletion smb2pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2510,13 +2510,22 @@ static int smb2_create_sd_buffer(struct ksmbd_work *work,
}

static void ksmbd_acls_fattr(struct smb_fattr *fattr,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap,
#else
struct user_namespace *mnt_userns,
#endif
struct inode *inode)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
#else
vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
#endif

fattr->cf_uid = vfsuid_into_kuid(vfsuid);
fattr->cf_gid = vfsgid_into_kgid(vfsgid);
Expand Down Expand Up @@ -3079,7 +3088,11 @@ int smb2_open(struct ksmbd_work *work)
struct smb_ntsd *pntsd;
int pntsd_size, ace_num = 0;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
ksmbd_acls_fattr(&fattr, idmap, inode);
#else
ksmbd_acls_fattr(&fattr, user_ns, inode);
#endif
if (fattr.cf_acls)
ace_num = fattr.cf_acls->a_count;
if (fattr.cf_dacls)
Expand Down Expand Up @@ -4955,14 +4968,20 @@ static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
{
struct smb311_posix_qinfo *file_info;
struct inode *inode = file_inode(fp->filp);
u64 time;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
vfsuid_t vfsuid = i_uid_into_vfsuid(user_ns, inode);
vfsgid_t vfsgid = i_gid_into_vfsgid(user_ns, inode);
#endif
#endif
u64 time;
int out_buf_len = sizeof(struct smb311_posix_qinfo) + 32;

file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
Expand Down Expand Up @@ -5439,7 +5458,11 @@ static int smb2_get_info_sec(struct ksmbd_work *work,
user_ns = file_mnt_user_ns(fp->filp);
#endif
inode = file_inode(fp->filp);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
ksmbd_acls_fattr(&fattr, idmap, inode);
#else
ksmbd_acls_fattr(&fattr, user_ns, inode);
#endif

if (test_share_config_flag(work->tcon->share_conf,
KSMBD_SHARE_FLAG_ACL_XATTR))
Expand Down
8 changes: 8 additions & 0 deletions vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,11 @@ int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
ACL_TYPE_DEFAULT);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode,
#else
rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode,
#endif
smb_acl, def_smb_acl);
if (rc) {
pr_err("failed to encode ndr to posix acl\n");
Expand Down Expand Up @@ -1873,7 +1877,11 @@ int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
ACL_TYPE_DEFAULT);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, smb_acl,
#else
rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode, smb_acl,
#endif
def_smb_acl);
if (rc) {
pr_err("failed to encode ndr to posix acl\n");
Expand Down

0 comments on commit 4352920

Please sign in to comment.