smbacl.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. // SPDX-License-Identifier: LGPL-2.1+
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2007,2008
  4. * Author(s): Steve French (sfrench@us.ibm.com)
  5. * Copyright (C) 2020 Samsung Electronics Co., Ltd.
  6. * Author(s): Namjae Jeon <linkinjeon@kernel.org>
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/string.h>
  11. #include <linux/mnt_idmapping.h>
  12. #include "smbacl.h"
  13. #include "smb_common.h"
  14. #include "server.h"
  15. #include "misc.h"
  16. #include "mgmt/share_config.h"
  17. static const struct smb_sid domain = {1, 4, {0, 0, 0, 0, 0, 5},
  18. {cpu_to_le32(21), cpu_to_le32(1), cpu_to_le32(2), cpu_to_le32(3),
  19. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  20. /* security id for everyone/world system group */
  21. static const struct smb_sid creator_owner = {
  22. 1, 1, {0, 0, 0, 0, 0, 3}, {0} };
  23. /* security id for everyone/world system group */
  24. static const struct smb_sid creator_group = {
  25. 1, 1, {0, 0, 0, 0, 0, 3}, {cpu_to_le32(1)} };
  26. /* security id for everyone/world system group */
  27. static const struct smb_sid sid_everyone = {
  28. 1, 1, {0, 0, 0, 0, 0, 1}, {0} };
  29. /* security id for Authenticated Users system group */
  30. static const struct smb_sid sid_authusers = {
  31. 1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(11)} };
  32. /* S-1-22-1 Unmapped Unix users */
  33. static const struct smb_sid sid_unix_users = {1, 1, {0, 0, 0, 0, 0, 22},
  34. {cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  35. /* S-1-22-2 Unmapped Unix groups */
  36. static const struct smb_sid sid_unix_groups = { 1, 1, {0, 0, 0, 0, 0, 22},
  37. {cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  38. /*
  39. * See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
  40. */
  41. /* S-1-5-88 MS NFS and Apple style UID/GID/mode */
  42. /* S-1-5-88-1 Unix uid */
  43. static const struct smb_sid sid_unix_NFS_users = { 1, 2, {0, 0, 0, 0, 0, 5},
  44. {cpu_to_le32(88),
  45. cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  46. /* S-1-5-88-2 Unix gid */
  47. static const struct smb_sid sid_unix_NFS_groups = { 1, 2, {0, 0, 0, 0, 0, 5},
  48. {cpu_to_le32(88),
  49. cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  50. /* S-1-5-88-3 Unix mode */
  51. static const struct smb_sid sid_unix_NFS_mode = { 1, 2, {0, 0, 0, 0, 0, 5},
  52. {cpu_to_le32(88),
  53. cpu_to_le32(3), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  54. /*
  55. * if the two SIDs (roughly equivalent to a UUID for a user or group) are
  56. * the same returns zero, if they do not match returns non-zero.
  57. */
  58. int compare_sids(const struct smb_sid *ctsid, const struct smb_sid *cwsid)
  59. {
  60. int i;
  61. int num_subauth, num_sat, num_saw;
  62. if (!ctsid || !cwsid)
  63. return 1;
  64. /* compare the revision */
  65. if (ctsid->revision != cwsid->revision) {
  66. if (ctsid->revision > cwsid->revision)
  67. return 1;
  68. else
  69. return -1;
  70. }
  71. /* compare all of the six auth values */
  72. for (i = 0; i < NUM_AUTHS; ++i) {
  73. if (ctsid->authority[i] != cwsid->authority[i]) {
  74. if (ctsid->authority[i] > cwsid->authority[i])
  75. return 1;
  76. else
  77. return -1;
  78. }
  79. }
  80. /* compare all of the subauth values if any */
  81. num_sat = ctsid->num_subauth;
  82. num_saw = cwsid->num_subauth;
  83. num_subauth = min(num_sat, num_saw);
  84. if (num_subauth) {
  85. for (i = 0; i < num_subauth; ++i) {
  86. if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) {
  87. if (le32_to_cpu(ctsid->sub_auth[i]) >
  88. le32_to_cpu(cwsid->sub_auth[i]))
  89. return 1;
  90. else
  91. return -1;
  92. }
  93. }
  94. }
  95. return 0; /* sids compare/match */
  96. }
  97. static void smb_copy_sid(struct smb_sid *dst, const struct smb_sid *src)
  98. {
  99. int i;
  100. dst->revision = src->revision;
  101. dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES);
  102. for (i = 0; i < NUM_AUTHS; ++i)
  103. dst->authority[i] = src->authority[i];
  104. for (i = 0; i < dst->num_subauth; ++i)
  105. dst->sub_auth[i] = src->sub_auth[i];
  106. }
  107. /*
  108. * change posix mode to reflect permissions
  109. * pmode is the existing mode (we only want to overwrite part of this
  110. * bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007
  111. */
  112. static umode_t access_flags_to_mode(struct smb_fattr *fattr, __le32 ace_flags,
  113. int type)
  114. {
  115. __u32 flags = le32_to_cpu(ace_flags);
  116. umode_t mode = 0;
  117. if (flags & GENERIC_ALL) {
  118. mode = 0777;
  119. ksmbd_debug(SMB, "all perms\n");
  120. return mode;
  121. }
  122. if ((flags & GENERIC_READ) || (flags & FILE_READ_RIGHTS))
  123. mode = 0444;
  124. if ((flags & GENERIC_WRITE) || (flags & FILE_WRITE_RIGHTS)) {
  125. mode |= 0222;
  126. if (S_ISDIR(fattr->cf_mode))
  127. mode |= 0111;
  128. }
  129. if ((flags & GENERIC_EXECUTE) || (flags & FILE_EXEC_RIGHTS))
  130. mode |= 0111;
  131. if (type == ACCESS_DENIED_ACE_TYPE || type == ACCESS_DENIED_OBJECT_ACE_TYPE)
  132. mode = ~mode;
  133. ksmbd_debug(SMB, "access flags 0x%x mode now %04o\n", flags, mode);
  134. return mode;
  135. }
  136. /*
  137. * Generate access flags to reflect permissions mode is the existing mode.
  138. * This function is called for every ACE in the DACL whose SID matches
  139. * with either owner or group or everyone.
  140. */
  141. static void mode_to_access_flags(umode_t mode, umode_t bits_to_use,
  142. __u32 *pace_flags)
  143. {
  144. /* reset access mask */
  145. *pace_flags = 0x0;
  146. /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */
  147. mode &= bits_to_use;
  148. /*
  149. * check for R/W/X UGO since we do not know whose flags
  150. * is this but we have cleared all the bits sans RWX for
  151. * either user or group or other as per bits_to_use
  152. */
  153. if (mode & 0444)
  154. *pace_flags |= SET_FILE_READ_RIGHTS;
  155. if (mode & 0222)
  156. *pace_flags |= FILE_WRITE_RIGHTS;
  157. if (mode & 0111)
  158. *pace_flags |= SET_FILE_EXEC_RIGHTS;
  159. ksmbd_debug(SMB, "mode: %o, access flags now 0x%x\n",
  160. mode, *pace_flags);
  161. }
  162. static __u16 fill_ace_for_sid(struct smb_ace *pntace,
  163. const struct smb_sid *psid, int type, int flags,
  164. umode_t mode, umode_t bits)
  165. {
  166. int i;
  167. __u16 size = 0;
  168. __u32 access_req = 0;
  169. pntace->type = type;
  170. pntace->flags = flags;
  171. mode_to_access_flags(mode, bits, &access_req);
  172. if (!access_req)
  173. access_req = SET_MINIMUM_RIGHTS;
  174. pntace->access_req = cpu_to_le32(access_req);
  175. pntace->sid.revision = psid->revision;
  176. pntace->sid.num_subauth = psid->num_subauth;
  177. for (i = 0; i < NUM_AUTHS; i++)
  178. pntace->sid.authority[i] = psid->authority[i];
  179. for (i = 0; i < psid->num_subauth; i++)
  180. pntace->sid.sub_auth[i] = psid->sub_auth[i];
  181. size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4);
  182. pntace->size = cpu_to_le16(size);
  183. return size;
  184. }
  185. void id_to_sid(unsigned int cid, uint sidtype, struct smb_sid *ssid)
  186. {
  187. switch (sidtype) {
  188. case SIDOWNER:
  189. smb_copy_sid(ssid, &server_conf.domain_sid);
  190. break;
  191. case SIDUNIX_USER:
  192. smb_copy_sid(ssid, &sid_unix_users);
  193. break;
  194. case SIDUNIX_GROUP:
  195. smb_copy_sid(ssid, &sid_unix_groups);
  196. break;
  197. case SIDCREATOR_OWNER:
  198. smb_copy_sid(ssid, &creator_owner);
  199. return;
  200. case SIDCREATOR_GROUP:
  201. smb_copy_sid(ssid, &creator_group);
  202. return;
  203. case SIDNFS_USER:
  204. smb_copy_sid(ssid, &sid_unix_NFS_users);
  205. break;
  206. case SIDNFS_GROUP:
  207. smb_copy_sid(ssid, &sid_unix_NFS_groups);
  208. break;
  209. case SIDNFS_MODE:
  210. smb_copy_sid(ssid, &sid_unix_NFS_mode);
  211. break;
  212. default:
  213. return;
  214. }
  215. /* RID */
  216. ssid->sub_auth[ssid->num_subauth] = cpu_to_le32(cid);
  217. ssid->num_subauth++;
  218. }
  219. static int sid_to_id(struct mnt_idmap *idmap,
  220. struct smb_sid *psid, uint sidtype,
  221. struct smb_fattr *fattr)
  222. {
  223. int rc = -EINVAL;
  224. /*
  225. * If we have too many subauthorities, then something is really wrong.
  226. * Just return an error.
  227. */
  228. if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
  229. pr_err("%s: %u subauthorities is too many!\n",
  230. __func__, psid->num_subauth);
  231. return -EIO;
  232. }
  233. if (psid->num_subauth == 0) {
  234. pr_err("%s: zero subauthorities!\n", __func__);
  235. return -EIO;
  236. }
  237. if (sidtype == SIDOWNER) {
  238. kuid_t uid;
  239. uid_t id;
  240. id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]);
  241. uid = KUIDT_INIT(id);
  242. uid = from_vfsuid(idmap, &init_user_ns, VFSUIDT_INIT(uid));
  243. if (uid_valid(uid)) {
  244. fattr->cf_uid = uid;
  245. rc = 0;
  246. }
  247. } else {
  248. kgid_t gid;
  249. gid_t id;
  250. id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]);
  251. gid = KGIDT_INIT(id);
  252. gid = from_vfsgid(idmap, &init_user_ns, VFSGIDT_INIT(gid));
  253. if (gid_valid(gid)) {
  254. fattr->cf_gid = gid;
  255. rc = 0;
  256. }
  257. }
  258. return rc;
  259. }
  260. void posix_state_to_acl(struct posix_acl_state *state,
  261. struct posix_acl_entry *pace)
  262. {
  263. int i;
  264. pace->e_tag = ACL_USER_OBJ;
  265. pace->e_perm = state->owner.allow;
  266. for (i = 0; i < state->users->n; i++) {
  267. pace++;
  268. pace->e_tag = ACL_USER;
  269. pace->e_uid = state->users->aces[i].uid;
  270. pace->e_perm = state->users->aces[i].perms.allow;
  271. }
  272. pace++;
  273. pace->e_tag = ACL_GROUP_OBJ;
  274. pace->e_perm = state->group.allow;
  275. for (i = 0; i < state->groups->n; i++) {
  276. pace++;
  277. pace->e_tag = ACL_GROUP;
  278. pace->e_gid = state->groups->aces[i].gid;
  279. pace->e_perm = state->groups->aces[i].perms.allow;
  280. }
  281. if (state->users->n || state->groups->n) {
  282. pace++;
  283. pace->e_tag = ACL_MASK;
  284. pace->e_perm = state->mask.allow;
  285. }
  286. pace++;
  287. pace->e_tag = ACL_OTHER;
  288. pace->e_perm = state->other.allow;
  289. }
  290. int init_acl_state(struct posix_acl_state *state, u16 cnt)
  291. {
  292. int alloc;
  293. memset(state, 0, sizeof(struct posix_acl_state));
  294. /*
  295. * In the worst case, each individual acl could be for a distinct
  296. * named user or group, but we don't know which, so we allocate
  297. * enough space for either:
  298. */
  299. alloc = sizeof(struct posix_ace_state_array)
  300. + cnt * sizeof(struct posix_user_ace_state);
  301. state->users = kzalloc(alloc, KSMBD_DEFAULT_GFP);
  302. if (!state->users)
  303. return -ENOMEM;
  304. state->groups = kzalloc(alloc, KSMBD_DEFAULT_GFP);
  305. if (!state->groups) {
  306. kfree(state->users);
  307. return -ENOMEM;
  308. }
  309. return 0;
  310. }
  311. void free_acl_state(struct posix_acl_state *state)
  312. {
  313. kfree(state->users);
  314. kfree(state->groups);
  315. }
  316. static void parse_dacl(struct mnt_idmap *idmap,
  317. struct smb_acl *pdacl, char *end_of_acl,
  318. struct smb_sid *pownersid, struct smb_sid *pgrpsid,
  319. struct smb_fattr *fattr)
  320. {
  321. int i, ret;
  322. u16 num_aces = 0;
  323. unsigned int acl_size;
  324. char *acl_base;
  325. struct smb_ace **ppace;
  326. struct posix_acl_entry *cf_pace, *cf_pdace;
  327. struct posix_acl_state acl_state, default_acl_state;
  328. umode_t mode = 0, acl_mode;
  329. bool owner_found = false, group_found = false, others_found = false;
  330. if (!pdacl)
  331. return;
  332. /* validate that we do not go past end of acl */
  333. if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl) ||
  334. end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
  335. pr_err("ACL too small to parse DACL\n");
  336. return;
  337. }
  338. ksmbd_debug(SMB, "DACL revision %d size %d num aces %d\n",
  339. le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
  340. le16_to_cpu(pdacl->num_aces));
  341. acl_base = (char *)pdacl;
  342. acl_size = sizeof(struct smb_acl);
  343. num_aces = le16_to_cpu(pdacl->num_aces);
  344. if (num_aces <= 0)
  345. return;
  346. if (num_aces > (le16_to_cpu(pdacl->size) - sizeof(struct smb_acl)) /
  347. (offsetof(struct smb_ace, sid) +
  348. offsetof(struct smb_sid, sub_auth) + sizeof(__le16)))
  349. return;
  350. ret = init_acl_state(&acl_state, num_aces);
  351. if (ret)
  352. return;
  353. ret = init_acl_state(&default_acl_state, num_aces);
  354. if (ret) {
  355. free_acl_state(&acl_state);
  356. return;
  357. }
  358. ppace = kmalloc_array(num_aces, sizeof(struct smb_ace *), KSMBD_DEFAULT_GFP);
  359. if (!ppace) {
  360. free_acl_state(&default_acl_state);
  361. free_acl_state(&acl_state);
  362. return;
  363. }
  364. /*
  365. * reset rwx permissions for user/group/other.
  366. * Also, if num_aces is 0 i.e. DACL has no ACEs,
  367. * user/group/other have no permissions
  368. */
  369. for (i = 0; i < num_aces; ++i) {
  370. if (end_of_acl - acl_base < acl_size)
  371. break;
  372. ppace[i] = (struct smb_ace *)(acl_base + acl_size);
  373. acl_base = (char *)ppace[i];
  374. acl_size = offsetof(struct smb_ace, sid) +
  375. offsetof(struct smb_sid, sub_auth);
  376. if (end_of_acl - acl_base < acl_size ||
  377. ppace[i]->sid.num_subauth == 0 ||
  378. ppace[i]->sid.num_subauth > SID_MAX_SUB_AUTHORITIES ||
  379. (end_of_acl - acl_base <
  380. acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth) ||
  381. (le16_to_cpu(ppace[i]->size) <
  382. acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth))
  383. break;
  384. acl_size = le16_to_cpu(ppace[i]->size);
  385. ppace[i]->access_req =
  386. smb_map_generic_desired_access(ppace[i]->access_req);
  387. if (!(compare_sids(&ppace[i]->sid, &sid_unix_NFS_mode))) {
  388. fattr->cf_mode =
  389. le32_to_cpu(ppace[i]->sid.sub_auth[2]);
  390. break;
  391. } else if (!compare_sids(&ppace[i]->sid, pownersid)) {
  392. acl_mode = access_flags_to_mode(fattr,
  393. ppace[i]->access_req,
  394. ppace[i]->type);
  395. acl_mode &= 0700;
  396. if (!owner_found) {
  397. mode &= ~(0700);
  398. mode |= acl_mode;
  399. }
  400. owner_found = true;
  401. } else if (!compare_sids(&ppace[i]->sid, pgrpsid) ||
  402. ppace[i]->sid.sub_auth[ppace[i]->sid.num_subauth - 1] ==
  403. DOMAIN_USER_RID_LE) {
  404. acl_mode = access_flags_to_mode(fattr,
  405. ppace[i]->access_req,
  406. ppace[i]->type);
  407. acl_mode &= 0070;
  408. if (!group_found) {
  409. mode &= ~(0070);
  410. mode |= acl_mode;
  411. }
  412. group_found = true;
  413. } else if (!compare_sids(&ppace[i]->sid, &sid_everyone)) {
  414. acl_mode = access_flags_to_mode(fattr,
  415. ppace[i]->access_req,
  416. ppace[i]->type);
  417. acl_mode &= 0007;
  418. if (!others_found) {
  419. mode &= ~(0007);
  420. mode |= acl_mode;
  421. }
  422. others_found = true;
  423. } else if (!compare_sids(&ppace[i]->sid, &creator_owner)) {
  424. continue;
  425. } else if (!compare_sids(&ppace[i]->sid, &creator_group)) {
  426. continue;
  427. } else if (!compare_sids(&ppace[i]->sid, &sid_authusers)) {
  428. continue;
  429. } else {
  430. struct smb_fattr temp_fattr;
  431. acl_mode = access_flags_to_mode(fattr, ppace[i]->access_req,
  432. ppace[i]->type);
  433. temp_fattr.cf_uid = INVALID_UID;
  434. ret = sid_to_id(idmap, &ppace[i]->sid, SIDOWNER, &temp_fattr);
  435. if (ret || uid_eq(temp_fattr.cf_uid, INVALID_UID)) {
  436. pr_err("%s: Error %d mapping Owner SID to uid\n",
  437. __func__, ret);
  438. continue;
  439. }
  440. acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
  441. acl_state.users->aces[acl_state.users->n].uid =
  442. temp_fattr.cf_uid;
  443. acl_state.users->aces[acl_state.users->n++].perms.allow =
  444. ((acl_mode & 0700) >> 6) | 0004;
  445. default_acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
  446. default_acl_state.users->aces[default_acl_state.users->n].uid =
  447. temp_fattr.cf_uid;
  448. default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
  449. ((acl_mode & 0700) >> 6) | 0004;
  450. }
  451. }
  452. kfree(ppace);
  453. if (owner_found) {
  454. /* The owner must be set to at least read-only. */
  455. acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
  456. acl_state.users->aces[acl_state.users->n].uid = fattr->cf_uid;
  457. acl_state.users->aces[acl_state.users->n++].perms.allow =
  458. ((mode & 0700) >> 6) | 0004;
  459. default_acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
  460. default_acl_state.users->aces[default_acl_state.users->n].uid =
  461. fattr->cf_uid;
  462. default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
  463. ((mode & 0700) >> 6) | 0004;
  464. }
  465. if (group_found) {
  466. acl_state.group.allow = (mode & 0070) >> 3;
  467. acl_state.groups->aces[acl_state.groups->n].gid =
  468. fattr->cf_gid;
  469. acl_state.groups->aces[acl_state.groups->n++].perms.allow =
  470. (mode & 0070) >> 3;
  471. default_acl_state.group.allow = (mode & 0070) >> 3;
  472. default_acl_state.groups->aces[default_acl_state.groups->n].gid =
  473. fattr->cf_gid;
  474. default_acl_state.groups->aces[default_acl_state.groups->n++].perms.allow =
  475. (mode & 0070) >> 3;
  476. }
  477. if (others_found) {
  478. fattr->cf_mode &= ~(0007);
  479. fattr->cf_mode |= mode & 0007;
  480. acl_state.other.allow = mode & 0007;
  481. default_acl_state.other.allow = mode & 0007;
  482. }
  483. if (acl_state.users->n || acl_state.groups->n) {
  484. acl_state.mask.allow = 0x07;
  485. if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
  486. fattr->cf_acls =
  487. posix_acl_alloc(acl_state.users->n +
  488. acl_state.groups->n + 4, KSMBD_DEFAULT_GFP);
  489. if (fattr->cf_acls) {
  490. cf_pace = fattr->cf_acls->a_entries;
  491. posix_state_to_acl(&acl_state, cf_pace);
  492. }
  493. }
  494. }
  495. if (default_acl_state.users->n || default_acl_state.groups->n) {
  496. default_acl_state.mask.allow = 0x07;
  497. if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
  498. fattr->cf_dacls =
  499. posix_acl_alloc(default_acl_state.users->n +
  500. default_acl_state.groups->n + 4, KSMBD_DEFAULT_GFP);
  501. if (fattr->cf_dacls) {
  502. cf_pdace = fattr->cf_dacls->a_entries;
  503. posix_state_to_acl(&default_acl_state, cf_pdace);
  504. }
  505. }
  506. }
  507. free_acl_state(&acl_state);
  508. free_acl_state(&default_acl_state);
  509. }
  510. static void set_posix_acl_entries_dacl(struct mnt_idmap *idmap,
  511. struct smb_ace *pndace,
  512. struct smb_fattr *fattr, u16 *num_aces,
  513. u16 *size, u32 nt_aces_num)
  514. {
  515. struct posix_acl_entry *pace;
  516. struct smb_sid *sid;
  517. struct smb_ace *ntace;
  518. int i, j;
  519. if (!fattr->cf_acls)
  520. goto posix_default_acl;
  521. pace = fattr->cf_acls->a_entries;
  522. for (i = 0; i < fattr->cf_acls->a_count; i++, pace++) {
  523. int flags = 0;
  524. sid = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP);
  525. if (!sid)
  526. break;
  527. if (pace->e_tag == ACL_USER) {
  528. uid_t uid;
  529. unsigned int sid_type = SIDOWNER;
  530. uid = posix_acl_uid_translate(idmap, pace);
  531. if (!uid)
  532. sid_type = SIDUNIX_USER;
  533. id_to_sid(uid, sid_type, sid);
  534. } else if (pace->e_tag == ACL_GROUP) {
  535. gid_t gid;
  536. gid = posix_acl_gid_translate(idmap, pace);
  537. id_to_sid(gid, SIDUNIX_GROUP, sid);
  538. } else if (pace->e_tag == ACL_OTHER && !nt_aces_num) {
  539. smb_copy_sid(sid, &sid_everyone);
  540. } else {
  541. kfree(sid);
  542. continue;
  543. }
  544. ntace = pndace;
  545. for (j = 0; j < nt_aces_num; j++) {
  546. if (ntace->sid.sub_auth[ntace->sid.num_subauth - 1] ==
  547. sid->sub_auth[sid->num_subauth - 1])
  548. goto pass_same_sid;
  549. ntace = (struct smb_ace *)((char *)ntace +
  550. le16_to_cpu(ntace->size));
  551. }
  552. if (S_ISDIR(fattr->cf_mode) && pace->e_tag == ACL_OTHER)
  553. flags = 0x03;
  554. ntace = (struct smb_ace *)((char *)pndace + *size);
  555. *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, flags,
  556. pace->e_perm, 0777);
  557. (*num_aces)++;
  558. if (pace->e_tag == ACL_USER)
  559. ntace->access_req |=
  560. FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
  561. if (S_ISDIR(fattr->cf_mode) &&
  562. (pace->e_tag == ACL_USER || pace->e_tag == ACL_GROUP)) {
  563. ntace = (struct smb_ace *)((char *)pndace + *size);
  564. *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED,
  565. 0x03, pace->e_perm, 0777);
  566. (*num_aces)++;
  567. if (pace->e_tag == ACL_USER)
  568. ntace->access_req |=
  569. FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
  570. }
  571. pass_same_sid:
  572. kfree(sid);
  573. }
  574. if (nt_aces_num)
  575. return;
  576. posix_default_acl:
  577. if (!fattr->cf_dacls)
  578. return;
  579. pace = fattr->cf_dacls->a_entries;
  580. for (i = 0; i < fattr->cf_dacls->a_count; i++, pace++) {
  581. sid = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP);
  582. if (!sid)
  583. break;
  584. if (pace->e_tag == ACL_USER) {
  585. uid_t uid;
  586. uid = posix_acl_uid_translate(idmap, pace);
  587. id_to_sid(uid, SIDCREATOR_OWNER, sid);
  588. } else if (pace->e_tag == ACL_GROUP) {
  589. gid_t gid;
  590. gid = posix_acl_gid_translate(idmap, pace);
  591. id_to_sid(gid, SIDCREATOR_GROUP, sid);
  592. } else {
  593. kfree(sid);
  594. continue;
  595. }
  596. ntace = (struct smb_ace *)((char *)pndace + *size);
  597. *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, 0x0b,
  598. pace->e_perm, 0777);
  599. (*num_aces)++;
  600. if (pace->e_tag == ACL_USER)
  601. ntace->access_req |=
  602. FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
  603. kfree(sid);
  604. }
  605. }
  606. static void set_ntacl_dacl(struct mnt_idmap *idmap,
  607. struct smb_acl *pndacl,
  608. struct smb_acl *nt_dacl,
  609. unsigned int aces_size,
  610. const struct smb_sid *pownersid,
  611. const struct smb_sid *pgrpsid,
  612. struct smb_fattr *fattr)
  613. {
  614. struct smb_ace *ntace, *pndace;
  615. u16 nt_num_aces = le16_to_cpu(nt_dacl->num_aces), num_aces = 0;
  616. unsigned short size = 0;
  617. int i;
  618. pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
  619. if (nt_num_aces) {
  620. ntace = (struct smb_ace *)((char *)nt_dacl + sizeof(struct smb_acl));
  621. for (i = 0; i < nt_num_aces; i++) {
  622. unsigned short nt_ace_size;
  623. if (offsetof(struct smb_ace, access_req) > aces_size)
  624. break;
  625. nt_ace_size = le16_to_cpu(ntace->size);
  626. if (nt_ace_size > aces_size)
  627. break;
  628. memcpy((char *)pndace + size, ntace, nt_ace_size);
  629. size += nt_ace_size;
  630. aces_size -= nt_ace_size;
  631. ntace = (struct smb_ace *)((char *)ntace + nt_ace_size);
  632. num_aces++;
  633. }
  634. }
  635. set_posix_acl_entries_dacl(idmap, pndace, fattr,
  636. &num_aces, &size, nt_num_aces);
  637. pndacl->num_aces = cpu_to_le16(num_aces);
  638. pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
  639. }
  640. static void set_mode_dacl(struct mnt_idmap *idmap,
  641. struct smb_acl *pndacl, struct smb_fattr *fattr)
  642. {
  643. struct smb_ace *pace, *pndace;
  644. u16 num_aces = 0;
  645. u16 size = 0, ace_size = 0;
  646. uid_t uid;
  647. const struct smb_sid *sid;
  648. pace = pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
  649. if (fattr->cf_acls) {
  650. set_posix_acl_entries_dacl(idmap, pndace, fattr,
  651. &num_aces, &size, num_aces);
  652. goto out;
  653. }
  654. /* owner RID */
  655. uid = from_kuid(&init_user_ns, fattr->cf_uid);
  656. if (uid)
  657. sid = &server_conf.domain_sid;
  658. else
  659. sid = &sid_unix_users;
  660. ace_size = fill_ace_for_sid(pace, sid, ACCESS_ALLOWED, 0,
  661. fattr->cf_mode, 0700);
  662. pace->sid.sub_auth[pace->sid.num_subauth++] = cpu_to_le32(uid);
  663. pace->size = cpu_to_le16(ace_size + 4);
  664. size += le16_to_cpu(pace->size);
  665. pace = (struct smb_ace *)((char *)pndace + size);
  666. /* Group RID */
  667. ace_size = fill_ace_for_sid(pace, &sid_unix_groups,
  668. ACCESS_ALLOWED, 0, fattr->cf_mode, 0070);
  669. pace->sid.sub_auth[pace->sid.num_subauth++] =
  670. cpu_to_le32(from_kgid(&init_user_ns, fattr->cf_gid));
  671. pace->size = cpu_to_le16(ace_size + 4);
  672. size += le16_to_cpu(pace->size);
  673. pace = (struct smb_ace *)((char *)pndace + size);
  674. num_aces = 3;
  675. if (S_ISDIR(fattr->cf_mode)) {
  676. pace = (struct smb_ace *)((char *)pndace + size);
  677. /* creator owner */
  678. size += fill_ace_for_sid(pace, &creator_owner, ACCESS_ALLOWED,
  679. 0x0b, fattr->cf_mode, 0700);
  680. pace = (struct smb_ace *)((char *)pndace + size);
  681. /* creator group */
  682. size += fill_ace_for_sid(pace, &creator_group, ACCESS_ALLOWED,
  683. 0x0b, fattr->cf_mode, 0070);
  684. pace = (struct smb_ace *)((char *)pndace + size);
  685. num_aces = 5;
  686. }
  687. /* other */
  688. size += fill_ace_for_sid(pace, &sid_everyone, ACCESS_ALLOWED, 0,
  689. fattr->cf_mode, 0007);
  690. out:
  691. pndacl->num_aces = cpu_to_le16(num_aces);
  692. pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
  693. }
  694. static int parse_sid(struct smb_sid *psid, char *end_of_acl)
  695. {
  696. /*
  697. * validate that we do not go past end of ACL - sid must be at least 8
  698. * bytes long (assuming no sub-auths - e.g. the null SID
  699. */
  700. if (end_of_acl < (char *)psid + 8) {
  701. pr_err("ACL too small to parse SID %p\n", psid);
  702. return -EINVAL;
  703. }
  704. if (!psid->num_subauth)
  705. return 0;
  706. if (psid->num_subauth > SID_MAX_SUB_AUTHORITIES ||
  707. end_of_acl < (char *)psid + 8 + sizeof(__le32) * psid->num_subauth)
  708. return -EINVAL;
  709. return 0;
  710. }
  711. /* Convert CIFS ACL to POSIX form */
  712. int parse_sec_desc(struct mnt_idmap *idmap, struct smb_ntsd *pntsd,
  713. int acl_len, struct smb_fattr *fattr)
  714. {
  715. int rc = 0;
  716. struct smb_sid *owner_sid_ptr, *group_sid_ptr;
  717. struct smb_acl *dacl_ptr; /* no need for SACL ptr */
  718. char *end_of_acl = ((char *)pntsd) + acl_len;
  719. __u32 dacloffset;
  720. int pntsd_type;
  721. if (!pntsd)
  722. return -EIO;
  723. if (acl_len < sizeof(struct smb_ntsd))
  724. return -EINVAL;
  725. owner_sid_ptr = (struct smb_sid *)((char *)pntsd +
  726. le32_to_cpu(pntsd->osidoffset));
  727. group_sid_ptr = (struct smb_sid *)((char *)pntsd +
  728. le32_to_cpu(pntsd->gsidoffset));
  729. dacloffset = le32_to_cpu(pntsd->dacloffset);
  730. dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset);
  731. ksmbd_debug(SMB,
  732. "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n",
  733. pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
  734. le32_to_cpu(pntsd->gsidoffset),
  735. le32_to_cpu(pntsd->sacloffset), dacloffset);
  736. pntsd_type = le16_to_cpu(pntsd->type);
  737. if (!(pntsd_type & DACL_PRESENT)) {
  738. ksmbd_debug(SMB, "DACL_PRESENT in DACL type is not set\n");
  739. return rc;
  740. }
  741. pntsd->type = cpu_to_le16(DACL_PRESENT);
  742. if (pntsd->osidoffset) {
  743. if (le32_to_cpu(pntsd->osidoffset) < sizeof(struct smb_ntsd))
  744. return -EINVAL;
  745. rc = parse_sid(owner_sid_ptr, end_of_acl);
  746. if (rc) {
  747. pr_err("%s: Error %d parsing Owner SID\n", __func__, rc);
  748. return rc;
  749. }
  750. rc = sid_to_id(idmap, owner_sid_ptr, SIDOWNER, fattr);
  751. if (rc) {
  752. pr_err("%s: Error %d mapping Owner SID to uid\n",
  753. __func__, rc);
  754. owner_sid_ptr = NULL;
  755. }
  756. }
  757. if (pntsd->gsidoffset) {
  758. if (le32_to_cpu(pntsd->gsidoffset) < sizeof(struct smb_ntsd))
  759. return -EINVAL;
  760. rc = parse_sid(group_sid_ptr, end_of_acl);
  761. if (rc) {
  762. pr_err("%s: Error %d mapping Owner SID to gid\n",
  763. __func__, rc);
  764. return rc;
  765. }
  766. rc = sid_to_id(idmap, group_sid_ptr, SIDUNIX_GROUP, fattr);
  767. if (rc) {
  768. pr_err("%s: Error %d mapping Group SID to gid\n",
  769. __func__, rc);
  770. group_sid_ptr = NULL;
  771. }
  772. }
  773. if ((pntsd_type & (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) ==
  774. (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ))
  775. pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
  776. if (pntsd_type & DACL_PROTECTED)
  777. pntsd->type |= cpu_to_le16(DACL_PROTECTED);
  778. if (dacloffset) {
  779. if (dacloffset < sizeof(struct smb_ntsd))
  780. return -EINVAL;
  781. parse_dacl(idmap, dacl_ptr, end_of_acl,
  782. owner_sid_ptr, group_sid_ptr, fattr);
  783. }
  784. return 0;
  785. }
  786. /* Convert permission bits from mode to equivalent CIFS ACL */
  787. int build_sec_desc(struct mnt_idmap *idmap,
  788. struct smb_ntsd *pntsd, struct smb_ntsd *ppntsd,
  789. int ppntsd_size, int addition_info, __u32 *secdesclen,
  790. struct smb_fattr *fattr)
  791. {
  792. int rc = 0;
  793. __u32 offset;
  794. struct smb_sid *owner_sid_ptr, *group_sid_ptr;
  795. struct smb_sid *nowner_sid_ptr, *ngroup_sid_ptr;
  796. struct smb_acl *dacl_ptr = NULL; /* no need for SACL ptr */
  797. uid_t uid;
  798. gid_t gid;
  799. unsigned int sid_type = SIDOWNER;
  800. nowner_sid_ptr = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP);
  801. if (!nowner_sid_ptr)
  802. return -ENOMEM;
  803. uid = from_kuid(&init_user_ns, fattr->cf_uid);
  804. if (!uid)
  805. sid_type = SIDUNIX_USER;
  806. id_to_sid(uid, sid_type, nowner_sid_ptr);
  807. ngroup_sid_ptr = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP);
  808. if (!ngroup_sid_ptr) {
  809. kfree(nowner_sid_ptr);
  810. return -ENOMEM;
  811. }
  812. gid = from_kgid(&init_user_ns, fattr->cf_gid);
  813. id_to_sid(gid, SIDUNIX_GROUP, ngroup_sid_ptr);
  814. offset = sizeof(struct smb_ntsd);
  815. pntsd->sacloffset = 0;
  816. pntsd->revision = cpu_to_le16(1);
  817. pntsd->type = cpu_to_le16(SELF_RELATIVE);
  818. if (ppntsd)
  819. pntsd->type |= ppntsd->type;
  820. if (addition_info & OWNER_SECINFO) {
  821. pntsd->osidoffset = cpu_to_le32(offset);
  822. owner_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
  823. smb_copy_sid(owner_sid_ptr, nowner_sid_ptr);
  824. offset += 1 + 1 + 6 + (nowner_sid_ptr->num_subauth * 4);
  825. }
  826. if (addition_info & GROUP_SECINFO) {
  827. pntsd->gsidoffset = cpu_to_le32(offset);
  828. group_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
  829. smb_copy_sid(group_sid_ptr, ngroup_sid_ptr);
  830. offset += 1 + 1 + 6 + (ngroup_sid_ptr->num_subauth * 4);
  831. }
  832. if (addition_info & DACL_SECINFO) {
  833. pntsd->type |= cpu_to_le16(DACL_PRESENT);
  834. dacl_ptr = (struct smb_acl *)((char *)pntsd + offset);
  835. dacl_ptr->revision = cpu_to_le16(2);
  836. dacl_ptr->size = cpu_to_le16(sizeof(struct smb_acl));
  837. dacl_ptr->num_aces = 0;
  838. if (!ppntsd) {
  839. set_mode_dacl(idmap, dacl_ptr, fattr);
  840. } else {
  841. struct smb_acl *ppdacl_ptr;
  842. unsigned int dacl_offset = le32_to_cpu(ppntsd->dacloffset);
  843. int ppdacl_size, ntacl_size = ppntsd_size - dacl_offset;
  844. if (!dacl_offset ||
  845. (dacl_offset + sizeof(struct smb_acl) > ppntsd_size))
  846. goto out;
  847. ppdacl_ptr = (struct smb_acl *)((char *)ppntsd + dacl_offset);
  848. ppdacl_size = le16_to_cpu(ppdacl_ptr->size);
  849. if (ppdacl_size > ntacl_size ||
  850. ppdacl_size < sizeof(struct smb_acl))
  851. goto out;
  852. set_ntacl_dacl(idmap, dacl_ptr, ppdacl_ptr,
  853. ntacl_size - sizeof(struct smb_acl),
  854. nowner_sid_ptr, ngroup_sid_ptr,
  855. fattr);
  856. }
  857. pntsd->dacloffset = cpu_to_le32(offset);
  858. offset += le16_to_cpu(dacl_ptr->size);
  859. }
  860. out:
  861. kfree(nowner_sid_ptr);
  862. kfree(ngroup_sid_ptr);
  863. *secdesclen = offset;
  864. return rc;
  865. }
  866. static void smb_set_ace(struct smb_ace *ace, const struct smb_sid *sid, u8 type,
  867. u8 flags, __le32 access_req)
  868. {
  869. ace->type = type;
  870. ace->flags = flags;
  871. ace->access_req = access_req;
  872. smb_copy_sid(&ace->sid, sid);
  873. ace->size = cpu_to_le16(1 + 1 + 2 + 4 + 1 + 1 + 6 + (sid->num_subauth * 4));
  874. }
  875. int smb_inherit_dacl(struct ksmbd_conn *conn,
  876. const struct path *path,
  877. unsigned int uid, unsigned int gid)
  878. {
  879. const struct smb_sid *psid, *creator = NULL;
  880. struct smb_ace *parent_aces, *aces;
  881. struct smb_acl *parent_pdacl;
  882. struct smb_ntsd *parent_pntsd = NULL;
  883. struct smb_sid owner_sid, group_sid;
  884. struct dentry *parent = path->dentry->d_parent;
  885. struct mnt_idmap *idmap = mnt_idmap(path->mnt);
  886. int inherited_flags = 0, flags = 0, i, nt_size = 0, pdacl_size;
  887. int rc = 0, pntsd_type, pntsd_size, acl_len, aces_size;
  888. unsigned int dacloffset;
  889. size_t dacl_struct_end;
  890. u16 num_aces, ace_cnt = 0;
  891. char *aces_base;
  892. bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode);
  893. pntsd_size = ksmbd_vfs_get_sd_xattr(conn, idmap,
  894. parent, &parent_pntsd);
  895. if (pntsd_size <= 0)
  896. return -ENOENT;
  897. dacloffset = le32_to_cpu(parent_pntsd->dacloffset);
  898. if (!dacloffset ||
  899. check_add_overflow(dacloffset, sizeof(struct smb_acl), &dacl_struct_end) ||
  900. dacl_struct_end > (size_t)pntsd_size) {
  901. rc = -EINVAL;
  902. goto free_parent_pntsd;
  903. }
  904. parent_pdacl = (struct smb_acl *)((char *)parent_pntsd + dacloffset);
  905. acl_len = pntsd_size - dacloffset;
  906. num_aces = le16_to_cpu(parent_pdacl->num_aces);
  907. pntsd_type = le16_to_cpu(parent_pntsd->type);
  908. pdacl_size = le16_to_cpu(parent_pdacl->size);
  909. if (pdacl_size > acl_len || pdacl_size < sizeof(struct smb_acl)) {
  910. rc = -EINVAL;
  911. goto free_parent_pntsd;
  912. }
  913. aces_base = kmalloc(sizeof(struct smb_ace) * num_aces * 2,
  914. KSMBD_DEFAULT_GFP);
  915. if (!aces_base) {
  916. rc = -ENOMEM;
  917. goto free_parent_pntsd;
  918. }
  919. aces = (struct smb_ace *)aces_base;
  920. parent_aces = (struct smb_ace *)((char *)parent_pdacl +
  921. sizeof(struct smb_acl));
  922. aces_size = acl_len - sizeof(struct smb_acl);
  923. if (pntsd_type & DACL_AUTO_INHERITED)
  924. inherited_flags = INHERITED_ACE;
  925. for (i = 0; i < num_aces; i++) {
  926. int pace_size;
  927. if (offsetof(struct smb_ace, access_req) > aces_size)
  928. break;
  929. pace_size = le16_to_cpu(parent_aces->size);
  930. if (pace_size > aces_size)
  931. break;
  932. aces_size -= pace_size;
  933. flags = parent_aces->flags;
  934. if (!smb_inherit_flags(flags, is_dir))
  935. goto pass;
  936. if (is_dir) {
  937. flags &= ~(INHERIT_ONLY_ACE | INHERITED_ACE);
  938. if (!(flags & CONTAINER_INHERIT_ACE))
  939. flags |= INHERIT_ONLY_ACE;
  940. if (flags & NO_PROPAGATE_INHERIT_ACE)
  941. flags = 0;
  942. } else {
  943. flags = 0;
  944. }
  945. if (!compare_sids(&creator_owner, &parent_aces->sid)) {
  946. creator = &creator_owner;
  947. id_to_sid(uid, SIDOWNER, &owner_sid);
  948. psid = &owner_sid;
  949. } else if (!compare_sids(&creator_group, &parent_aces->sid)) {
  950. creator = &creator_group;
  951. id_to_sid(gid, SIDUNIX_GROUP, &group_sid);
  952. psid = &group_sid;
  953. } else {
  954. creator = NULL;
  955. psid = &parent_aces->sid;
  956. }
  957. if (is_dir && creator && flags & CONTAINER_INHERIT_ACE) {
  958. smb_set_ace(aces, psid, parent_aces->type, inherited_flags,
  959. parent_aces->access_req);
  960. nt_size += le16_to_cpu(aces->size);
  961. ace_cnt++;
  962. aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
  963. flags |= INHERIT_ONLY_ACE;
  964. psid = creator;
  965. } else if (is_dir && !(parent_aces->flags & NO_PROPAGATE_INHERIT_ACE)) {
  966. psid = &parent_aces->sid;
  967. }
  968. smb_set_ace(aces, psid, parent_aces->type, flags | inherited_flags,
  969. parent_aces->access_req);
  970. nt_size += le16_to_cpu(aces->size);
  971. aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
  972. ace_cnt++;
  973. pass:
  974. parent_aces = (struct smb_ace *)((char *)parent_aces + pace_size);
  975. }
  976. if (nt_size > 0) {
  977. struct smb_ntsd *pntsd;
  978. struct smb_acl *pdacl;
  979. struct smb_sid *powner_sid = NULL, *pgroup_sid = NULL;
  980. int powner_sid_size = 0, pgroup_sid_size = 0, pntsd_size;
  981. int pntsd_alloc_size;
  982. if (parent_pntsd->osidoffset) {
  983. powner_sid = (struct smb_sid *)((char *)parent_pntsd +
  984. le32_to_cpu(parent_pntsd->osidoffset));
  985. powner_sid_size = 1 + 1 + 6 + (powner_sid->num_subauth * 4);
  986. }
  987. if (parent_pntsd->gsidoffset) {
  988. pgroup_sid = (struct smb_sid *)((char *)parent_pntsd +
  989. le32_to_cpu(parent_pntsd->gsidoffset));
  990. pgroup_sid_size = 1 + 1 + 6 + (pgroup_sid->num_subauth * 4);
  991. }
  992. pntsd_alloc_size = sizeof(struct smb_ntsd) + powner_sid_size +
  993. pgroup_sid_size + sizeof(struct smb_acl) + nt_size;
  994. pntsd = kzalloc(pntsd_alloc_size, KSMBD_DEFAULT_GFP);
  995. if (!pntsd) {
  996. rc = -ENOMEM;
  997. goto free_aces_base;
  998. }
  999. pntsd->revision = cpu_to_le16(1);
  1000. pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PRESENT);
  1001. if (le16_to_cpu(parent_pntsd->type) & DACL_AUTO_INHERITED)
  1002. pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
  1003. pntsd_size = sizeof(struct smb_ntsd);
  1004. pntsd->osidoffset = parent_pntsd->osidoffset;
  1005. pntsd->gsidoffset = parent_pntsd->gsidoffset;
  1006. pntsd->dacloffset = parent_pntsd->dacloffset;
  1007. if ((u64)le32_to_cpu(pntsd->osidoffset) + powner_sid_size >
  1008. pntsd_alloc_size) {
  1009. rc = -EINVAL;
  1010. kfree(pntsd);
  1011. goto free_aces_base;
  1012. }
  1013. if ((u64)le32_to_cpu(pntsd->gsidoffset) + pgroup_sid_size >
  1014. pntsd_alloc_size) {
  1015. rc = -EINVAL;
  1016. kfree(pntsd);
  1017. goto free_aces_base;
  1018. }
  1019. if ((u64)le32_to_cpu(pntsd->dacloffset) + sizeof(struct smb_acl) + nt_size >
  1020. pntsd_alloc_size) {
  1021. rc = -EINVAL;
  1022. kfree(pntsd);
  1023. goto free_aces_base;
  1024. }
  1025. if (pntsd->osidoffset) {
  1026. struct smb_sid *owner_sid = (struct smb_sid *)((char *)pntsd +
  1027. le32_to_cpu(pntsd->osidoffset));
  1028. memcpy(owner_sid, powner_sid, powner_sid_size);
  1029. pntsd_size += powner_sid_size;
  1030. }
  1031. if (pntsd->gsidoffset) {
  1032. struct smb_sid *group_sid = (struct smb_sid *)((char *)pntsd +
  1033. le32_to_cpu(pntsd->gsidoffset));
  1034. memcpy(group_sid, pgroup_sid, pgroup_sid_size);
  1035. pntsd_size += pgroup_sid_size;
  1036. }
  1037. if (pntsd->dacloffset) {
  1038. struct smb_ace *pace;
  1039. pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
  1040. pdacl->revision = cpu_to_le16(2);
  1041. pdacl->size = cpu_to_le16(sizeof(struct smb_acl) + nt_size);
  1042. pdacl->num_aces = cpu_to_le16(ace_cnt);
  1043. pace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
  1044. memcpy(pace, aces_base, nt_size);
  1045. pntsd_size += sizeof(struct smb_acl) + nt_size;
  1046. }
  1047. ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, pntsd_size, false);
  1048. kfree(pntsd);
  1049. }
  1050. free_aces_base:
  1051. kfree(aces_base);
  1052. free_parent_pntsd:
  1053. kfree(parent_pntsd);
  1054. return rc;
  1055. }
  1056. bool smb_inherit_flags(int flags, bool is_dir)
  1057. {
  1058. if (!is_dir)
  1059. return (flags & OBJECT_INHERIT_ACE) != 0;
  1060. if (flags & OBJECT_INHERIT_ACE && !(flags & NO_PROPAGATE_INHERIT_ACE))
  1061. return true;
  1062. if (flags & CONTAINER_INHERIT_ACE)
  1063. return true;
  1064. return false;
  1065. }
  1066. int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
  1067. __le32 *pdaccess, int uid)
  1068. {
  1069. struct mnt_idmap *idmap = mnt_idmap(path->mnt);
  1070. struct smb_ntsd *pntsd = NULL;
  1071. struct smb_acl *pdacl;
  1072. struct posix_acl *posix_acls;
  1073. int rc = 0, pntsd_size, acl_size, aces_size, pdacl_size;
  1074. unsigned int dacl_offset;
  1075. size_t dacl_struct_end;
  1076. struct smb_sid sid;
  1077. int granted = le32_to_cpu(*pdaccess & ~FILE_MAXIMAL_ACCESS_LE);
  1078. struct smb_ace *ace;
  1079. int i, found = 0;
  1080. unsigned int access_bits = 0;
  1081. struct smb_ace *others_ace = NULL;
  1082. struct posix_acl_entry *pa_entry;
  1083. unsigned int sid_type = SIDOWNER;
  1084. unsigned short ace_size;
  1085. ksmbd_debug(SMB, "check permission using windows acl\n");
  1086. pntsd_size = ksmbd_vfs_get_sd_xattr(conn, idmap,
  1087. path->dentry, &pntsd);
  1088. if (pntsd_size <= 0 || !pntsd)
  1089. goto err_out;
  1090. dacl_offset = le32_to_cpu(pntsd->dacloffset);
  1091. if (!dacl_offset ||
  1092. check_add_overflow(dacl_offset, sizeof(struct smb_acl), &dacl_struct_end) ||
  1093. dacl_struct_end > (size_t)pntsd_size)
  1094. goto err_out;
  1095. pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
  1096. acl_size = pntsd_size - dacl_offset;
  1097. pdacl_size = le16_to_cpu(pdacl->size);
  1098. if (pdacl_size > acl_size || pdacl_size < sizeof(struct smb_acl))
  1099. goto err_out;
  1100. if (!pdacl->num_aces) {
  1101. if (!(pdacl_size - sizeof(struct smb_acl)) &&
  1102. *pdaccess & ~(FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE)) {
  1103. rc = -EACCES;
  1104. goto err_out;
  1105. }
  1106. goto err_out;
  1107. }
  1108. if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) {
  1109. granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
  1110. DELETE;
  1111. ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
  1112. aces_size = acl_size - sizeof(struct smb_acl);
  1113. for (i = 0; i < le16_to_cpu(pdacl->num_aces); i++) {
  1114. if (offsetof(struct smb_ace, access_req) > aces_size)
  1115. break;
  1116. ace_size = le16_to_cpu(ace->size);
  1117. if (ace_size > aces_size)
  1118. break;
  1119. aces_size -= ace_size;
  1120. granted |= le32_to_cpu(ace->access_req);
  1121. ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
  1122. }
  1123. if (!pdacl->num_aces)
  1124. granted = GENERIC_ALL_FLAGS;
  1125. }
  1126. if (!uid)
  1127. sid_type = SIDUNIX_USER;
  1128. id_to_sid(uid, sid_type, &sid);
  1129. ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
  1130. aces_size = acl_size - sizeof(struct smb_acl);
  1131. for (i = 0; i < le16_to_cpu(pdacl->num_aces); i++) {
  1132. if (offsetof(struct smb_ace, access_req) > aces_size)
  1133. break;
  1134. ace_size = le16_to_cpu(ace->size);
  1135. if (ace_size > aces_size)
  1136. break;
  1137. aces_size -= ace_size;
  1138. if (!compare_sids(&sid, &ace->sid) ||
  1139. !compare_sids(&sid_unix_NFS_mode, &ace->sid)) {
  1140. found = 1;
  1141. break;
  1142. }
  1143. if (!compare_sids(&sid_everyone, &ace->sid))
  1144. others_ace = ace;
  1145. ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
  1146. }
  1147. if (*pdaccess & FILE_MAXIMAL_ACCESS_LE && found) {
  1148. granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
  1149. DELETE;
  1150. granted |= le32_to_cpu(ace->access_req);
  1151. if (!pdacl->num_aces)
  1152. granted = GENERIC_ALL_FLAGS;
  1153. }
  1154. if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
  1155. posix_acls = get_inode_acl(d_inode(path->dentry), ACL_TYPE_ACCESS);
  1156. if (!IS_ERR_OR_NULL(posix_acls) && !found) {
  1157. unsigned int id = -1;
  1158. pa_entry = posix_acls->a_entries;
  1159. for (i = 0; i < posix_acls->a_count; i++, pa_entry++) {
  1160. if (pa_entry->e_tag == ACL_USER)
  1161. id = posix_acl_uid_translate(idmap, pa_entry);
  1162. else if (pa_entry->e_tag == ACL_GROUP)
  1163. id = posix_acl_gid_translate(idmap, pa_entry);
  1164. else
  1165. continue;
  1166. if (id == uid) {
  1167. mode_to_access_flags(pa_entry->e_perm,
  1168. 0777,
  1169. &access_bits);
  1170. if (!access_bits)
  1171. access_bits =
  1172. SET_MINIMUM_RIGHTS;
  1173. posix_acl_release(posix_acls);
  1174. goto check_access_bits;
  1175. }
  1176. }
  1177. }
  1178. if (!IS_ERR_OR_NULL(posix_acls))
  1179. posix_acl_release(posix_acls);
  1180. }
  1181. if (!found) {
  1182. if (others_ace) {
  1183. ace = others_ace;
  1184. } else {
  1185. ksmbd_debug(SMB, "Can't find corresponding sid\n");
  1186. rc = -EACCES;
  1187. goto err_out;
  1188. }
  1189. }
  1190. switch (ace->type) {
  1191. case ACCESS_ALLOWED_ACE_TYPE:
  1192. access_bits = le32_to_cpu(ace->access_req);
  1193. break;
  1194. case ACCESS_DENIED_ACE_TYPE:
  1195. case ACCESS_DENIED_CALLBACK_ACE_TYPE:
  1196. access_bits = le32_to_cpu(~ace->access_req);
  1197. break;
  1198. }
  1199. check_access_bits:
  1200. if (granted &
  1201. ~(access_bits | FILE_READ_ATTRIBUTES | READ_CONTROL | WRITE_DAC | DELETE)) {
  1202. ksmbd_debug(SMB, "Access denied with winACL, granted : %x, access_req : %x\n",
  1203. granted, le32_to_cpu(ace->access_req));
  1204. rc = -EACCES;
  1205. goto err_out;
  1206. }
  1207. *pdaccess = cpu_to_le32(granted);
  1208. err_out:
  1209. kfree(pntsd);
  1210. return rc;
  1211. }
  1212. int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
  1213. const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
  1214. bool type_check, bool get_write)
  1215. {
  1216. int rc;
  1217. struct smb_fattr fattr = {{0}};
  1218. struct inode *inode = d_inode(path->dentry);
  1219. struct mnt_idmap *idmap = mnt_idmap(path->mnt);
  1220. struct iattr newattrs;
  1221. fattr.cf_uid = INVALID_UID;
  1222. fattr.cf_gid = INVALID_GID;
  1223. fattr.cf_mode = inode->i_mode;
  1224. rc = parse_sec_desc(idmap, pntsd, ntsd_len, &fattr);
  1225. if (rc)
  1226. goto out;
  1227. newattrs.ia_valid = ATTR_CTIME;
  1228. if (!uid_eq(fattr.cf_uid, INVALID_UID)) {
  1229. newattrs.ia_valid |= ATTR_UID;
  1230. newattrs.ia_uid = fattr.cf_uid;
  1231. }
  1232. if (!gid_eq(fattr.cf_gid, INVALID_GID)) {
  1233. newattrs.ia_valid |= ATTR_GID;
  1234. newattrs.ia_gid = fattr.cf_gid;
  1235. }
  1236. newattrs.ia_valid |= ATTR_MODE;
  1237. newattrs.ia_mode = (inode->i_mode & ~0777) | (fattr.cf_mode & 0777);
  1238. ksmbd_vfs_remove_acl_xattrs(idmap, path);
  1239. /* Update posix acls */
  1240. if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && fattr.cf_dacls) {
  1241. rc = set_posix_acl(idmap, path->dentry,
  1242. ACL_TYPE_ACCESS, fattr.cf_acls);
  1243. if (rc < 0)
  1244. ksmbd_debug(SMB,
  1245. "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
  1246. rc);
  1247. if (S_ISDIR(inode->i_mode) && fattr.cf_dacls) {
  1248. rc = set_posix_acl(idmap, path->dentry,
  1249. ACL_TYPE_DEFAULT, fattr.cf_dacls);
  1250. if (rc)
  1251. ksmbd_debug(SMB,
  1252. "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
  1253. rc);
  1254. }
  1255. }
  1256. inode_lock(inode);
  1257. rc = notify_change(idmap, path->dentry, &newattrs, NULL);
  1258. inode_unlock(inode);
  1259. if (rc)
  1260. goto out;
  1261. /* Check it only calling from SD BUFFER context */
  1262. if (type_check && !(le16_to_cpu(pntsd->type) & DACL_PRESENT))
  1263. goto out;
  1264. if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) {
  1265. /* Update WinACL in xattr */
  1266. ksmbd_vfs_remove_sd_xattrs(idmap, path);
  1267. ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, ntsd_len,
  1268. get_write);
  1269. }
  1270. out:
  1271. posix_acl_release(fattr.cf_acls);
  1272. posix_acl_release(fattr.cf_dacls);
  1273. return rc;
  1274. }
  1275. void ksmbd_init_domain(u32 *sub_auth)
  1276. {
  1277. int i;
  1278. memcpy(&server_conf.domain_sid, &domain, sizeof(struct smb_sid));
  1279. for (i = 0; i < 3; ++i)
  1280. server_conf.domain_sid.sub_auth[i + 1] = cpu_to_le32(sub_auth[i]);
  1281. }