ndr.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. #include <linux/fs.h>
  7. #include "glob.h"
  8. #include "ndr.h"
  9. static inline char *ndr_get_field(struct ndr *n)
  10. {
  11. return n->data + n->offset;
  12. }
  13. static int try_to_realloc_ndr_blob(struct ndr *n, size_t sz)
  14. {
  15. char *data;
  16. data = krealloc(n->data, n->offset + sz + 1024, KSMBD_DEFAULT_GFP);
  17. if (!data)
  18. return -ENOMEM;
  19. n->data = data;
  20. n->length += 1024;
  21. memset(n->data + n->offset, 0, 1024);
  22. return 0;
  23. }
  24. static int ndr_write_int16(struct ndr *n, __u16 value)
  25. {
  26. if (n->length <= n->offset + sizeof(value)) {
  27. int ret;
  28. ret = try_to_realloc_ndr_blob(n, sizeof(value));
  29. if (ret)
  30. return ret;
  31. }
  32. *(__le16 *)ndr_get_field(n) = cpu_to_le16(value);
  33. n->offset += sizeof(value);
  34. return 0;
  35. }
  36. static int ndr_write_int32(struct ndr *n, __u32 value)
  37. {
  38. if (n->length <= n->offset + sizeof(value)) {
  39. int ret;
  40. ret = try_to_realloc_ndr_blob(n, sizeof(value));
  41. if (ret)
  42. return ret;
  43. }
  44. *(__le32 *)ndr_get_field(n) = cpu_to_le32(value);
  45. n->offset += sizeof(value);
  46. return 0;
  47. }
  48. static int ndr_write_int64(struct ndr *n, __u64 value)
  49. {
  50. if (n->length <= n->offset + sizeof(value)) {
  51. int ret;
  52. ret = try_to_realloc_ndr_blob(n, sizeof(value));
  53. if (ret)
  54. return ret;
  55. }
  56. *(__le64 *)ndr_get_field(n) = cpu_to_le64(value);
  57. n->offset += sizeof(value);
  58. return 0;
  59. }
  60. static int ndr_write_bytes(struct ndr *n, void *value, size_t sz)
  61. {
  62. if (n->length <= n->offset + sz) {
  63. int ret;
  64. ret = try_to_realloc_ndr_blob(n, sz);
  65. if (ret)
  66. return ret;
  67. }
  68. memcpy(ndr_get_field(n), value, sz);
  69. n->offset += sz;
  70. return 0;
  71. }
  72. static int ndr_write_string(struct ndr *n, char *value)
  73. {
  74. size_t sz;
  75. sz = strlen(value) + 1;
  76. if (n->length <= n->offset + sz) {
  77. int ret;
  78. ret = try_to_realloc_ndr_blob(n, sz);
  79. if (ret)
  80. return ret;
  81. }
  82. memcpy(ndr_get_field(n), value, sz);
  83. n->offset += sz;
  84. n->offset = ALIGN(n->offset, 2);
  85. return 0;
  86. }
  87. static int ndr_read_string(struct ndr *n, void *value, size_t sz)
  88. {
  89. int len;
  90. if (n->offset + sz > n->length)
  91. return -EINVAL;
  92. len = strnlen(ndr_get_field(n), sz);
  93. if (value)
  94. memcpy(value, ndr_get_field(n), len);
  95. len++;
  96. n->offset += len;
  97. n->offset = ALIGN(n->offset, 2);
  98. return 0;
  99. }
  100. static int ndr_read_bytes(struct ndr *n, void *value, size_t sz)
  101. {
  102. if (n->offset + sz > n->length)
  103. return -EINVAL;
  104. if (value)
  105. memcpy(value, ndr_get_field(n), sz);
  106. n->offset += sz;
  107. return 0;
  108. }
  109. static int ndr_read_int16(struct ndr *n, __u16 *value)
  110. {
  111. if (n->offset + sizeof(__u16) > n->length)
  112. return -EINVAL;
  113. if (value)
  114. *value = le16_to_cpu(*(__le16 *)ndr_get_field(n));
  115. n->offset += sizeof(__u16);
  116. return 0;
  117. }
  118. static int ndr_read_int32(struct ndr *n, __u32 *value)
  119. {
  120. if (n->offset + sizeof(__u32) > n->length)
  121. return -EINVAL;
  122. if (value)
  123. *value = le32_to_cpu(*(__le32 *)ndr_get_field(n));
  124. n->offset += sizeof(__u32);
  125. return 0;
  126. }
  127. static int ndr_read_int64(struct ndr *n, __u64 *value)
  128. {
  129. if (n->offset + sizeof(__u64) > n->length)
  130. return -EINVAL;
  131. if (value)
  132. *value = le64_to_cpu(*(__le64 *)ndr_get_field(n));
  133. n->offset += sizeof(__u64);
  134. return 0;
  135. }
  136. int ndr_encode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
  137. {
  138. char hex_attr[12] = {0};
  139. int ret;
  140. n->offset = 0;
  141. n->length = 1024;
  142. n->data = kzalloc(n->length, KSMBD_DEFAULT_GFP);
  143. if (!n->data)
  144. return -ENOMEM;
  145. if (da->version == 3) {
  146. snprintf(hex_attr, 10, "0x%x", da->attr);
  147. ret = ndr_write_string(n, hex_attr);
  148. } else {
  149. ret = ndr_write_string(n, "");
  150. }
  151. if (ret)
  152. return ret;
  153. ret = ndr_write_int16(n, da->version);
  154. if (ret)
  155. return ret;
  156. ret = ndr_write_int32(n, da->version);
  157. if (ret)
  158. return ret;
  159. ret = ndr_write_int32(n, da->flags);
  160. if (ret)
  161. return ret;
  162. ret = ndr_write_int32(n, da->attr);
  163. if (ret)
  164. return ret;
  165. if (da->version == 3) {
  166. ret = ndr_write_int32(n, da->ea_size);
  167. if (ret)
  168. return ret;
  169. ret = ndr_write_int64(n, da->size);
  170. if (ret)
  171. return ret;
  172. ret = ndr_write_int64(n, da->alloc_size);
  173. } else {
  174. ret = ndr_write_int64(n, da->itime);
  175. }
  176. if (ret)
  177. return ret;
  178. ret = ndr_write_int64(n, da->create_time);
  179. if (ret)
  180. return ret;
  181. if (da->version == 3)
  182. ret = ndr_write_int64(n, da->change_time);
  183. return ret;
  184. }
  185. int ndr_decode_dos_attr(struct ndr *n, struct xattr_dos_attrib *da)
  186. {
  187. char hex_attr[12];
  188. unsigned int version2;
  189. int ret;
  190. n->offset = 0;
  191. ret = ndr_read_string(n, hex_attr, sizeof(hex_attr));
  192. if (ret)
  193. return ret;
  194. ret = ndr_read_int16(n, &da->version);
  195. if (ret)
  196. return ret;
  197. if (da->version != 3 && da->version != 4) {
  198. ksmbd_debug(VFS, "v%d version is not supported\n", da->version);
  199. return -EINVAL;
  200. }
  201. ret = ndr_read_int32(n, &version2);
  202. if (ret)
  203. return ret;
  204. if (da->version != version2) {
  205. ksmbd_debug(VFS, "ndr version mismatched(version: %d, version2: %d)\n",
  206. da->version, version2);
  207. return -EINVAL;
  208. }
  209. ret = ndr_read_int32(n, NULL);
  210. if (ret)
  211. return ret;
  212. ret = ndr_read_int32(n, &da->attr);
  213. if (ret)
  214. return ret;
  215. if (da->version == 4) {
  216. ret = ndr_read_int64(n, &da->itime);
  217. if (ret)
  218. return ret;
  219. ret = ndr_read_int64(n, &da->create_time);
  220. } else {
  221. ret = ndr_read_int32(n, NULL);
  222. if (ret)
  223. return ret;
  224. ret = ndr_read_int64(n, NULL);
  225. if (ret)
  226. return ret;
  227. ret = ndr_read_int64(n, NULL);
  228. if (ret)
  229. return ret;
  230. ret = ndr_read_int64(n, &da->create_time);
  231. if (ret)
  232. return ret;
  233. ret = ndr_read_int64(n, NULL);
  234. }
  235. return ret;
  236. }
  237. static int ndr_encode_posix_acl_entry(struct ndr *n, struct xattr_smb_acl *acl)
  238. {
  239. int i, ret;
  240. ret = ndr_write_int32(n, acl->count);
  241. if (ret)
  242. return ret;
  243. n->offset = ALIGN(n->offset, 8);
  244. ret = ndr_write_int32(n, acl->count);
  245. if (ret)
  246. return ret;
  247. ret = ndr_write_int32(n, 0);
  248. if (ret)
  249. return ret;
  250. for (i = 0; i < acl->count; i++) {
  251. n->offset = ALIGN(n->offset, 8);
  252. ret = ndr_write_int16(n, acl->entries[i].type);
  253. if (ret)
  254. return ret;
  255. ret = ndr_write_int16(n, acl->entries[i].type);
  256. if (ret)
  257. return ret;
  258. if (acl->entries[i].type == SMB_ACL_USER) {
  259. n->offset = ALIGN(n->offset, 8);
  260. ret = ndr_write_int64(n, acl->entries[i].uid);
  261. } else if (acl->entries[i].type == SMB_ACL_GROUP) {
  262. n->offset = ALIGN(n->offset, 8);
  263. ret = ndr_write_int64(n, acl->entries[i].gid);
  264. }
  265. if (ret)
  266. return ret;
  267. /* push permission */
  268. ret = ndr_write_int32(n, acl->entries[i].perm);
  269. }
  270. return ret;
  271. }
  272. int ndr_encode_posix_acl(struct ndr *n,
  273. struct mnt_idmap *idmap,
  274. struct inode *inode,
  275. struct xattr_smb_acl *acl,
  276. struct xattr_smb_acl *def_acl)
  277. {
  278. unsigned int ref_id = 0x00020000;
  279. int ret;
  280. vfsuid_t vfsuid;
  281. vfsgid_t vfsgid;
  282. n->offset = 0;
  283. n->length = 1024;
  284. n->data = kzalloc(n->length, KSMBD_DEFAULT_GFP);
  285. if (!n->data)
  286. return -ENOMEM;
  287. if (acl) {
  288. /* ACL ACCESS */
  289. ret = ndr_write_int32(n, ref_id);
  290. ref_id += 4;
  291. } else {
  292. ret = ndr_write_int32(n, 0);
  293. }
  294. if (ret)
  295. return ret;
  296. if (def_acl) {
  297. /* DEFAULT ACL ACCESS */
  298. ret = ndr_write_int32(n, ref_id);
  299. ref_id += 4;
  300. } else {
  301. ret = ndr_write_int32(n, 0);
  302. }
  303. if (ret)
  304. return ret;
  305. vfsuid = i_uid_into_vfsuid(idmap, inode);
  306. ret = ndr_write_int64(n, from_kuid(&init_user_ns, vfsuid_into_kuid(vfsuid)));
  307. if (ret)
  308. return ret;
  309. vfsgid = i_gid_into_vfsgid(idmap, inode);
  310. ret = ndr_write_int64(n, from_kgid(&init_user_ns, vfsgid_into_kgid(vfsgid)));
  311. if (ret)
  312. return ret;
  313. ret = ndr_write_int32(n, inode->i_mode);
  314. if (ret)
  315. return ret;
  316. if (acl) {
  317. ret = ndr_encode_posix_acl_entry(n, acl);
  318. if (def_acl && !ret)
  319. ret = ndr_encode_posix_acl_entry(n, def_acl);
  320. }
  321. return ret;
  322. }
  323. int ndr_encode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
  324. {
  325. unsigned int ref_id = 0x00020004;
  326. int ret;
  327. n->offset = 0;
  328. n->length = 2048;
  329. n->data = kzalloc(n->length, KSMBD_DEFAULT_GFP);
  330. if (!n->data)
  331. return -ENOMEM;
  332. ret = ndr_write_int16(n, acl->version);
  333. if (ret)
  334. return ret;
  335. ret = ndr_write_int32(n, acl->version);
  336. if (ret)
  337. return ret;
  338. ret = ndr_write_int16(n, 2);
  339. if (ret)
  340. return ret;
  341. ret = ndr_write_int32(n, ref_id);
  342. if (ret)
  343. return ret;
  344. /* push hash type and hash 64bytes */
  345. ret = ndr_write_int16(n, acl->hash_type);
  346. if (ret)
  347. return ret;
  348. ret = ndr_write_bytes(n, acl->hash, XATTR_SD_HASH_SIZE);
  349. if (ret)
  350. return ret;
  351. ret = ndr_write_bytes(n, acl->desc, acl->desc_len);
  352. if (ret)
  353. return ret;
  354. ret = ndr_write_int64(n, acl->current_time);
  355. if (ret)
  356. return ret;
  357. ret = ndr_write_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE);
  358. if (ret)
  359. return ret;
  360. /* push ndr for security descriptor */
  361. ret = ndr_write_bytes(n, acl->sd_buf, acl->sd_size);
  362. return ret;
  363. }
  364. int ndr_decode_v4_ntacl(struct ndr *n, struct xattr_ntacl *acl)
  365. {
  366. unsigned int version2;
  367. int ret;
  368. n->offset = 0;
  369. ret = ndr_read_int16(n, &acl->version);
  370. if (ret)
  371. return ret;
  372. if (acl->version != 4) {
  373. ksmbd_debug(VFS, "v%d version is not supported\n", acl->version);
  374. return -EINVAL;
  375. }
  376. ret = ndr_read_int32(n, &version2);
  377. if (ret)
  378. return ret;
  379. if (acl->version != version2) {
  380. ksmbd_debug(VFS, "ndr version mismatched(version: %d, version2: %d)\n",
  381. acl->version, version2);
  382. return -EINVAL;
  383. }
  384. /* Read Level */
  385. ret = ndr_read_int16(n, NULL);
  386. if (ret)
  387. return ret;
  388. /* Read Ref Id */
  389. ret = ndr_read_int32(n, NULL);
  390. if (ret)
  391. return ret;
  392. ret = ndr_read_int16(n, &acl->hash_type);
  393. if (ret)
  394. return ret;
  395. ret = ndr_read_bytes(n, acl->hash, XATTR_SD_HASH_SIZE);
  396. if (ret)
  397. return ret;
  398. ndr_read_bytes(n, acl->desc, 10);
  399. if (strncmp(acl->desc, "posix_acl", 9)) {
  400. pr_err("Invalid acl description : %s\n", acl->desc);
  401. return -EINVAL;
  402. }
  403. /* Read Time */
  404. ret = ndr_read_int64(n, NULL);
  405. if (ret)
  406. return ret;
  407. /* Read Posix ACL hash */
  408. ret = ndr_read_bytes(n, acl->posix_acl_hash, XATTR_SD_HASH_SIZE);
  409. if (ret)
  410. return ret;
  411. acl->sd_size = n->length - n->offset;
  412. acl->sd_buf = kzalloc(acl->sd_size, KSMBD_DEFAULT_GFP);
  413. if (!acl->sd_buf)
  414. return -ENOMEM;
  415. ret = ndr_read_bytes(n, acl->sd_buf, acl->sd_size);
  416. return ret;
  417. }