blob: 5052be9261d91efb810757256efb913eb97ebc7b [file] [log] [blame]
Namjae Jeone2f34482021-03-16 10:49:09 +09001// 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 Leecb5b0472021-06-25 07:02:09 +090012static inline char *ndr_get_field(struct ndr *n)
13{
14 return n->data + n->offset;
15}
Namjae Jeone2f34482021-03-16 10:49:09 +090016
Namjae Jeone2f34482021-03-16 10:49:09 +090017static 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 Jeon303fff22021-09-03 07:09:44 +090031static int ndr_write_int16(struct ndr *n, __u16 value)
Namjae Jeone2f34482021-03-16 10:49:09 +090032{
Namjae Jeon303fff22021-09-03 07:09:44 +090033 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 Jeone2f34482021-03-16 10:49:09 +090040
Hyunchul Leecb5b0472021-06-25 07:02:09 +090041 *(__le16 *)ndr_get_field(n) = cpu_to_le16(value);
Namjae Jeone2f34482021-03-16 10:49:09 +090042 n->offset += sizeof(value);
Namjae Jeon303fff22021-09-03 07:09:44 +090043 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +090044}
45
Namjae Jeon303fff22021-09-03 07:09:44 +090046static int ndr_write_int32(struct ndr *n, __u32 value)
Namjae Jeone2f34482021-03-16 10:49:09 +090047{
Namjae Jeon303fff22021-09-03 07:09:44 +090048 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 Jeone2f34482021-03-16 10:49:09 +090055
Hyunchul Leecb5b0472021-06-25 07:02:09 +090056 *(__le32 *)ndr_get_field(n) = cpu_to_le32(value);
Namjae Jeone2f34482021-03-16 10:49:09 +090057 n->offset += sizeof(value);
Namjae Jeon303fff22021-09-03 07:09:44 +090058 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +090059}
60
Namjae Jeon303fff22021-09-03 07:09:44 +090061static int ndr_write_int64(struct ndr *n, __u64 value)
Namjae Jeone2f34482021-03-16 10:49:09 +090062{
Namjae Jeon303fff22021-09-03 07:09:44 +090063 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 Jeone2f34482021-03-16 10:49:09 +090070
Hyunchul Leecb5b0472021-06-25 07:02:09 +090071 *(__le64 *)ndr_get_field(n) = cpu_to_le64(value);
Namjae Jeone2f34482021-03-16 10:49:09 +090072 n->offset += sizeof(value);
Namjae Jeon303fff22021-09-03 07:09:44 +090073 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +090074}
75
76static int ndr_write_bytes(struct ndr *n, void *value, size_t sz)
77{
Namjae Jeon303fff22021-09-03 07:09:44 +090078 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 Jeone2f34482021-03-16 10:49:09 +090085
Hyunchul Leecb5b0472021-06-25 07:02:09 +090086 memcpy(ndr_get_field(n), value, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +090087 n->offset += sz;
88 return 0;
89}
90
Hyunchul Lee1d904ea2021-07-23 13:01:06 +090091static int ndr_write_string(struct ndr *n, char *value)
Namjae Jeone2f34482021-03-16 10:49:09 +090092{
Hyunchul Lee1d904ea2021-07-23 13:01:06 +090093 size_t sz;
94
95 sz = strlen(value) + 1;
Namjae Jeon303fff22021-09-03 07:09:44 +090096 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 Jeone2f34482021-03-16 10:49:09 +0900103
Hyunchul Lee1d904ea2021-07-23 13:01:06 +0900104 memcpy(ndr_get_field(n), value, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +0900105 n->offset += sz;
Hyunchul Leec2220322021-06-25 07:02:08 +0900106 n->offset = ALIGN(n->offset, 2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900107 return 0;
108}
109
110static int ndr_read_string(struct ndr *n, void *value, size_t sz)
111{
Namjae Jeon303fff22021-09-03 07:09:44 +0900112 int len;
Namjae Jeone2f34482021-03-16 10:49:09 +0900113
Namjae Jeon303fff22021-09-03 07:09:44 +0900114 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 Jeone2f34482021-03-16 10:49:09 +0900120 len++;
121 n->offset += len;
Hyunchul Leec2220322021-06-25 07:02:08 +0900122 n->offset = ALIGN(n->offset, 2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900123 return 0;
124}
125
126static int ndr_read_bytes(struct ndr *n, void *value, size_t sz)
127{
Namjae Jeon303fff22021-09-03 07:09:44 +0900128 if (n->offset + sz > n->length)
129 return -EINVAL;
130
131 if (value)
132 memcpy(value, ndr_get_field(n), sz);
Namjae Jeone2f34482021-03-16 10:49:09 +0900133 n->offset += sz;
134 return 0;
135}
136
Namjae Jeon303fff22021-09-03 07:09:44 +0900137static int ndr_read_int16(struct ndr *n, __u16 *value)
Namjae Jeone2f34482021-03-16 10:49:09 +0900138{
Namjae Jeon303fff22021-09-03 07:09:44 +0900139 if (n->offset + sizeof(__u16) > n->length)
140 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900141
Namjae Jeon303fff22021-09-03 07:09:44 +0900142 if (value)
143 *value = le16_to_cpu(*(__le16 *)ndr_get_field(n));
Namjae Jeone2f34482021-03-16 10:49:09 +0900144 n->offset += sizeof(__u16);
Namjae Jeon303fff22021-09-03 07:09:44 +0900145 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900146}
147
Namjae Jeon303fff22021-09-03 07:09:44 +0900148static int ndr_read_int32(struct ndr *n, __u32 *value)
Namjae Jeone2f34482021-03-16 10:49:09 +0900149{
Namjae Jeon303fff22021-09-03 07:09:44 +0900150 if (n->offset + sizeof(__u32) > n->length)
Dan Carpenteref399462021-11-30 15:50:47 +0300151 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900152
Namjae Jeon303fff22021-09-03 07:09:44 +0900153 if (value)
154 *value = le32_to_cpu(*(__le32 *)ndr_get_field(n));
Namjae Jeone2f34482021-03-16 10:49:09 +0900155 n->offset += sizeof(__u32);
Namjae Jeon303fff22021-09-03 07:09:44 +0900156 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900157}
158
Namjae Jeon303fff22021-09-03 07:09:44 +0900159static int ndr_read_int64(struct ndr *n, __u64 *value)
Namjae Jeone2f34482021-03-16 10:49:09 +0900160{
Namjae Jeon303fff22021-09-03 07:09:44 +0900161 if (n->offset + sizeof(__u64) > n->length)
162 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900163
Namjae Jeon303fff22021-09-03 07:09:44 +0900164 if (value)
165 *value = le64_to_cpu(*(__le64 *)ndr_get_field(n));
Namjae Jeone2f34482021-03-16 10:49:09 +0900166 n->offset += sizeof(__u64);
Namjae Jeon303fff22021-09-03 07:09:44 +0900167 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900168}
169
170int ndr_encode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
171{
172 char hex_attr[12] = {0};
Namjae Jeon303fff22021-09-03 07:09:44 +0900173 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900174
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 Jeon303fff22021-09-03 07:09:44 +0900183 ret = ndr_write_string(n, hex_attr);
Namjae Jeone2f34482021-03-16 10:49:09 +0900184 } else {
Namjae Jeon303fff22021-09-03 07:09:44 +0900185 ret = ndr_write_string(n, "");
Namjae Jeone2f34482021-03-16 10:49:09 +0900186 }
Namjae Jeon303fff22021-09-03 07:09:44 +0900187 if (ret)
188 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900189
Namjae Jeon303fff22021-09-03 07:09:44 +0900190 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 Jeone2f34482021-03-16 10:49:09 +0900206 if (da->version == 3) {
Namjae Jeon303fff22021-09-03 07:09:44 +0900207 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 Jeon64b39f42021-03-30 14:25:35 +0900214 } else {
Namjae Jeon303fff22021-09-03 07:09:44 +0900215 ret = ndr_write_int64(n, da->itime);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900216 }
Namjae Jeon303fff22021-09-03 07:09:44 +0900217 if (ret)
218 return ret;
219
220 ret = ndr_write_int64(n, da->create_time);
221 if (ret)
222 return ret;
223
Namjae Jeone2f34482021-03-16 10:49:09 +0900224 if (da->version == 3)
Namjae Jeon303fff22021-09-03 07:09:44 +0900225 ret = ndr_write_int64(n, da->change_time);
226 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900227}
228
229int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
230{
Namjae Jeon303fff22021-09-03 07:09:44 +0900231 char hex_attr[12];
232 unsigned int version2;
233 int ret;
Namjae Jeon7d5d8d72021-08-27 10:18:05 +0900234
Namjae Jeone2f34482021-03-16 10:49:09 +0900235 n->offset = 0;
Namjae Jeon303fff22021-09-03 07:09:44 +0900236 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 Jeone2f34482021-03-16 10:49:09 +0900243
244 if (da->version != 3 && da->version != 4) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900245 pr_err("v%d version is not supported\n", da->version);
Namjae Jeone2f34482021-03-16 10:49:09 +0900246 return -EINVAL;
247 }
248
Namjae Jeon303fff22021-09-03 07:09:44 +0900249 ret = ndr_read_int32(n, &version2);
250 if (ret)
251 return ret;
252
Namjae Jeone2f34482021-03-16 10:49:09 +0900253 if (da->version != version2) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900254 pr_err("ndr version mismatched(version: %d, version2: %d)\n",
255 da->version, version2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900256 return -EINVAL;
257 }
258
Namjae Jeon303fff22021-09-03 07:09:44 +0900259 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 Jeone2f34482021-03-16 10:49:09 +0900267 if (da->version == 4) {
Namjae Jeon303fff22021-09-03 07:09:44 +0900268 ret = ndr_read_int64(n, &da->itime);
269 if (ret)
270 return ret;
271
272 ret = ndr_read_int64(n, &da->create_time);
Namjae Jeone2f34482021-03-16 10:49:09 +0900273 } else {
Namjae Jeon303fff22021-09-03 07:09:44 +0900274 ret = ndr_read_int32(n, NULL);
275 if (ret)
276 return ret;
277
Colin Ian King36bbeb32021-09-06 14:44:38 +0100278 ret = ndr_read_int64(n, NULL);
Namjae Jeon303fff22021-09-03 07:09:44 +0900279 if (ret)
280 return ret;
281
Colin Ian King36bbeb32021-09-06 14:44:38 +0100282 ret = ndr_read_int64(n, NULL);
Namjae Jeon303fff22021-09-03 07:09:44 +0900283 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 Jeone2f34482021-03-16 10:49:09 +0900291 }
292
Namjae Jeon303fff22021-09-03 07:09:44 +0900293 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900294}
295
296static int ndr_encode_posix_acl_entry(struct ndr *n, struct xattr_smb_acl *acl)
297{
Namjae Jeon303fff22021-09-03 07:09:44 +0900298 int i, ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900299
Namjae Jeon303fff22021-09-03 07:09:44 +0900300 ret = ndr_write_int32(n, acl->count);
301 if (ret)
302 return ret;
303
Hyunchul Leec2220322021-06-25 07:02:08 +0900304 n->offset = ALIGN(n->offset, 8);
Namjae Jeon303fff22021-09-03 07:09:44 +0900305 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 Jeone2f34482021-03-16 10:49:09 +0900312
313 for (i = 0; i < acl->count; i++) {
Hyunchul Leec2220322021-06-25 07:02:08 +0900314 n->offset = ALIGN(n->offset, 8);
Namjae Jeon303fff22021-09-03 07:09:44 +0900315 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 Jeone2f34482021-03-16 10:49:09 +0900322
323 if (acl->entries[i].type == SMB_ACL_USER) {
Hyunchul Leec2220322021-06-25 07:02:08 +0900324 n->offset = ALIGN(n->offset, 8);
Namjae Jeon303fff22021-09-03 07:09:44 +0900325 ret = ndr_write_int64(n, acl->entries[i].uid);
Namjae Jeone2f34482021-03-16 10:49:09 +0900326 } else if (acl->entries[i].type == SMB_ACL_GROUP) {
Hyunchul Leec2220322021-06-25 07:02:08 +0900327 n->offset = ALIGN(n->offset, 8);
Namjae Jeon303fff22021-09-03 07:09:44 +0900328 ret = ndr_write_int64(n, acl->entries[i].gid);
Namjae Jeone2f34482021-03-16 10:49:09 +0900329 }
Namjae Jeon303fff22021-09-03 07:09:44 +0900330 if (ret)
331 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900332
333 /* push permission */
Namjae Jeon303fff22021-09-03 07:09:44 +0900334 ret = ndr_write_int32(n, acl->entries[i].perm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900335 }
336
Namjae Jeon303fff22021-09-03 07:09:44 +0900337 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900338}
339
Hyunchul Leeaf349832021-06-30 18:25:53 +0900340int ndr_encode_posix_acl(struct ndr *n,
341 struct user_namespace *user_ns,
342 struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +0900343 struct xattr_smb_acl *acl,
344 struct xattr_smb_acl *def_acl)
Namjae Jeone2f34482021-03-16 10:49:09 +0900345{
Namjae Jeon303fff22021-09-03 07:09:44 +0900346 unsigned int ref_id = 0x00020000;
347 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900348
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 Jeon303fff22021-09-03 07:09:44 +0900357 ret = ndr_write_int32(n, ref_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900358 ref_id += 4;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900359 } else {
Namjae Jeon303fff22021-09-03 07:09:44 +0900360 ret = ndr_write_int32(n, 0);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900361 }
Namjae Jeon303fff22021-09-03 07:09:44 +0900362 if (ret)
363 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900364
365 if (def_acl) {
366 /* DEFAULT ACL ACCESS */
Namjae Jeon303fff22021-09-03 07:09:44 +0900367 ret = ndr_write_int32(n, ref_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900368 ref_id += 4;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900369 } else {
Namjae Jeon303fff22021-09-03 07:09:44 +0900370 ret = ndr_write_int32(n, 0);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900371 }
Namjae Jeon303fff22021-09-03 07:09:44 +0900372 if (ret)
373 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900374
Namjae Jeon303fff22021-09-03 07:09:44 +0900375 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 Jeone2f34482021-03-16 10:49:09 +0900384
385 if (acl) {
Namjae Jeon303fff22021-09-03 07:09:44 +0900386 ret = ndr_encode_posix_acl_entry(n, acl);
387 if (def_acl && !ret)
388 ret = ndr_encode_posix_acl_entry(n, def_acl);
Namjae Jeone2f34482021-03-16 10:49:09 +0900389 }
Namjae Jeon303fff22021-09-03 07:09:44 +0900390 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900391}
392
393int ndr_encode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
394{
Namjae Jeon303fff22021-09-03 07:09:44 +0900395 unsigned int ref_id = 0x00020004;
396 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900397
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 Jeon303fff22021-09-03 07:09:44 +0900404 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 Jeone2f34482021-03-16 10:49:09 +0900419
420 /* push hash type and hash 64bytes */
Namjae Jeon303fff22021-09-03 07:09:44 +0900421 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 Jeone2f34482021-03-16 10:49:09 +0900440
441 /* push ndr for security descriptor */
Namjae Jeon303fff22021-09-03 07:09:44 +0900442 ret = ndr_write_bytes(n, acl->sd_buf, acl->sd_size);
443 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900444}
445
446int ndr_decode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
447{
Namjae Jeon303fff22021-09-03 07:09:44 +0900448 unsigned int version2;
449 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900450
451 n->offset = 0;
Namjae Jeon303fff22021-09-03 07:09:44 +0900452 ret = ndr_read_int16(n, &acl->version);
453 if (ret)
454 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900455 if (acl->version != 4) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900456 pr_err("v%d version is not supported\n", acl->version);
Namjae Jeone2f34482021-03-16 10:49:09 +0900457 return -EINVAL;
458 }
459
Namjae Jeon303fff22021-09-03 07:09:44 +0900460 ret = ndr_read_int32(n, &version2);
461 if (ret)
462 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900463 if (acl->version != version2) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900464 pr_err("ndr version mismatched(version: %d, version2: %d)\n",
465 acl->version, version2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900466 return -EINVAL;
467 }
468
469 /* Read Level */
Namjae Jeon303fff22021-09-03 07:09:44 +0900470 ret = ndr_read_int16(n, NULL);
471 if (ret)
472 return ret;
473
Namjae Jeone2f34482021-03-16 10:49:09 +0900474 /* Read Ref Id */
Namjae Jeon303fff22021-09-03 07:09:44 +0900475 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 Jeone2f34482021-03-16 10:49:09 +0900486
487 ndr_read_bytes(n, acl->desc, 10);
488 if (strncmp(acl->desc, "posix_acl", 9)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900489 pr_err("Invalid acl description : %s\n", acl->desc);
Namjae Jeone2f34482021-03-16 10:49:09 +0900490 return -EINVAL;
491 }
492
493 /* Read Time */
Namjae Jeon303fff22021-09-03 07:09:44 +0900494 ret = ndr_read_int64(n, NULL);
495 if (ret)
496 return ret;
497
Namjae Jeone2f34482021-03-16 10:49:09 +0900498 /* Read Posix ACL hash */
Namjae Jeon303fff22021-09-03 07:09:44 +0900499 ret = ndr_read_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE);
500 if (ret)
501 return ret;
502
Namjae Jeone2f34482021-03-16 10:49:09 +0900503 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 Jeon303fff22021-09-03 07:09:44 +0900508 ret = ndr_read_bytes(n, acl->sd_buf, acl->sd_size);
509 return ret;
Namjae Jeone2f34482021-03-16 10:49:09 +0900510}