1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 Samsung Electronics Co., Ltd.
4 * Author(s): Namjae Jeon <linkinjeon@kernel.org>
12 static inline char *ndr_get_field(struct ndr *n)
14 return n->data + n->offset;
17 static int try_to_realloc_ndr_blob(struct ndr *n, size_t sz)
21 data = krealloc(n->data, n->offset + sz + 1024, GFP_KERNEL);
27 memset(n->data + n->offset, 0, 1024);
31 static int ndr_write_int16(struct ndr *n, __u16 value)
33 if (n->length <= n->offset + sizeof(value)) {
36 ret = try_to_realloc_ndr_blob(n, sizeof(value));
41 *(__le16 *)ndr_get_field(n) = cpu_to_le16(value);
42 n->offset += sizeof(value);
46 static int ndr_write_int32(struct ndr *n, __u32 value)
48 if (n->length <= n->offset + sizeof(value)) {
51 ret = try_to_realloc_ndr_blob(n, sizeof(value));
56 *(__le32 *)ndr_get_field(n) = cpu_to_le32(value);
57 n->offset += sizeof(value);
61 static int ndr_write_int64(struct ndr *n, __u64 value)
63 if (n->length <= n->offset + sizeof(value)) {
66 ret = try_to_realloc_ndr_blob(n, sizeof(value));
71 *(__le64 *)ndr_get_field(n) = cpu_to_le64(value);
72 n->offset += sizeof(value);
76 static int ndr_write_bytes(struct ndr *n, void *value, size_t sz)
78 if (n->length <= n->offset + sz) {
81 ret = try_to_realloc_ndr_blob(n, sz);
86 memcpy(ndr_get_field(n), value, sz);
91 static int ndr_write_string(struct ndr *n, char *value)
95 sz = strlen(value) + 1;
96 if (n->length <= n->offset + sz) {
99 ret = try_to_realloc_ndr_blob(n, sz);
104 memcpy(ndr_get_field(n), value, sz);
106 n->offset = ALIGN(n->offset, 2);
110 static int ndr_read_string(struct ndr *n, void *value, size_t sz)
114 if (n->offset + sz > n->length)
117 len = strnlen(ndr_get_field(n), sz);
119 memcpy(value, ndr_get_field(n), len);
122 n->offset = ALIGN(n->offset, 2);
126 static int ndr_read_bytes(struct ndr *n, void *value, size_t sz)
128 if (n->offset + sz > n->length)
132 memcpy(value, ndr_get_field(n), sz);
137 static int ndr_read_int16(struct ndr *n, __u16 *value)
139 if (n->offset + sizeof(__u16) > n->length)
143 *value = le16_to_cpu(*(__le16 *)ndr_get_field(n));
144 n->offset += sizeof(__u16);
148 static int ndr_read_int32(struct ndr *n, __u32 *value)
150 if (n->offset + sizeof(__u32) > n->length)
154 *value = le32_to_cpu(*(__le32 *)ndr_get_field(n));
155 n->offset += sizeof(__u32);
159 static int ndr_read_int64(struct ndr *n, __u64 *value)
161 if (n->offset + sizeof(__u64) > n->length)
165 *value = le64_to_cpu(*(__le64 *)ndr_get_field(n));
166 n->offset += sizeof(__u64);
170 int ndr_encode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
172 char hex_attr[12] = {0};
177 n->data = kzalloc(n->length, GFP_KERNEL);
181 if (da->version == 3) {
182 snprintf(hex_attr, 10, "0x%x", da->attr);
183 ret = ndr_write_string(n, hex_attr);
185 ret = ndr_write_string(n, "");
190 ret = ndr_write_int16(n, da->version);
194 ret = ndr_write_int32(n, da->version);
198 ret = ndr_write_int32(n, da->flags);
202 ret = ndr_write_int32(n, da->attr);
206 if (da->version == 3) {
207 ret = ndr_write_int32(n, da->ea_size);
210 ret = ndr_write_int64(n, da->size);
213 ret = ndr_write_int64(n, da->alloc_size);
215 ret = ndr_write_int64(n, da->itime);
220 ret = ndr_write_int64(n, da->create_time);
224 if (da->version == 3)
225 ret = ndr_write_int64(n, da->change_time);
229 int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
232 unsigned int version2;
236 ret = ndr_read_string(n, hex_attr, sizeof(hex_attr));
240 ret = ndr_read_int16(n, &da->version);
244 if (da->version != 3 && da->version != 4) {
245 ksmbd_debug(VFS, "v%d version is not supported\n", da->version);
249 ret = ndr_read_int32(n, &version2);
253 if (da->version != version2) {
254 ksmbd_debug(VFS, "ndr version mismatched(version: %d, version2: %d)\n",
255 da->version, version2);
259 ret = ndr_read_int32(n, NULL);
263 ret = ndr_read_int32(n, &da->attr);
267 if (da->version == 4) {
268 ret = ndr_read_int64(n, &da->itime);
272 ret = ndr_read_int64(n, &da->create_time);
274 ret = ndr_read_int32(n, NULL);
278 ret = ndr_read_int64(n, NULL);
282 ret = ndr_read_int64(n, NULL);
286 ret = ndr_read_int64(n, &da->create_time);
290 ret = ndr_read_int64(n, NULL);
296 static int ndr_encode_posix_acl_entry(struct ndr *n, struct xattr_smb_acl *acl)
300 ret = ndr_write_int32(n, acl->count);
304 n->offset = ALIGN(n->offset, 8);
305 ret = ndr_write_int32(n, acl->count);
309 ret = ndr_write_int32(n, 0);
313 for (i = 0; i < acl->count; i++) {
314 n->offset = ALIGN(n->offset, 8);
315 ret = ndr_write_int16(n, acl->entries[i].type);
319 ret = ndr_write_int16(n, acl->entries[i].type);
323 if (acl->entries[i].type == SMB_ACL_USER) {
324 n->offset = ALIGN(n->offset, 8);
325 ret = ndr_write_int64(n, acl->entries[i].uid);
326 } else if (acl->entries[i].type == SMB_ACL_GROUP) {
327 n->offset = ALIGN(n->offset, 8);
328 ret = ndr_write_int64(n, acl->entries[i].gid);
333 /* push permission */
334 ret = ndr_write_int32(n, acl->entries[i].perm);
340 int ndr_encode_posix_acl(struct ndr *n,
341 struct mnt_idmap *idmap,
343 struct xattr_smb_acl *acl,
344 struct xattr_smb_acl *def_acl)
346 unsigned int ref_id = 0x00020000;
353 n->data = kzalloc(n->length, GFP_KERNEL);
359 ret = ndr_write_int32(n, ref_id);
362 ret = ndr_write_int32(n, 0);
368 /* DEFAULT ACL ACCESS */
369 ret = ndr_write_int32(n, ref_id);
372 ret = ndr_write_int32(n, 0);
377 vfsuid = i_uid_into_vfsuid(idmap, inode);
378 ret = ndr_write_int64(n, from_kuid(&init_user_ns, vfsuid_into_kuid(vfsuid)));
381 vfsgid = i_gid_into_vfsgid(idmap, inode);
382 ret = ndr_write_int64(n, from_kgid(&init_user_ns, vfsgid_into_kgid(vfsgid)));
385 ret = ndr_write_int32(n, inode->i_mode);
390 ret = ndr_encode_posix_acl_entry(n, acl);
392 ret = ndr_encode_posix_acl_entry(n, def_acl);
397 int ndr_encode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
399 unsigned int ref_id = 0x00020004;
404 n->data = kzalloc(n->length, GFP_KERNEL);
408 ret = ndr_write_int16(n, acl->version);
412 ret = ndr_write_int32(n, acl->version);
416 ret = ndr_write_int16(n, 2);
420 ret = ndr_write_int32(n, ref_id);
424 /* push hash type and hash 64bytes */
425 ret = ndr_write_int16(n, acl->hash_type);
429 ret = ndr_write_bytes(n, acl->hash, XATTR_SD_HASH_SIZE);
433 ret = ndr_write_bytes(n, acl->desc, acl->desc_len);
437 ret = ndr_write_int64(n, acl->current_time);
441 ret = ndr_write_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE);
445 /* push ndr for security descriptor */
446 ret = ndr_write_bytes(n, acl->sd_buf, acl->sd_size);
450 int ndr_decode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
452 unsigned int version2;
456 ret = ndr_read_int16(n, &acl->version);
459 if (acl->version != 4) {
460 ksmbd_debug(VFS, "v%d version is not supported\n", acl->version);
464 ret = ndr_read_int32(n, &version2);
467 if (acl->version != version2) {
468 ksmbd_debug(VFS, "ndr version mismatched(version: %d, version2: %d)\n",
469 acl->version, version2);
474 ret = ndr_read_int16(n, NULL);
479 ret = ndr_read_int32(n, NULL);
483 ret = ndr_read_int16(n, &acl->hash_type);
487 ret = ndr_read_bytes(n, acl->hash, XATTR_SD_HASH_SIZE);
491 ndr_read_bytes(n, acl->desc, 10);
492 if (strncmp(acl->desc, "posix_acl", 9)) {
493 pr_err("Invalid acl description : %s\n", acl->desc);
498 ret = ndr_read_int64(n, NULL);
502 /* Read Posix ACL hash */
503 ret = ndr_read_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE);
507 acl->sd_size = n->length - n->offset;
508 acl->sd_buf = kzalloc(acl->sd_size, GFP_KERNEL);
512 ret = ndr_read_bytes(n, acl->sd_buf, acl->sd_size);