25 #include <linux/slab.h>
26 #include <linux/string.h>
37 static const struct cifs_sid sid_everyone = {
38 1, 1, {0, 0, 0, 0, 0, 1}, {0} };
40 static const struct cifs_sid sid_authusers = {
43 static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
48 shrink_idmap_tree(
struct rb_root *root,
int nr_to_scan,
int *nr_rem,
60 if (nr_to_scan == 0 || *nr_del == nr_to_scan)
85 spin_lock(&siduidlock);
86 shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
87 spin_unlock(&siduidlock);
90 spin_lock(&sidgidlock);
91 shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
92 spin_unlock(&sidgidlock);
95 spin_lock(&uidsidlock);
96 shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
97 spin_unlock(&uidsidlock);
100 spin_lock(&gidsidlock);
101 shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
102 spin_unlock(&gidsidlock);
108 sid_rb_insert(
struct rb_root *root,
unsigned long cid,
120 if (cid > lsidid->
id) {
124 if (cid < lsidid->
id) {
132 (*psidid)->refcount = 0;
134 sprintf((*psidid)->sidstr,
"%s", typestr);
135 strptr = (*psidid)->sidstr +
strlen((*psidid)->sidstr);
141 rb_link_node(&(*psidid)->rbnode, parent, linkto);
146 sid_rb_search(
struct rb_root *root,
unsigned long cid)
153 if (cid > lsidid->
id)
155 else if (cid < lsidid->
id)
164 static struct shrinker cifs_shrinker = {
165 .shrink = cifs_idmap_shrinker,
170 cifs_idmap_key_instantiate(
struct key *
key,
struct key_preparsed_payload *prep)
178 memcpy(payload, prep->data, prep->datalen);
180 key->datalen = prep->datalen;
185 cifs_idmap_key_destroy(
struct key *key)
187 kfree(key->payload.data);
191 .name =
"cifs.idmap",
192 .instantiate = cifs_idmap_key_instantiate,
193 .destroy = cifs_idmap_key_destroy,
199 sid_to_str(
struct cifs_sid *sidptr,
char *sidstr)
208 strptr = sidstr +
strlen(sidstr);
211 strptr = sidstr +
strlen(sidstr);
213 for (i = 0; i < 6; ++
i) {
216 strptr = sidstr +
strlen(sidstr);
222 sprintf(strptr,
"-%ld", saval);
223 strptr = sidstr +
strlen(sidstr);
230 memcpy(dst, src,
sizeof(*dst));
258 cifs_copy_sid(&(*psidid)->sid, sidptr);
260 (*psidid)->refcount = 0;
262 sprintf((*psidid)->sidstr,
"%s", typestr);
263 strptr = (*psidid)->sidstr +
strlen((*psidid)->sidstr);
264 sid_to_str(&(*psidid)->sid, strptr);
269 rb_link_node(&(*psidid)->rbnode, parent, linkto);
295 sidid_pending_wait(
void *
unused)
306 const struct cred *saved_cred;
313 cidlock = &siduidlock;
316 cidlock = &sidgidlock;
322 psidid = sid_rb_search(cidtree, cid);
325 spin_unlock(cidlock);
337 psidid = sid_rb_search(cidtree, cid);
340 spin_unlock(cidlock);
345 sid_rb_insert(cidtree, cid, &psidid,
346 sidtype ==
SIDOWNER ?
"oi:" :
"gi:");
348 spin_unlock(cidlock);
352 spin_unlock(cidlock);
364 cifs_copy_sid(ssid, &psidid->
sid);
377 if (IS_ERR(sidkey)) {
379 cFYI(1,
"%s: Can't map and id to a SID", __func__);
380 }
else if (sidkey->datalen <
sizeof(
struct cifs_sid)) {
382 cFYI(1,
"%s: Downcall contained malformed key "
383 "(datalen=%hu)", __func__, sidkey->datalen);
385 lsid = (
struct cifs_sid *)sidkey->payload.data;
386 cifs_copy_sid(&psidid->
sid, lsid);
387 cifs_copy_sid(ssid, &psidid->
sid);
400 cFYI(1,
"%s: sidid_pending_wait interrupted %d",
406 cifs_copy_sid(ssid, &psidid->
sid);
422 const struct cred *saved_cred;
429 cidlock = &siduidlock;
433 cidlock = &sidgidlock;
439 psidid = id_rb_search(cidtree, psid);
442 spin_unlock(cidlock);
454 psidid = id_rb_search(cidtree, psid);
457 spin_unlock(cidlock);
462 id_rb_insert(cidtree, psid, &psidid,
463 sidtype ==
SIDOWNER ?
"os:" :
"gs:");
465 spin_unlock(cidlock);
469 spin_unlock(cidlock);
493 cFYI(1,
"%s: Can't map SID to an id", __func__);
495 cid = *(
unsigned long *)idkey->payload.value;
509 cFYI(1,
"%s: sidid_pending_wait interrupted %d",
535 cFYI(1,
"Registering the %s key type", cifs_idmap_key_type.name);
548 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
549 KEY_USR_VIEW | KEY_USR_READ,
550 KEY_ALLOC_NOT_IN_QUOTA);
551 if (IS_ERR(keyring)) {
552 ret = PTR_ERR(keyring);
553 goto failed_put_cred;
566 set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
567 cred->thread_keyring = keyring;
582 cFYI(1,
"cifs idmap keyring: %d", key_serial(keyring));
599 cFYI(1,
"Unregistered %s key type", cifs_idmap_key_type.name);
609 spin_lock(&siduidlock);
612 spin_unlock(&siduidlock);
615 spin_lock(&sidgidlock);
618 spin_unlock(&sidgidlock);
621 spin_lock(&uidsidlock);
624 spin_unlock(&uidsidlock);
627 spin_lock(&gidsidlock);
630 spin_unlock(&gidsidlock);
640 if ((!ctsid) || (!cwsid))
652 for (i = 0; i < 6; ++
i) {
664 num_subauth = num_sat < num_saw ? num_sat : num_saw;
682 static void copy_sec_desc(
const struct cifs_ntsd *pntsd,
685 struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
686 struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr;
697 owner_sid_ptr = (
struct cifs_sid *)((
char *)pntsd +
699 nowner_sid_ptr = (
struct cifs_sid *)((
char *)pnntsd + sidsoffset);
700 cifs_copy_sid(nowner_sid_ptr, owner_sid_ptr);
703 group_sid_ptr = (
struct cifs_sid *)((
char *)pntsd +
705 ngroup_sid_ptr = (
struct cifs_sid *)((
char *)pnntsd + sidsoffset +
707 cifs_copy_sid(ngroup_sid_ptr, group_sid_ptr);
745 cERROR(1,
"unknown access control type %d", type);
750 if (flags & GENERIC_ALL) {
755 if ((flags & GENERIC_WRITE) ||
756 ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS))
757 *pmode |= (
S_IWUGO & (*pbits_to_set));
758 if ((flags & GENERIC_READ) ||
760 *pmode |= (
S_IRUGO & (*pbits_to_set));
761 if ((flags & GENERIC_EXECUTE) ||
763 *pmode |= (
S_IXUGO & (*pbits_to_set));
765 cFYI(
DBG2,
"access flags 0x%x mode now 0x%x", flags, *pmode);
794 cFYI(
DBG2,
"mode: 0x%x, access flags now 0x%x", mode, *pace_flags);
807 mode_to_access_flags(nmode, bits, &access_req);
814 for (i = 0; i < 6; i++)
819 size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->
num_subauth * 4);
826 #ifdef CONFIG_CIFS_DEBUG2
827 static void dump_ace(
struct cifs_ace *pace,
char *end_of_acl)
839 cERROR(1,
"ACL too small to parse ACE");
843 num_subauth = pace->
sid.num_subauth;
846 cFYI(1,
"ACE revision %d num_auth %d type %d flags %d size %d",
847 pace->
sid.revision, pace->
sid.num_subauth, pace->
type,
850 cFYI(1,
"ACE sub_auth[%d]: 0x%x", i,
863 static void parse_dacl(
struct cifs_acl *pdacl,
char *end_of_acl,
884 cERROR(1,
"ACL too small to parse DACL");
888 cFYI(
DBG2,
"DACL revision %d size %d num aces %d",
897 acl_base = (
char *)pdacl;
911 cERROR(1,
"DACL memory allocation error");
916 ppace[
i] = (
struct cifs_ace *) (acl_base + acl_size);
917 #ifdef CONFIG_CIFS_DEBUG2
918 dump_ace(ppace[i], end_of_acl);
921 access_flags_to_mode(ppace[i]->access_req,
926 access_flags_to_mode(ppace[i]->access_req,
931 access_flags_to_mode(ppace[i]->access_req,
936 access_flags_to_mode(ppace[i]->access_req,
946 acl_base = (
char *)ppace[i];
957 static int set_chmod_dacl(
struct cifs_acl *pndacl,
struct cifs_sid *pownersid,
965 size += fill_ace_for_sid((
struct cifs_ace *) ((
char *)pnndacl + size),
967 size += fill_ace_for_sid((
struct cifs_ace *)((
char *)pnndacl + size),
969 size += fill_ace_for_sid((
struct cifs_ace *)((
char *)pnndacl + size),
970 &sid_everyone, nmode,
S_IRWXO);
979 static int parse_sid(
struct cifs_sid *psid,
char *end_of_acl)
985 if (end_of_acl < (
char *)psid + 8) {
986 cERROR(1,
"ACL too small to parse SID %p", psid);
991 #ifdef CONFIG_CIFS_DEBUG2
993 cFYI(1,
"SID revision %d num_auth %d",
997 cFYI(1,
"SID sub_auth[%d]: 0x%x ", i,
1013 static int parse_sec_desc(
struct cifs_sb_info *cifs_sb,
1017 struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
1019 char *end_of_acl = ((
char *)pntsd) + acl_len;
1025 owner_sid_ptr = (
struct cifs_sid *)((
char *)pntsd +
1027 group_sid_ptr = (
struct cifs_sid *)((
char *)pntsd +
1031 cFYI(
DBG2,
"revision %d type 0x%x ooffset 0x%x goffset 0x%x "
1032 "sacloffset 0x%x dacloffset 0x%x",
1037 rc = parse_sid(owner_sid_ptr, end_of_acl);
1039 cFYI(1,
"%s: Error %d parsing Owner SID", __func__, rc);
1042 rc = sid_to_id(cifs_sb, owner_sid_ptr, fattr,
SIDOWNER);
1044 cFYI(1,
"%s: Error %d mapping Owner SID to uid", __func__, rc);
1048 rc = parse_sid(group_sid_ptr, end_of_acl);
1050 cFYI(1,
"%s: Error %d mapping Owner SID to gid", __func__, rc);
1053 rc = sid_to_id(cifs_sb, group_sid_ptr, fattr,
SIDGROUP);
1055 cFYI(1,
"%s: Error %d mapping Group SID to gid", __func__, rc);
1060 parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr,
1061 group_sid_ptr, fattr);
1076 struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
1077 struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr;
1082 owner_sid_ptr = (
struct cifs_sid *)((
char *)pntsd +
1084 group_sid_ptr = (
struct cifs_sid *)((
char *)pntsd +
1089 ndacl_ptr = (
struct cifs_acl *)((
char *)pnntsd + ndacloffset);
1091 ndacl_ptr->
size = 0;
1094 rc = set_chmod_dacl(ndacl_ptr, owner_sid_ptr, group_sid_ptr,
1098 copy_sec_desc(pntsd, pnntsd, sidsoffset);
1101 memcpy(pnntsd, pntsd, secdesclen);
1103 owner_sid_ptr = (
struct cifs_sid *)((
char *)pnntsd +
1107 if (!nowner_sid_ptr)
1109 rc = id_to_sid(uid,
SIDOWNER, nowner_sid_ptr);
1111 cFYI(1,
"%s: Mapping error %d for owner id %d",
1113 kfree(nowner_sid_ptr);
1116 cifs_copy_sid(owner_sid_ptr, nowner_sid_ptr);
1117 kfree(nowner_sid_ptr);
1121 group_sid_ptr = (
struct cifs_sid *)((
char *)pnntsd +
1125 if (!ngroup_sid_ptr)
1127 rc = id_to_sid(gid,
SIDGROUP, ngroup_sid_ptr);
1129 cFYI(1,
"%s: Mapping error %d for group id %d",
1131 kfree(ngroup_sid_ptr);
1134 cifs_copy_sid(group_sid_ptr, ngroup_sid_ptr);
1135 kfree(ngroup_sid_ptr);
1152 return ERR_CAST(tlink);
1160 cFYI(1,
"%s: rc = %d ACL len %d", __func__, rc, *pacllen);
1167 const char *
path,
u32 *pacllen)
1172 int rc, create_options = 0;
1178 return ERR_CAST(tlink);
1180 tcon = tlink_tcon(tlink);
1197 cFYI(1,
"%s: rc = %d ACL len %d", __func__, rc, *pacllen);
1214 return get_cifs_acl_by_path(cifs_sb, path, pacllen);
1216 pntsd = get_cifs_acl_by_fid(cifs_sb, open_file->
fid.netfid, pacllen);
1223 struct inode *
inode,
const char *path,
int aclflag)
1227 int rc, access_flags, create_options = 0;
1234 return PTR_ERR(tlink);
1236 tcon = tlink_tcon(tlink);
1251 cERROR(1,
"Unable to open file to set ACL");
1256 cFYI(
DBG2,
"SetCIFSACL rc = %d", rc);
1274 cFYI(
DBG2,
"converting ACL to mode for %s", path);
1277 pntsd = get_cifs_acl_by_fid(cifs_sb, *pfid, &acllen);
1282 if (IS_ERR(pntsd)) {
1283 rc = PTR_ERR(pntsd);
1284 cERROR(1,
"%s: error %d getting sec desc", __func__, rc);
1286 rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr);
1289 cERROR(1,
"parse sec desc failed rc = %d", rc);
1302 __u32 secdesclen = 0;
1306 cFYI(
DBG2,
"set ACL from mode for %s", path);
1314 if (IS_ERR(pntsd)) {
1315 rc = PTR_ERR(pntsd);
1316 cERROR(1,
"%s: error %d getting sec desc", __func__, rc);
1326 cERROR(1,
"Unable to allocate security descriptor");
1331 rc = build_sec_desc(pntsd, pnntsd, secdesclen, nmode, uid, gid,
1334 cFYI(
DBG2,
"build_sec_desc rc: %d", rc);
1340 cFYI(
DBG2,
"set_cifs_acl rc: %d", rc);