1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 Samsung Electronics Co., Ltd.
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 void ndr_write_int16(struct ndr *n, __u16 value)
33 if (n->length <= n->offset + sizeof(value))
34 try_to_realloc_ndr_blob(n, sizeof(value));
36 *(__le16 *)ndr_get_field(n) = cpu_to_le16(value);
37 n->offset += sizeof(value);
40 static void ndr_write_int32(struct ndr *n, __u32 value)
42 if (n->length <= n->offset + sizeof(value))
43 try_to_realloc_ndr_blob(n, sizeof(value));
45 *(__le32 *)ndr_get_field(n) = cpu_to_le32(value);
46 n->offset += sizeof(value);
49 static void ndr_write_int64(struct ndr *n, __u64 value)
51 if (n->length <= n->offset + sizeof(value))
52 try_to_realloc_ndr_blob(n, sizeof(value));
54 *(__le64 *)ndr_get_field(n) = cpu_to_le64(value);
55 n->offset += sizeof(value);
58 static int ndr_write_bytes(struct ndr *n, void *value, size_t sz)
60 if (n->length <= n->offset + sz)
61 try_to_realloc_ndr_blob(n, sz);
63 memcpy(ndr_get_field(n), value, sz);
68 static int ndr_write_string(struct ndr *n, char *value)
72 sz = strlen(value) + 1;
73 if (n->length <= n->offset + sz)
74 try_to_realloc_ndr_blob(n, sz);
76 memcpy(ndr_get_field(n), value, sz);
78 n->offset = ALIGN(n->offset, 2);
82 static int ndr_read_string(struct ndr *n, void *value, size_t sz)
84 int len = strnlen(ndr_get_field(n), sz);
86 memcpy(value, ndr_get_field(n), len);
89 n->offset = ALIGN(n->offset, 2);
93 static int ndr_read_bytes(struct ndr *n, void *value, size_t sz)
95 memcpy(value, ndr_get_field(n), sz);
100 static __u16 ndr_read_int16(struct ndr *n)
104 ret = le16_to_cpu(*(__le16 *)ndr_get_field(n));
105 n->offset += sizeof(__u16);
109 static __u32 ndr_read_int32(struct ndr *n)
113 ret = le32_to_cpu(*(__le32 *)ndr_get_field(n));
114 n->offset += sizeof(__u32);
118 static __u64 ndr_read_int64(struct ndr *n)
122 ret = le64_to_cpu(*(__le64 *)ndr_get_field(n));
123 n->offset += sizeof(__u64);
127 int ndr_encode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
129 char hex_attr[12] = {0};
133 n->data = kzalloc(n->length, GFP_KERNEL);
137 if (da->version == 3) {
138 snprintf(hex_attr, 10, "0x%x", da->attr);
139 ndr_write_string(n, hex_attr);
141 ndr_write_string(n, "");
143 ndr_write_int16(n, da->version);
144 ndr_write_int32(n, da->version);
146 ndr_write_int32(n, da->flags);
147 ndr_write_int32(n, da->attr);
148 if (da->version == 3) {
149 ndr_write_int32(n, da->ea_size);
150 ndr_write_int64(n, da->size);
151 ndr_write_int64(n, da->alloc_size);
153 ndr_write_int64(n, da->itime);
155 ndr_write_int64(n, da->create_time);
156 if (da->version == 3)
157 ndr_write_int64(n, da->change_time);
161 int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
166 hex_attr = kzalloc(n->length, GFP_KERNEL);
171 ndr_read_string(n, hex_attr, n->length);
173 da->version = ndr_read_int16(n);
175 if (da->version != 3 && da->version != 4) {
176 pr_err("v%d version is not supported\n", da->version);
180 version2 = ndr_read_int32(n);
181 if (da->version != version2) {
182 pr_err("ndr version mismatched(version: %d, version2: %d)\n",
183 da->version, version2);
188 da->attr = ndr_read_int32(n);
189 if (da->version == 4) {
190 da->itime = ndr_read_int64(n);
191 da->create_time = ndr_read_int64(n);
196 da->create_time = ndr_read_int64(n);
203 static int ndr_encode_posix_acl_entry(struct ndr *n, struct xattr_smb_acl *acl)
207 ndr_write_int32(n, acl->count);
208 n->offset = ALIGN(n->offset, 8);
209 ndr_write_int32(n, acl->count);
210 ndr_write_int32(n, 0);
212 for (i = 0; i < acl->count; i++) {
213 n->offset = ALIGN(n->offset, 8);
214 ndr_write_int16(n, acl->entries[i].type);
215 ndr_write_int16(n, acl->entries[i].type);
217 if (acl->entries[i].type == SMB_ACL_USER) {
218 n->offset = ALIGN(n->offset, 8);
219 ndr_write_int64(n, acl->entries[i].uid);
220 } else if (acl->entries[i].type == SMB_ACL_GROUP) {
221 n->offset = ALIGN(n->offset, 8);
222 ndr_write_int64(n, acl->entries[i].gid);
225 /* push permission */
226 ndr_write_int32(n, acl->entries[i].perm);
232 int ndr_encode_posix_acl(struct ndr *n,
233 struct user_namespace *user_ns,
235 struct xattr_smb_acl *acl,
236 struct xattr_smb_acl *def_acl)
238 int ref_id = 0x00020000;
242 n->data = kzalloc(n->length, GFP_KERNEL);
248 ndr_write_int32(n, ref_id);
251 ndr_write_int32(n, 0);
255 /* DEFAULT ACL ACCESS */
256 ndr_write_int32(n, ref_id);
259 ndr_write_int32(n, 0);
262 ndr_write_int64(n, from_kuid(user_ns, inode->i_uid));
263 ndr_write_int64(n, from_kgid(user_ns, inode->i_gid));
264 ndr_write_int32(n, inode->i_mode);
267 ndr_encode_posix_acl_entry(n, acl);
269 ndr_encode_posix_acl_entry(n, def_acl);
274 int ndr_encode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
276 int ref_id = 0x00020004;
280 n->data = kzalloc(n->length, GFP_KERNEL);
284 ndr_write_int16(n, acl->version);
285 ndr_write_int32(n, acl->version);
286 ndr_write_int16(n, 2);
287 ndr_write_int32(n, ref_id);
289 /* push hash type and hash 64bytes */
290 ndr_write_int16(n, acl->hash_type);
291 ndr_write_bytes(n, acl->hash, XATTR_SD_HASH_SIZE);
292 ndr_write_bytes(n, acl->desc, acl->desc_len);
293 ndr_write_int64(n, acl->current_time);
294 ndr_write_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE);
296 /* push ndr for security descriptor */
297 ndr_write_bytes(n, acl->sd_buf, acl->sd_size);
302 int ndr_decode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
307 acl->version = ndr_read_int16(n);
308 if (acl->version != 4) {
309 pr_err("v%d version is not supported\n", acl->version);
313 version2 = ndr_read_int32(n);
314 if (acl->version != version2) {
315 pr_err("ndr version mismatched(version: %d, version2: %d)\n",
316 acl->version, version2);
324 acl->hash_type = ndr_read_int16(n);
325 ndr_read_bytes(n, acl->hash, XATTR_SD_HASH_SIZE);
327 ndr_read_bytes(n, acl->desc, 10);
328 if (strncmp(acl->desc, "posix_acl", 9)) {
329 pr_err("Invalid acl description : %s\n", acl->desc);
335 /* Read Posix ACL hash */
336 ndr_read_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE);
337 acl->sd_size = n->length - n->offset;
338 acl->sd_buf = kzalloc(acl->sd_size, GFP_KERNEL);
342 ndr_read_bytes(n, acl->sd_buf, acl->sd_size);