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