keyring.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Filesystem-level keyring for fscrypt
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. /*
  8. * This file implements management of fscrypt master keys in the
  9. * filesystem-level keyring, including the ioctls:
  10. *
  11. * - FS_IOC_ADD_ENCRYPTION_KEY
  12. * - FS_IOC_REMOVE_ENCRYPTION_KEY
  13. * - FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
  14. * - FS_IOC_GET_ENCRYPTION_KEY_STATUS
  15. *
  16. * See the "User API" section of Documentation/filesystems/fscrypt.rst for more
  17. * information about these ioctls.
  18. */
  19. #include <linux/unaligned.h>
  20. #include <crypto/skcipher.h>
  21. #include <linux/key-type.h>
  22. #include <linux/random.h>
  23. #include <linux/seq_file.h>
  24. #include "fscrypt_private.h"
  25. /* The master encryption keys for a filesystem (->s_master_keys) */
  26. struct fscrypt_keyring {
  27. /*
  28. * Lock that protects ->key_hashtable. It does *not* protect the
  29. * fscrypt_master_key structs themselves.
  30. */
  31. spinlock_t lock;
  32. /* Hash table that maps fscrypt_key_specifier to fscrypt_master_key */
  33. struct hlist_head key_hashtable[128];
  34. };
  35. static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
  36. {
  37. fscrypt_destroy_hkdf(&secret->hkdf);
  38. memzero_explicit(secret, sizeof(*secret));
  39. }
  40. static void move_master_key_secret(struct fscrypt_master_key_secret *dst,
  41. struct fscrypt_master_key_secret *src)
  42. {
  43. memcpy(dst, src, sizeof(*dst));
  44. memzero_explicit(src, sizeof(*src));
  45. }
  46. static void fscrypt_free_master_key(struct rcu_head *head)
  47. {
  48. struct fscrypt_master_key *mk =
  49. container_of(head, struct fscrypt_master_key, mk_rcu_head);
  50. /*
  51. * The master key secret and any embedded subkeys should have already
  52. * been wiped when the last active reference to the fscrypt_master_key
  53. * struct was dropped; doing it here would be unnecessarily late.
  54. * Nevertheless, use kfree_sensitive() in case anything was missed.
  55. */
  56. kfree_sensitive(mk);
  57. }
  58. void fscrypt_put_master_key(struct fscrypt_master_key *mk)
  59. {
  60. if (!refcount_dec_and_test(&mk->mk_struct_refs))
  61. return;
  62. /*
  63. * No structural references left, so free ->mk_users, and also free the
  64. * fscrypt_master_key struct itself after an RCU grace period ensures
  65. * that concurrent keyring lookups can no longer find it.
  66. */
  67. WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 0);
  68. if (mk->mk_users) {
  69. /* Clear the keyring so the quota gets released right away. */
  70. keyring_clear(mk->mk_users);
  71. key_put(mk->mk_users);
  72. mk->mk_users = NULL;
  73. }
  74. call_rcu(&mk->mk_rcu_head, fscrypt_free_master_key);
  75. }
  76. void fscrypt_put_master_key_activeref(struct super_block *sb,
  77. struct fscrypt_master_key *mk)
  78. {
  79. size_t i;
  80. if (!refcount_dec_and_test(&mk->mk_active_refs))
  81. return;
  82. /*
  83. * No active references left, so complete the full removal of this
  84. * fscrypt_master_key struct by removing it from the keyring and
  85. * destroying any subkeys embedded in it.
  86. */
  87. if (WARN_ON_ONCE(!sb->s_master_keys))
  88. return;
  89. spin_lock(&sb->s_master_keys->lock);
  90. hlist_del_rcu(&mk->mk_node);
  91. spin_unlock(&sb->s_master_keys->lock);
  92. /*
  93. * ->mk_active_refs == 0 implies that ->mk_present is false and
  94. * ->mk_decrypted_inodes is empty.
  95. */
  96. WARN_ON_ONCE(mk->mk_present);
  97. WARN_ON_ONCE(!list_empty(&mk->mk_decrypted_inodes));
  98. for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
  99. fscrypt_destroy_prepared_key(
  100. sb, &mk->mk_direct_keys[i]);
  101. fscrypt_destroy_prepared_key(
  102. sb, &mk->mk_iv_ino_lblk_64_keys[i]);
  103. fscrypt_destroy_prepared_key(
  104. sb, &mk->mk_iv_ino_lblk_32_keys[i]);
  105. }
  106. memzero_explicit(&mk->mk_ino_hash_key,
  107. sizeof(mk->mk_ino_hash_key));
  108. mk->mk_ino_hash_key_initialized = false;
  109. /* Drop the structural ref associated with the active refs. */
  110. fscrypt_put_master_key(mk);
  111. }
  112. /*
  113. * This transitions the key state from present to incompletely removed, and then
  114. * potentially to absent (depending on whether inodes remain).
  115. */
  116. static void fscrypt_initiate_key_removal(struct super_block *sb,
  117. struct fscrypt_master_key *mk)
  118. {
  119. WRITE_ONCE(mk->mk_present, false);
  120. wipe_master_key_secret(&mk->mk_secret);
  121. fscrypt_put_master_key_activeref(sb, mk);
  122. }
  123. static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
  124. {
  125. if (spec->__reserved)
  126. return false;
  127. return master_key_spec_len(spec) != 0;
  128. }
  129. static int fscrypt_user_key_instantiate(struct key *key,
  130. struct key_preparsed_payload *prep)
  131. {
  132. /*
  133. * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
  134. * each key, regardless of the exact key size. The amount of memory
  135. * actually used is greater than the size of the raw key anyway.
  136. */
  137. return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE);
  138. }
  139. static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m)
  140. {
  141. seq_puts(m, key->description);
  142. }
  143. /*
  144. * Type of key in ->mk_users. Each key of this type represents a particular
  145. * user who has added a particular master key.
  146. *
  147. * Note that the name of this key type really should be something like
  148. * ".fscrypt-user" instead of simply ".fscrypt". But the shorter name is chosen
  149. * mainly for simplicity of presentation in /proc/keys when read by a non-root
  150. * user. And it is expected to be rare that a key is actually added by multiple
  151. * users, since users should keep their encryption keys confidential.
  152. */
  153. static struct key_type key_type_fscrypt_user = {
  154. .name = ".fscrypt",
  155. .instantiate = fscrypt_user_key_instantiate,
  156. .describe = fscrypt_user_key_describe,
  157. };
  158. #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE \
  159. (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
  160. CONST_STRLEN("-users") + 1)
  161. #define FSCRYPT_MK_USER_DESCRIPTION_SIZE \
  162. (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
  163. static void format_mk_users_keyring_description(
  164. char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],
  165. const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
  166. {
  167. sprintf(description, "fscrypt-%*phN-users",
  168. FSCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier);
  169. }
  170. static void format_mk_user_description(
  171. char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],
  172. const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
  173. {
  174. sprintf(description, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE,
  175. mk_identifier, __kuid_val(current_fsuid()));
  176. }
  177. /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */
  178. static int allocate_filesystem_keyring(struct super_block *sb)
  179. {
  180. struct fscrypt_keyring *keyring;
  181. if (sb->s_master_keys)
  182. return 0;
  183. keyring = kzalloc(sizeof(*keyring), GFP_KERNEL);
  184. if (!keyring)
  185. return -ENOMEM;
  186. spin_lock_init(&keyring->lock);
  187. /*
  188. * Pairs with the smp_load_acquire() in fscrypt_find_master_key().
  189. * I.e., here we publish ->s_master_keys with a RELEASE barrier so that
  190. * concurrent tasks can ACQUIRE it.
  191. */
  192. smp_store_release(&sb->s_master_keys, keyring);
  193. return 0;
  194. }
  195. /*
  196. * Release all encryption keys that have been added to the filesystem, along
  197. * with the keyring that contains them.
  198. *
  199. * This is called at unmount time, after all potentially-encrypted inodes have
  200. * been evicted. The filesystem's underlying block device(s) are still
  201. * available at this time; this is important because after user file accesses
  202. * have been allowed, this function may need to evict keys from the keyslots of
  203. * an inline crypto engine, which requires the block device(s).
  204. */
  205. void fscrypt_destroy_keyring(struct super_block *sb)
  206. {
  207. struct fscrypt_keyring *keyring = sb->s_master_keys;
  208. size_t i;
  209. if (!keyring)
  210. return;
  211. for (i = 0; i < ARRAY_SIZE(keyring->key_hashtable); i++) {
  212. struct hlist_head *bucket = &keyring->key_hashtable[i];
  213. struct fscrypt_master_key *mk;
  214. struct hlist_node *tmp;
  215. hlist_for_each_entry_safe(mk, tmp, bucket, mk_node) {
  216. /*
  217. * Since all potentially-encrypted inodes were already
  218. * evicted, every key remaining in the keyring should
  219. * have an empty inode list, and should only still be in
  220. * the keyring due to the single active ref associated
  221. * with ->mk_present. There should be no structural
  222. * refs beyond the one associated with the active ref.
  223. */
  224. WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 1);
  225. WARN_ON_ONCE(refcount_read(&mk->mk_struct_refs) != 1);
  226. WARN_ON_ONCE(!mk->mk_present);
  227. fscrypt_initiate_key_removal(sb, mk);
  228. }
  229. }
  230. kfree_sensitive(keyring);
  231. sb->s_master_keys = NULL;
  232. }
  233. static struct hlist_head *
  234. fscrypt_mk_hash_bucket(struct fscrypt_keyring *keyring,
  235. const struct fscrypt_key_specifier *mk_spec)
  236. {
  237. /*
  238. * Since key specifiers should be "random" values, it is sufficient to
  239. * use a trivial hash function that just takes the first several bits of
  240. * the key specifier.
  241. */
  242. unsigned long i = get_unaligned((unsigned long *)&mk_spec->u);
  243. return &keyring->key_hashtable[i % ARRAY_SIZE(keyring->key_hashtable)];
  244. }
  245. /*
  246. * Find the specified master key struct in ->s_master_keys and take a structural
  247. * ref to it. The structural ref guarantees that the key struct continues to
  248. * exist, but it does *not* guarantee that ->s_master_keys continues to contain
  249. * the key struct. The structural ref needs to be dropped by
  250. * fscrypt_put_master_key(). Returns NULL if the key struct is not found.
  251. */
  252. struct fscrypt_master_key *
  253. fscrypt_find_master_key(struct super_block *sb,
  254. const struct fscrypt_key_specifier *mk_spec)
  255. {
  256. struct fscrypt_keyring *keyring;
  257. struct hlist_head *bucket;
  258. struct fscrypt_master_key *mk;
  259. /*
  260. * Pairs with the smp_store_release() in allocate_filesystem_keyring().
  261. * I.e., another task can publish ->s_master_keys concurrently,
  262. * executing a RELEASE barrier. We need to use smp_load_acquire() here
  263. * to safely ACQUIRE the memory the other task published.
  264. */
  265. keyring = smp_load_acquire(&sb->s_master_keys);
  266. if (keyring == NULL)
  267. return NULL; /* No keyring yet, so no keys yet. */
  268. bucket = fscrypt_mk_hash_bucket(keyring, mk_spec);
  269. rcu_read_lock();
  270. switch (mk_spec->type) {
  271. case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
  272. hlist_for_each_entry_rcu(mk, bucket, mk_node) {
  273. if (mk->mk_spec.type ==
  274. FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
  275. memcmp(mk->mk_spec.u.descriptor,
  276. mk_spec->u.descriptor,
  277. FSCRYPT_KEY_DESCRIPTOR_SIZE) == 0 &&
  278. refcount_inc_not_zero(&mk->mk_struct_refs))
  279. goto out;
  280. }
  281. break;
  282. case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
  283. hlist_for_each_entry_rcu(mk, bucket, mk_node) {
  284. if (mk->mk_spec.type ==
  285. FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
  286. memcmp(mk->mk_spec.u.identifier,
  287. mk_spec->u.identifier,
  288. FSCRYPT_KEY_IDENTIFIER_SIZE) == 0 &&
  289. refcount_inc_not_zero(&mk->mk_struct_refs))
  290. goto out;
  291. }
  292. break;
  293. }
  294. mk = NULL;
  295. out:
  296. rcu_read_unlock();
  297. return mk;
  298. }
  299. static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
  300. {
  301. char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE];
  302. struct key *keyring;
  303. format_mk_users_keyring_description(description,
  304. mk->mk_spec.u.identifier);
  305. keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
  306. current_cred(), KEY_POS_SEARCH |
  307. KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
  308. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  309. if (IS_ERR(keyring))
  310. return PTR_ERR(keyring);
  311. mk->mk_users = keyring;
  312. return 0;
  313. }
  314. /*
  315. * Find the current user's "key" in the master key's ->mk_users.
  316. * Returns ERR_PTR(-ENOKEY) if not found.
  317. */
  318. static struct key *find_master_key_user(struct fscrypt_master_key *mk)
  319. {
  320. char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
  321. key_ref_t keyref;
  322. format_mk_user_description(description, mk->mk_spec.u.identifier);
  323. /*
  324. * We need to mark the keyring reference as "possessed" so that we
  325. * acquire permission to search it, via the KEY_POS_SEARCH permission.
  326. */
  327. keyref = keyring_search(make_key_ref(mk->mk_users, true /*possessed*/),
  328. &key_type_fscrypt_user, description, false);
  329. if (IS_ERR(keyref)) {
  330. if (PTR_ERR(keyref) == -EAGAIN || /* not found */
  331. PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
  332. keyref = ERR_PTR(-ENOKEY);
  333. return ERR_CAST(keyref);
  334. }
  335. return key_ref_to_ptr(keyref);
  336. }
  337. /*
  338. * Give the current user a "key" in ->mk_users. This charges the user's quota
  339. * and marks the master key as added by the current user, so that it cannot be
  340. * removed by another user with the key. Either ->mk_sem must be held for
  341. * write, or the master key must be still undergoing initialization.
  342. */
  343. static int add_master_key_user(struct fscrypt_master_key *mk)
  344. {
  345. char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
  346. struct key *mk_user;
  347. int err;
  348. format_mk_user_description(description, mk->mk_spec.u.identifier);
  349. mk_user = key_alloc(&key_type_fscrypt_user, description,
  350. current_fsuid(), current_gid(), current_cred(),
  351. KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
  352. if (IS_ERR(mk_user))
  353. return PTR_ERR(mk_user);
  354. err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
  355. key_put(mk_user);
  356. return err;
  357. }
  358. /*
  359. * Remove the current user's "key" from ->mk_users.
  360. * ->mk_sem must be held for write.
  361. *
  362. * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
  363. */
  364. static int remove_master_key_user(struct fscrypt_master_key *mk)
  365. {
  366. struct key *mk_user;
  367. int err;
  368. mk_user = find_master_key_user(mk);
  369. if (IS_ERR(mk_user))
  370. return PTR_ERR(mk_user);
  371. err = key_unlink(mk->mk_users, mk_user);
  372. key_put(mk_user);
  373. return err;
  374. }
  375. /*
  376. * Allocate a new fscrypt_master_key, transfer the given secret over to it, and
  377. * insert it into sb->s_master_keys.
  378. */
  379. static int add_new_master_key(struct super_block *sb,
  380. struct fscrypt_master_key_secret *secret,
  381. const struct fscrypt_key_specifier *mk_spec)
  382. {
  383. struct fscrypt_keyring *keyring = sb->s_master_keys;
  384. struct fscrypt_master_key *mk;
  385. int err;
  386. mk = kzalloc(sizeof(*mk), GFP_KERNEL);
  387. if (!mk)
  388. return -ENOMEM;
  389. init_rwsem(&mk->mk_sem);
  390. refcount_set(&mk->mk_struct_refs, 1);
  391. mk->mk_spec = *mk_spec;
  392. INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
  393. spin_lock_init(&mk->mk_decrypted_inodes_lock);
  394. if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
  395. err = allocate_master_key_users_keyring(mk);
  396. if (err)
  397. goto out_put;
  398. err = add_master_key_user(mk);
  399. if (err)
  400. goto out_put;
  401. }
  402. move_master_key_secret(&mk->mk_secret, secret);
  403. mk->mk_present = true;
  404. refcount_set(&mk->mk_active_refs, 1); /* ->mk_present is true */
  405. spin_lock(&keyring->lock);
  406. hlist_add_head_rcu(&mk->mk_node,
  407. fscrypt_mk_hash_bucket(keyring, mk_spec));
  408. spin_unlock(&keyring->lock);
  409. return 0;
  410. out_put:
  411. fscrypt_put_master_key(mk);
  412. return err;
  413. }
  414. #define KEY_DEAD 1
  415. static int add_existing_master_key(struct fscrypt_master_key *mk,
  416. struct fscrypt_master_key_secret *secret)
  417. {
  418. int err;
  419. /*
  420. * If the current user is already in ->mk_users, then there's nothing to
  421. * do. Otherwise, we need to add the user to ->mk_users. (Neither is
  422. * applicable for v1 policy keys, which have NULL ->mk_users.)
  423. */
  424. if (mk->mk_users) {
  425. struct key *mk_user = find_master_key_user(mk);
  426. if (mk_user != ERR_PTR(-ENOKEY)) {
  427. if (IS_ERR(mk_user))
  428. return PTR_ERR(mk_user);
  429. key_put(mk_user);
  430. return 0;
  431. }
  432. err = add_master_key_user(mk);
  433. if (err)
  434. return err;
  435. }
  436. /* If the key is incompletely removed, make it present again. */
  437. if (!mk->mk_present) {
  438. if (!refcount_inc_not_zero(&mk->mk_active_refs)) {
  439. /*
  440. * Raced with the last active ref being dropped, so the
  441. * key has become, or is about to become, "absent".
  442. * Therefore, we need to allocate a new key struct.
  443. */
  444. return KEY_DEAD;
  445. }
  446. move_master_key_secret(&mk->mk_secret, secret);
  447. WRITE_ONCE(mk->mk_present, true);
  448. }
  449. return 0;
  450. }
  451. static int do_add_master_key(struct super_block *sb,
  452. struct fscrypt_master_key_secret *secret,
  453. const struct fscrypt_key_specifier *mk_spec)
  454. {
  455. static DEFINE_MUTEX(fscrypt_add_key_mutex);
  456. struct fscrypt_master_key *mk;
  457. int err;
  458. mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */
  459. mk = fscrypt_find_master_key(sb, mk_spec);
  460. if (!mk) {
  461. /* Didn't find the key in ->s_master_keys. Add it. */
  462. err = allocate_filesystem_keyring(sb);
  463. if (!err)
  464. err = add_new_master_key(sb, secret, mk_spec);
  465. } else {
  466. /*
  467. * Found the key in ->s_master_keys. Add the user to ->mk_users
  468. * if needed, and make the key "present" again if possible.
  469. */
  470. down_write(&mk->mk_sem);
  471. err = add_existing_master_key(mk, secret);
  472. up_write(&mk->mk_sem);
  473. if (err == KEY_DEAD) {
  474. /*
  475. * We found a key struct, but it's already been fully
  476. * removed. Ignore the old struct and add a new one.
  477. * fscrypt_add_key_mutex means we don't need to worry
  478. * about concurrent adds.
  479. */
  480. err = add_new_master_key(sb, secret, mk_spec);
  481. }
  482. fscrypt_put_master_key(mk);
  483. }
  484. mutex_unlock(&fscrypt_add_key_mutex);
  485. return err;
  486. }
  487. static int add_master_key(struct super_block *sb,
  488. struct fscrypt_master_key_secret *secret,
  489. struct fscrypt_key_specifier *key_spec)
  490. {
  491. int err;
  492. if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
  493. err = fscrypt_init_hkdf(&secret->hkdf, secret->raw,
  494. secret->size);
  495. if (err)
  496. return err;
  497. /*
  498. * Now that the HKDF context is initialized, the raw key is no
  499. * longer needed.
  500. */
  501. memzero_explicit(secret->raw, secret->size);
  502. /* Calculate the key identifier */
  503. err = fscrypt_hkdf_expand(&secret->hkdf,
  504. HKDF_CONTEXT_KEY_IDENTIFIER, NULL, 0,
  505. key_spec->u.identifier,
  506. FSCRYPT_KEY_IDENTIFIER_SIZE);
  507. if (err)
  508. return err;
  509. }
  510. return do_add_master_key(sb, secret, key_spec);
  511. }
  512. static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep)
  513. {
  514. const struct fscrypt_provisioning_key_payload *payload = prep->data;
  515. if (prep->datalen < sizeof(*payload) + FSCRYPT_MIN_KEY_SIZE ||
  516. prep->datalen > sizeof(*payload) + FSCRYPT_MAX_KEY_SIZE)
  517. return -EINVAL;
  518. if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
  519. payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
  520. return -EINVAL;
  521. if (payload->__reserved)
  522. return -EINVAL;
  523. prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL);
  524. if (!prep->payload.data[0])
  525. return -ENOMEM;
  526. prep->quotalen = prep->datalen;
  527. return 0;
  528. }
  529. static void fscrypt_provisioning_key_free_preparse(
  530. struct key_preparsed_payload *prep)
  531. {
  532. kfree_sensitive(prep->payload.data[0]);
  533. }
  534. static void fscrypt_provisioning_key_describe(const struct key *key,
  535. struct seq_file *m)
  536. {
  537. seq_puts(m, key->description);
  538. if (key_is_positive(key)) {
  539. const struct fscrypt_provisioning_key_payload *payload =
  540. key->payload.data[0];
  541. seq_printf(m, ": %u [%u]", key->datalen, payload->type);
  542. }
  543. }
  544. static void fscrypt_provisioning_key_destroy(struct key *key)
  545. {
  546. kfree_sensitive(key->payload.data[0]);
  547. }
  548. static struct key_type key_type_fscrypt_provisioning = {
  549. .name = "fscrypt-provisioning",
  550. .preparse = fscrypt_provisioning_key_preparse,
  551. .free_preparse = fscrypt_provisioning_key_free_preparse,
  552. .instantiate = generic_key_instantiate,
  553. .describe = fscrypt_provisioning_key_describe,
  554. .destroy = fscrypt_provisioning_key_destroy,
  555. };
  556. /*
  557. * Retrieve the raw key from the Linux keyring key specified by 'key_id', and
  558. * store it into 'secret'.
  559. *
  560. * The key must be of type "fscrypt-provisioning" and must have the field
  561. * fscrypt_provisioning_key_payload::type set to 'type', indicating that it's
  562. * only usable with fscrypt with the particular KDF version identified by
  563. * 'type'. We don't use the "logon" key type because there's no way to
  564. * completely restrict the use of such keys; they can be used by any kernel API
  565. * that accepts "logon" keys and doesn't require a specific service prefix.
  566. *
  567. * The ability to specify the key via Linux keyring key is intended for cases
  568. * where userspace needs to re-add keys after the filesystem is unmounted and
  569. * re-mounted. Most users should just provide the raw key directly instead.
  570. */
  571. static int get_keyring_key(u32 key_id, u32 type,
  572. struct fscrypt_master_key_secret *secret)
  573. {
  574. key_ref_t ref;
  575. struct key *key;
  576. const struct fscrypt_provisioning_key_payload *payload;
  577. int err;
  578. ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH);
  579. if (IS_ERR(ref))
  580. return PTR_ERR(ref);
  581. key = key_ref_to_ptr(ref);
  582. if (key->type != &key_type_fscrypt_provisioning)
  583. goto bad_key;
  584. payload = key->payload.data[0];
  585. /* Don't allow fscrypt v1 keys to be used as v2 keys and vice versa. */
  586. if (payload->type != type)
  587. goto bad_key;
  588. secret->size = key->datalen - sizeof(*payload);
  589. memcpy(secret->raw, payload->raw, secret->size);
  590. err = 0;
  591. goto out_put;
  592. bad_key:
  593. err = -EKEYREJECTED;
  594. out_put:
  595. key_ref_put(ref);
  596. return err;
  597. }
  598. /*
  599. * Add a master encryption key to the filesystem, causing all files which were
  600. * encrypted with it to appear "unlocked" (decrypted) when accessed.
  601. *
  602. * When adding a key for use by v1 encryption policies, this ioctl is
  603. * privileged, and userspace must provide the 'key_descriptor'.
  604. *
  605. * When adding a key for use by v2+ encryption policies, this ioctl is
  606. * unprivileged. This is needed, in general, to allow non-root users to use
  607. * encryption without encountering the visibility problems of process-subscribed
  608. * keyrings and the inability to properly remove keys. This works by having
  609. * each key identified by its cryptographically secure hash --- the
  610. * 'key_identifier'. The cryptographic hash ensures that a malicious user
  611. * cannot add the wrong key for a given identifier. Furthermore, each added key
  612. * is charged to the appropriate user's quota for the keyrings service, which
  613. * prevents a malicious user from adding too many keys. Finally, we forbid a
  614. * user from removing a key while other users have added it too, which prevents
  615. * a user who knows another user's key from causing a denial-of-service by
  616. * removing it at an inopportune time. (We tolerate that a user who knows a key
  617. * can prevent other users from removing it.)
  618. *
  619. * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of
  620. * Documentation/filesystems/fscrypt.rst.
  621. */
  622. int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
  623. {
  624. struct super_block *sb = file_inode(filp)->i_sb;
  625. struct fscrypt_add_key_arg __user *uarg = _uarg;
  626. struct fscrypt_add_key_arg arg;
  627. struct fscrypt_master_key_secret secret;
  628. int err;
  629. if (copy_from_user(&arg, uarg, sizeof(arg)))
  630. return -EFAULT;
  631. if (!valid_key_spec(&arg.key_spec))
  632. return -EINVAL;
  633. if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
  634. return -EINVAL;
  635. /*
  636. * Only root can add keys that are identified by an arbitrary descriptor
  637. * rather than by a cryptographic hash --- since otherwise a malicious
  638. * user could add the wrong key.
  639. */
  640. if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
  641. !capable(CAP_SYS_ADMIN))
  642. return -EACCES;
  643. memset(&secret, 0, sizeof(secret));
  644. if (arg.key_id) {
  645. if (arg.raw_size != 0)
  646. return -EINVAL;
  647. err = get_keyring_key(arg.key_id, arg.key_spec.type, &secret);
  648. if (err)
  649. goto out_wipe_secret;
  650. } else {
  651. if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
  652. arg.raw_size > FSCRYPT_MAX_KEY_SIZE)
  653. return -EINVAL;
  654. secret.size = arg.raw_size;
  655. err = -EFAULT;
  656. if (copy_from_user(secret.raw, uarg->raw, secret.size))
  657. goto out_wipe_secret;
  658. }
  659. err = add_master_key(sb, &secret, &arg.key_spec);
  660. if (err)
  661. goto out_wipe_secret;
  662. /* Return the key identifier to userspace, if applicable */
  663. err = -EFAULT;
  664. if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
  665. copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier,
  666. FSCRYPT_KEY_IDENTIFIER_SIZE))
  667. goto out_wipe_secret;
  668. err = 0;
  669. out_wipe_secret:
  670. wipe_master_key_secret(&secret);
  671. return err;
  672. }
  673. EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
  674. static void
  675. fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret *secret)
  676. {
  677. static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
  678. get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
  679. memset(secret, 0, sizeof(*secret));
  680. secret->size = FSCRYPT_MAX_KEY_SIZE;
  681. memcpy(secret->raw, test_key, FSCRYPT_MAX_KEY_SIZE);
  682. }
  683. int fscrypt_get_test_dummy_key_identifier(
  684. u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
  685. {
  686. struct fscrypt_master_key_secret secret;
  687. int err;
  688. fscrypt_get_test_dummy_secret(&secret);
  689. err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
  690. if (err)
  691. goto out;
  692. err = fscrypt_hkdf_expand(&secret.hkdf, HKDF_CONTEXT_KEY_IDENTIFIER,
  693. NULL, 0, key_identifier,
  694. FSCRYPT_KEY_IDENTIFIER_SIZE);
  695. out:
  696. wipe_master_key_secret(&secret);
  697. return err;
  698. }
  699. /**
  700. * fscrypt_add_test_dummy_key() - add the test dummy encryption key
  701. * @sb: the filesystem instance to add the key to
  702. * @key_spec: the key specifier of the test dummy encryption key
  703. *
  704. * Add the key for the test_dummy_encryption mount option to the filesystem. To
  705. * prevent misuse of this mount option, a per-boot random key is used instead of
  706. * a hardcoded one. This makes it so that any encrypted files created using
  707. * this option won't be accessible after a reboot.
  708. *
  709. * Return: 0 on success, -errno on failure
  710. */
  711. int fscrypt_add_test_dummy_key(struct super_block *sb,
  712. struct fscrypt_key_specifier *key_spec)
  713. {
  714. struct fscrypt_master_key_secret secret;
  715. int err;
  716. fscrypt_get_test_dummy_secret(&secret);
  717. err = add_master_key(sb, &secret, key_spec);
  718. wipe_master_key_secret(&secret);
  719. return err;
  720. }
  721. /*
  722. * Verify that the current user has added a master key with the given identifier
  723. * (returns -ENOKEY if not). This is needed to prevent a user from encrypting
  724. * their files using some other user's key which they don't actually know.
  725. * Cryptographically this isn't much of a problem, but the semantics of this
  726. * would be a bit weird, so it's best to just forbid it.
  727. *
  728. * The system administrator (CAP_FOWNER) can override this, which should be
  729. * enough for any use cases where encryption policies are being set using keys
  730. * that were chosen ahead of time but aren't available at the moment.
  731. *
  732. * Note that the key may have already removed by the time this returns, but
  733. * that's okay; we just care whether the key was there at some point.
  734. *
  735. * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
  736. */
  737. int fscrypt_verify_key_added(struct super_block *sb,
  738. const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
  739. {
  740. struct fscrypt_key_specifier mk_spec;
  741. struct fscrypt_master_key *mk;
  742. struct key *mk_user;
  743. int err;
  744. mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
  745. memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
  746. mk = fscrypt_find_master_key(sb, &mk_spec);
  747. if (!mk) {
  748. err = -ENOKEY;
  749. goto out;
  750. }
  751. down_read(&mk->mk_sem);
  752. mk_user = find_master_key_user(mk);
  753. if (IS_ERR(mk_user)) {
  754. err = PTR_ERR(mk_user);
  755. } else {
  756. key_put(mk_user);
  757. err = 0;
  758. }
  759. up_read(&mk->mk_sem);
  760. fscrypt_put_master_key(mk);
  761. out:
  762. if (err == -ENOKEY && capable(CAP_FOWNER))
  763. err = 0;
  764. return err;
  765. }
  766. /*
  767. * Try to evict the inode's dentries from the dentry cache. If the inode is a
  768. * directory, then it can have at most one dentry; however, that dentry may be
  769. * pinned by child dentries, so first try to evict the children too.
  770. */
  771. static void shrink_dcache_inode(struct inode *inode)
  772. {
  773. struct dentry *dentry;
  774. if (S_ISDIR(inode->i_mode)) {
  775. dentry = d_find_any_alias(inode);
  776. if (dentry) {
  777. shrink_dcache_parent(dentry);
  778. dput(dentry);
  779. }
  780. }
  781. d_prune_aliases(inode);
  782. }
  783. static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
  784. {
  785. struct fscrypt_inode_info *ci;
  786. struct inode *inode;
  787. struct inode *toput_inode = NULL;
  788. spin_lock(&mk->mk_decrypted_inodes_lock);
  789. list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
  790. inode = ci->ci_inode;
  791. spin_lock(&inode->i_lock);
  792. if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
  793. spin_unlock(&inode->i_lock);
  794. continue;
  795. }
  796. __iget(inode);
  797. spin_unlock(&inode->i_lock);
  798. spin_unlock(&mk->mk_decrypted_inodes_lock);
  799. shrink_dcache_inode(inode);
  800. iput(toput_inode);
  801. toput_inode = inode;
  802. spin_lock(&mk->mk_decrypted_inodes_lock);
  803. }
  804. spin_unlock(&mk->mk_decrypted_inodes_lock);
  805. iput(toput_inode);
  806. }
  807. static int check_for_busy_inodes(struct super_block *sb,
  808. struct fscrypt_master_key *mk)
  809. {
  810. struct list_head *pos;
  811. size_t busy_count = 0;
  812. unsigned long ino;
  813. char ino_str[50] = "";
  814. spin_lock(&mk->mk_decrypted_inodes_lock);
  815. list_for_each(pos, &mk->mk_decrypted_inodes)
  816. busy_count++;
  817. if (busy_count == 0) {
  818. spin_unlock(&mk->mk_decrypted_inodes_lock);
  819. return 0;
  820. }
  821. {
  822. /* select an example file to show for debugging purposes */
  823. struct inode *inode =
  824. list_first_entry(&mk->mk_decrypted_inodes,
  825. struct fscrypt_inode_info,
  826. ci_master_key_link)->ci_inode;
  827. ino = inode->i_ino;
  828. }
  829. spin_unlock(&mk->mk_decrypted_inodes_lock);
  830. /* If the inode is currently being created, ino may still be 0. */
  831. if (ino)
  832. snprintf(ino_str, sizeof(ino_str), ", including ino %lu", ino);
  833. fscrypt_warn(NULL,
  834. "%s: %zu inode(s) still busy after removing key with %s %*phN%s",
  835. sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
  836. master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
  837. ino_str);
  838. return -EBUSY;
  839. }
  840. static int try_to_lock_encrypted_files(struct super_block *sb,
  841. struct fscrypt_master_key *mk)
  842. {
  843. int err1;
  844. int err2;
  845. /*
  846. * An inode can't be evicted while it is dirty or has dirty pages.
  847. * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
  848. *
  849. * Just do it the easy way: call sync_filesystem(). It's overkill, but
  850. * it works, and it's more important to minimize the amount of caches we
  851. * drop than the amount of data we sync. Also, unprivileged users can
  852. * already call sync_filesystem() via sys_syncfs() or sys_sync().
  853. */
  854. down_read(&sb->s_umount);
  855. err1 = sync_filesystem(sb);
  856. up_read(&sb->s_umount);
  857. /* If a sync error occurs, still try to evict as much as possible. */
  858. /*
  859. * Inodes are pinned by their dentries, so we have to evict their
  860. * dentries. shrink_dcache_sb() would suffice, but would be overkill
  861. * and inappropriate for use by unprivileged users. So instead go
  862. * through the inodes' alias lists and try to evict each dentry.
  863. */
  864. evict_dentries_for_decrypted_inodes(mk);
  865. /*
  866. * evict_dentries_for_decrypted_inodes() already iput() each inode in
  867. * the list; any inodes for which that dropped the last reference will
  868. * have been evicted due to fscrypt_drop_inode() detecting the key
  869. * removal and telling the VFS to evict the inode. So to finish, we
  870. * just need to check whether any inodes couldn't be evicted.
  871. */
  872. err2 = check_for_busy_inodes(sb, mk);
  873. return err1 ?: err2;
  874. }
  875. /*
  876. * Try to remove an fscrypt master encryption key.
  877. *
  878. * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
  879. * claim to the key, then removes the key itself if no other users have claims.
  880. * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
  881. * key itself.
  882. *
  883. * To "remove the key itself", first we transition the key to the "incompletely
  884. * removed" state, so that no more inodes can be unlocked with it. Then we try
  885. * to evict all cached inodes that had been unlocked with the key.
  886. *
  887. * If all inodes were evicted, then we unlink the fscrypt_master_key from the
  888. * keyring. Otherwise it remains in the keyring in the "incompletely removed"
  889. * state where it tracks the list of remaining inodes. Userspace can execute
  890. * the ioctl again later to retry eviction, or alternatively can re-add the key.
  891. *
  892. * For more details, see the "Removing keys" section of
  893. * Documentation/filesystems/fscrypt.rst.
  894. */
  895. static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
  896. {
  897. struct super_block *sb = file_inode(filp)->i_sb;
  898. struct fscrypt_remove_key_arg __user *uarg = _uarg;
  899. struct fscrypt_remove_key_arg arg;
  900. struct fscrypt_master_key *mk;
  901. u32 status_flags = 0;
  902. int err;
  903. bool inodes_remain;
  904. if (copy_from_user(&arg, uarg, sizeof(arg)))
  905. return -EFAULT;
  906. if (!valid_key_spec(&arg.key_spec))
  907. return -EINVAL;
  908. if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
  909. return -EINVAL;
  910. /*
  911. * Only root can add and remove keys that are identified by an arbitrary
  912. * descriptor rather than by a cryptographic hash.
  913. */
  914. if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
  915. !capable(CAP_SYS_ADMIN))
  916. return -EACCES;
  917. /* Find the key being removed. */
  918. mk = fscrypt_find_master_key(sb, &arg.key_spec);
  919. if (!mk)
  920. return -ENOKEY;
  921. down_write(&mk->mk_sem);
  922. /* If relevant, remove current user's (or all users) claim to the key */
  923. if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
  924. if (all_users)
  925. err = keyring_clear(mk->mk_users);
  926. else
  927. err = remove_master_key_user(mk);
  928. if (err) {
  929. up_write(&mk->mk_sem);
  930. goto out_put_key;
  931. }
  932. if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
  933. /*
  934. * Other users have still added the key too. We removed
  935. * the current user's claim to the key, but we still
  936. * can't remove the key itself.
  937. */
  938. status_flags |=
  939. FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
  940. err = 0;
  941. up_write(&mk->mk_sem);
  942. goto out_put_key;
  943. }
  944. }
  945. /* No user claims remaining. Initiate removal of the key. */
  946. err = -ENOKEY;
  947. if (mk->mk_present) {
  948. fscrypt_initiate_key_removal(sb, mk);
  949. err = 0;
  950. }
  951. inodes_remain = refcount_read(&mk->mk_active_refs) > 0;
  952. up_write(&mk->mk_sem);
  953. if (inodes_remain) {
  954. /* Some inodes still reference this key; try to evict them. */
  955. err = try_to_lock_encrypted_files(sb, mk);
  956. if (err == -EBUSY) {
  957. status_flags |=
  958. FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
  959. err = 0;
  960. }
  961. }
  962. /*
  963. * We return 0 if we successfully did something: removed a claim to the
  964. * key, initiated removal of the key, or tried locking the files again.
  965. * Users need to check the informational status flags if they care
  966. * whether the key has been fully removed including all files locked.
  967. */
  968. out_put_key:
  969. fscrypt_put_master_key(mk);
  970. if (err == 0)
  971. err = put_user(status_flags, &uarg->removal_status_flags);
  972. return err;
  973. }
  974. int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
  975. {
  976. return do_remove_key(filp, uarg, false);
  977. }
  978. EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
  979. int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
  980. {
  981. if (!capable(CAP_SYS_ADMIN))
  982. return -EACCES;
  983. return do_remove_key(filp, uarg, true);
  984. }
  985. EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
  986. /*
  987. * Retrieve the status of an fscrypt master encryption key.
  988. *
  989. * We set ->status to indicate whether the key is absent, present, or
  990. * incompletely removed. (For an explanation of what these statuses mean and
  991. * how they are represented internally, see struct fscrypt_master_key.) This
  992. * field allows applications to easily determine the status of an encrypted
  993. * directory without using a hack such as trying to open a regular file in it
  994. * (which can confuse the "incompletely removed" status with absent or present).
  995. *
  996. * In addition, for v2 policy keys we allow applications to determine, via
  997. * ->status_flags and ->user_count, whether the key has been added by the
  998. * current user, by other users, or by both. Most applications should not need
  999. * this, since ordinarily only one user should know a given key. However, if a
  1000. * secret key is shared by multiple users, applications may wish to add an
  1001. * already-present key to prevent other users from removing it. This ioctl can
  1002. * be used to check whether that really is the case before the work is done to
  1003. * add the key --- which might e.g. require prompting the user for a passphrase.
  1004. *
  1005. * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of
  1006. * Documentation/filesystems/fscrypt.rst.
  1007. */
  1008. int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
  1009. {
  1010. struct super_block *sb = file_inode(filp)->i_sb;
  1011. struct fscrypt_get_key_status_arg arg;
  1012. struct fscrypt_master_key *mk;
  1013. int err;
  1014. if (copy_from_user(&arg, uarg, sizeof(arg)))
  1015. return -EFAULT;
  1016. if (!valid_key_spec(&arg.key_spec))
  1017. return -EINVAL;
  1018. if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
  1019. return -EINVAL;
  1020. arg.status_flags = 0;
  1021. arg.user_count = 0;
  1022. memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
  1023. mk = fscrypt_find_master_key(sb, &arg.key_spec);
  1024. if (!mk) {
  1025. arg.status = FSCRYPT_KEY_STATUS_ABSENT;
  1026. err = 0;
  1027. goto out;
  1028. }
  1029. down_read(&mk->mk_sem);
  1030. if (!mk->mk_present) {
  1031. arg.status = refcount_read(&mk->mk_active_refs) > 0 ?
  1032. FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED :
  1033. FSCRYPT_KEY_STATUS_ABSENT /* raced with full removal */;
  1034. err = 0;
  1035. goto out_release_key;
  1036. }
  1037. arg.status = FSCRYPT_KEY_STATUS_PRESENT;
  1038. if (mk->mk_users) {
  1039. struct key *mk_user;
  1040. arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
  1041. mk_user = find_master_key_user(mk);
  1042. if (!IS_ERR(mk_user)) {
  1043. arg.status_flags |=
  1044. FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
  1045. key_put(mk_user);
  1046. } else if (mk_user != ERR_PTR(-ENOKEY)) {
  1047. err = PTR_ERR(mk_user);
  1048. goto out_release_key;
  1049. }
  1050. }
  1051. err = 0;
  1052. out_release_key:
  1053. up_read(&mk->mk_sem);
  1054. fscrypt_put_master_key(mk);
  1055. out:
  1056. if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
  1057. err = -EFAULT;
  1058. return err;
  1059. }
  1060. EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
  1061. int __init fscrypt_init_keyring(void)
  1062. {
  1063. int err;
  1064. err = register_key_type(&key_type_fscrypt_user);
  1065. if (err)
  1066. return err;
  1067. err = register_key_type(&key_type_fscrypt_provisioning);
  1068. if (err)
  1069. goto err_unregister_fscrypt_user;
  1070. return 0;
  1071. err_unregister_fscrypt_user:
  1072. unregister_key_type(&key_type_fscrypt_user);
  1073. return err;
  1074. }