policy.c 27 KB

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