Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/cifsacl.c |
| 3 | * |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2007,2008 |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * Contains the routines for mapping CIFS/NTFS ACLs |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 24 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 26 | #include <linux/string.h> |
| 27 | #include <linux/keyctl.h> |
| 28 | #include <linux/key-type.h> |
| 29 | #include <keys/user-type.h> |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 30 | #include "cifspdu.h" |
| 31 | #include "cifsglob.h" |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 32 | #include "cifsacl.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 33 | #include "cifsproto.h" |
| 34 | #include "cifs_debug.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 35 | |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 36 | /* security id for everyone/world system group */ |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 37 | static const struct cifs_sid sid_everyone = { |
| 38 | 1, 1, {0, 0, 0, 0, 0, 1}, {0} }; |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 39 | /* security id for Authenticated Users system group */ |
| 40 | static const struct cifs_sid sid_authusers = { |
Steve French | 4f61258 | 2011-05-27 20:40:18 +0000 | [diff] [blame] | 41 | 1, 1, {0, 0, 0, 0, 0, 5}, {__constant_cpu_to_le32(11)} }; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 42 | /* group users */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 43 | static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} }; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 44 | |
Jeff Layton | b1a6dc2 | 2012-11-25 08:00:38 -0500 | [diff] [blame] | 45 | static const struct cred *root_cred; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 46 | |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 47 | static int |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 48 | cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep) |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 49 | { |
| 50 | char *payload; |
| 51 | |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 52 | payload = kmalloc(prep->datalen, GFP_KERNEL); |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 53 | if (!payload) |
| 54 | return -ENOMEM; |
| 55 | |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 56 | memcpy(payload, prep->data, prep->datalen); |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 57 | key->payload.data = payload; |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 58 | key->datalen = prep->datalen; |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static inline void |
| 63 | cifs_idmap_key_destroy(struct key *key) |
| 64 | { |
| 65 | kfree(key->payload.data); |
| 66 | } |
| 67 | |
Jeff Layton | b1a6dc2 | 2012-11-25 08:00:38 -0500 | [diff] [blame] | 68 | static struct key_type cifs_idmap_key_type = { |
Shirish Pargaonkar | c4aca0c | 2011-05-06 02:35:00 -0500 | [diff] [blame] | 69 | .name = "cifs.idmap", |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 70 | .instantiate = cifs_idmap_key_instantiate, |
| 71 | .destroy = cifs_idmap_key_destroy, |
| 72 | .describe = user_describe, |
| 73 | .match = user_match, |
| 74 | }; |
| 75 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 76 | static char * |
| 77 | sid_to_key_str(struct cifs_sid *sidptr, unsigned int type) |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 78 | { |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 79 | int i, len; |
Jeff Layton | ee13b2b | 2012-11-25 08:00:38 -0500 | [diff] [blame] | 80 | unsigned int saval; |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 81 | char *sidstr, *strptr; |
| 82 | |
| 83 | /* 3 bytes for prefix */ |
| 84 | sidstr = kmalloc(3 + SID_STRING_BASE_SIZE + |
| 85 | (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth), |
| 86 | GFP_KERNEL); |
| 87 | if (!sidstr) |
| 88 | return sidstr; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 89 | |
| 90 | strptr = sidstr; |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 91 | len = sprintf(strptr, "%cs:S-%hhu", type == SIDOWNER ? 'o' : 'g', |
| 92 | sidptr->revision); |
| 93 | strptr += len; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 94 | |
Jeff Layton | 852e229 | 2012-11-25 08:00:36 -0500 | [diff] [blame] | 95 | for (i = 0; i < NUM_AUTHS; ++i) { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 96 | if (sidptr->authority[i]) { |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 97 | len = sprintf(strptr, "-%hhu", sidptr->authority[i]); |
| 98 | strptr += len; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < sidptr->num_subauth; ++i) { |
| 103 | saval = le32_to_cpu(sidptr->sub_auth[i]); |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 104 | len = sprintf(strptr, "-%u", saval); |
| 105 | strptr += len; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 106 | } |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 107 | |
| 108 | return sidstr; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Jeff Layton | 436bb43 | 2012-11-25 08:00:36 -0500 | [diff] [blame] | 111 | /* |
| 112 | * if the two SIDs (roughly equivalent to a UUID for a user or group) are |
| 113 | * the same returns zero, if they do not match returns non-zero. |
| 114 | */ |
| 115 | static int |
| 116 | compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid) |
| 117 | { |
| 118 | int i; |
| 119 | int num_subauth, num_sat, num_saw; |
| 120 | |
| 121 | if ((!ctsid) || (!cwsid)) |
| 122 | return 1; |
| 123 | |
| 124 | /* compare the revision */ |
| 125 | if (ctsid->revision != cwsid->revision) { |
| 126 | if (ctsid->revision > cwsid->revision) |
| 127 | return 1; |
| 128 | else |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | /* compare all of the six auth values */ |
| 133 | for (i = 0; i < NUM_AUTHS; ++i) { |
| 134 | if (ctsid->authority[i] != cwsid->authority[i]) { |
| 135 | if (ctsid->authority[i] > cwsid->authority[i]) |
| 136 | return 1; |
| 137 | else |
| 138 | return -1; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* compare all of the subauth values if any */ |
| 143 | num_sat = ctsid->num_subauth; |
| 144 | num_saw = cwsid->num_subauth; |
| 145 | num_subauth = num_sat < num_saw ? num_sat : num_saw; |
| 146 | if (num_subauth) { |
| 147 | for (i = 0; i < num_subauth; ++i) { |
| 148 | if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) { |
| 149 | if (le32_to_cpu(ctsid->sub_auth[i]) > |
| 150 | le32_to_cpu(cwsid->sub_auth[i])) |
| 151 | return 1; |
| 152 | else |
| 153 | return -1; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return 0; /* sids compare/match */ |
| 159 | } |
| 160 | |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 161 | static void |
Jeff Layton | 36960e4 | 2012-11-03 09:37:28 -0400 | [diff] [blame] | 162 | cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src) |
| 163 | { |
Jeff Layton | 36f87ee | 2012-11-25 08:00:37 -0500 | [diff] [blame] | 164 | int i; |
| 165 | |
| 166 | dst->revision = src->revision; |
Jeff Layton | 30c9d6c | 2012-11-25 08:00:37 -0500 | [diff] [blame] | 167 | dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES); |
Jeff Layton | 36f87ee | 2012-11-25 08:00:37 -0500 | [diff] [blame] | 168 | for (i = 0; i < NUM_AUTHS; ++i) |
| 169 | dst->authority[i] = src->authority[i]; |
| 170 | for (i = 0; i < dst->num_subauth; ++i) |
| 171 | dst->sub_auth[i] = src->sub_auth[i]; |
Jeff Layton | 36960e4 | 2012-11-03 09:37:28 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 174 | static int |
| 175 | id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid) |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 176 | { |
| 177 | int rc; |
Shirish Pargaonkar | 21fed0d | 2011-08-09 14:30:48 -0500 | [diff] [blame] | 178 | struct key *sidkey; |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 179 | char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */ |
Shirish Pargaonkar | 21fed0d | 2011-08-09 14:30:48 -0500 | [diff] [blame] | 180 | const struct cred *saved_cred; |
Shirish Pargaonkar | 21fed0d | 2011-08-09 14:30:48 -0500 | [diff] [blame] | 181 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 182 | rc = snprintf(desc, sizeof(desc), "%ci:%u", |
| 183 | sidtype == SIDOWNER ? 'o' : 'g', cid); |
| 184 | if (rc >= sizeof(desc)) |
Shirish Pargaonkar | 21fed0d | 2011-08-09 14:30:48 -0500 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 187 | rc = 0; |
| 188 | saved_cred = override_creds(root_cred); |
| 189 | sidkey = request_key(&cifs_idmap_key_type, desc, ""); |
| 190 | if (IS_ERR(sidkey)) { |
Shirish Pargaonkar | 21fed0d | 2011-08-09 14:30:48 -0500 | [diff] [blame] | 191 | rc = -EINVAL; |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 192 | cFYI(1, "%s: Can't map %cid %u to a SID", __func__, |
| 193 | sidtype == SIDOWNER ? 'u' : 'g', cid); |
| 194 | goto out_revert_creds; |
| 195 | } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) { |
| 196 | rc = -EIO; |
| 197 | cFYI(1, "%s: Downcall contained malformed key " |
| 198 | "(datalen=%hu)", __func__, sidkey->datalen); |
| 199 | goto out_key_put; |
Shirish Pargaonkar | 21fed0d | 2011-08-09 14:30:48 -0500 | [diff] [blame] | 200 | } |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 201 | cifs_copy_sid(ssid, (struct cifs_sid *)sidkey->payload.data); |
| 202 | out_key_put: |
| 203 | key_put(sidkey); |
| 204 | out_revert_creds: |
| 205 | revert_creds(saved_cred); |
Shirish Pargaonkar | 21fed0d | 2011-08-09 14:30:48 -0500 | [diff] [blame] | 206 | return rc; |
| 207 | } |
| 208 | |
| 209 | static int |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 210 | sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid, |
| 211 | struct cifs_fattr *fattr, uint sidtype) |
| 212 | { |
| 213 | int rc; |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 214 | struct key *sidkey; |
| 215 | char *sidstr; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 216 | const struct cred *saved_cred; |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 217 | uid_t fuid = cifs_sb->mnt_uid; |
| 218 | gid_t fgid = cifs_sb->mnt_gid; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 219 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 220 | /* |
| 221 | * If we have too many subauthorities, then something is really wrong. |
| 222 | * Just return an error. |
| 223 | */ |
| 224 | if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) { |
| 225 | cFYI(1, "%s: %u subauthorities is too many!", __func__, |
| 226 | psid->num_subauth); |
| 227 | return -EIO; |
| 228 | } |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 229 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 230 | sidstr = sid_to_key_str(psid, sidtype); |
| 231 | if (!sidstr) |
| 232 | return -ENOMEM; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 233 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 234 | saved_cred = override_creds(root_cred); |
| 235 | sidkey = request_key(&cifs_idmap_key_type, sidstr, ""); |
| 236 | if (IS_ERR(sidkey)) { |
| 237 | rc = -EINVAL; |
| 238 | cFYI(1, "%s: Can't map SID %s to a %cid", __func__, sidstr, |
| 239 | sidtype == SIDOWNER ? 'u' : 'g'); |
| 240 | goto out_revert_creds; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 244 | * FIXME: Here we assume that uid_t and gid_t are same size. It's |
| 245 | * probably a safe assumption but might be better to check based on |
| 246 | * sidtype. |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 247 | */ |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 248 | if (sidkey->datalen < sizeof(uid_t)) { |
| 249 | rc = -EIO; |
| 250 | cFYI(1, "%s: Downcall contained malformed key " |
| 251 | "(datalen=%hu)", __func__, sidkey->datalen); |
| 252 | goto out_key_put; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 255 | if (sidtype == SIDOWNER) |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 256 | fuid = *(uid_t *)sidkey->payload.value; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 257 | else |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 258 | fgid = *(gid_t *)sidkey->payload.value; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 259 | |
Jeff Layton | faa65f0 | 2012-12-03 06:05:29 -0500 | [diff] [blame^] | 260 | out_key_put: |
| 261 | key_put(sidkey); |
| 262 | out_revert_creds: |
| 263 | revert_creds(saved_cred); |
| 264 | kfree(sidstr); |
| 265 | |
| 266 | /* |
| 267 | * Note that we return 0 here unconditionally. If the mapping |
| 268 | * fails then we just fall back to using the mnt_uid/mnt_gid. |
| 269 | */ |
| 270 | if (sidtype == SIDOWNER) |
| 271 | fattr->cf_uid = fuid; |
| 272 | else |
| 273 | fattr->cf_gid = fgid; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 277 | int |
| 278 | init_cifs_idmap(void) |
| 279 | { |
| 280 | struct cred *cred; |
| 281 | struct key *keyring; |
| 282 | int ret; |
| 283 | |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 284 | cFYI(1, "Registering the %s key type", cifs_idmap_key_type.name); |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 285 | |
| 286 | /* create an override credential set with a special thread keyring in |
| 287 | * which requests are cached |
| 288 | * |
| 289 | * this is used to prevent malicious redirections from being installed |
| 290 | * with add_key(). |
| 291 | */ |
| 292 | cred = prepare_kernel_cred(NULL); |
| 293 | if (!cred) |
| 294 | return -ENOMEM; |
| 295 | |
| 296 | keyring = key_alloc(&key_type_keyring, ".cifs_idmap", 0, 0, cred, |
| 297 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | |
| 298 | KEY_USR_VIEW | KEY_USR_READ, |
| 299 | KEY_ALLOC_NOT_IN_QUOTA); |
| 300 | if (IS_ERR(keyring)) { |
| 301 | ret = PTR_ERR(keyring); |
| 302 | goto failed_put_cred; |
| 303 | } |
| 304 | |
| 305 | ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL); |
| 306 | if (ret < 0) |
| 307 | goto failed_put_key; |
| 308 | |
| 309 | ret = register_key_type(&cifs_idmap_key_type); |
| 310 | if (ret < 0) |
| 311 | goto failed_put_key; |
| 312 | |
| 313 | /* instruct request_key() to use this special keyring as a cache for |
| 314 | * the results it looks up */ |
David Howells | 700920e | 2012-01-18 15:31:45 +0000 | [diff] [blame] | 315 | set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags); |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 316 | cred->thread_keyring = keyring; |
| 317 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; |
| 318 | root_cred = cred; |
| 319 | |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 320 | cFYI(1, "cifs idmap keyring: %d", key_serial(keyring)); |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 321 | return 0; |
| 322 | |
| 323 | failed_put_key: |
| 324 | key_put(keyring); |
| 325 | failed_put_cred: |
| 326 | put_cred(cred); |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | exit_cifs_idmap(void) |
| 332 | { |
| 333 | key_revoke(root_cred->thread_keyring); |
| 334 | unregister_key_type(&cifs_idmap_key_type); |
| 335 | put_cred(root_cred); |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 336 | cFYI(1, "Unregistered %s key type", cifs_idmap_key_type.name); |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 337 | } |
| 338 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 339 | /* copy ntsd, owner sid, and group sid from a security descriptor to another */ |
| 340 | static void copy_sec_desc(const struct cifs_ntsd *pntsd, |
| 341 | struct cifs_ntsd *pnntsd, __u32 sidsoffset) |
| 342 | { |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 343 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 344 | struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr; |
| 345 | |
| 346 | /* copy security descriptor control portion */ |
| 347 | pnntsd->revision = pntsd->revision; |
| 348 | pnntsd->type = pntsd->type; |
| 349 | pnntsd->dacloffset = cpu_to_le32(sizeof(struct cifs_ntsd)); |
| 350 | pnntsd->sacloffset = 0; |
| 351 | pnntsd->osidoffset = cpu_to_le32(sidsoffset); |
| 352 | pnntsd->gsidoffset = cpu_to_le32(sidsoffset + sizeof(struct cifs_sid)); |
| 353 | |
| 354 | /* copy owner sid */ |
| 355 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 356 | le32_to_cpu(pntsd->osidoffset)); |
| 357 | nowner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset); |
Jeff Layton | 36960e4 | 2012-11-03 09:37:28 -0400 | [diff] [blame] | 358 | cifs_copy_sid(nowner_sid_ptr, owner_sid_ptr); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 359 | |
| 360 | /* copy group sid */ |
| 361 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 362 | le32_to_cpu(pntsd->gsidoffset)); |
| 363 | ngroup_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset + |
| 364 | sizeof(struct cifs_sid)); |
Jeff Layton | 36960e4 | 2012-11-03 09:37:28 -0400 | [diff] [blame] | 365 | cifs_copy_sid(ngroup_sid_ptr, group_sid_ptr); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 366 | |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 371 | /* |
| 372 | change posix mode to reflect permissions |
| 373 | pmode is the existing mode (we only want to overwrite part of this |
| 374 | bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007 |
| 375 | */ |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 376 | static void access_flags_to_mode(__le32 ace_flags, int type, umode_t *pmode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 377 | umode_t *pbits_to_set) |
Steve French | 4879b44 | 2007-10-19 21:57:39 +0000 | [diff] [blame] | 378 | { |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 379 | __u32 flags = le32_to_cpu(ace_flags); |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 380 | /* the order of ACEs is important. The canonical order is to begin with |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 381 | DENY entries followed by ALLOW, otherwise an allow entry could be |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 382 | encountered first, making the subsequent deny entry like "dead code" |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 383 | which would be superflous since Windows stops when a match is made |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 384 | for the operation you are trying to perform for your user */ |
| 385 | |
| 386 | /* For deny ACEs we change the mask so that subsequent allow access |
| 387 | control entries do not turn on the bits we are denying */ |
| 388 | if (type == ACCESS_DENIED) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 389 | if (flags & GENERIC_ALL) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 390 | *pbits_to_set &= ~S_IRWXUGO; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 391 | |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 392 | if ((flags & GENERIC_WRITE) || |
| 393 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 394 | *pbits_to_set &= ~S_IWUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 395 | if ((flags & GENERIC_READ) || |
| 396 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 397 | *pbits_to_set &= ~S_IRUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 398 | if ((flags & GENERIC_EXECUTE) || |
| 399 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 400 | *pbits_to_set &= ~S_IXUGO; |
| 401 | return; |
| 402 | } else if (type != ACCESS_ALLOWED) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 403 | cERROR(1, "unknown access control type %d", type); |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | /* else ACCESS_ALLOWED type */ |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 407 | |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 408 | if (flags & GENERIC_ALL) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 409 | *pmode |= (S_IRWXUGO & (*pbits_to_set)); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 410 | cFYI(DBG2, "all perms"); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 411 | return; |
| 412 | } |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 413 | if ((flags & GENERIC_WRITE) || |
| 414 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 415 | *pmode |= (S_IWUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 416 | if ((flags & GENERIC_READ) || |
| 417 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 418 | *pmode |= (S_IRUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 419 | if ((flags & GENERIC_EXECUTE) || |
| 420 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 421 | *pmode |= (S_IXUGO & (*pbits_to_set)); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 422 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 423 | cFYI(DBG2, "access flags 0x%x mode now 0x%x", flags, *pmode); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 427 | /* |
| 428 | Generate access flags to reflect permissions mode is the existing mode. |
| 429 | This function is called for every ACE in the DACL whose SID matches |
| 430 | with either owner or group or everyone. |
| 431 | */ |
| 432 | |
| 433 | static void mode_to_access_flags(umode_t mode, umode_t bits_to_use, |
| 434 | __u32 *pace_flags) |
| 435 | { |
| 436 | /* reset access mask */ |
| 437 | *pace_flags = 0x0; |
| 438 | |
| 439 | /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */ |
| 440 | mode &= bits_to_use; |
| 441 | |
| 442 | /* check for R/W/X UGO since we do not know whose flags |
| 443 | is this but we have cleared all the bits sans RWX for |
| 444 | either user or group or other as per bits_to_use */ |
| 445 | if (mode & S_IRUGO) |
| 446 | *pace_flags |= SET_FILE_READ_RIGHTS; |
| 447 | if (mode & S_IWUGO) |
| 448 | *pace_flags |= SET_FILE_WRITE_RIGHTS; |
| 449 | if (mode & S_IXUGO) |
| 450 | *pace_flags |= SET_FILE_EXEC_RIGHTS; |
| 451 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 452 | cFYI(DBG2, "mode: 0x%x, access flags now 0x%x", mode, *pace_flags); |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Al Viro | 2b210ad | 2008-03-29 03:09:18 +0000 | [diff] [blame] | 456 | static __u16 fill_ace_for_sid(struct cifs_ace *pntace, |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 457 | const struct cifs_sid *psid, __u64 nmode, umode_t bits) |
| 458 | { |
| 459 | int i; |
| 460 | __u16 size = 0; |
| 461 | __u32 access_req = 0; |
| 462 | |
| 463 | pntace->type = ACCESS_ALLOWED; |
| 464 | pntace->flags = 0x0; |
| 465 | mode_to_access_flags(nmode, bits, &access_req); |
| 466 | if (!access_req) |
| 467 | access_req = SET_MINIMUM_RIGHTS; |
| 468 | pntace->access_req = cpu_to_le32(access_req); |
| 469 | |
| 470 | pntace->sid.revision = psid->revision; |
| 471 | pntace->sid.num_subauth = psid->num_subauth; |
Jeff Layton | 852e229 | 2012-11-25 08:00:36 -0500 | [diff] [blame] | 472 | for (i = 0; i < NUM_AUTHS; i++) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 473 | pntace->sid.authority[i] = psid->authority[i]; |
| 474 | for (i = 0; i < psid->num_subauth; i++) |
| 475 | pntace->sid.sub_auth[i] = psid->sub_auth[i]; |
| 476 | |
| 477 | size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4); |
| 478 | pntace->size = cpu_to_le16(size); |
| 479 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 480 | return size; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 483 | |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 484 | #ifdef CONFIG_CIFS_DEBUG2 |
| 485 | static void dump_ace(struct cifs_ace *pace, char *end_of_acl) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 486 | { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 487 | int num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 488 | |
| 489 | /* validate that we do not go past end of acl */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 490 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 491 | if (le16_to_cpu(pace->size) < 16) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 492 | cERROR(1, "ACE too small %d", le16_to_cpu(pace->size)); |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | |
| 496 | if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 497 | cERROR(1, "ACL too small to parse ACE"); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 498 | return; |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 499 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 500 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 501 | num_subauth = pace->sid.num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 502 | if (num_subauth) { |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 503 | int i; |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 504 | cFYI(1, "ACE revision %d num_auth %d type %d flags %d size %d", |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 505 | pace->sid.revision, pace->sid.num_subauth, pace->type, |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 506 | pace->flags, le16_to_cpu(pace->size)); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 507 | for (i = 0; i < num_subauth; ++i) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 508 | cFYI(1, "ACE sub_auth[%d]: 0x%x", i, |
| 509 | le32_to_cpu(pace->sid.sub_auth[i])); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 510 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 511 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 512 | /* BB add length check to make sure that we do not have huge |
| 513 | num auths and therefore go off the end */ |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 514 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 515 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 516 | return; |
| 517 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 518 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 519 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 520 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 521 | static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl, |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 522 | struct cifs_sid *pownersid, struct cifs_sid *pgrpsid, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 523 | struct cifs_fattr *fattr) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 524 | { |
| 525 | int i; |
| 526 | int num_aces = 0; |
| 527 | int acl_size; |
| 528 | char *acl_base; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 529 | struct cifs_ace **ppace; |
| 530 | |
| 531 | /* BB need to add parm so we can store the SID BB */ |
| 532 | |
Steve French | 2b83457 | 2007-11-25 10:01:00 +0000 | [diff] [blame] | 533 | if (!pdacl) { |
| 534 | /* no DACL in the security descriptor, set |
| 535 | all the permissions for user/group/other */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 536 | fattr->cf_mode |= S_IRWXUGO; |
Steve French | 2b83457 | 2007-11-25 10:01:00 +0000 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 540 | /* validate that we do not go past end of acl */ |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 541 | if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 542 | cERROR(1, "ACL too small to parse DACL"); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 546 | cFYI(DBG2, "DACL revision %d size %d num aces %d", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 547 | le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size), |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 548 | le32_to_cpu(pdacl->num_aces)); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 549 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 550 | /* reset rwx permissions for user/group/other. |
| 551 | Also, if num_aces is 0 i.e. DACL has no ACEs, |
| 552 | user/group/other have no permissions */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 553 | fattr->cf_mode &= ~(S_IRWXUGO); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 554 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 555 | acl_base = (char *)pdacl; |
| 556 | acl_size = sizeof(struct cifs_acl); |
| 557 | |
Steve French | adbc035 | 2007-10-17 02:12:46 +0000 | [diff] [blame] | 558 | num_aces = le32_to_cpu(pdacl->num_aces); |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 559 | if (num_aces > 0) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 560 | umode_t user_mask = S_IRWXU; |
| 561 | umode_t group_mask = S_IRWXG; |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 562 | umode_t other_mask = S_IRWXU | S_IRWXG | S_IRWXO; |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 563 | |
Dan Carpenter | 7250170 | 2012-01-11 10:46:27 +0300 | [diff] [blame] | 564 | if (num_aces > ULONG_MAX / sizeof(struct cifs_ace *)) |
| 565 | return; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 566 | ppace = kmalloc(num_aces * sizeof(struct cifs_ace *), |
| 567 | GFP_KERNEL); |
Stanislav Fomichev | 8132b65 | 2011-02-06 02:05:28 +0300 | [diff] [blame] | 568 | if (!ppace) { |
| 569 | cERROR(1, "DACL memory allocation error"); |
| 570 | return; |
| 571 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 572 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 573 | for (i = 0; i < num_aces; ++i) { |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 574 | ppace[i] = (struct cifs_ace *) (acl_base + acl_size); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 575 | #ifdef CONFIG_CIFS_DEBUG2 |
| 576 | dump_ace(ppace[i], end_of_acl); |
| 577 | #endif |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 578 | if (compare_sids(&(ppace[i]->sid), pownersid) == 0) |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 579 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 580 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 581 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 582 | &user_mask); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 583 | if (compare_sids(&(ppace[i]->sid), pgrpsid) == 0) |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 584 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 585 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 586 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 587 | &group_mask); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 588 | if (compare_sids(&(ppace[i]->sid), &sid_everyone) == 0) |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 589 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 590 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 591 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 592 | &other_mask); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 593 | if (compare_sids(&(ppace[i]->sid), &sid_authusers) == 0) |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 594 | access_flags_to_mode(ppace[i]->access_req, |
| 595 | ppace[i]->type, |
| 596 | &fattr->cf_mode, |
| 597 | &other_mask); |
| 598 | |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 599 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 600 | /* memcpy((void *)(&(cifscred->aces[i])), |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 601 | (void *)ppace[i], |
| 602 | sizeof(struct cifs_ace)); */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 603 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 604 | acl_base = (char *)ppace[i]; |
| 605 | acl_size = le16_to_cpu(ppace[i]->size); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | kfree(ppace); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | return; |
| 612 | } |
| 613 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 614 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 615 | static int set_chmod_dacl(struct cifs_acl *pndacl, struct cifs_sid *pownersid, |
| 616 | struct cifs_sid *pgrpsid, __u64 nmode) |
| 617 | { |
Al Viro | 2b210ad | 2008-03-29 03:09:18 +0000 | [diff] [blame] | 618 | u16 size = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 619 | struct cifs_acl *pnndacl; |
| 620 | |
| 621 | pnndacl = (struct cifs_acl *)((char *)pndacl + sizeof(struct cifs_acl)); |
| 622 | |
| 623 | size += fill_ace_for_sid((struct cifs_ace *) ((char *)pnndacl + size), |
| 624 | pownersid, nmode, S_IRWXU); |
| 625 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 626 | pgrpsid, nmode, S_IRWXG); |
| 627 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 628 | &sid_everyone, nmode, S_IRWXO); |
| 629 | |
| 630 | pndacl->size = cpu_to_le16(size + sizeof(struct cifs_acl)); |
Shirish Pargaonkar | d9f382e | 2008-02-12 20:46:26 +0000 | [diff] [blame] | 631 | pndacl->num_aces = cpu_to_le32(3); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 632 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 633 | return 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 637 | static int parse_sid(struct cifs_sid *psid, char *end_of_acl) |
| 638 | { |
| 639 | /* BB need to add parm so we can store the SID BB */ |
| 640 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 641 | /* validate that we do not go past end of ACL - sid must be at least 8 |
| 642 | bytes long (assuming no sub-auths - e.g. the null SID */ |
| 643 | if (end_of_acl < (char *)psid + 8) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 644 | cERROR(1, "ACL too small to parse SID %p", psid); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 645 | return -EINVAL; |
| 646 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 647 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 648 | #ifdef CONFIG_CIFS_DEBUG2 |
Jeff Layton | fc03d8a | 2012-11-25 08:00:35 -0500 | [diff] [blame] | 649 | if (psid->num_subauth) { |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 650 | int i; |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 651 | cFYI(1, "SID revision %d num_auth %d", |
| 652 | psid->revision, psid->num_subauth); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 653 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 654 | for (i = 0; i < psid->num_subauth; i++) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 655 | cFYI(1, "SID sub_auth[%d]: 0x%x ", i, |
| 656 | le32_to_cpu(psid->sub_auth[i])); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 659 | /* BB add length check to make sure that we do not have huge |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 660 | num auths and therefore go off the end */ |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 661 | cFYI(1, "RID 0x%x", |
| 662 | le32_to_cpu(psid->sub_auth[psid->num_subauth-1])); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 663 | } |
Jeff Layton | fc03d8a | 2012-11-25 08:00:35 -0500 | [diff] [blame] | 664 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 665 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 669 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 670 | /* Convert CIFS ACL to POSIX form */ |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 671 | static int parse_sec_desc(struct cifs_sb_info *cifs_sb, |
| 672 | struct cifs_ntsd *pntsd, int acl_len, struct cifs_fattr *fattr) |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 673 | { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 674 | int rc = 0; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 675 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 676 | struct cifs_acl *dacl_ptr; /* no need for SACL ptr */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 677 | char *end_of_acl = ((char *)pntsd) + acl_len; |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 678 | __u32 dacloffset; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 679 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 680 | if (pntsd == NULL) |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 681 | return -EIO; |
| 682 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 683 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 684 | le32_to_cpu(pntsd->osidoffset)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 685 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 686 | le32_to_cpu(pntsd->gsidoffset)); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 687 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 688 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 689 | cFYI(DBG2, "revision %d type 0x%x ooffset 0x%x goffset 0x%x " |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 690 | "sacloffset 0x%x dacloffset 0x%x", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 691 | pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset), |
| 692 | le32_to_cpu(pntsd->gsidoffset), |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 693 | le32_to_cpu(pntsd->sacloffset), dacloffset); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 694 | /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 695 | rc = parse_sid(owner_sid_ptr, end_of_acl); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 696 | if (rc) { |
| 697 | cFYI(1, "%s: Error %d parsing Owner SID", __func__, rc); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 698 | return rc; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 699 | } |
| 700 | rc = sid_to_id(cifs_sb, owner_sid_ptr, fattr, SIDOWNER); |
| 701 | if (rc) { |
| 702 | cFYI(1, "%s: Error %d mapping Owner SID to uid", __func__, rc); |
| 703 | return rc; |
| 704 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 705 | |
| 706 | rc = parse_sid(group_sid_ptr, end_of_acl); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 707 | if (rc) { |
| 708 | cFYI(1, "%s: Error %d mapping Owner SID to gid", __func__, rc); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 709 | return rc; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 710 | } |
| 711 | rc = sid_to_id(cifs_sb, group_sid_ptr, fattr, SIDGROUP); |
| 712 | if (rc) { |
| 713 | cFYI(1, "%s: Error %d mapping Group SID to gid", __func__, rc); |
| 714 | return rc; |
| 715 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 716 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 717 | if (dacloffset) |
| 718 | parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 719 | group_sid_ptr, fattr); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 720 | else |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 721 | cFYI(1, "no ACL"); /* BB grant all or default perms? */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 722 | |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 723 | return rc; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 724 | } |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 725 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 726 | /* Convert permission bits from mode to equivalent CIFS ACL */ |
| 727 | static int build_sec_desc(struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd, |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 728 | __u32 secdesclen, __u64 nmode, uid_t uid, gid_t gid, int *aclflag) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 729 | { |
| 730 | int rc = 0; |
| 731 | __u32 dacloffset; |
| 732 | __u32 ndacloffset; |
| 733 | __u32 sidsoffset; |
| 734 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 735 | struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 736 | struct cifs_acl *dacl_ptr = NULL; /* no need for SACL ptr */ |
| 737 | struct cifs_acl *ndacl_ptr = NULL; /* no need for SACL ptr */ |
| 738 | |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 739 | if (nmode != NO_CHANGE_64) { /* chmod */ |
| 740 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 741 | le32_to_cpu(pntsd->osidoffset)); |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 742 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 743 | le32_to_cpu(pntsd->gsidoffset)); |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 744 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
| 745 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
| 746 | ndacloffset = sizeof(struct cifs_ntsd); |
| 747 | ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset); |
| 748 | ndacl_ptr->revision = dacl_ptr->revision; |
| 749 | ndacl_ptr->size = 0; |
| 750 | ndacl_ptr->num_aces = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 751 | |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 752 | rc = set_chmod_dacl(ndacl_ptr, owner_sid_ptr, group_sid_ptr, |
| 753 | nmode); |
| 754 | sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size); |
| 755 | /* copy sec desc control portion & owner and group sids */ |
| 756 | copy_sec_desc(pntsd, pnntsd, sidsoffset); |
| 757 | *aclflag = CIFS_ACL_DACL; |
| 758 | } else { |
| 759 | memcpy(pnntsd, pntsd, secdesclen); |
| 760 | if (uid != NO_CHANGE_32) { /* chown */ |
| 761 | owner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + |
| 762 | le32_to_cpu(pnntsd->osidoffset)); |
| 763 | nowner_sid_ptr = kmalloc(sizeof(struct cifs_sid), |
| 764 | GFP_KERNEL); |
| 765 | if (!nowner_sid_ptr) |
| 766 | return -ENOMEM; |
| 767 | rc = id_to_sid(uid, SIDOWNER, nowner_sid_ptr); |
| 768 | if (rc) { |
| 769 | cFYI(1, "%s: Mapping error %d for owner id %d", |
| 770 | __func__, rc, uid); |
| 771 | kfree(nowner_sid_ptr); |
| 772 | return rc; |
| 773 | } |
Jeff Layton | 36960e4 | 2012-11-03 09:37:28 -0400 | [diff] [blame] | 774 | cifs_copy_sid(owner_sid_ptr, nowner_sid_ptr); |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 775 | kfree(nowner_sid_ptr); |
| 776 | *aclflag = CIFS_ACL_OWNER; |
| 777 | } |
| 778 | if (gid != NO_CHANGE_32) { /* chgrp */ |
| 779 | group_sid_ptr = (struct cifs_sid *)((char *)pnntsd + |
| 780 | le32_to_cpu(pnntsd->gsidoffset)); |
| 781 | ngroup_sid_ptr = kmalloc(sizeof(struct cifs_sid), |
| 782 | GFP_KERNEL); |
| 783 | if (!ngroup_sid_ptr) |
| 784 | return -ENOMEM; |
| 785 | rc = id_to_sid(gid, SIDGROUP, ngroup_sid_ptr); |
| 786 | if (rc) { |
| 787 | cFYI(1, "%s: Mapping error %d for group id %d", |
| 788 | __func__, rc, gid); |
| 789 | kfree(ngroup_sid_ptr); |
| 790 | return rc; |
| 791 | } |
Jeff Layton | 36960e4 | 2012-11-03 09:37:28 -0400 | [diff] [blame] | 792 | cifs_copy_sid(group_sid_ptr, ngroup_sid_ptr); |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 793 | kfree(ngroup_sid_ptr); |
| 794 | *aclflag = CIFS_ACL_GROUP; |
| 795 | } |
| 796 | } |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 797 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 798 | return rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 801 | static struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb, |
| 802 | __u16 fid, u32 *pacllen) |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 803 | { |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 804 | struct cifs_ntsd *pntsd = NULL; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 805 | unsigned int xid; |
| 806 | int rc; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 807 | struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); |
| 808 | |
| 809 | if (IS_ERR(tlink)) |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 810 | return ERR_CAST(tlink); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 811 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 812 | xid = get_xid(); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 813 | rc = CIFSSMBGetCIFSACL(xid, tlink_tcon(tlink), fid, &pntsd, pacllen); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 814 | free_xid(xid); |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 815 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 816 | cifs_put_tlink(tlink); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 817 | |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 818 | cFYI(1, "%s: rc = %d ACL len %d", __func__, rc, *pacllen); |
| 819 | if (rc) |
| 820 | return ERR_PTR(rc); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 821 | return pntsd; |
| 822 | } |
| 823 | |
| 824 | static struct cifs_ntsd *get_cifs_acl_by_path(struct cifs_sb_info *cifs_sb, |
| 825 | const char *path, u32 *pacllen) |
| 826 | { |
| 827 | struct cifs_ntsd *pntsd = NULL; |
| 828 | int oplock = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 829 | unsigned int xid; |
| 830 | int rc, create_options = 0; |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 831 | __u16 fid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 832 | struct cifs_tcon *tcon; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 833 | struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 834 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 835 | if (IS_ERR(tlink)) |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 836 | return ERR_CAST(tlink); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 837 | |
| 838 | tcon = tlink_tcon(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 839 | xid = get_xid(); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 840 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 841 | if (backup_cred(cifs_sb)) |
| 842 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 843 | |
| 844 | rc = CIFSSMBOpen(xid, tcon, path, FILE_OPEN, READ_CONTROL, |
| 845 | create_options, &fid, &oplock, NULL, cifs_sb->local_nls, |
| 846 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 847 | if (!rc) { |
| 848 | rc = CIFSSMBGetCIFSACL(xid, tcon, fid, &pntsd, pacllen); |
| 849 | CIFSSMBClose(xid, tcon, fid); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 852 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 853 | free_xid(xid); |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 854 | |
| 855 | cFYI(1, "%s: rc = %d ACL len %d", __func__, rc, *pacllen); |
| 856 | if (rc) |
| 857 | return ERR_PTR(rc); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 858 | return pntsd; |
| 859 | } |
| 860 | |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 861 | /* Retrieve an ACL from the server */ |
Shirish Pargaonkar | fbeba8b | 2010-11-27 11:37:54 -0600 | [diff] [blame] | 862 | struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb, |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 863 | struct inode *inode, const char *path, |
| 864 | u32 *pacllen) |
| 865 | { |
| 866 | struct cifs_ntsd *pntsd = NULL; |
| 867 | struct cifsFileInfo *open_file = NULL; |
| 868 | |
| 869 | if (inode) |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 870 | open_file = find_readable_file(CIFS_I(inode), true); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 871 | if (!open_file) |
| 872 | return get_cifs_acl_by_path(cifs_sb, path, pacllen); |
| 873 | |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 874 | pntsd = get_cifs_acl_by_fid(cifs_sb, open_file->fid.netfid, pacllen); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 875 | cifsFileInfo_put(open_file); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 876 | return pntsd; |
| 877 | } |
| 878 | |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 879 | /* Set an ACL on the server */ |
| 880 | int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen, |
| 881 | struct inode *inode, const char *path, int aclflag) |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 882 | { |
| 883 | int oplock = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 884 | unsigned int xid; |
| 885 | int rc, access_flags, create_options = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 886 | __u16 fid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 887 | struct cifs_tcon *tcon; |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 888 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 889 | struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 890 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 891 | if (IS_ERR(tlink)) |
| 892 | return PTR_ERR(tlink); |
| 893 | |
| 894 | tcon = tlink_tcon(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 895 | xid = get_xid(); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 896 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 897 | if (backup_cred(cifs_sb)) |
| 898 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 899 | |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 900 | if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP) |
| 901 | access_flags = WRITE_OWNER; |
| 902 | else |
| 903 | access_flags = WRITE_DAC; |
| 904 | |
| 905 | rc = CIFSSMBOpen(xid, tcon, path, FILE_OPEN, access_flags, |
| 906 | create_options, &fid, &oplock, NULL, cifs_sb->local_nls, |
| 907 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 908 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 909 | cERROR(1, "Unable to open file to set ACL"); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 910 | goto out; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 913 | rc = CIFSSMBSetCIFSACL(xid, tcon, fid, pnntsd, acllen, aclflag); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 914 | cFYI(DBG2, "SetCIFSACL rc = %d", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 915 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 916 | CIFSSMBClose(xid, tcon, fid); |
| 917 | out: |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 918 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 919 | cifs_put_tlink(tlink); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 920 | return rc; |
| 921 | } |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 922 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 923 | /* Translate the CIFS ACL (simlar to NTFS ACL) for a file into mode bits */ |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 924 | int |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 925 | cifs_acl_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr, |
| 926 | struct inode *inode, const char *path, const __u16 *pfid) |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 927 | { |
| 928 | struct cifs_ntsd *pntsd = NULL; |
| 929 | u32 acllen = 0; |
| 930 | int rc = 0; |
| 931 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 932 | cFYI(DBG2, "converting ACL to mode for %s", path); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 933 | |
| 934 | if (pfid) |
| 935 | pntsd = get_cifs_acl_by_fid(cifs_sb, *pfid, &acllen); |
| 936 | else |
| 937 | pntsd = get_cifs_acl(cifs_sb, inode, path, &acllen); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 938 | |
| 939 | /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */ |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 940 | if (IS_ERR(pntsd)) { |
| 941 | rc = PTR_ERR(pntsd); |
| 942 | cERROR(1, "%s: error %d getting sec desc", __func__, rc); |
| 943 | } else { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 944 | rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr); |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 945 | kfree(pntsd); |
| 946 | if (rc) |
| 947 | cERROR(1, "parse sec desc failed rc = %d", rc); |
| 948 | } |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 949 | |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 950 | return rc; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 951 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 952 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 953 | /* Convert mode bits to an ACL so we can update the ACL on the server */ |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 954 | int |
| 955 | id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 nmode, |
| 956 | uid_t uid, gid_t gid) |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 957 | { |
| 958 | int rc = 0; |
Shirish Pargaonkar | a5ff376 | 2011-10-13 10:26:03 -0500 | [diff] [blame] | 959 | int aclflag = CIFS_ACL_DACL; /* default flag to set */ |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 960 | __u32 secdesclen = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 961 | struct cifs_ntsd *pntsd = NULL; /* acl obtained from server */ |
| 962 | struct cifs_ntsd *pnntsd = NULL; /* modified acl to be sent to server */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 963 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 964 | cFYI(DBG2, "set ACL from mode for %s", path); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 965 | |
| 966 | /* Get the security descriptor */ |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 967 | pntsd = get_cifs_acl(CIFS_SB(inode->i_sb), inode, path, &secdesclen); |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 968 | if (IS_ERR(pntsd)) { |
| 969 | rc = PTR_ERR(pntsd); |
| 970 | cERROR(1, "%s: error %d getting sec desc", __func__, rc); |
Jeff Layton | c78cd83 | 2012-11-25 08:00:35 -0500 | [diff] [blame] | 971 | goto out; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Jeff Layton | c78cd83 | 2012-11-25 08:00:35 -0500 | [diff] [blame] | 974 | /* |
| 975 | * Add three ACEs for owner, group, everyone getting rid of other ACEs |
| 976 | * as chmod disables ACEs and set the security descriptor. Allocate |
| 977 | * memory for the smb header, set security descriptor request security |
| 978 | * descriptor parameters, and secuirty descriptor itself |
| 979 | */ |
| 980 | secdesclen = max_t(u32, secdesclen, DEFSECDESCLEN); |
| 981 | pnntsd = kmalloc(secdesclen, GFP_KERNEL); |
| 982 | if (!pnntsd) { |
| 983 | cERROR(1, "Unable to allocate security descriptor"); |
| 984 | kfree(pntsd); |
| 985 | return -ENOMEM; |
| 986 | } |
| 987 | |
| 988 | rc = build_sec_desc(pntsd, pnntsd, secdesclen, nmode, uid, gid, |
| 989 | &aclflag); |
| 990 | |
| 991 | cFYI(DBG2, "build_sec_desc rc: %d", rc); |
| 992 | |
| 993 | if (!rc) { |
| 994 | /* Set the security descriptor */ |
| 995 | rc = set_cifs_acl(pnntsd, secdesclen, inode, path, aclflag); |
| 996 | cFYI(DBG2, "set_cifs_acl rc: %d", rc); |
| 997 | } |
| 998 | |
| 999 | kfree(pnntsd); |
| 1000 | kfree(pntsd); |
| 1001 | out: |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 1002 | return rc; |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1003 | } |