policy.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Encryption policy functions for per-file encryption support.
  4. *
  5. * Copyright (C) 2015, Google, Inc.
  6. * Copyright (C) 2015, Motorola Mobility.
  7. *
  8. * Originally written by Michael Halcrow, 2015.
  9. * Modified by Jaegeuk Kim, 2015.
  10. * Modified by Eric Biggers, 2019 for v2 policy support.
  11. */
  12. #include <linux/random.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/string.h>
  15. #include <linux/mount.h>
  16. #include "fscrypt_private.h"
  17. /**
  18. * fscrypt_policies_equal() - check whether two encryption policies are the same
  19. * @policy1: the first policy
  20. * @policy2: the second policy
  21. *
  22. * Return: %true if equal, else %false
  23. */
  24. bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
  25. const union fscrypt_policy *policy2)
  26. {
  27. if (policy1->version != policy2->version)
  28. return false;
  29. return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
  30. }
  31. static const union fscrypt_policy *
  32. fscrypt_get_dummy_policy(struct super_block *sb)
  33. {
  34. if (!sb->s_cop->get_dummy_policy)
  35. return NULL;
  36. return sb->s_cop->get_dummy_policy(sb);
  37. }
  38. static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
  39. {
  40. if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
  41. filenames_mode == FSCRYPT_MODE_AES_256_CTS)
  42. return true;
  43. if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
  44. filenames_mode == FSCRYPT_MODE_AES_128_CTS)
  45. return true;
  46. if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
  47. filenames_mode == FSCRYPT_MODE_ADIANTUM)
  48. return true;
  49. return false;
  50. }
  51. static bool supported_direct_key_modes(const struct inode *inode,
  52. u32 contents_mode, u32 filenames_mode)
  53. {
  54. const struct fscrypt_mode *mode;
  55. if (contents_mode != filenames_mode) {
  56. fscrypt_warn(inode,
  57. "Direct key flag not allowed with different contents and filenames modes");
  58. return false;
  59. }
  60. mode = &fscrypt_modes[contents_mode];
  61. if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
  62. fscrypt_warn(inode, "Direct key flag not allowed with %s",
  63. mode->friendly_name);
  64. return false;
  65. }
  66. return true;
  67. }
  68. static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
  69. const struct inode *inode,
  70. const char *type,
  71. int max_ino_bits, int max_lblk_bits)
  72. {
  73. struct super_block *sb = inode->i_sb;
  74. int ino_bits = 64, lblk_bits = 64;
  75. /*
  76. * IV_INO_LBLK_* exist only because of hardware limitations, and
  77. * currently the only known use case for them involves AES-256-XTS.
  78. * That's also all we test currently. For these reasons, for now only
  79. * allow AES-256-XTS here. This can be relaxed later if a use case for
  80. * IV_INO_LBLK_* with other encryption modes arises.
  81. */
  82. if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
  83. fscrypt_warn(inode,
  84. "Can't use %s policy with contents mode other than AES-256-XTS",
  85. type);
  86. return false;
  87. }
  88. /*
  89. * It's unsafe to include inode numbers in the IVs if the filesystem can
  90. * potentially renumber inodes, e.g. via filesystem shrinking.
  91. */
  92. if (!sb->s_cop->has_stable_inodes ||
  93. !sb->s_cop->has_stable_inodes(sb)) {
  94. fscrypt_warn(inode,
  95. "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
  96. type, sb->s_id);
  97. return false;
  98. }
  99. if (sb->s_cop->get_ino_and_lblk_bits)
  100. sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
  101. if (ino_bits > max_ino_bits) {
  102. fscrypt_warn(inode,
  103. "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
  104. type, sb->s_id);
  105. return false;
  106. }
  107. if (lblk_bits > max_lblk_bits) {
  108. fscrypt_warn(inode,
  109. "Can't use %s policy on filesystem '%s' because its block numbers are too long",
  110. type, sb->s_id);
  111. return false;
  112. }
  113. return true;
  114. }
  115. static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
  116. const struct inode *inode)
  117. {
  118. if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
  119. policy->filenames_encryption_mode)) {
  120. fscrypt_warn(inode,
  121. "Unsupported encryption modes (contents %d, filenames %d)",
  122. policy->contents_encryption_mode,
  123. policy->filenames_encryption_mode);
  124. return false;
  125. }
  126. if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
  127. FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
  128. fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
  129. policy->flags);
  130. return false;
  131. }
  132. if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
  133. !supported_direct_key_modes(inode, policy->contents_encryption_mode,
  134. policy->filenames_encryption_mode))
  135. return false;
  136. if (IS_CASEFOLDED(inode)) {
  137. /* With v1, there's no way to derive dirhash keys. */
  138. fscrypt_warn(inode,
  139. "v1 policies can't be used on casefolded directories");
  140. return false;
  141. }
  142. return true;
  143. }
  144. static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
  145. const struct inode *inode)
  146. {
  147. int count = 0;
  148. if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
  149. policy->filenames_encryption_mode)) {
  150. fscrypt_warn(inode,
  151. "Unsupported encryption modes (contents %d, filenames %d)",
  152. policy->contents_encryption_mode,
  153. policy->filenames_encryption_mode);
  154. return false;
  155. }
  156. if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
  157. FSCRYPT_POLICY_FLAG_DIRECT_KEY |
  158. FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
  159. FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
  160. fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
  161. policy->flags);
  162. return false;
  163. }
  164. count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
  165. count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
  166. count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
  167. if (count > 1) {
  168. fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
  169. policy->flags);
  170. return false;
  171. }
  172. if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
  173. !supported_direct_key_modes(inode, policy->contents_encryption_mode,
  174. policy->filenames_encryption_mode))
  175. return false;
  176. if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
  177. !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
  178. 32, 32))
  179. return false;
  180. /*
  181. * IV_INO_LBLK_32 hashes the inode number, so in principle it can
  182. * support any ino_bits. However, currently the inode number is gotten
  183. * from inode::i_ino which is 'unsigned long'. So for now the
  184. * implementation limit is 32 bits.
  185. */
  186. if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
  187. !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
  188. 32, 32))
  189. return false;
  190. if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
  191. fscrypt_warn(inode, "Reserved bits set in encryption policy");
  192. return false;
  193. }
  194. return true;
  195. }
  196. /**
  197. * fscrypt_supported_policy() - check whether an encryption policy is supported
  198. * @policy_u: the encryption policy
  199. * @inode: the inode on which the policy will be used
  200. *
  201. * Given an encryption policy, check whether all its encryption modes and other
  202. * settings are supported by this kernel on the given inode. (But we don't
  203. * currently don't check for crypto API support here, so attempting to use an
  204. * algorithm not configured into the crypto API will still fail later.)
  205. *
  206. * Return: %true if supported, else %false
  207. */
  208. bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
  209. const struct inode *inode)
  210. {
  211. switch (policy_u->version) {
  212. case FSCRYPT_POLICY_V1:
  213. return fscrypt_supported_v1_policy(&policy_u->v1, inode);
  214. case FSCRYPT_POLICY_V2:
  215. return fscrypt_supported_v2_policy(&policy_u->v2, inode);
  216. }
  217. return false;
  218. }
  219. /**
  220. * fscrypt_new_context() - create a new fscrypt_context
  221. * @ctx_u: output context
  222. * @policy_u: input policy
  223. * @nonce: nonce to use
  224. *
  225. * Create an fscrypt_context for an inode that is being assigned the given
  226. * encryption policy. @nonce must be a new random nonce.
  227. *
  228. * Return: the size of the new context in bytes.
  229. */
  230. static int fscrypt_new_context(union fscrypt_context *ctx_u,
  231. const union fscrypt_policy *policy_u,
  232. const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
  233. {
  234. memset(ctx_u, 0, sizeof(*ctx_u));
  235. switch (policy_u->version) {
  236. case FSCRYPT_POLICY_V1: {
  237. const struct fscrypt_policy_v1 *policy = &policy_u->v1;
  238. struct fscrypt_context_v1 *ctx = &ctx_u->v1;
  239. ctx->version = FSCRYPT_CONTEXT_V1;
  240. ctx->contents_encryption_mode =
  241. policy->contents_encryption_mode;
  242. ctx->filenames_encryption_mode =
  243. policy->filenames_encryption_mode;
  244. ctx->flags = policy->flags;
  245. memcpy(ctx->master_key_descriptor,
  246. policy->master_key_descriptor,
  247. sizeof(ctx->master_key_descriptor));
  248. memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
  249. return sizeof(*ctx);
  250. }
  251. case FSCRYPT_POLICY_V2: {
  252. const struct fscrypt_policy_v2 *policy = &policy_u->v2;
  253. struct fscrypt_context_v2 *ctx = &ctx_u->v2;
  254. ctx->version = FSCRYPT_CONTEXT_V2;
  255. ctx->contents_encryption_mode =
  256. policy->contents_encryption_mode;
  257. ctx->filenames_encryption_mode =
  258. policy->filenames_encryption_mode;
  259. ctx->flags = policy->flags;
  260. memcpy(ctx->master_key_identifier,
  261. policy->master_key_identifier,
  262. sizeof(ctx->master_key_identifier));
  263. memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
  264. return sizeof(*ctx);
  265. }
  266. }
  267. BUG();
  268. }
  269. /**
  270. * fscrypt_policy_from_context() - convert an fscrypt_context to
  271. * an fscrypt_policy
  272. * @policy_u: output policy
  273. * @ctx_u: input context
  274. * @ctx_size: size of input context in bytes
  275. *
  276. * Given an fscrypt_context, build the corresponding fscrypt_policy.
  277. *
  278. * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
  279. * version number or size.
  280. *
  281. * This does *not* validate the settings within the policy itself, e.g. the
  282. * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
  283. */
  284. int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
  285. const union fscrypt_context *ctx_u,
  286. int ctx_size)
  287. {
  288. memset(policy_u, 0, sizeof(*policy_u));
  289. if (!fscrypt_context_is_valid(ctx_u, ctx_size))
  290. return -EINVAL;
  291. switch (ctx_u->version) {
  292. case FSCRYPT_CONTEXT_V1: {
  293. const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
  294. struct fscrypt_policy_v1 *policy = &policy_u->v1;
  295. policy->version = FSCRYPT_POLICY_V1;
  296. policy->contents_encryption_mode =
  297. ctx->contents_encryption_mode;
  298. policy->filenames_encryption_mode =
  299. ctx->filenames_encryption_mode;
  300. policy->flags = ctx->flags;
  301. memcpy(policy->master_key_descriptor,
  302. ctx->master_key_descriptor,
  303. sizeof(policy->master_key_descriptor));
  304. return 0;
  305. }
  306. case FSCRYPT_CONTEXT_V2: {
  307. const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
  308. struct fscrypt_policy_v2 *policy = &policy_u->v2;
  309. policy->version = FSCRYPT_POLICY_V2;
  310. policy->contents_encryption_mode =
  311. ctx->contents_encryption_mode;
  312. policy->filenames_encryption_mode =
  313. ctx->filenames_encryption_mode;
  314. policy->flags = ctx->flags;
  315. memcpy(policy->__reserved, ctx->__reserved,
  316. sizeof(policy->__reserved));
  317. memcpy(policy->master_key_identifier,
  318. ctx->master_key_identifier,
  319. sizeof(policy->master_key_identifier));
  320. return 0;
  321. }
  322. }
  323. /* unreachable */
  324. return -EINVAL;
  325. }
  326. /* Retrieve an inode's encryption policy */
  327. static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
  328. {
  329. const struct fscrypt_info *ci;
  330. union fscrypt_context ctx;
  331. int ret;
  332. ci = fscrypt_get_info(inode);
  333. if (ci) {
  334. /* key available, use the cached policy */
  335. *policy = ci->ci_policy;
  336. return 0;
  337. }
  338. if (!IS_ENCRYPTED(inode))
  339. return -ENODATA;
  340. ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  341. if (ret < 0)
  342. return (ret == -ERANGE) ? -EINVAL : ret;
  343. return fscrypt_policy_from_context(policy, &ctx, ret);
  344. }
  345. static int set_encryption_policy(struct inode *inode,
  346. const union fscrypt_policy *policy)
  347. {
  348. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  349. union fscrypt_context ctx;
  350. int ctxsize;
  351. int err;
  352. if (!fscrypt_supported_policy(policy, inode))
  353. return -EINVAL;
  354. switch (policy->version) {
  355. case FSCRYPT_POLICY_V1:
  356. /*
  357. * The original encryption policy version provided no way of
  358. * verifying that the correct master key was supplied, which was
  359. * insecure in scenarios where multiple users have access to the
  360. * same encrypted files (even just read-only access). The new
  361. * encryption policy version fixes this and also implies use of
  362. * an improved key derivation function and allows non-root users
  363. * to securely remove keys. So as long as compatibility with
  364. * old kernels isn't required, it is recommended to use the new
  365. * policy version for all new encrypted directories.
  366. */
  367. pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
  368. current->comm, current->pid);
  369. break;
  370. case FSCRYPT_POLICY_V2:
  371. err = fscrypt_verify_key_added(inode->i_sb,
  372. policy->v2.master_key_identifier);
  373. if (err)
  374. return err;
  375. if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
  376. pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy. This should only be used if there are certain hardware limitations.\n",
  377. current->comm, current->pid);
  378. break;
  379. default:
  380. WARN_ON(1);
  381. return -EINVAL;
  382. }
  383. get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
  384. ctxsize = fscrypt_new_context(&ctx, policy, nonce);
  385. return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
  386. }
  387. int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
  388. {
  389. union fscrypt_policy policy;
  390. union fscrypt_policy existing_policy;
  391. struct inode *inode = file_inode(filp);
  392. u8 version;
  393. int size;
  394. int ret;
  395. if (get_user(policy.version, (const u8 __user *)arg))
  396. return -EFAULT;
  397. size = fscrypt_policy_size(&policy);
  398. if (size <= 0)
  399. return -EINVAL;
  400. /*
  401. * We should just copy the remaining 'size - 1' bytes here, but a
  402. * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
  403. * think that size can be 0 here (despite the check above!) *and* that
  404. * it's a compile-time constant. Thus it would think copy_from_user()
  405. * is passed compile-time constant ULONG_MAX, causing the compile-time
  406. * buffer overflow check to fail, breaking the build. This only occurred
  407. * when building an i386 kernel with -Os and branch profiling enabled.
  408. *
  409. * Work around it by just copying the first byte again...
  410. */
  411. version = policy.version;
  412. if (copy_from_user(&policy, arg, size))
  413. return -EFAULT;
  414. policy.version = version;
  415. if (!inode_owner_or_capable(inode))
  416. return -EACCES;
  417. ret = mnt_want_write_file(filp);
  418. if (ret)
  419. return ret;
  420. inode_lock(inode);
  421. ret = fscrypt_get_policy(inode, &existing_policy);
  422. if (ret == -ENODATA) {
  423. if (!S_ISDIR(inode->i_mode))
  424. ret = -ENOTDIR;
  425. else if (IS_DEADDIR(inode))
  426. ret = -ENOENT;
  427. else if (!inode->i_sb->s_cop->empty_dir(inode))
  428. ret = -ENOTEMPTY;
  429. else
  430. ret = set_encryption_policy(inode, &policy);
  431. } else if (ret == -EINVAL ||
  432. (ret == 0 && !fscrypt_policies_equal(&policy,
  433. &existing_policy))) {
  434. /* The file already uses a different encryption policy. */
  435. ret = -EEXIST;
  436. }
  437. inode_unlock(inode);
  438. mnt_drop_write_file(filp);
  439. return ret;
  440. }
  441. EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
  442. /* Original ioctl version; can only get the original policy version */
  443. int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
  444. {
  445. union fscrypt_policy policy;
  446. int err;
  447. err = fscrypt_get_policy(file_inode(filp), &policy);
  448. if (err)
  449. return err;
  450. if (policy.version != FSCRYPT_POLICY_V1)
  451. return -EINVAL;
  452. if (copy_to_user(arg, &policy, sizeof(policy.v1)))
  453. return -EFAULT;
  454. return 0;
  455. }
  456. EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
  457. /* Extended ioctl version; can get policies of any version */
  458. int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
  459. {
  460. struct fscrypt_get_policy_ex_arg arg;
  461. union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
  462. size_t policy_size;
  463. int err;
  464. /* arg is policy_size, then policy */
  465. BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
  466. BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
  467. offsetof(typeof(arg), policy));
  468. BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
  469. err = fscrypt_get_policy(file_inode(filp), policy);
  470. if (err)
  471. return err;
  472. policy_size = fscrypt_policy_size(policy);
  473. if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
  474. return -EFAULT;
  475. if (policy_size > arg.policy_size)
  476. return -EOVERFLOW;
  477. arg.policy_size = policy_size;
  478. if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
  479. return -EFAULT;
  480. return 0;
  481. }
  482. EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
  483. /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
  484. int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
  485. {
  486. struct inode *inode = file_inode(filp);
  487. union fscrypt_context ctx;
  488. int ret;
  489. ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  490. if (ret < 0)
  491. return ret;
  492. if (!fscrypt_context_is_valid(&ctx, ret))
  493. return -EINVAL;
  494. if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
  495. FSCRYPT_FILE_NONCE_SIZE))
  496. return -EFAULT;
  497. return 0;
  498. }
  499. EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
  500. /**
  501. * fscrypt_has_permitted_context() - is a file's encryption policy permitted
  502. * within its directory?
  503. *
  504. * @parent: inode for parent directory
  505. * @child: inode for file being looked up, opened, or linked into @parent
  506. *
  507. * Filesystems must call this before permitting access to an inode in a
  508. * situation where the parent directory is encrypted (either before allowing
  509. * ->lookup() to succeed, or for a regular file before allowing it to be opened)
  510. * and before any operation that involves linking an inode into an encrypted
  511. * directory, including link, rename, and cross rename. It enforces the
  512. * constraint that within a given encrypted directory tree, all files use the
  513. * same encryption policy. The pre-access check is needed to detect potentially
  514. * malicious offline violations of this constraint, while the link and rename
  515. * checks are needed to prevent online violations of this constraint.
  516. *
  517. * Return: 1 if permitted, 0 if forbidden.
  518. */
  519. int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
  520. {
  521. union fscrypt_policy parent_policy, child_policy;
  522. int err, err1, err2;
  523. /* No restrictions on file types which are never encrypted */
  524. if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
  525. !S_ISLNK(child->i_mode))
  526. return 1;
  527. /* No restrictions if the parent directory is unencrypted */
  528. if (!IS_ENCRYPTED(parent))
  529. return 1;
  530. /* Encrypted directories must not contain unencrypted files */
  531. if (!IS_ENCRYPTED(child))
  532. return 0;
  533. /*
  534. * Both parent and child are encrypted, so verify they use the same
  535. * encryption policy. Compare the fscrypt_info structs if the keys are
  536. * available, otherwise retrieve and compare the fscrypt_contexts.
  537. *
  538. * Note that the fscrypt_context retrieval will be required frequently
  539. * when accessing an encrypted directory tree without the key.
  540. * Performance-wise this is not a big deal because we already don't
  541. * really optimize for file access without the key (to the extent that
  542. * such access is even possible), given that any attempted access
  543. * already causes a fscrypt_context retrieval and keyring search.
  544. *
  545. * In any case, if an unexpected error occurs, fall back to "forbidden".
  546. */
  547. err = fscrypt_get_encryption_info(parent, true);
  548. if (err)
  549. return 0;
  550. err = fscrypt_get_encryption_info(child, true);
  551. if (err)
  552. return 0;
  553. err1 = fscrypt_get_policy(parent, &parent_policy);
  554. err2 = fscrypt_get_policy(child, &child_policy);
  555. /*
  556. * Allow the case where the parent and child both have an unrecognized
  557. * encryption policy, so that files with an unrecognized encryption
  558. * policy can be deleted.
  559. */
  560. if (err1 == -EINVAL && err2 == -EINVAL)
  561. return 1;
  562. if (err1 || err2)
  563. return 0;
  564. return fscrypt_policies_equal(&parent_policy, &child_policy);
  565. }
  566. EXPORT_SYMBOL(fscrypt_has_permitted_context);
  567. /*
  568. * Return the encryption policy that new files in the directory will inherit, or
  569. * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also
  570. * ensure that its key is set up, so that the new filename can be encrypted.
  571. */
  572. const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
  573. {
  574. int err;
  575. if (IS_ENCRYPTED(dir)) {
  576. err = fscrypt_require_key(dir);
  577. if (err)
  578. return ERR_PTR(err);
  579. return &dir->i_crypt_info->ci_policy;
  580. }
  581. return fscrypt_get_dummy_policy(dir->i_sb);
  582. }
  583. /**
  584. * fscrypt_set_context() - Set the fscrypt context of a new inode
  585. * @inode: a new inode
  586. * @fs_data: private data given by FS and passed to ->set_context()
  587. *
  588. * This should be called after fscrypt_prepare_new_inode(), generally during a
  589. * filesystem transaction. Everything here must be %GFP_NOFS-safe.
  590. *
  591. * Return: 0 on success, -errno on failure
  592. */
  593. int fscrypt_set_context(struct inode *inode, void *fs_data)
  594. {
  595. struct fscrypt_info *ci = inode->i_crypt_info;
  596. union fscrypt_context ctx;
  597. int ctxsize;
  598. /* fscrypt_prepare_new_inode() should have set up the key already. */
  599. if (WARN_ON_ONCE(!ci))
  600. return -ENOKEY;
  601. BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
  602. ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce);
  603. /*
  604. * This may be the first time the inode number is available, so do any
  605. * delayed key setup that requires the inode number.
  606. */
  607. if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
  608. (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
  609. const struct fscrypt_master_key *mk =
  610. ci->ci_master_key->payload.data[0];
  611. fscrypt_hash_inode_number(ci, mk);
  612. }
  613. return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
  614. }
  615. EXPORT_SYMBOL_GPL(fscrypt_set_context);
  616. /**
  617. * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
  618. * @sb: the filesystem on which test_dummy_encryption is being specified
  619. * @arg: the argument to the test_dummy_encryption option. May be NULL.
  620. * @dummy_policy: the filesystem's current dummy policy (input/output, see
  621. * below)
  622. *
  623. * Handle the test_dummy_encryption mount option by creating a dummy encryption
  624. * policy, saving it in @dummy_policy, and adding the corresponding dummy
  625. * encryption key to the filesystem. If the @dummy_policy is already set, then
  626. * instead validate that it matches @arg. Don't support changing it via
  627. * remount, as that is difficult to do safely.
  628. *
  629. * Return: 0 on success (dummy policy set, or the same policy is already set);
  630. * -EEXIST if a different dummy policy is already set;
  631. * or another -errno value.
  632. */
  633. int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
  634. struct fscrypt_dummy_policy *dummy_policy)
  635. {
  636. struct fscrypt_key_specifier key_spec = { 0 };
  637. int version;
  638. union fscrypt_policy *policy = NULL;
  639. int err;
  640. if (!arg)
  641. arg = "v2";
  642. if (!strcmp(arg, "v1")) {
  643. version = FSCRYPT_POLICY_V1;
  644. key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
  645. memset(key_spec.u.descriptor, 0x42,
  646. FSCRYPT_KEY_DESCRIPTOR_SIZE);
  647. } else if (!strcmp(arg, "v2")) {
  648. version = FSCRYPT_POLICY_V2;
  649. key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
  650. /* key_spec.u.identifier gets filled in when adding the key */
  651. } else {
  652. err = -EINVAL;
  653. goto out;
  654. }
  655. policy = kzalloc(sizeof(*policy), GFP_KERNEL);
  656. if (!policy) {
  657. err = -ENOMEM;
  658. goto out;
  659. }
  660. err = fscrypt_add_test_dummy_key(sb, &key_spec);
  661. if (err)
  662. goto out;
  663. policy->version = version;
  664. switch (policy->version) {
  665. case FSCRYPT_POLICY_V1:
  666. policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
  667. policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
  668. memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor,
  669. FSCRYPT_KEY_DESCRIPTOR_SIZE);
  670. break;
  671. case FSCRYPT_POLICY_V2:
  672. policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
  673. policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
  674. memcpy(policy->v2.master_key_identifier, key_spec.u.identifier,
  675. FSCRYPT_KEY_IDENTIFIER_SIZE);
  676. break;
  677. default:
  678. WARN_ON(1);
  679. err = -EINVAL;
  680. goto out;
  681. }
  682. if (dummy_policy->policy) {
  683. if (fscrypt_policies_equal(policy, dummy_policy->policy))
  684. err = 0;
  685. else
  686. err = -EEXIST;
  687. goto out;
  688. }
  689. dummy_policy->policy = policy;
  690. policy = NULL;
  691. err = 0;
  692. out:
  693. kfree(policy);
  694. return err;
  695. }
  696. EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
  697. /**
  698. * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
  699. * @seq: the seq_file to print the option to
  700. * @sep: the separator character to use
  701. * @sb: the filesystem whose options are being shown
  702. *
  703. * Show the test_dummy_encryption mount option, if it was specified.
  704. * This is mainly used for /proc/mounts.
  705. */
  706. void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
  707. struct super_block *sb)
  708. {
  709. const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
  710. int vers;
  711. if (!policy)
  712. return;
  713. vers = policy->version;
  714. if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
  715. vers = 1;
  716. seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
  717. }
  718. EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);