policy.c 27 KB

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