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> |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 26 | #include "cifspdu.h" |
| 27 | #include "cifsglob.h" |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 28 | #include "cifsacl.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 29 | #include "cifsproto.h" |
| 30 | #include "cifs_debug.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 31 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 32 | |
| 33 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 34 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 35 | static struct cifs_wksid wksidarr[NUM_WK_SIDS] = { |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 36 | {{1, 0, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0} }, "null user"}, |
| 37 | {{1, 1, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0} }, "nobody"}, |
Jeff Layton | 536abdb | 2008-07-12 13:48:00 -0700 | [diff] [blame] | 38 | {{1, 1, {0, 0, 0, 0, 0, 5}, {__constant_cpu_to_le32(11), 0, 0, 0, 0} }, "net-users"}, |
| 39 | {{1, 1, {0, 0, 0, 0, 0, 5}, {__constant_cpu_to_le32(18), 0, 0, 0, 0} }, "sys"}, |
| 40 | {{1, 2, {0, 0, 0, 0, 0, 5}, {__constant_cpu_to_le32(32), __constant_cpu_to_le32(544), 0, 0, 0} }, "root"}, |
| 41 | {{1, 2, {0, 0, 0, 0, 0, 5}, {__constant_cpu_to_le32(32), __constant_cpu_to_le32(545), 0, 0, 0} }, "users"}, |
| 42 | {{1, 2, {0, 0, 0, 0, 0, 5}, {__constant_cpu_to_le32(32), __constant_cpu_to_le32(546), 0, 0, 0} }, "guest"} } |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 43 | ; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 44 | |
| 45 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 46 | /* security id for everyone */ |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 47 | static const struct cifs_sid sid_everyone = { |
| 48 | 1, 1, {0, 0, 0, 0, 0, 1}, {0} }; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 49 | /* group users */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 50 | 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] | 51 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 52 | |
| 53 | int match_sid(struct cifs_sid *ctsid) |
| 54 | { |
| 55 | int i, j; |
| 56 | int num_subauth, num_sat, num_saw; |
| 57 | struct cifs_sid *cwsid; |
| 58 | |
| 59 | if (!ctsid) |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 60 | return -1; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 61 | |
| 62 | for (i = 0; i < NUM_WK_SIDS; ++i) { |
| 63 | cwsid = &(wksidarr[i].cifssid); |
| 64 | |
| 65 | /* compare the revision */ |
| 66 | if (ctsid->revision != cwsid->revision) |
| 67 | continue; |
| 68 | |
| 69 | /* compare all of the six auth values */ |
| 70 | for (j = 0; j < 6; ++j) { |
| 71 | if (ctsid->authority[j] != cwsid->authority[j]) |
| 72 | break; |
| 73 | } |
| 74 | if (j < 6) |
| 75 | continue; /* all of the auth values did not match */ |
| 76 | |
| 77 | /* compare all of the subauth values if any */ |
Dave Kleikamp | ce51ae1 | 2007-10-16 21:35:39 +0000 | [diff] [blame] | 78 | num_sat = ctsid->num_subauth; |
| 79 | num_saw = cwsid->num_subauth; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 80 | num_subauth = num_sat < num_saw ? num_sat : num_saw; |
| 81 | if (num_subauth) { |
| 82 | for (j = 0; j < num_subauth; ++j) { |
| 83 | if (ctsid->sub_auth[j] != cwsid->sub_auth[j]) |
| 84 | break; |
| 85 | } |
| 86 | if (j < num_subauth) |
| 87 | continue; /* all sub_auth values do not match */ |
| 88 | } |
| 89 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 90 | cFYI(1, "matching sid: %s\n", wksidarr[i].sidname); |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 91 | return 0; /* sids compare/match */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 94 | cFYI(1, "No matching sid"); |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 95 | return -1; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 98 | /* if the two SIDs (roughly equivalent to a UUID for a user or group) are |
| 99 | the same returns 1, if they do not match returns 0 */ |
Steve French | 630f3f0 | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 100 | int compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid) |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 101 | { |
| 102 | int i; |
| 103 | int num_subauth, num_sat, num_saw; |
| 104 | |
| 105 | if ((!ctsid) || (!cwsid)) |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 106 | return 0; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 107 | |
| 108 | /* compare the revision */ |
| 109 | if (ctsid->revision != cwsid->revision) |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 110 | return 0; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 111 | |
| 112 | /* compare all of the six auth values */ |
| 113 | for (i = 0; i < 6; ++i) { |
| 114 | if (ctsid->authority[i] != cwsid->authority[i]) |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 115 | return 0; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* compare all of the subauth values if any */ |
Steve French | adbc035 | 2007-10-17 02:12:46 +0000 | [diff] [blame] | 119 | num_sat = ctsid->num_subauth; |
Steve French | adddd49 | 2007-10-17 02:48:17 +0000 | [diff] [blame] | 120 | num_saw = cwsid->num_subauth; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 121 | num_subauth = num_sat < num_saw ? num_sat : num_saw; |
| 122 | if (num_subauth) { |
| 123 | for (i = 0; i < num_subauth; ++i) { |
| 124 | if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 125 | return 0; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 129 | return 1; /* sids compare/match */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 132 | |
| 133 | /* copy ntsd, owner sid, and group sid from a security descriptor to another */ |
| 134 | static void copy_sec_desc(const struct cifs_ntsd *pntsd, |
| 135 | struct cifs_ntsd *pnntsd, __u32 sidsoffset) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 140 | struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr; |
| 141 | |
| 142 | /* copy security descriptor control portion */ |
| 143 | pnntsd->revision = pntsd->revision; |
| 144 | pnntsd->type = pntsd->type; |
| 145 | pnntsd->dacloffset = cpu_to_le32(sizeof(struct cifs_ntsd)); |
| 146 | pnntsd->sacloffset = 0; |
| 147 | pnntsd->osidoffset = cpu_to_le32(sidsoffset); |
| 148 | pnntsd->gsidoffset = cpu_to_le32(sidsoffset + sizeof(struct cifs_sid)); |
| 149 | |
| 150 | /* copy owner sid */ |
| 151 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 152 | le32_to_cpu(pntsd->osidoffset)); |
| 153 | nowner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset); |
| 154 | |
| 155 | nowner_sid_ptr->revision = owner_sid_ptr->revision; |
| 156 | nowner_sid_ptr->num_subauth = owner_sid_ptr->num_subauth; |
| 157 | for (i = 0; i < 6; i++) |
| 158 | nowner_sid_ptr->authority[i] = owner_sid_ptr->authority[i]; |
| 159 | for (i = 0; i < 5; i++) |
| 160 | nowner_sid_ptr->sub_auth[i] = owner_sid_ptr->sub_auth[i]; |
| 161 | |
| 162 | /* copy group sid */ |
| 163 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 164 | le32_to_cpu(pntsd->gsidoffset)); |
| 165 | ngroup_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset + |
| 166 | sizeof(struct cifs_sid)); |
| 167 | |
| 168 | ngroup_sid_ptr->revision = group_sid_ptr->revision; |
| 169 | ngroup_sid_ptr->num_subauth = group_sid_ptr->num_subauth; |
| 170 | for (i = 0; i < 6; i++) |
| 171 | ngroup_sid_ptr->authority[i] = group_sid_ptr->authority[i]; |
| 172 | for (i = 0; i < 5; i++) |
Shirish Pargaonkar | b1910ad | 2008-07-24 14:53:20 +0000 | [diff] [blame] | 173 | ngroup_sid_ptr->sub_auth[i] = group_sid_ptr->sub_auth[i]; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 174 | |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | |
Steve French | 630f3f0 | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 179 | /* |
| 180 | change posix mode to reflect permissions |
| 181 | pmode is the existing mode (we only want to overwrite part of this |
| 182 | bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007 |
| 183 | */ |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 184 | 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] | 185 | umode_t *pbits_to_set) |
Steve French | 4879b44 | 2007-10-19 21:57:39 +0000 | [diff] [blame] | 186 | { |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 187 | __u32 flags = le32_to_cpu(ace_flags); |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 188 | /* 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] | 189 | DENY entries followed by ALLOW, otherwise an allow entry could be |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 190 | encountered first, making the subsequent deny entry like "dead code" |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 191 | which would be superflous since Windows stops when a match is made |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 192 | for the operation you are trying to perform for your user */ |
| 193 | |
| 194 | /* For deny ACEs we change the mask so that subsequent allow access |
| 195 | control entries do not turn on the bits we are denying */ |
| 196 | if (type == ACCESS_DENIED) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 197 | if (flags & GENERIC_ALL) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 198 | *pbits_to_set &= ~S_IRWXUGO; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 199 | |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 200 | if ((flags & GENERIC_WRITE) || |
| 201 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 202 | *pbits_to_set &= ~S_IWUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 203 | if ((flags & GENERIC_READ) || |
| 204 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 205 | *pbits_to_set &= ~S_IRUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 206 | if ((flags & GENERIC_EXECUTE) || |
| 207 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 208 | *pbits_to_set &= ~S_IXUGO; |
| 209 | return; |
| 210 | } else if (type != ACCESS_ALLOWED) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 211 | cERROR(1, "unknown access control type %d", type); |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | /* else ACCESS_ALLOWED type */ |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 215 | |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 216 | if (flags & GENERIC_ALL) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 217 | *pmode |= (S_IRWXUGO & (*pbits_to_set)); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 218 | cFYI(DBG2, "all perms"); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 219 | return; |
| 220 | } |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 221 | if ((flags & GENERIC_WRITE) || |
| 222 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 223 | *pmode |= (S_IWUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 224 | if ((flags & GENERIC_READ) || |
| 225 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 226 | *pmode |= (S_IRUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 227 | if ((flags & GENERIC_EXECUTE) || |
| 228 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 229 | *pmode |= (S_IXUGO & (*pbits_to_set)); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 230 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 231 | cFYI(DBG2, "access flags 0x%x mode now 0x%x", flags, *pmode); |
Steve French | 630f3f0 | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 235 | /* |
| 236 | Generate access flags to reflect permissions mode is the existing mode. |
| 237 | This function is called for every ACE in the DACL whose SID matches |
| 238 | with either owner or group or everyone. |
| 239 | */ |
| 240 | |
| 241 | static void mode_to_access_flags(umode_t mode, umode_t bits_to_use, |
| 242 | __u32 *pace_flags) |
| 243 | { |
| 244 | /* reset access mask */ |
| 245 | *pace_flags = 0x0; |
| 246 | |
| 247 | /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */ |
| 248 | mode &= bits_to_use; |
| 249 | |
| 250 | /* check for R/W/X UGO since we do not know whose flags |
| 251 | is this but we have cleared all the bits sans RWX for |
| 252 | either user or group or other as per bits_to_use */ |
| 253 | if (mode & S_IRUGO) |
| 254 | *pace_flags |= SET_FILE_READ_RIGHTS; |
| 255 | if (mode & S_IWUGO) |
| 256 | *pace_flags |= SET_FILE_WRITE_RIGHTS; |
| 257 | if (mode & S_IXUGO) |
| 258 | *pace_flags |= SET_FILE_EXEC_RIGHTS; |
| 259 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 260 | 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] | 261 | return; |
| 262 | } |
| 263 | |
Al Viro | 2b210ad | 2008-03-29 03:09:18 +0000 | [diff] [blame] | 264 | static __u16 fill_ace_for_sid(struct cifs_ace *pntace, |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 265 | const struct cifs_sid *psid, __u64 nmode, umode_t bits) |
| 266 | { |
| 267 | int i; |
| 268 | __u16 size = 0; |
| 269 | __u32 access_req = 0; |
| 270 | |
| 271 | pntace->type = ACCESS_ALLOWED; |
| 272 | pntace->flags = 0x0; |
| 273 | mode_to_access_flags(nmode, bits, &access_req); |
| 274 | if (!access_req) |
| 275 | access_req = SET_MINIMUM_RIGHTS; |
| 276 | pntace->access_req = cpu_to_le32(access_req); |
| 277 | |
| 278 | pntace->sid.revision = psid->revision; |
| 279 | pntace->sid.num_subauth = psid->num_subauth; |
| 280 | for (i = 0; i < 6; i++) |
| 281 | pntace->sid.authority[i] = psid->authority[i]; |
| 282 | for (i = 0; i < psid->num_subauth; i++) |
| 283 | pntace->sid.sub_auth[i] = psid->sub_auth[i]; |
| 284 | |
| 285 | size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4); |
| 286 | pntace->size = cpu_to_le16(size); |
| 287 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 288 | return size; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 291 | |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 292 | #ifdef CONFIG_CIFS_DEBUG2 |
| 293 | static void dump_ace(struct cifs_ace *pace, char *end_of_acl) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 294 | { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 295 | int num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 296 | |
| 297 | /* validate that we do not go past end of acl */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 298 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 299 | if (le16_to_cpu(pace->size) < 16) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 300 | cERROR(1, "ACE too small %d", le16_to_cpu(pace->size)); |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | |
| 304 | if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 305 | cERROR(1, "ACL too small to parse ACE"); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 306 | return; |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 307 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 308 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 309 | num_subauth = pace->sid.num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 310 | if (num_subauth) { |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 311 | int i; |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 312 | 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] | 313 | pace->sid.revision, pace->sid.num_subauth, pace->type, |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 314 | pace->flags, le16_to_cpu(pace->size)); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 315 | for (i = 0; i < num_subauth; ++i) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 316 | cFYI(1, "ACE sub_auth[%d]: 0x%x", i, |
| 317 | le32_to_cpu(pace->sid.sub_auth[i])); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 318 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 319 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 320 | /* BB add length check to make sure that we do not have huge |
| 321 | num auths and therefore go off the end */ |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 322 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 323 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 324 | return; |
| 325 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 326 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 327 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 328 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 329 | static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl, |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 330 | struct cifs_sid *pownersid, struct cifs_sid *pgrpsid, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 331 | struct cifs_fattr *fattr) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 332 | { |
| 333 | int i; |
| 334 | int num_aces = 0; |
| 335 | int acl_size; |
| 336 | char *acl_base; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 337 | struct cifs_ace **ppace; |
| 338 | |
| 339 | /* BB need to add parm so we can store the SID BB */ |
| 340 | |
Steve French | 2b83457 | 2007-11-25 10:01:00 +0000 | [diff] [blame] | 341 | if (!pdacl) { |
| 342 | /* no DACL in the security descriptor, set |
| 343 | all the permissions for user/group/other */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 344 | fattr->cf_mode |= S_IRWXUGO; |
Steve French | 2b83457 | 2007-11-25 10:01:00 +0000 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 348 | /* validate that we do not go past end of acl */ |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 349 | if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 350 | cERROR(1, "ACL too small to parse DACL"); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 354 | cFYI(DBG2, "DACL revision %d size %d num aces %d", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 355 | le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size), |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 356 | le32_to_cpu(pdacl->num_aces)); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 357 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 358 | /* reset rwx permissions for user/group/other. |
| 359 | Also, if num_aces is 0 i.e. DACL has no ACEs, |
| 360 | user/group/other have no permissions */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 361 | fattr->cf_mode &= ~(S_IRWXUGO); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 362 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 363 | acl_base = (char *)pdacl; |
| 364 | acl_size = sizeof(struct cifs_acl); |
| 365 | |
Steve French | adbc035 | 2007-10-17 02:12:46 +0000 | [diff] [blame] | 366 | num_aces = le32_to_cpu(pdacl->num_aces); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 367 | if (num_aces > 0) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 368 | umode_t user_mask = S_IRWXU; |
| 369 | umode_t group_mask = S_IRWXG; |
| 370 | umode_t other_mask = S_IRWXO; |
| 371 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 372 | ppace = kmalloc(num_aces * sizeof(struct cifs_ace *), |
| 373 | GFP_KERNEL); |
| 374 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 375 | for (i = 0; i < num_aces; ++i) { |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 376 | ppace[i] = (struct cifs_ace *) (acl_base + acl_size); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 377 | #ifdef CONFIG_CIFS_DEBUG2 |
| 378 | dump_ace(ppace[i], end_of_acl); |
| 379 | #endif |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 380 | if (compare_sids(&(ppace[i]->sid), pownersid)) |
| 381 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 382 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 383 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 384 | &user_mask); |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 385 | if (compare_sids(&(ppace[i]->sid), pgrpsid)) |
| 386 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 387 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 388 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 389 | &group_mask); |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 390 | if (compare_sids(&(ppace[i]->sid), &sid_everyone)) |
| 391 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 392 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 393 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 394 | &other_mask); |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 395 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 396 | /* memcpy((void *)(&(cifscred->aces[i])), |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 397 | (void *)ppace[i], |
| 398 | sizeof(struct cifs_ace)); */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 399 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 400 | acl_base = (char *)ppace[i]; |
| 401 | acl_size = le16_to_cpu(ppace[i]->size); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | kfree(ppace); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return; |
| 408 | } |
| 409 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 410 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 411 | static int set_chmod_dacl(struct cifs_acl *pndacl, struct cifs_sid *pownersid, |
| 412 | struct cifs_sid *pgrpsid, __u64 nmode) |
| 413 | { |
Al Viro | 2b210ad | 2008-03-29 03:09:18 +0000 | [diff] [blame] | 414 | u16 size = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 415 | struct cifs_acl *pnndacl; |
| 416 | |
| 417 | pnndacl = (struct cifs_acl *)((char *)pndacl + sizeof(struct cifs_acl)); |
| 418 | |
| 419 | size += fill_ace_for_sid((struct cifs_ace *) ((char *)pnndacl + size), |
| 420 | pownersid, nmode, S_IRWXU); |
| 421 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 422 | pgrpsid, nmode, S_IRWXG); |
| 423 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 424 | &sid_everyone, nmode, S_IRWXO); |
| 425 | |
| 426 | pndacl->size = cpu_to_le16(size + sizeof(struct cifs_acl)); |
Shirish Pargaonkar | d9f382e | 2008-02-12 20:46:26 +0000 | [diff] [blame] | 427 | pndacl->num_aces = cpu_to_le32(3); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 428 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 429 | return 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 433 | static int parse_sid(struct cifs_sid *psid, char *end_of_acl) |
| 434 | { |
| 435 | /* BB need to add parm so we can store the SID BB */ |
| 436 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 437 | /* validate that we do not go past end of ACL - sid must be at least 8 |
| 438 | bytes long (assuming no sub-auths - e.g. the null SID */ |
| 439 | if (end_of_acl < (char *)psid + 8) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 440 | cERROR(1, "ACL too small to parse SID %p", psid); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 441 | return -EINVAL; |
| 442 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 443 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 444 | if (psid->num_subauth) { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 445 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 446 | int i; |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 447 | cFYI(1, "SID revision %d num_auth %d", |
| 448 | psid->revision, psid->num_subauth); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 449 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 450 | for (i = 0; i < psid->num_subauth; i++) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 451 | cFYI(1, "SID sub_auth[%d]: 0x%x ", i, |
| 452 | le32_to_cpu(psid->sub_auth[i])); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 455 | /* 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] | 456 | num auths and therefore go off the end */ |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 457 | cFYI(1, "RID 0x%x", |
| 458 | le32_to_cpu(psid->sub_auth[psid->num_subauth-1])); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 459 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 465 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 466 | /* Convert CIFS ACL to POSIX form */ |
Steve French | 630f3f0 | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 467 | static int parse_sec_desc(struct cifs_ntsd *pntsd, int acl_len, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 468 | struct cifs_fattr *fattr) |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 469 | { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 470 | int rc; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 471 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 472 | struct cifs_acl *dacl_ptr; /* no need for SACL ptr */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 473 | char *end_of_acl = ((char *)pntsd) + acl_len; |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 474 | __u32 dacloffset; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 475 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 476 | if (pntsd == NULL) |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 477 | return -EIO; |
| 478 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 479 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 480 | le32_to_cpu(pntsd->osidoffset)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 481 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 482 | le32_to_cpu(pntsd->gsidoffset)); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 483 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 484 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 485 | 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] | 486 | "sacloffset 0x%x dacloffset 0x%x", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 487 | pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset), |
| 488 | le32_to_cpu(pntsd->gsidoffset), |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 489 | le32_to_cpu(pntsd->sacloffset), dacloffset); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 490 | /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 491 | rc = parse_sid(owner_sid_ptr, end_of_acl); |
| 492 | if (rc) |
| 493 | return rc; |
| 494 | |
| 495 | rc = parse_sid(group_sid_ptr, end_of_acl); |
| 496 | if (rc) |
| 497 | return rc; |
| 498 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 499 | if (dacloffset) |
| 500 | parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 501 | group_sid_ptr, fattr); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 502 | else |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 503 | cFYI(1, "no ACL"); /* BB grant all or default perms? */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 504 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 505 | /* cifscred->uid = owner_sid_ptr->rid; |
| 506 | cifscred->gid = group_sid_ptr->rid; |
| 507 | memcpy((void *)(&(cifscred->osid)), (void *)owner_sid_ptr, |
Steve French | 630f3f0 | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 508 | sizeof(struct cifs_sid)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 509 | memcpy((void *)(&(cifscred->gsid)), (void *)group_sid_ptr, |
Steve French | 630f3f0 | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 510 | sizeof(struct cifs_sid)); */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 511 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 512 | return 0; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 513 | } |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 514 | |
| 515 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 516 | /* Convert permission bits from mode to equivalent CIFS ACL */ |
| 517 | static int build_sec_desc(struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd, |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 518 | struct inode *inode, __u64 nmode) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 519 | { |
| 520 | int rc = 0; |
| 521 | __u32 dacloffset; |
| 522 | __u32 ndacloffset; |
| 523 | __u32 sidsoffset; |
| 524 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 525 | struct cifs_acl *dacl_ptr = NULL; /* no need for SACL ptr */ |
| 526 | struct cifs_acl *ndacl_ptr = NULL; /* no need for SACL ptr */ |
| 527 | |
| 528 | if ((inode == NULL) || (pntsd == NULL) || (pnntsd == NULL)) |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 529 | return -EIO; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 530 | |
| 531 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 532 | le32_to_cpu(pntsd->osidoffset)); |
| 533 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 534 | le32_to_cpu(pntsd->gsidoffset)); |
| 535 | |
| 536 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
| 537 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
| 538 | |
| 539 | ndacloffset = sizeof(struct cifs_ntsd); |
| 540 | ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset); |
| 541 | ndacl_ptr->revision = dacl_ptr->revision; |
| 542 | ndacl_ptr->size = 0; |
| 543 | ndacl_ptr->num_aces = 0; |
| 544 | |
| 545 | rc = set_chmod_dacl(ndacl_ptr, owner_sid_ptr, group_sid_ptr, nmode); |
| 546 | |
| 547 | sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size); |
| 548 | |
| 549 | /* copy security descriptor control portion and owner and group sid */ |
| 550 | copy_sec_desc(pntsd, pnntsd, sidsoffset); |
| 551 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 552 | return rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 555 | static struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb, |
| 556 | __u16 fid, u32 *pacllen) |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 557 | { |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 558 | struct cifs_ntsd *pntsd = NULL; |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 559 | int xid, rc; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 560 | |
| 561 | xid = GetXid(); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 562 | rc = CIFSSMBGetCIFSACL(xid, cifs_sb->tcon, fid, &pntsd, pacllen); |
| 563 | FreeXid(xid); |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 564 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 565 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 566 | cFYI(1, "GetCIFSACL rc = %d ACL len %d", rc, *pacllen); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 567 | return pntsd; |
| 568 | } |
| 569 | |
| 570 | static struct cifs_ntsd *get_cifs_acl_by_path(struct cifs_sb_info *cifs_sb, |
| 571 | const char *path, u32 *pacllen) |
| 572 | { |
| 573 | struct cifs_ntsd *pntsd = NULL; |
| 574 | int oplock = 0; |
| 575 | int xid, rc; |
| 576 | __u16 fid; |
| 577 | |
| 578 | xid = GetXid(); |
| 579 | |
| 580 | rc = CIFSSMBOpen(xid, cifs_sb->tcon, path, FILE_OPEN, READ_CONTROL, 0, |
| 581 | &fid, &oplock, NULL, cifs_sb->local_nls, |
| 582 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 583 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 584 | cERROR(1, "Unable to open file to get ACL"); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 585 | goto out; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 588 | rc = CIFSSMBGetCIFSACL(xid, cifs_sb->tcon, fid, &pntsd, pacllen); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 589 | cFYI(1, "GetCIFSACL rc = %d ACL len %d", rc, *pacllen); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 590 | |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 591 | CIFSSMBClose(xid, cifs_sb->tcon, fid); |
| 592 | out: |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 593 | FreeXid(xid); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 594 | return pntsd; |
| 595 | } |
| 596 | |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 597 | /* Retrieve an ACL from the server */ |
| 598 | static struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb, |
| 599 | struct inode *inode, const char *path, |
| 600 | u32 *pacllen) |
| 601 | { |
| 602 | struct cifs_ntsd *pntsd = NULL; |
| 603 | struct cifsFileInfo *open_file = NULL; |
| 604 | |
| 605 | if (inode) |
| 606 | open_file = find_readable_file(CIFS_I(inode)); |
| 607 | if (!open_file) |
| 608 | return get_cifs_acl_by_path(cifs_sb, path, pacllen); |
| 609 | |
| 610 | pntsd = get_cifs_acl_by_fid(cifs_sb, open_file->netfid, pacllen); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 611 | cifsFileInfo_put(open_file); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 612 | return pntsd; |
| 613 | } |
| 614 | |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 615 | static int set_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb, __u16 fid, |
| 616 | struct cifs_ntsd *pnntsd, u32 acllen) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 617 | { |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 618 | int xid, rc; |
| 619 | |
| 620 | xid = GetXid(); |
| 621 | rc = CIFSSMBSetCIFSACL(xid, cifs_sb->tcon, fid, pnntsd, acllen); |
| 622 | FreeXid(xid); |
| 623 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 624 | cFYI(DBG2, "SetCIFSACL rc = %d", rc); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 625 | return rc; |
| 626 | } |
| 627 | |
| 628 | static int set_cifs_acl_by_path(struct cifs_sb_info *cifs_sb, const char *path, |
| 629 | struct cifs_ntsd *pnntsd, u32 acllen) |
| 630 | { |
| 631 | int oplock = 0; |
| 632 | int xid, rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 633 | __u16 fid; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 634 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 635 | xid = GetXid(); |
| 636 | |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 637 | rc = CIFSSMBOpen(xid, cifs_sb->tcon, path, FILE_OPEN, WRITE_DAC, 0, |
| 638 | &fid, &oplock, NULL, cifs_sb->local_nls, |
| 639 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 640 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 641 | cERROR(1, "Unable to open file to set ACL"); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 642 | goto out; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | rc = CIFSSMBSetCIFSACL(xid, cifs_sb->tcon, fid, pnntsd, acllen); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 646 | cFYI(DBG2, "SetCIFSACL rc = %d", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 647 | |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 648 | CIFSSMBClose(xid, cifs_sb->tcon, fid); |
| 649 | out: |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 650 | FreeXid(xid); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 651 | return rc; |
| 652 | } |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 653 | |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 654 | /* Set an ACL on the server */ |
| 655 | static int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen, |
| 656 | struct inode *inode, const char *path) |
| 657 | { |
| 658 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 659 | struct cifsFileInfo *open_file; |
| 660 | int rc; |
| 661 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 662 | cFYI(DBG2, "set ACL for %s from mode 0x%x", path, inode->i_mode); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 663 | |
| 664 | open_file = find_readable_file(CIFS_I(inode)); |
| 665 | if (!open_file) |
| 666 | return set_cifs_acl_by_path(cifs_sb, path, pnntsd, acllen); |
| 667 | |
| 668 | rc = set_cifs_acl_by_fid(cifs_sb, open_file->netfid, pnntsd, acllen); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 669 | cifsFileInfo_put(open_file); |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 670 | return rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 673 | /* Translate the CIFS ACL (simlar to NTFS ACL) for a file into mode bits */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 674 | void |
| 675 | cifs_acl_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr, |
| 676 | struct inode *inode, const char *path, const __u16 *pfid) |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 677 | { |
| 678 | struct cifs_ntsd *pntsd = NULL; |
| 679 | u32 acllen = 0; |
| 680 | int rc = 0; |
| 681 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 682 | cFYI(DBG2, "converting ACL to mode for %s", path); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 683 | |
| 684 | if (pfid) |
| 685 | pntsd = get_cifs_acl_by_fid(cifs_sb, *pfid, &acllen); |
| 686 | else |
| 687 | pntsd = get_cifs_acl(cifs_sb, inode, path, &acllen); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 688 | |
| 689 | /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */ |
| 690 | if (pntsd) |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 691 | rc = parse_sec_desc(pntsd, acllen, fattr); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 692 | if (rc) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 693 | cFYI(1, "parse sec desc failed rc = %d", rc); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 694 | |
| 695 | kfree(pntsd); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 696 | return; |
| 697 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 698 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 699 | /* Convert mode bits to an ACL so we can update the ACL on the server */ |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 700 | int mode_to_acl(struct inode *inode, const char *path, __u64 nmode) |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 701 | { |
| 702 | int rc = 0; |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 703 | __u32 secdesclen = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 704 | struct cifs_ntsd *pntsd = NULL; /* acl obtained from server */ |
| 705 | struct cifs_ntsd *pnntsd = NULL; /* modified acl to be sent to server */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 706 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 707 | cFYI(DBG2, "set ACL from mode for %s", path); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 708 | |
| 709 | /* Get the security descriptor */ |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 710 | pntsd = get_cifs_acl(CIFS_SB(inode->i_sb), inode, path, &secdesclen); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 711 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 712 | /* Add three ACEs for owner, group, everyone getting rid of |
| 713 | other ACEs as chmod disables ACEs and set the security descriptor */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 714 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 715 | if (pntsd) { |
| 716 | /* allocate memory for the smb header, |
| 717 | set security descriptor request security descriptor |
| 718 | parameters, and secuirty descriptor itself */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 719 | |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 720 | secdesclen = secdesclen < DEFSECDESCLEN ? |
| 721 | DEFSECDESCLEN : secdesclen; |
| 722 | pnntsd = kmalloc(secdesclen, GFP_KERNEL); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 723 | if (!pnntsd) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 724 | cERROR(1, "Unable to allocate security descriptor"); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 725 | kfree(pntsd); |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 726 | return -ENOMEM; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 727 | } |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 728 | |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 729 | rc = build_sec_desc(pntsd, pnntsd, inode, nmode); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 730 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 731 | cFYI(DBG2, "build_sec_desc rc: %d", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 732 | |
| 733 | if (!rc) { |
| 734 | /* Set the security descriptor */ |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 735 | rc = set_cifs_acl(pnntsd, secdesclen, inode, path); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 736 | cFYI(DBG2, "set_cifs_acl rc: %d", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | kfree(pnntsd); |
| 740 | kfree(pntsd); |
| 741 | } |
| 742 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 743 | return rc; |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 744 | } |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 745 | #endif /* CONFIG_CIFS_EXPERIMENTAL */ |