Skip to content

Commit bd9684b

Browse files
committed
cifs: implement get acl method
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 cifs wasn't able to implement get and set acl inode operations because it needs access to the dentry. Now that we extended the set acl inode operation to take a dentry argument and added a new get acl inode operation that takes a dentry argument we can let cifs implement get and set acl inode operations. This is mostly a copy and paste of the codepaths currently used in cifs' posix acl xattr handler. After we have fully implemented the posix acl api and switched the vfs over to it, the cifs specific posix acl xattr handler and associated code will be removed and the code duplication will go away. 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/[email protected] [1] Signed-off-by: Christian Brauner (Microsoft) <[email protected]>
1 parent 7420332 commit bd9684b

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed

fs/cifs/cifsacl.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
#include <linux/string.h>
1414
#include <linux/keyctl.h>
1515
#include <linux/key-type.h>
16+
#include <uapi/linux/posix_acl.h>
1617
#include <keys/user-type.h>
1718
#include "cifspdu.h"
1819
#include "cifsglob.h"
1920
#include "cifsacl.h"
2021
#include "cifsproto.h"
2122
#include "cifs_debug.h"
2223
#include "fs_context.h"
24+
#include "cifs_fs_sb.h"
25+
#include "cifs_unicode.h"
2326

2427
/* security id for everyone/world system group */
2528
static const struct cifs_sid sid_everyone = {
@@ -1668,3 +1671,67 @@ id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 *pnmode,
16681671
kfree(pntsd);
16691672
return rc;
16701673
}
1674+
1675+
struct posix_acl *cifs_get_acl(struct user_namespace *mnt_userns,
1676+
struct dentry *dentry, int type)
1677+
{
1678+
#if defined(CONFIG_CIFS_ALLOW_INSECURE_LEGACY) && defined(CONFIG_CIFS_POSIX)
1679+
struct posix_acl *acl = NULL;
1680+
ssize_t rc = -EOPNOTSUPP;
1681+
unsigned int xid;
1682+
struct super_block *sb = dentry->d_sb;
1683+
struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1684+
struct tcon_link *tlink;
1685+
struct cifs_tcon *pTcon;
1686+
const char *full_path;
1687+
void *page;
1688+
1689+
tlink = cifs_sb_tlink(cifs_sb);
1690+
if (IS_ERR(tlink))
1691+
return ERR_CAST(tlink);
1692+
pTcon = tlink_tcon(tlink);
1693+
1694+
xid = get_xid();
1695+
page = alloc_dentry_path();
1696+
1697+
full_path = build_path_from_dentry(dentry, page);
1698+
if (IS_ERR(full_path)) {
1699+
acl = ERR_CAST(full_path);
1700+
goto out;
1701+
}
1702+
1703+
/* return alt name if available as pseudo attr */
1704+
switch (type) {
1705+
case ACL_TYPE_ACCESS:
1706+
if (sb->s_flags & SB_POSIXACL)
1707+
rc = cifs_do_get_acl(xid, pTcon, full_path, &acl,
1708+
ACL_TYPE_ACCESS,
1709+
cifs_sb->local_nls,
1710+
cifs_remap(cifs_sb));
1711+
break;
1712+
1713+
case ACL_TYPE_DEFAULT:
1714+
if (sb->s_flags & SB_POSIXACL)
1715+
rc = cifs_do_get_acl(xid, pTcon, full_path, &acl,
1716+
ACL_TYPE_DEFAULT,
1717+
cifs_sb->local_nls,
1718+
cifs_remap(cifs_sb));
1719+
break;
1720+
}
1721+
1722+
if (rc < 0) {
1723+
if (rc == -EINVAL)
1724+
acl = ERR_PTR(-EOPNOTSUPP);
1725+
else
1726+
acl = ERR_PTR(rc);
1727+
}
1728+
1729+
out:
1730+
free_dentry_path(page);
1731+
free_xid(xid);
1732+
cifs_put_tlink(tlink);
1733+
return acl;
1734+
#else
1735+
return ERR_PTR(-EOPNOTSUPP);
1736+
#endif
1737+
}

fs/cifs/cifsfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ const struct inode_operations cifs_dir_inode_ops = {
11331133
.symlink = cifs_symlink,
11341134
.mknod = cifs_mknod,
11351135
.listxattr = cifs_listxattr,
1136+
.get_acl = cifs_get_acl,
11361137
};
11371138

11381139
const struct inode_operations cifs_file_inode_ops = {
@@ -1141,6 +1142,7 @@ const struct inode_operations cifs_file_inode_ops = {
11411142
.permission = cifs_permission,
11421143
.listxattr = cifs_listxattr,
11431144
.fiemap = cifs_fiemap,
1145+
.get_acl = cifs_get_acl,
11441146
};
11451147

11461148
const struct inode_operations cifs_symlink_inode_ops = {

fs/cifs/cifsproto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ extern struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *, struct inode *,
224224
const char *, u32 *, u32);
225225
extern struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *,
226226
const struct cifs_fid *, u32 *, u32);
227+
extern struct posix_acl *cifs_get_acl(struct user_namespace *mnt_userns,
228+
struct dentry *dentry, int type);
227229
extern int set_cifs_acl(struct cifs_ntsd *, __u32, struct inode *,
228230
const char *, int);
229231
extern unsigned int setup_authusers_ACE(struct cifs_ace *pace);
@@ -541,6 +543,10 @@ extern int CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
541543
const unsigned char *searchName,
542544
char *acl_inf, const int buflen, const int acl_type,
543545
const struct nls_table *nls_codepage, int remap_special_chars);
546+
extern int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
547+
const unsigned char *searchName,
548+
struct posix_acl **acl, const int acl_type,
549+
const struct nls_table *nls_codepage, int remap);
544550
extern int CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
545551
const unsigned char *fileName,
546552
const char *local_acl, const int buflen, const int acl_type,

fs/cifs/cifssmb.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,6 +3212,202 @@ CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
32123212
return rc;
32133213
}
32143214

3215+
#ifdef CONFIG_FS_POSIX_ACL
3216+
/**
3217+
* cifs_init_posix_acl - convert ACL from cifs to POSIX ACL format
3218+
* @ace: POSIX ACL entry to store converted ACL into
3219+
* @cifs_ace: ACL in cifs format
3220+
*
3221+
* Convert an Access Control Entry from wire format to local POSIX xattr
3222+
* format.
3223+
*
3224+
* Note that the @cifs_uid member is used to store both {g,u}id_t.
3225+
*/
3226+
static void cifs_init_posix_acl(struct posix_acl_entry *ace,
3227+
struct cifs_posix_ace *cifs_ace)
3228+
{
3229+
/* u8 cifs fields do not need le conversion */
3230+
ace->e_perm = cifs_ace->cifs_e_perm;
3231+
ace->e_tag = cifs_ace->cifs_e_tag;
3232+
3233+
switch (ace->e_tag) {
3234+
case ACL_USER:
3235+
ace->e_uid = make_kuid(&init_user_ns,
3236+
le64_to_cpu(cifs_ace->cifs_uid));
3237+
break;
3238+
case ACL_GROUP:
3239+
ace->e_gid = make_kgid(&init_user_ns,
3240+
le64_to_cpu(cifs_ace->cifs_uid));
3241+
break;
3242+
}
3243+
return;
3244+
}
3245+
3246+
/**
3247+
* cifs_to_posix_acl - copy cifs ACL format to POSIX ACL format
3248+
* @acl: ACLs returned in POSIX ACL format
3249+
* @src: ACLs in cifs format
3250+
* @acl_type: type of POSIX ACL requested
3251+
* @size_of_data_area: size of SMB we got
3252+
*
3253+
* This function converts ACLs from cifs format to POSIX ACL format.
3254+
* If @acl is NULL then the size of the buffer required to store POSIX ACLs in
3255+
* their uapi format is returned.
3256+
*/
3257+
static int cifs_to_posix_acl(struct posix_acl **acl, char *src,
3258+
const int acl_type, const int size_of_data_area)
3259+
{
3260+
int size = 0;
3261+
__u16 count;
3262+
struct cifs_posix_ace *pACE;
3263+
struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3264+
struct posix_acl *kacl = NULL;
3265+
struct posix_acl_entry *pa, *pe;
3266+
3267+
if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3268+
return -EOPNOTSUPP;
3269+
3270+
if (acl_type == ACL_TYPE_ACCESS) {
3271+
count = le16_to_cpu(cifs_acl->access_entry_count);
3272+
pACE = &cifs_acl->ace_array[0];
3273+
size = sizeof(struct cifs_posix_acl);
3274+
size += sizeof(struct cifs_posix_ace) * count;
3275+
/* check if we would go beyond end of SMB */
3276+
if (size_of_data_area < size) {
3277+
cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3278+
size_of_data_area, size);
3279+
return -EINVAL;
3280+
}
3281+
} else if (acl_type == ACL_TYPE_DEFAULT) {
3282+
count = le16_to_cpu(cifs_acl->access_entry_count);
3283+
size = sizeof(struct cifs_posix_acl);
3284+
size += sizeof(struct cifs_posix_ace) * count;
3285+
/* skip past access ACEs to get to default ACEs */
3286+
pACE = &cifs_acl->ace_array[count];
3287+
count = le16_to_cpu(cifs_acl->default_entry_count);
3288+
size += sizeof(struct cifs_posix_ace) * count;
3289+
/* check if we would go beyond end of SMB */
3290+
if (size_of_data_area < size)
3291+
return -EINVAL;
3292+
} else {
3293+
/* illegal type */
3294+
return -EINVAL;
3295+
}
3296+
3297+
/* Allocate number of POSIX ACLs to store in VFS format. */
3298+
kacl = posix_acl_alloc(count, GFP_NOFS);
3299+
if (!kacl)
3300+
return -ENOMEM;
3301+
3302+
FOREACH_ACL_ENTRY(pa, kacl, pe) {
3303+
cifs_init_posix_acl(pa, pACE);
3304+
pACE++;
3305+
}
3306+
3307+
*acl = kacl;
3308+
return 0;
3309+
}
3310+
3311+
int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
3312+
const unsigned char *searchName, struct posix_acl **acl,
3313+
const int acl_type, const struct nls_table *nls_codepage,
3314+
int remap)
3315+
{
3316+
/* SMB_QUERY_POSIX_ACL */
3317+
TRANSACTION2_QPI_REQ *pSMB = NULL;
3318+
TRANSACTION2_QPI_RSP *pSMBr = NULL;
3319+
int rc = 0;
3320+
int bytes_returned;
3321+
int name_len;
3322+
__u16 params, byte_count;
3323+
3324+
cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
3325+
3326+
queryAclRetry:
3327+
rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3328+
(void **) &pSMBr);
3329+
if (rc)
3330+
return rc;
3331+
3332+
if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3333+
name_len =
3334+
cifsConvertToUTF16((__le16 *) pSMB->FileName,
3335+
searchName, PATH_MAX, nls_codepage,
3336+
remap);
3337+
name_len++; /* trailing null */
3338+
name_len *= 2;
3339+
pSMB->FileName[name_len] = 0;
3340+
pSMB->FileName[name_len+1] = 0;
3341+
} else {
3342+
name_len = copy_path_name(pSMB->FileName, searchName);
3343+
}
3344+
3345+
params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3346+
pSMB->TotalDataCount = 0;
3347+
pSMB->MaxParameterCount = cpu_to_le16(2);
3348+
/* BB find exact max data count below from sess structure BB */
3349+
pSMB->MaxDataCount = cpu_to_le16(4000);
3350+
pSMB->MaxSetupCount = 0;
3351+
pSMB->Reserved = 0;
3352+
pSMB->Flags = 0;
3353+
pSMB->Timeout = 0;
3354+
pSMB->Reserved2 = 0;
3355+
pSMB->ParameterOffset = cpu_to_le16(
3356+
offsetof(struct smb_com_transaction2_qpi_req,
3357+
InformationLevel) - 4);
3358+
pSMB->DataCount = 0;
3359+
pSMB->DataOffset = 0;
3360+
pSMB->SetupCount = 1;
3361+
pSMB->Reserved3 = 0;
3362+
pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3363+
byte_count = params + 1 /* pad */ ;
3364+
pSMB->TotalParameterCount = cpu_to_le16(params);
3365+
pSMB->ParameterCount = pSMB->TotalParameterCount;
3366+
pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3367+
pSMB->Reserved4 = 0;
3368+
inc_rfc1001_len(pSMB, byte_count);
3369+
pSMB->ByteCount = cpu_to_le16(byte_count);
3370+
3371+
rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3372+
(struct smb_hdr *) pSMBr, &bytes_returned, 0);
3373+
cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3374+
if (rc) {
3375+
cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
3376+
} else {
3377+
/* decode response */
3378+
3379+
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3380+
/* BB also check enough total bytes returned */
3381+
if (rc || get_bcc(&pSMBr->hdr) < 2)
3382+
rc = -EIO; /* bad smb */
3383+
else {
3384+
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3385+
__u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3386+
rc = cifs_to_posix_acl(acl,
3387+
(char *)&pSMBr->hdr.Protocol+data_offset,
3388+
acl_type, count);
3389+
}
3390+
}
3391+
cifs_buf_release(pSMB);
3392+
/*
3393+
* The else branch after SendReceive() doesn't return EAGAIN so if we
3394+
* allocated @acl in cifs_to_posix_acl() we are guaranteed to return
3395+
* here and don't leak POSIX ACLs.
3396+
*/
3397+
if (rc == -EAGAIN)
3398+
goto queryAclRetry;
3399+
return rc;
3400+
}
3401+
#else
3402+
int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
3403+
const unsigned char *searchName, struct posix_acl **acl,
3404+
const int acl_type, const struct nls_table *nls_codepage,
3405+
int remap)
3406+
{
3407+
return -EOPNOTSUPP;
3408+
}
3409+
#endif /* CONFIG_FS_POSIX_ACL */
3410+
32153411
int
32163412
CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
32173413
const int netfid, __u64 *pExtAttrBits, __u64 *pMask)

0 commit comments

Comments
 (0)