Skip to content

Commit dc1af4c

Browse files
committed
cifs: implement set 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 bd9684b commit dc1af4c

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed

fs/cifs/cifsacl.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <linux/keyctl.h>
1515
#include <linux/key-type.h>
1616
#include <uapi/linux/posix_acl.h>
17+
#include <linux/posix_acl.h>
18+
#include <linux/posix_acl_xattr.h>
1719
#include <keys/user-type.h>
1820
#include "cifspdu.h"
1921
#include "cifsglob.h"
@@ -1735,3 +1737,73 @@ struct posix_acl *cifs_get_acl(struct user_namespace *mnt_userns,
17351737
return ERR_PTR(-EOPNOTSUPP);
17361738
#endif
17371739
}
1740+
1741+
int cifs_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
1742+
struct posix_acl *acl, int type)
1743+
{
1744+
#if defined(CONFIG_CIFS_ALLOW_INSECURE_LEGACY) && defined(CONFIG_CIFS_POSIX)
1745+
int rc = -EOPNOTSUPP;
1746+
unsigned int xid;
1747+
struct super_block *sb = dentry->d_sb;
1748+
struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1749+
struct tcon_link *tlink;
1750+
struct cifs_tcon *pTcon;
1751+
const char *full_path;
1752+
void *page;
1753+
1754+
tlink = cifs_sb_tlink(cifs_sb);
1755+
if (IS_ERR(tlink))
1756+
return PTR_ERR(tlink);
1757+
pTcon = tlink_tcon(tlink);
1758+
1759+
xid = get_xid();
1760+
page = alloc_dentry_path();
1761+
1762+
full_path = build_path_from_dentry(dentry, page);
1763+
if (IS_ERR(full_path)) {
1764+
rc = PTR_ERR(full_path);
1765+
goto out;
1766+
}
1767+
/* return dos attributes as pseudo xattr */
1768+
/* return alt name if available as pseudo attr */
1769+
1770+
/* if proc/fs/cifs/streamstoxattr is set then
1771+
search server for EAs or streams to
1772+
returns as xattrs */
1773+
if (posix_acl_xattr_size(acl->a_count) > CIFSMaxBufSize) {
1774+
cifs_dbg(FYI, "size of EA value too large\n");
1775+
rc = -EOPNOTSUPP;
1776+
goto out;
1777+
}
1778+
1779+
switch (type) {
1780+
case ACL_TYPE_ACCESS:
1781+
if (!acl)
1782+
goto out;
1783+
if (sb->s_flags & SB_POSIXACL)
1784+
rc = cifs_do_set_acl(xid, pTcon, full_path, acl,
1785+
ACL_TYPE_ACCESS,
1786+
cifs_sb->local_nls,
1787+
cifs_remap(cifs_sb));
1788+
break;
1789+
1790+
case ACL_TYPE_DEFAULT:
1791+
if (!acl)
1792+
goto out;
1793+
if (sb->s_flags & SB_POSIXACL)
1794+
rc = cifs_do_set_acl(xid, pTcon, full_path, acl,
1795+
ACL_TYPE_DEFAULT,
1796+
cifs_sb->local_nls,
1797+
cifs_remap(cifs_sb));
1798+
break;
1799+
}
1800+
1801+
out:
1802+
free_dentry_path(page);
1803+
free_xid(xid);
1804+
cifs_put_tlink(tlink);
1805+
return rc;
1806+
#else
1807+
return -EOPNOTSUPP;
1808+
#endif
1809+
}

fs/cifs/cifsfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@ const struct inode_operations cifs_dir_inode_ops = {
11341134
.mknod = cifs_mknod,
11351135
.listxattr = cifs_listxattr,
11361136
.get_acl = cifs_get_acl,
1137+
.set_acl = cifs_set_acl,
11371138
};
11381139

11391140
const struct inode_operations cifs_file_inode_ops = {
@@ -1143,6 +1144,7 @@ const struct inode_operations cifs_file_inode_ops = {
11431144
.listxattr = cifs_listxattr,
11441145
.fiemap = cifs_fiemap,
11451146
.get_acl = cifs_get_acl,
1147+
.set_acl = cifs_set_acl,
11461148
};
11471149

11481150
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
@@ -226,6 +226,8 @@ extern struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *,
226226
const struct cifs_fid *, u32 *, u32);
227227
extern struct posix_acl *cifs_get_acl(struct user_namespace *mnt_userns,
228228
struct dentry *dentry, int type);
229+
extern int cifs_set_acl(struct user_namespace *mnt_userns,
230+
struct dentry *dentry, struct posix_acl *acl, int type);
229231
extern int set_cifs_acl(struct cifs_ntsd *, __u32, struct inode *,
230232
const char *, int);
231233
extern unsigned int setup_authusers_ACE(struct cifs_ace *pace);
@@ -551,6 +553,10 @@ extern int CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
551553
const unsigned char *fileName,
552554
const char *local_acl, const int buflen, const int acl_type,
553555
const struct nls_table *nls_codepage, int remap_special_chars);
556+
extern int cifs_do_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
557+
const unsigned char *fileName,
558+
const struct posix_acl *acl, const int acl_type,
559+
const struct nls_table *nls_codepage, int remap);
554560
extern int CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
555561
const int netfid, __u64 *pExtAttrBits, __u64 *pMask);
556562
#endif /* CIFS_ALLOW_INSECURE_LEGACY */

fs/cifs/cifssmb.c

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,6 +3308,83 @@ static int cifs_to_posix_acl(struct posix_acl **acl, char *src,
33083308
return 0;
33093309
}
33103310

3311+
/**
3312+
* cifs_init_ace - convert ACL entry from POSIX ACL to cifs format
3313+
* @cifs_ace: the cifs ACL entry to store into
3314+
* @local_ace: the POSIX ACL entry to convert
3315+
*/
3316+
static void cifs_init_ace(struct cifs_posix_ace *cifs_ace,
3317+
const struct posix_acl_entry *local_ace)
3318+
{
3319+
cifs_ace->cifs_e_perm = local_ace->e_perm;
3320+
cifs_ace->cifs_e_tag = local_ace->e_tag;
3321+
3322+
switch (local_ace->e_tag) {
3323+
case ACL_USER:
3324+
cifs_ace->cifs_uid =
3325+
cpu_to_le64(from_kuid(&init_user_ns, local_ace->e_uid));
3326+
break;
3327+
case ACL_GROUP:
3328+
cifs_ace->cifs_uid =
3329+
cpu_to_le64(from_kgid(&init_user_ns, local_ace->e_gid));
3330+
break;
3331+
default:
3332+
cifs_ace->cifs_uid = cpu_to_le64(-1);
3333+
}
3334+
}
3335+
3336+
/**
3337+
* posix_acl_to_cifs - convert ACLs from POSIX ACL to cifs format
3338+
* @parm_data: ACLs in cifs format to conver to
3339+
* @acl: ACLs in POSIX ACL format to convert from
3340+
* @acl_type: the type of POSIX ACLs stored in @acl
3341+
*
3342+
* Return: the number cifs ACL entries after conversion
3343+
*/
3344+
static __u16 posix_acl_to_cifs(char *parm_data, const struct posix_acl *acl,
3345+
const int acl_type)
3346+
{
3347+
__u16 rc = 0;
3348+
struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3349+
const struct posix_acl_entry *pa, *pe;
3350+
int count;
3351+
int i = 0;
3352+
3353+
if ((acl == NULL) || (cifs_acl == NULL))
3354+
return 0;
3355+
3356+
count = acl->a_count;
3357+
cifs_dbg(FYI, "setting acl with %d entries\n", count);
3358+
3359+
/*
3360+
* Note that the uapi POSIX ACL version is verified by the VFS and is
3361+
* independent of the cifs ACL version. Changing the POSIX ACL version
3362+
* is a uapi change and if it's changed we will pass down the POSIX ACL
3363+
* version in struct posix_acl from the VFS. For now there's really
3364+
* only one that all filesystems know how to deal with.
3365+
*/
3366+
cifs_acl->version = cpu_to_le16(1);
3367+
if (acl_type == ACL_TYPE_ACCESS) {
3368+
cifs_acl->access_entry_count = cpu_to_le16(count);
3369+
cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
3370+
} else if (acl_type == ACL_TYPE_DEFAULT) {
3371+
cifs_acl->default_entry_count = cpu_to_le16(count);
3372+
cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
3373+
} else {
3374+
cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
3375+
return 0;
3376+
}
3377+
FOREACH_ACL_ENTRY(pa, acl, pe) {
3378+
cifs_init_ace(&cifs_acl->ace_array[i++], pa);
3379+
}
3380+
if (rc == 0) {
3381+
rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3382+
rc += sizeof(struct cifs_posix_acl);
3383+
/* BB add check to make sure ACL does not overflow SMB */
3384+
}
3385+
return rc;
3386+
}
3387+
33113388
int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
33123389
const unsigned char *searchName, struct posix_acl **acl,
33133390
const int acl_type, const struct nls_table *nls_codepage,
@@ -3398,6 +3475,81 @@ int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
33983475
goto queryAclRetry;
33993476
return rc;
34003477
}
3478+
3479+
int cifs_do_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3480+
const unsigned char *fileName, const struct posix_acl *acl,
3481+
const int acl_type, const struct nls_table *nls_codepage,
3482+
int remap)
3483+
{
3484+
struct smb_com_transaction2_spi_req *pSMB = NULL;
3485+
struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3486+
char *parm_data;
3487+
int name_len;
3488+
int rc = 0;
3489+
int bytes_returned = 0;
3490+
__u16 params, byte_count, data_count, param_offset, offset;
3491+
3492+
cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
3493+
setAclRetry:
3494+
rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3495+
(void **) &pSMBr);
3496+
if (rc)
3497+
return rc;
3498+
if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3499+
name_len =
3500+
cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3501+
PATH_MAX, nls_codepage, remap);
3502+
name_len++; /* trailing null */
3503+
name_len *= 2;
3504+
} else {
3505+
name_len = copy_path_name(pSMB->FileName, fileName);
3506+
}
3507+
params = 6 + name_len;
3508+
pSMB->MaxParameterCount = cpu_to_le16(2);
3509+
/* BB find max SMB size from sess */
3510+
pSMB->MaxDataCount = cpu_to_le16(1000);
3511+
pSMB->MaxSetupCount = 0;
3512+
pSMB->Reserved = 0;
3513+
pSMB->Flags = 0;
3514+
pSMB->Timeout = 0;
3515+
pSMB->Reserved2 = 0;
3516+
param_offset = offsetof(struct smb_com_transaction2_spi_req,
3517+
InformationLevel) - 4;
3518+
offset = param_offset + params;
3519+
parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3520+
pSMB->ParameterOffset = cpu_to_le16(param_offset);
3521+
3522+
/* convert to on the wire format for POSIX ACL */
3523+
data_count = posix_acl_to_cifs(parm_data, acl, acl_type);
3524+
3525+
if (data_count == 0) {
3526+
rc = -EOPNOTSUPP;
3527+
goto setACLerrorExit;
3528+
}
3529+
pSMB->DataOffset = cpu_to_le16(offset);
3530+
pSMB->SetupCount = 1;
3531+
pSMB->Reserved3 = 0;
3532+
pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3533+
pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3534+
byte_count = 3 /* pad */ + params + data_count;
3535+
pSMB->DataCount = cpu_to_le16(data_count);
3536+
pSMB->TotalDataCount = pSMB->DataCount;
3537+
pSMB->ParameterCount = cpu_to_le16(params);
3538+
pSMB->TotalParameterCount = pSMB->ParameterCount;
3539+
pSMB->Reserved4 = 0;
3540+
inc_rfc1001_len(pSMB, byte_count);
3541+
pSMB->ByteCount = cpu_to_le16(byte_count);
3542+
rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3543+
(struct smb_hdr *) pSMBr, &bytes_returned, 0);
3544+
if (rc)
3545+
cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
3546+
3547+
setACLerrorExit:
3548+
cifs_buf_release(pSMB);
3549+
if (rc == -EAGAIN)
3550+
goto setAclRetry;
3551+
return rc;
3552+
}
34013553
#else
34023554
int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
34033555
const unsigned char *searchName, struct posix_acl **acl,
@@ -3406,6 +3558,14 @@ int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon,
34063558
{
34073559
return -EOPNOTSUPP;
34083560
}
3561+
3562+
int cifs_do_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3563+
const unsigned char *fileName, const struct posix_acl *acl,
3564+
const int acl_type, const struct nls_table *nls_codepage,
3565+
int remap)
3566+
{
3567+
return -EOPNOTSUPP;
3568+
}
34093569
#endif /* CONFIG_FS_POSIX_ACL */
34103570

34113571
int

0 commit comments

Comments
 (0)