Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2021 Samsung Electronics Co., Ltd. |
| 4 | * Author(s): Namjae Jeon <linkinjeon@kernel.org> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/fs.h> |
| 8 | |
| 9 | #include "glob.h" |
| 10 | #include "ndr.h" |
| 11 | |
Hyunchul Lee | cb5b047 | 2021-06-25 07:02:09 +0900 | [diff] [blame] | 12 | static inline char *ndr_get_field(struct ndr *n) |
| 13 | { |
| 14 | return n->data + n->offset; |
| 15 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 16 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 17 | static int try_to_realloc_ndr_blob(struct ndr *n, size_t sz) |
| 18 | { |
| 19 | char *data; |
| 20 | |
| 21 | data = krealloc(n->data, n->offset + sz + 1024, GFP_KERNEL); |
| 22 | if (!data) |
| 23 | return -ENOMEM; |
| 24 | |
| 25 | n->data = data; |
| 26 | n->length += 1024; |
| 27 | memset(n->data + n->offset, 0, 1024); |
| 28 | return 0; |
| 29 | } |
| 30 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 31 | static int ndr_write_int16(struct ndr *n, __u16 value) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 32 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 33 | if (n->length <= n->offset + sizeof(value)) { |
| 34 | int ret; |
| 35 | |
| 36 | ret = try_to_realloc_ndr_blob(n, sizeof(value)); |
| 37 | if (ret) |
| 38 | return ret; |
| 39 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 40 | |
Hyunchul Lee | cb5b047 | 2021-06-25 07:02:09 +0900 | [diff] [blame] | 41 | *(__le16 *)ndr_get_field(n) = cpu_to_le16(value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 42 | n->offset += sizeof(value); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 43 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 44 | } |
| 45 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 46 | static int ndr_write_int32(struct ndr *n, __u32 value) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 47 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 48 | if (n->length <= n->offset + sizeof(value)) { |
| 49 | int ret; |
| 50 | |
| 51 | ret = try_to_realloc_ndr_blob(n, sizeof(value)); |
| 52 | if (ret) |
| 53 | return ret; |
| 54 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 55 | |
Hyunchul Lee | cb5b047 | 2021-06-25 07:02:09 +0900 | [diff] [blame] | 56 | *(__le32 *)ndr_get_field(n) = cpu_to_le32(value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 57 | n->offset += sizeof(value); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 58 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 61 | static int ndr_write_int64(struct ndr *n, __u64 value) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 62 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 63 | if (n->length <= n->offset + sizeof(value)) { |
| 64 | int ret; |
| 65 | |
| 66 | ret = try_to_realloc_ndr_blob(n, sizeof(value)); |
| 67 | if (ret) |
| 68 | return ret; |
| 69 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 70 | |
Hyunchul Lee | cb5b047 | 2021-06-25 07:02:09 +0900 | [diff] [blame] | 71 | *(__le64 *)ndr_get_field(n) = cpu_to_le64(value); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 72 | n->offset += sizeof(value); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 73 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int ndr_write_bytes(struct ndr *n, void *value, size_t sz) |
| 77 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 78 | if (n->length <= n->offset + sz) { |
| 79 | int ret; |
| 80 | |
| 81 | ret = try_to_realloc_ndr_blob(n, sz); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 85 | |
Hyunchul Lee | cb5b047 | 2021-06-25 07:02:09 +0900 | [diff] [blame] | 86 | memcpy(ndr_get_field(n), value, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 87 | n->offset += sz; |
| 88 | return 0; |
| 89 | } |
| 90 | |
Hyunchul Lee | 1d904ea | 2021-07-23 13:01:06 +0900 | [diff] [blame] | 91 | static int ndr_write_string(struct ndr *n, char *value) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 92 | { |
Hyunchul Lee | 1d904ea | 2021-07-23 13:01:06 +0900 | [diff] [blame] | 93 | size_t sz; |
| 94 | |
| 95 | sz = strlen(value) + 1; |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 96 | if (n->length <= n->offset + sz) { |
| 97 | int ret; |
| 98 | |
| 99 | ret = try_to_realloc_ndr_blob(n, sz); |
| 100 | if (ret) |
| 101 | return ret; |
| 102 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 103 | |
Hyunchul Lee | 1d904ea | 2021-07-23 13:01:06 +0900 | [diff] [blame] | 104 | memcpy(ndr_get_field(n), value, sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 105 | n->offset += sz; |
Hyunchul Lee | c222032 | 2021-06-25 07:02:08 +0900 | [diff] [blame] | 106 | n->offset = ALIGN(n->offset, 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int ndr_read_string(struct ndr *n, void *value, size_t sz) |
| 111 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 112 | int len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 113 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 114 | if (n->offset + sz > n->length) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | len = strnlen(ndr_get_field(n), sz); |
| 118 | if (value) |
| 119 | memcpy(value, ndr_get_field(n), len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 120 | len++; |
| 121 | n->offset += len; |
Hyunchul Lee | c222032 | 2021-06-25 07:02:08 +0900 | [diff] [blame] | 122 | n->offset = ALIGN(n->offset, 2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int ndr_read_bytes(struct ndr *n, void *value, size_t sz) |
| 127 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 128 | if (n->offset + sz > n->length) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | if (value) |
| 132 | memcpy(value, ndr_get_field(n), sz); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 133 | n->offset += sz; |
| 134 | return 0; |
| 135 | } |
| 136 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 137 | static int ndr_read_int16(struct ndr *n, __u16 *value) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 138 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 139 | if (n->offset + sizeof(__u16) > n->length) |
| 140 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 141 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 142 | if (value) |
| 143 | *value = le16_to_cpu(*(__le16 *)ndr_get_field(n)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 144 | n->offset += sizeof(__u16); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 145 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 146 | } |
| 147 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 148 | static int ndr_read_int32(struct ndr *n, __u32 *value) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 149 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 150 | if (n->offset + sizeof(__u32) > n->length) |
Dan Carpenter | ef39946 | 2021-11-30 15:50:47 +0300 | [diff] [blame] | 151 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 152 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 153 | if (value) |
| 154 | *value = le32_to_cpu(*(__le32 *)ndr_get_field(n)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 155 | n->offset += sizeof(__u32); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 156 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 159 | static int ndr_read_int64(struct ndr *n, __u64 *value) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 160 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 161 | if (n->offset + sizeof(__u64) > n->length) |
| 162 | return -EINVAL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 163 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 164 | if (value) |
| 165 | *value = le64_to_cpu(*(__le64 *)ndr_get_field(n)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 166 | n->offset += sizeof(__u64); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 167 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int ndr_encode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da) |
| 171 | { |
| 172 | char hex_attr[12] = {0}; |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 173 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 174 | |
| 175 | n->offset = 0; |
| 176 | n->length = 1024; |
| 177 | n->data = kzalloc(n->length, GFP_KERNEL); |
| 178 | if (!n->data) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | if (da->version == 3) { |
| 182 | snprintf(hex_attr, 10, "0x%x", da->attr); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 183 | ret = ndr_write_string(n, hex_attr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 184 | } else { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 185 | ret = ndr_write_string(n, ""); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 186 | } |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 187 | if (ret) |
| 188 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 189 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 190 | ret = ndr_write_int16(n, da->version); |
| 191 | if (ret) |
| 192 | return ret; |
| 193 | |
| 194 | ret = ndr_write_int32(n, da->version); |
| 195 | if (ret) |
| 196 | return ret; |
| 197 | |
| 198 | ret = ndr_write_int32(n, da->flags); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | |
| 202 | ret = ndr_write_int32(n, da->attr); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 206 | if (da->version == 3) { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 207 | ret = ndr_write_int32(n, da->ea_size); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | ret = ndr_write_int64(n, da->size); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | ret = ndr_write_int64(n, da->alloc_size); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 214 | } else { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 215 | ret = ndr_write_int64(n, da->itime); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 216 | } |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 217 | if (ret) |
| 218 | return ret; |
| 219 | |
| 220 | ret = ndr_write_int64(n, da->create_time); |
| 221 | if (ret) |
| 222 | return ret; |
| 223 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 224 | if (da->version == 3) |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 225 | ret = ndr_write_int64(n, da->change_time); |
| 226 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da) |
| 230 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 231 | char hex_attr[12]; |
| 232 | unsigned int version2; |
| 233 | int ret; |
Namjae Jeon | 7d5d8d7 | 2021-08-27 10:18:05 +0900 | [diff] [blame] | 234 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 235 | n->offset = 0; |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 236 | ret = ndr_read_string(n, hex_attr, sizeof(hex_attr)); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | ret = ndr_read_int16(n, &da->version); |
| 241 | if (ret) |
| 242 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 243 | |
| 244 | if (da->version != 3 && da->version != 4) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 245 | pr_err("v%d version is not supported\n", da->version); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 246 | return -EINVAL; |
| 247 | } |
| 248 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 249 | ret = ndr_read_int32(n, &version2); |
| 250 | if (ret) |
| 251 | return ret; |
| 252 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 253 | if (da->version != version2) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 254 | pr_err("ndr version mismatched(version: %d, version2: %d)\n", |
| 255 | da->version, version2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 256 | return -EINVAL; |
| 257 | } |
| 258 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 259 | ret = ndr_read_int32(n, NULL); |
| 260 | if (ret) |
| 261 | return ret; |
| 262 | |
| 263 | ret = ndr_read_int32(n, &da->attr); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 267 | if (da->version == 4) { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 268 | ret = ndr_read_int64(n, &da->itime); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | |
| 272 | ret = ndr_read_int64(n, &da->create_time); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 273 | } else { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 274 | ret = ndr_read_int32(n, NULL); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
Colin Ian King | 36bbeb3 | 2021-09-06 14:44:38 +0100 | [diff] [blame] | 278 | ret = ndr_read_int64(n, NULL); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 279 | if (ret) |
| 280 | return ret; |
| 281 | |
Colin Ian King | 36bbeb3 | 2021-09-06 14:44:38 +0100 | [diff] [blame] | 282 | ret = ndr_read_int64(n, NULL); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 283 | if (ret) |
| 284 | return ret; |
| 285 | |
| 286 | ret = ndr_read_int64(n, &da->create_time); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | |
| 290 | ret = ndr_read_int64(n, NULL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 291 | } |
| 292 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 293 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int ndr_encode_posix_acl_entry(struct ndr *n, struct xattr_smb_acl *acl) |
| 297 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 298 | int i, ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 299 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 300 | ret = ndr_write_int32(n, acl->count); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | |
Hyunchul Lee | c222032 | 2021-06-25 07:02:08 +0900 | [diff] [blame] | 304 | n->offset = ALIGN(n->offset, 8); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 305 | ret = ndr_write_int32(n, acl->count); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | |
| 309 | ret = ndr_write_int32(n, 0); |
| 310 | if (ret) |
| 311 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 312 | |
| 313 | for (i = 0; i < acl->count; i++) { |
Hyunchul Lee | c222032 | 2021-06-25 07:02:08 +0900 | [diff] [blame] | 314 | n->offset = ALIGN(n->offset, 8); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 315 | ret = ndr_write_int16(n, acl->entries[i].type); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | |
| 319 | ret = ndr_write_int16(n, acl->entries[i].type); |
| 320 | if (ret) |
| 321 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 322 | |
| 323 | if (acl->entries[i].type == SMB_ACL_USER) { |
Hyunchul Lee | c222032 | 2021-06-25 07:02:08 +0900 | [diff] [blame] | 324 | n->offset = ALIGN(n->offset, 8); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 325 | ret = ndr_write_int64(n, acl->entries[i].uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 326 | } else if (acl->entries[i].type == SMB_ACL_GROUP) { |
Hyunchul Lee | c222032 | 2021-06-25 07:02:08 +0900 | [diff] [blame] | 327 | n->offset = ALIGN(n->offset, 8); |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 328 | ret = ndr_write_int64(n, acl->entries[i].gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 329 | } |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 330 | if (ret) |
| 331 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 332 | |
| 333 | /* push permission */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 334 | ret = ndr_write_int32(n, acl->entries[i].perm); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 335 | } |
| 336 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 337 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 338 | } |
| 339 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 340 | int ndr_encode_posix_acl(struct ndr *n, |
| 341 | struct user_namespace *user_ns, |
| 342 | struct inode *inode, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 343 | struct xattr_smb_acl *acl, |
| 344 | struct xattr_smb_acl *def_acl) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 345 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 346 | unsigned int ref_id = 0x00020000; |
| 347 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 348 | |
| 349 | n->offset = 0; |
| 350 | n->length = 1024; |
| 351 | n->data = kzalloc(n->length, GFP_KERNEL); |
| 352 | if (!n->data) |
| 353 | return -ENOMEM; |
| 354 | |
| 355 | if (acl) { |
| 356 | /* ACL ACCESS */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 357 | ret = ndr_write_int32(n, ref_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 358 | ref_id += 4; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 359 | } else { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 360 | ret = ndr_write_int32(n, 0); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 361 | } |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 362 | if (ret) |
| 363 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 364 | |
| 365 | if (def_acl) { |
| 366 | /* DEFAULT ACL ACCESS */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 367 | ret = ndr_write_int32(n, ref_id); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 368 | ref_id += 4; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 369 | } else { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 370 | ret = ndr_write_int32(n, 0); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 371 | } |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 372 | if (ret) |
| 373 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 374 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 375 | ret = ndr_write_int64(n, from_kuid(&init_user_ns, i_uid_into_mnt(user_ns, inode))); |
| 376 | if (ret) |
| 377 | return ret; |
| 378 | ret = ndr_write_int64(n, from_kgid(&init_user_ns, i_gid_into_mnt(user_ns, inode))); |
| 379 | if (ret) |
| 380 | return ret; |
| 381 | ret = ndr_write_int32(n, inode->i_mode); |
| 382 | if (ret) |
| 383 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 384 | |
| 385 | if (acl) { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 386 | ret = ndr_encode_posix_acl_entry(n, acl); |
| 387 | if (def_acl && !ret) |
| 388 | ret = ndr_encode_posix_acl_entry(n, def_acl); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 389 | } |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 390 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | int ndr_encode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl) |
| 394 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 395 | unsigned int ref_id = 0x00020004; |
| 396 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 397 | |
| 398 | n->offset = 0; |
| 399 | n->length = 2048; |
| 400 | n->data = kzalloc(n->length, GFP_KERNEL); |
| 401 | if (!n->data) |
| 402 | return -ENOMEM; |
| 403 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 404 | ret = ndr_write_int16(n, acl->version); |
| 405 | if (ret) |
| 406 | return ret; |
| 407 | |
| 408 | ret = ndr_write_int32(n, acl->version); |
| 409 | if (ret) |
| 410 | return ret; |
| 411 | |
| 412 | ret = ndr_write_int16(n, 2); |
| 413 | if (ret) |
| 414 | return ret; |
| 415 | |
| 416 | ret = ndr_write_int32(n, ref_id); |
| 417 | if (ret) |
| 418 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 419 | |
| 420 | /* push hash type and hash 64bytes */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 421 | ret = ndr_write_int16(n, acl->hash_type); |
| 422 | if (ret) |
| 423 | return ret; |
| 424 | |
| 425 | ret = ndr_write_bytes(n, acl->hash, XATTR_SD_HASH_SIZE); |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | |
| 429 | ret = ndr_write_bytes(n, acl->desc, acl->desc_len); |
| 430 | if (ret) |
| 431 | return ret; |
| 432 | |
| 433 | ret = ndr_write_int64(n, acl->current_time); |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | |
| 437 | ret = ndr_write_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE); |
| 438 | if (ret) |
| 439 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 440 | |
| 441 | /* push ndr for security descriptor */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 442 | ret = ndr_write_bytes(n, acl->sd_buf, acl->sd_size); |
| 443 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | int ndr_decode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl) |
| 447 | { |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 448 | unsigned int version2; |
| 449 | int ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 450 | |
| 451 | n->offset = 0; |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 452 | ret = ndr_read_int16(n, &acl->version); |
| 453 | if (ret) |
| 454 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 455 | if (acl->version != 4) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 456 | pr_err("v%d version is not supported\n", acl->version); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 457 | return -EINVAL; |
| 458 | } |
| 459 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 460 | ret = ndr_read_int32(n, &version2); |
| 461 | if (ret) |
| 462 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 463 | if (acl->version != version2) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 464 | pr_err("ndr version mismatched(version: %d, version2: %d)\n", |
| 465 | acl->version, version2); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 466 | return -EINVAL; |
| 467 | } |
| 468 | |
| 469 | /* Read Level */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 470 | ret = ndr_read_int16(n, NULL); |
| 471 | if (ret) |
| 472 | return ret; |
| 473 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 474 | /* Read Ref Id */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 475 | ret = ndr_read_int32(n, NULL); |
| 476 | if (ret) |
| 477 | return ret; |
| 478 | |
| 479 | ret = ndr_read_int16(n, &acl->hash_type); |
| 480 | if (ret) |
| 481 | return ret; |
| 482 | |
| 483 | ret = ndr_read_bytes(n, acl->hash, XATTR_SD_HASH_SIZE); |
| 484 | if (ret) |
| 485 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 486 | |
| 487 | ndr_read_bytes(n, acl->desc, 10); |
| 488 | if (strncmp(acl->desc, "posix_acl", 9)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 489 | pr_err("Invalid acl description : %s\n", acl->desc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | /* Read Time */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 494 | ret = ndr_read_int64(n, NULL); |
| 495 | if (ret) |
| 496 | return ret; |
| 497 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 498 | /* Read Posix ACL hash */ |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 499 | ret = ndr_read_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE); |
| 500 | if (ret) |
| 501 | return ret; |
| 502 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 503 | acl->sd_size = n->length - n->offset; |
| 504 | acl->sd_buf = kzalloc(acl->sd_size, GFP_KERNEL); |
| 505 | if (!acl->sd_buf) |
| 506 | return -ENOMEM; |
| 507 | |
Namjae Jeon | 303fff2 | 2021-09-03 07:09:44 +0900 | [diff] [blame] | 508 | ret = ndr_read_bytes(n, acl->sd_buf, acl->sd_size); |
| 509 | return ret; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 510 | } |