cert_store.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DIAG 0x320 support and certificate store handling
  4. *
  5. * Copyright IBM Corp. 2023
  6. * Author(s): Anastasia Eskova <anastasia.eskova@ibm.com>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/fs.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/key-type.h>
  15. #include <linux/key.h>
  16. #include <linux/keyctl.h>
  17. #include <linux/kobject.h>
  18. #include <linux/module.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/slab.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/vmalloc.h>
  23. #include <crypto/sha2.h>
  24. #include <keys/user-type.h>
  25. #include <asm/debug.h>
  26. #include <asm/diag.h>
  27. #include <asm/ebcdic.h>
  28. #include <asm/sclp.h>
  29. #define DIAG_MAX_RETRIES 10
  30. #define VCE_FLAGS_VALID_MASK 0x80
  31. #define ISM_LEN_DWORDS 4
  32. #define VCSSB_LEN_BYTES 128
  33. #define VCSSB_LEN_NO_CERTS 4
  34. #define VCB_LEN_NO_CERTS 64
  35. #define VC_NAME_LEN_BYTES 64
  36. #define CERT_STORE_KEY_TYPE_NAME "cert_store_key"
  37. #define CERT_STORE_KEYRING_NAME "cert_store"
  38. static debug_info_t *cert_store_dbf;
  39. static debug_info_t *cert_store_hexdump;
  40. #define pr_dbf_msg(fmt, ...) \
  41. debug_sprintf_event(cert_store_dbf, 3, fmt "\n", ## __VA_ARGS__)
  42. enum diag320_subcode {
  43. DIAG320_SUBCODES = 0,
  44. DIAG320_STORAGE = 1,
  45. DIAG320_CERT_BLOCK = 2,
  46. };
  47. enum diag320_rc {
  48. DIAG320_RC_OK = 0x0001,
  49. DIAG320_RC_CS_NOMATCH = 0x0306,
  50. };
  51. /* Verification Certificates Store Support Block (VCSSB). */
  52. struct vcssb {
  53. u32 vcssb_length;
  54. u8 pad_0x04[3];
  55. u8 version;
  56. u8 pad_0x08[8];
  57. u32 cs_token;
  58. u8 pad_0x14[12];
  59. u16 total_vc_index_count;
  60. u16 max_vc_index_count;
  61. u8 pad_0x24[28];
  62. u32 max_vce_length;
  63. u32 max_vcxe_length;
  64. u8 pad_0x48[8];
  65. u32 max_single_vcb_length;
  66. u32 total_vcb_length;
  67. u32 max_single_vcxb_length;
  68. u32 total_vcxb_length;
  69. u8 pad_0x60[32];
  70. } __packed __aligned(8);
  71. /* Verification Certificate Entry (VCE) Header. */
  72. struct vce_header {
  73. u32 vce_length;
  74. u8 flags;
  75. u8 key_type;
  76. u16 vc_index;
  77. u8 vc_name[VC_NAME_LEN_BYTES]; /* EBCDIC */
  78. u8 vc_format;
  79. u8 pad_0x49;
  80. u16 key_id_length;
  81. u8 pad_0x4c;
  82. u8 vc_hash_type;
  83. u16 vc_hash_length;
  84. u8 pad_0x50[4];
  85. u32 vc_length;
  86. u8 pad_0x58[8];
  87. u16 vc_hash_offset;
  88. u16 vc_offset;
  89. u8 pad_0x64[28];
  90. } __packed __aligned(4);
  91. /* Verification Certificate Block (VCB) Header. */
  92. struct vcb_header {
  93. u32 vcb_input_length;
  94. u8 pad_0x04[4];
  95. u16 first_vc_index;
  96. u16 last_vc_index;
  97. u32 pad_0x0c;
  98. u32 cs_token;
  99. u8 pad_0x14[12];
  100. u32 vcb_output_length;
  101. u8 pad_0x24[3];
  102. u8 version;
  103. u16 stored_vc_count;
  104. u16 remaining_vc_count;
  105. u8 pad_0x2c[20];
  106. } __packed __aligned(4);
  107. /* Verification Certificate Block (VCB). */
  108. struct vcb {
  109. struct vcb_header vcb_hdr;
  110. u8 vcb_buf[];
  111. } __packed __aligned(4);
  112. /* Verification Certificate Entry (VCE). */
  113. struct vce {
  114. struct vce_header vce_hdr;
  115. u8 cert_data_buf[];
  116. } __packed __aligned(4);
  117. static void cert_store_key_describe(const struct key *key, struct seq_file *m)
  118. {
  119. char ascii[VC_NAME_LEN_BYTES + 1];
  120. /*
  121. * First 64 bytes of the key description is key name in EBCDIC CP 500.
  122. * Convert it to ASCII for displaying in /proc/keys.
  123. */
  124. strscpy(ascii, key->description, sizeof(ascii));
  125. EBCASC_500(ascii, VC_NAME_LEN_BYTES);
  126. seq_puts(m, ascii);
  127. seq_puts(m, &key->description[VC_NAME_LEN_BYTES]);
  128. if (key_is_positive(key))
  129. seq_printf(m, ": %u", key->datalen);
  130. }
  131. /*
  132. * Certificate store key type takes over properties of
  133. * user key but cannot be updated.
  134. */
  135. static struct key_type key_type_cert_store_key = {
  136. .name = CERT_STORE_KEY_TYPE_NAME,
  137. .preparse = user_preparse,
  138. .free_preparse = user_free_preparse,
  139. .instantiate = generic_key_instantiate,
  140. .revoke = user_revoke,
  141. .destroy = user_destroy,
  142. .describe = cert_store_key_describe,
  143. .read = user_read,
  144. };
  145. /* Logging functions. */
  146. static void pr_dbf_vcb(const struct vcb *b)
  147. {
  148. pr_dbf_msg("VCB Header:");
  149. pr_dbf_msg("vcb_input_length: %d", b->vcb_hdr.vcb_input_length);
  150. pr_dbf_msg("first_vc_index: %d", b->vcb_hdr.first_vc_index);
  151. pr_dbf_msg("last_vc_index: %d", b->vcb_hdr.last_vc_index);
  152. pr_dbf_msg("cs_token: %d", b->vcb_hdr.cs_token);
  153. pr_dbf_msg("vcb_output_length: %d", b->vcb_hdr.vcb_output_length);
  154. pr_dbf_msg("version: %d", b->vcb_hdr.version);
  155. pr_dbf_msg("stored_vc_count: %d", b->vcb_hdr.stored_vc_count);
  156. pr_dbf_msg("remaining_vc_count: %d", b->vcb_hdr.remaining_vc_count);
  157. }
  158. static void pr_dbf_vce(const struct vce *e)
  159. {
  160. unsigned char vc_name[VC_NAME_LEN_BYTES + 1];
  161. char log_string[VC_NAME_LEN_BYTES + 40];
  162. pr_dbf_msg("VCE Header:");
  163. pr_dbf_msg("vce_hdr.vce_length: %d", e->vce_hdr.vce_length);
  164. pr_dbf_msg("vce_hdr.flags: %d", e->vce_hdr.flags);
  165. pr_dbf_msg("vce_hdr.key_type: %d", e->vce_hdr.key_type);
  166. pr_dbf_msg("vce_hdr.vc_index: %d", e->vce_hdr.vc_index);
  167. pr_dbf_msg("vce_hdr.vc_format: %d", e->vce_hdr.vc_format);
  168. pr_dbf_msg("vce_hdr.key_id_length: %d", e->vce_hdr.key_id_length);
  169. pr_dbf_msg("vce_hdr.vc_hash_type: %d", e->vce_hdr.vc_hash_type);
  170. pr_dbf_msg("vce_hdr.vc_hash_length: %d", e->vce_hdr.vc_hash_length);
  171. pr_dbf_msg("vce_hdr.vc_hash_offset: %d", e->vce_hdr.vc_hash_offset);
  172. pr_dbf_msg("vce_hdr.vc_length: %d", e->vce_hdr.vc_length);
  173. pr_dbf_msg("vce_hdr.vc_offset: %d", e->vce_hdr.vc_offset);
  174. /* Certificate name in ASCII. */
  175. memcpy(vc_name, e->vce_hdr.vc_name, VC_NAME_LEN_BYTES);
  176. EBCASC_500(vc_name, VC_NAME_LEN_BYTES);
  177. vc_name[VC_NAME_LEN_BYTES] = '\0';
  178. snprintf(log_string, sizeof(log_string),
  179. "index: %d vce_hdr.vc_name (ASCII): %s",
  180. e->vce_hdr.vc_index, vc_name);
  181. debug_text_event(cert_store_hexdump, 3, log_string);
  182. /* Certificate data. */
  183. debug_text_event(cert_store_hexdump, 3, "VCE: Certificate data start");
  184. debug_event(cert_store_hexdump, 3, (u8 *)e->cert_data_buf, 128);
  185. debug_text_event(cert_store_hexdump, 3, "VCE: Certificate data end");
  186. debug_event(cert_store_hexdump, 3,
  187. (u8 *)e->cert_data_buf + e->vce_hdr.vce_length - 128, 128);
  188. }
  189. static void pr_dbf_vcssb(const struct vcssb *s)
  190. {
  191. debug_text_event(cert_store_hexdump, 3, "DIAG320 Subcode1");
  192. debug_event(cert_store_hexdump, 3, (u8 *)s, VCSSB_LEN_BYTES);
  193. pr_dbf_msg("VCSSB:");
  194. pr_dbf_msg("vcssb_length: %u", s->vcssb_length);
  195. pr_dbf_msg("version: %u", s->version);
  196. pr_dbf_msg("cs_token: %u", s->cs_token);
  197. pr_dbf_msg("total_vc_index_count: %u", s->total_vc_index_count);
  198. pr_dbf_msg("max_vc_index_count: %u", s->max_vc_index_count);
  199. pr_dbf_msg("max_vce_length: %u", s->max_vce_length);
  200. pr_dbf_msg("max_vcxe_length: %u", s->max_vce_length);
  201. pr_dbf_msg("max_single_vcb_length: %u", s->max_single_vcb_length);
  202. pr_dbf_msg("total_vcb_length: %u", s->total_vcb_length);
  203. pr_dbf_msg("max_single_vcxb_length: %u", s->max_single_vcxb_length);
  204. pr_dbf_msg("total_vcxb_length: %u", s->total_vcxb_length);
  205. }
  206. static int __diag320(unsigned long subcode, void *addr)
  207. {
  208. union register_pair rp = { .even = (unsigned long)addr, };
  209. asm volatile(
  210. " diag %[rp],%[subcode],0x320\n"
  211. "0: nopr %%r7\n"
  212. EX_TABLE(0b, 0b)
  213. : [rp] "+d" (rp.pair)
  214. : [subcode] "d" (subcode)
  215. : "cc", "memory");
  216. return rp.odd;
  217. }
  218. static int diag320(unsigned long subcode, void *addr)
  219. {
  220. diag_stat_inc(DIAG_STAT_X320);
  221. return __diag320(subcode, addr);
  222. }
  223. /*
  224. * Calculate SHA256 hash of the VCE certificate and compare it to hash stored in
  225. * VCE. Return -EINVAL if hashes don't match.
  226. */
  227. static int check_certificate_hash(const struct vce *vce)
  228. {
  229. u8 hash[SHA256_DIGEST_SIZE];
  230. u16 vc_hash_length;
  231. u8 *vce_hash;
  232. vce_hash = (u8 *)vce + vce->vce_hdr.vc_hash_offset;
  233. vc_hash_length = vce->vce_hdr.vc_hash_length;
  234. sha256((u8 *)vce + vce->vce_hdr.vc_offset, vce->vce_hdr.vc_length, hash);
  235. if (memcmp(vce_hash, hash, vc_hash_length) == 0)
  236. return 0;
  237. pr_dbf_msg("SHA256 hash of received certificate does not match");
  238. debug_text_event(cert_store_hexdump, 3, "VCE hash:");
  239. debug_event(cert_store_hexdump, 3, vce_hash, SHA256_DIGEST_SIZE);
  240. debug_text_event(cert_store_hexdump, 3, "Calculated hash:");
  241. debug_event(cert_store_hexdump, 3, hash, SHA256_DIGEST_SIZE);
  242. return -EINVAL;
  243. }
  244. static int check_certificate_valid(const struct vce *vce)
  245. {
  246. if (!(vce->vce_hdr.flags & VCE_FLAGS_VALID_MASK)) {
  247. pr_dbf_msg("Certificate entry is invalid");
  248. return -EINVAL;
  249. }
  250. if (vce->vce_hdr.vc_format != 1) {
  251. pr_dbf_msg("Certificate format is not supported");
  252. return -EINVAL;
  253. }
  254. if (vce->vce_hdr.vc_hash_type != 1) {
  255. pr_dbf_msg("Hash type is not supported");
  256. return -EINVAL;
  257. }
  258. return check_certificate_hash(vce);
  259. }
  260. static struct key *get_user_session_keyring(void)
  261. {
  262. key_ref_t us_keyring_ref;
  263. us_keyring_ref = lookup_user_key(KEY_SPEC_USER_SESSION_KEYRING,
  264. KEY_LOOKUP_CREATE, KEY_NEED_LINK);
  265. if (IS_ERR(us_keyring_ref)) {
  266. pr_dbf_msg("Couldn't get user session keyring: %ld",
  267. PTR_ERR(us_keyring_ref));
  268. return ERR_PTR(-ENOKEY);
  269. }
  270. key_ref_put(us_keyring_ref);
  271. return key_ref_to_ptr(us_keyring_ref);
  272. }
  273. /* Invalidate all keys from cert_store keyring. */
  274. static int invalidate_keyring_keys(struct key *keyring)
  275. {
  276. unsigned long num_keys, key_index;
  277. size_t keyring_payload_len;
  278. key_serial_t *key_array;
  279. struct key *current_key;
  280. int rc;
  281. keyring_payload_len = key_type_keyring.read(keyring, NULL, 0);
  282. num_keys = keyring_payload_len / sizeof(key_serial_t);
  283. key_array = kcalloc(num_keys, sizeof(key_serial_t), GFP_KERNEL);
  284. if (!key_array)
  285. return -ENOMEM;
  286. rc = key_type_keyring.read(keyring, (char *)key_array, keyring_payload_len);
  287. if (rc != keyring_payload_len) {
  288. pr_dbf_msg("Couldn't read keyring payload");
  289. goto out;
  290. }
  291. for (key_index = 0; key_index < num_keys; key_index++) {
  292. current_key = key_lookup(key_array[key_index]);
  293. pr_dbf_msg("Invalidating key %08x", current_key->serial);
  294. key_invalidate(current_key);
  295. key_put(current_key);
  296. rc = key_unlink(keyring, current_key);
  297. if (rc) {
  298. pr_dbf_msg("Couldn't unlink key %08x: %d", current_key->serial, rc);
  299. break;
  300. }
  301. }
  302. out:
  303. kfree(key_array);
  304. return rc;
  305. }
  306. static struct key *find_cs_keyring(void)
  307. {
  308. key_ref_t cs_keyring_ref;
  309. struct key *cs_keyring;
  310. cs_keyring_ref = keyring_search(make_key_ref(get_user_session_keyring(), true),
  311. &key_type_keyring, CERT_STORE_KEYRING_NAME,
  312. false);
  313. if (!IS_ERR(cs_keyring_ref)) {
  314. cs_keyring = key_ref_to_ptr(cs_keyring_ref);
  315. key_ref_put(cs_keyring_ref);
  316. goto found;
  317. }
  318. /* Search default locations: thread, process, session keyrings */
  319. cs_keyring = request_key(&key_type_keyring, CERT_STORE_KEYRING_NAME, NULL);
  320. if (IS_ERR(cs_keyring))
  321. return NULL;
  322. key_put(cs_keyring);
  323. found:
  324. return cs_keyring;
  325. }
  326. static void cleanup_cs_keys(void)
  327. {
  328. struct key *cs_keyring;
  329. cs_keyring = find_cs_keyring();
  330. if (!cs_keyring)
  331. return;
  332. pr_dbf_msg("Found cert_store keyring. Purging...");
  333. /*
  334. * Remove cert_store_key_type in case invalidation
  335. * of old cert_store keys failed (= severe error).
  336. */
  337. if (invalidate_keyring_keys(cs_keyring))
  338. unregister_key_type(&key_type_cert_store_key);
  339. keyring_clear(cs_keyring);
  340. key_invalidate(cs_keyring);
  341. key_put(cs_keyring);
  342. key_unlink(get_user_session_keyring(), cs_keyring);
  343. }
  344. static struct key *create_cs_keyring(void)
  345. {
  346. static struct key *cs_keyring;
  347. /* Cleanup previous cs_keyring and all associated keys if any. */
  348. cleanup_cs_keys();
  349. cs_keyring = keyring_alloc(CERT_STORE_KEYRING_NAME, GLOBAL_ROOT_UID,
  350. GLOBAL_ROOT_GID, current_cred(),
  351. (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ,
  352. KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_SET_KEEP,
  353. NULL, get_user_session_keyring());
  354. if (IS_ERR(cs_keyring)) {
  355. pr_dbf_msg("Can't allocate cert_store keyring");
  356. return NULL;
  357. }
  358. pr_dbf_msg("Successfully allocated cert_store keyring: %08x", cs_keyring->serial);
  359. /*
  360. * In case a previous clean-up ran into an
  361. * error and unregistered key type.
  362. */
  363. register_key_type(&key_type_cert_store_key);
  364. return cs_keyring;
  365. }
  366. /*
  367. * Allocate memory and create key description in format
  368. * [key name in EBCDIC]:[VCE index]:[CS token].
  369. * Return a pointer to key description or NULL if memory
  370. * allocation failed. Memory should be freed by caller.
  371. */
  372. static char *get_key_description(struct vcssb *vcssb, const struct vce *vce)
  373. {
  374. size_t len, name_len;
  375. u32 cs_token;
  376. char *desc;
  377. cs_token = vcssb->cs_token;
  378. /* Description string contains "%64s:%05u:%010u\0". */
  379. name_len = sizeof(vce->vce_hdr.vc_name);
  380. len = name_len + 1 + 5 + 1 + 10 + 1;
  381. desc = kmalloc(len, GFP_KERNEL);
  382. if (!desc)
  383. return NULL;
  384. memcpy(desc, vce->vce_hdr.vc_name, name_len);
  385. snprintf(desc + name_len, len - name_len, ":%05u:%010u",
  386. vce->vce_hdr.vc_index, cs_token);
  387. return desc;
  388. }
  389. /*
  390. * Create a key of type "cert_store_key" using the data from VCE for key
  391. * payload and key description. Link the key to "cert_store" keyring.
  392. */
  393. static int create_key_from_vce(struct vcssb *vcssb, struct vce *vce,
  394. struct key *keyring)
  395. {
  396. key_ref_t newkey;
  397. char *desc;
  398. int rc;
  399. desc = get_key_description(vcssb, vce);
  400. if (!desc)
  401. return -ENOMEM;
  402. newkey = key_create_or_update(
  403. make_key_ref(keyring, true), CERT_STORE_KEY_TYPE_NAME,
  404. desc, (u8 *)vce + vce->vce_hdr.vc_offset,
  405. vce->vce_hdr.vc_length,
  406. (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ,
  407. KEY_ALLOC_NOT_IN_QUOTA);
  408. rc = PTR_ERR_OR_ZERO(newkey);
  409. if (rc) {
  410. pr_dbf_msg("Couldn't create a key from Certificate Entry (%d)", rc);
  411. rc = -ENOKEY;
  412. goto out;
  413. }
  414. key_ref_put(newkey);
  415. out:
  416. kfree(desc);
  417. return rc;
  418. }
  419. /* Get Verification Certificate Storage Size block with DIAG320 subcode2. */
  420. static int get_vcssb(struct vcssb *vcssb)
  421. {
  422. int diag320_rc;
  423. memset(vcssb, 0, sizeof(*vcssb));
  424. vcssb->vcssb_length = VCSSB_LEN_BYTES;
  425. diag320_rc = diag320(DIAG320_STORAGE, vcssb);
  426. pr_dbf_vcssb(vcssb);
  427. if (diag320_rc != DIAG320_RC_OK) {
  428. pr_dbf_msg("Diag 320 Subcode 1 returned bad RC: %04x", diag320_rc);
  429. return -EIO;
  430. }
  431. if (vcssb->vcssb_length == VCSSB_LEN_NO_CERTS) {
  432. pr_dbf_msg("No certificates available for current configuration");
  433. return -ENOKEY;
  434. }
  435. return 0;
  436. }
  437. static u32 get_4k_mult_vcb_size(struct vcssb *vcssb)
  438. {
  439. return round_up(vcssb->max_single_vcb_length, PAGE_SIZE);
  440. }
  441. /* Fill input fields of single-entry VCB that will be read by LPAR. */
  442. static void fill_vcb_input(struct vcssb *vcssb, struct vcb *vcb, u16 index)
  443. {
  444. memset(vcb, 0, sizeof(*vcb));
  445. vcb->vcb_hdr.vcb_input_length = get_4k_mult_vcb_size(vcssb);
  446. vcb->vcb_hdr.cs_token = vcssb->cs_token;
  447. /* Request single entry. */
  448. vcb->vcb_hdr.first_vc_index = index;
  449. vcb->vcb_hdr.last_vc_index = index;
  450. }
  451. static void extract_vce_from_sevcb(struct vcb *vcb, struct vce *vce)
  452. {
  453. struct vce *extracted_vce;
  454. extracted_vce = (struct vce *)vcb->vcb_buf;
  455. memcpy(vce, vcb->vcb_buf, extracted_vce->vce_hdr.vce_length);
  456. pr_dbf_vce(vce);
  457. }
  458. static int get_sevcb(struct vcssb *vcssb, u16 index, struct vcb *vcb)
  459. {
  460. int rc, diag320_rc;
  461. fill_vcb_input(vcssb, vcb, index);
  462. diag320_rc = diag320(DIAG320_CERT_BLOCK, vcb);
  463. pr_dbf_msg("Diag 320 Subcode2 RC %2x", diag320_rc);
  464. pr_dbf_vcb(vcb);
  465. switch (diag320_rc) {
  466. case DIAG320_RC_OK:
  467. rc = 0;
  468. if (vcb->vcb_hdr.vcb_output_length == VCB_LEN_NO_CERTS) {
  469. pr_dbf_msg("No certificate entry for index %u", index);
  470. rc = -ENOKEY;
  471. } else if (vcb->vcb_hdr.remaining_vc_count != 0) {
  472. /* Retry on insufficient space. */
  473. pr_dbf_msg("Couldn't get all requested certificates");
  474. rc = -EAGAIN;
  475. }
  476. break;
  477. case DIAG320_RC_CS_NOMATCH:
  478. pr_dbf_msg("Certificate Store token mismatch");
  479. rc = -EAGAIN;
  480. break;
  481. default:
  482. pr_dbf_msg("Diag 320 Subcode2 returned bad rc (0x%4x)", diag320_rc);
  483. rc = -EINVAL;
  484. break;
  485. }
  486. return rc;
  487. }
  488. /*
  489. * Allocate memory for single-entry VCB, get VCB via DIAG320 subcode 2 call,
  490. * extract VCE and create a key from its' certificate.
  491. */
  492. static int create_key_from_sevcb(struct vcssb *vcssb, u16 index,
  493. struct key *keyring)
  494. {
  495. struct vcb *vcb;
  496. struct vce *vce;
  497. int rc;
  498. rc = -ENOMEM;
  499. vcb = vmalloc(get_4k_mult_vcb_size(vcssb));
  500. vce = vmalloc(vcssb->max_single_vcb_length - sizeof(vcb->vcb_hdr));
  501. if (!vcb || !vce)
  502. goto out;
  503. rc = get_sevcb(vcssb, index, vcb);
  504. if (rc)
  505. goto out;
  506. extract_vce_from_sevcb(vcb, vce);
  507. rc = check_certificate_valid(vce);
  508. if (rc)
  509. goto out;
  510. rc = create_key_from_vce(vcssb, vce, keyring);
  511. if (rc)
  512. goto out;
  513. pr_dbf_msg("Successfully created key from Certificate Entry %d", index);
  514. out:
  515. vfree(vce);
  516. vfree(vcb);
  517. return rc;
  518. }
  519. /*
  520. * Request a single-entry VCB for each VCE available for the partition.
  521. * Create a key from it and link it to cert_store keyring. If no keys
  522. * could be created (i.e. VCEs were invalid) return -ENOKEY.
  523. */
  524. static int add_certificates_to_keyring(struct vcssb *vcssb, struct key *keyring)
  525. {
  526. int rc, index, count, added;
  527. count = 0;
  528. added = 0;
  529. /* Certificate Store entries indices start with 1 and have no gaps. */
  530. for (index = 1; index < vcssb->total_vc_index_count + 1; index++) {
  531. pr_dbf_msg("Creating key from VCE %u", index);
  532. rc = create_key_from_sevcb(vcssb, index, keyring);
  533. count++;
  534. if (rc == -EAGAIN)
  535. return rc;
  536. if (rc)
  537. pr_dbf_msg("Creating key from VCE %u failed (%d)", index, rc);
  538. else
  539. added++;
  540. }
  541. if (added == 0) {
  542. pr_dbf_msg("Processed %d entries. No keys created", count);
  543. return -ENOKEY;
  544. }
  545. pr_info("Added %d of %d keys to cert_store keyring", added, count);
  546. /*
  547. * Do not allow to link more keys to certificate store keyring after all
  548. * the VCEs were processed.
  549. */
  550. rc = keyring_restrict(make_key_ref(keyring, true), NULL, NULL);
  551. if (rc)
  552. pr_dbf_msg("Failed to set restriction to cert_store keyring (%d)", rc);
  553. return 0;
  554. }
  555. /*
  556. * Check which DIAG320 subcodes are installed.
  557. * Return -ENOENT if subcodes 1 or 2 are not available.
  558. */
  559. static int query_diag320_subcodes(void)
  560. {
  561. unsigned long ism[ISM_LEN_DWORDS];
  562. int rc;
  563. rc = diag320(0, ism);
  564. if (rc != DIAG320_RC_OK) {
  565. pr_dbf_msg("DIAG320 subcode query returned %04x", rc);
  566. return -ENOENT;
  567. }
  568. debug_text_event(cert_store_hexdump, 3, "DIAG320 Subcode 0");
  569. debug_event(cert_store_hexdump, 3, ism, sizeof(ism));
  570. if (!test_bit_inv(1, ism) || !test_bit_inv(2, ism)) {
  571. pr_dbf_msg("Not all required DIAG320 subcodes are installed");
  572. return -ENOENT;
  573. }
  574. return 0;
  575. }
  576. /*
  577. * Check if Certificate Store is supported by the firmware and DIAG320 subcodes
  578. * 1 and 2 are installed. Create cert_store keyring and link all certificates
  579. * available for the current partition to it as "cert_store_key" type
  580. * keys. On refresh or error invalidate cert_store keyring and destroy
  581. * all keys of "cert_store_key" type.
  582. */
  583. static int fill_cs_keyring(void)
  584. {
  585. struct key *cs_keyring;
  586. struct vcssb *vcssb;
  587. int rc;
  588. rc = -ENOMEM;
  589. vcssb = kmalloc(VCSSB_LEN_BYTES, GFP_KERNEL);
  590. if (!vcssb)
  591. goto cleanup_keys;
  592. rc = -ENOENT;
  593. if (!sclp.has_diag320) {
  594. pr_dbf_msg("Certificate Store is not supported");
  595. goto cleanup_keys;
  596. }
  597. rc = query_diag320_subcodes();
  598. if (rc)
  599. goto cleanup_keys;
  600. rc = get_vcssb(vcssb);
  601. if (rc)
  602. goto cleanup_keys;
  603. rc = -ENOMEM;
  604. cs_keyring = create_cs_keyring();
  605. if (!cs_keyring)
  606. goto cleanup_keys;
  607. rc = add_certificates_to_keyring(vcssb, cs_keyring);
  608. if (rc)
  609. goto cleanup_cs_keyring;
  610. goto out;
  611. cleanup_cs_keyring:
  612. key_put(cs_keyring);
  613. cleanup_keys:
  614. cleanup_cs_keys();
  615. out:
  616. kfree(vcssb);
  617. return rc;
  618. }
  619. static DEFINE_MUTEX(cs_refresh_lock);
  620. static int cs_status_val = -1;
  621. static ssize_t cs_status_show(struct kobject *kobj,
  622. struct kobj_attribute *attr, char *buf)
  623. {
  624. if (cs_status_val == -1)
  625. return sysfs_emit(buf, "uninitialized\n");
  626. else if (cs_status_val == 0)
  627. return sysfs_emit(buf, "ok\n");
  628. return sysfs_emit(buf, "failed (%d)\n", cs_status_val);
  629. }
  630. static struct kobj_attribute cs_status_attr = __ATTR_RO(cs_status);
  631. static ssize_t refresh_store(struct kobject *kobj, struct kobj_attribute *attr,
  632. const char *buf, size_t count)
  633. {
  634. int rc, retries;
  635. pr_dbf_msg("Refresh certificate store information requested");
  636. rc = mutex_lock_interruptible(&cs_refresh_lock);
  637. if (rc)
  638. return rc;
  639. for (retries = 0; retries < DIAG_MAX_RETRIES; retries++) {
  640. /* Request certificates from certificate store. */
  641. rc = fill_cs_keyring();
  642. if (rc)
  643. pr_dbf_msg("Failed to refresh certificate store information (%d)", rc);
  644. if (rc != -EAGAIN)
  645. break;
  646. }
  647. cs_status_val = rc;
  648. mutex_unlock(&cs_refresh_lock);
  649. return rc ?: count;
  650. }
  651. static struct kobj_attribute refresh_attr = __ATTR_WO(refresh);
  652. static const struct attribute *cert_store_attrs[] __initconst = {
  653. &cs_status_attr.attr,
  654. &refresh_attr.attr,
  655. NULL,
  656. };
  657. static struct kobject *cert_store_kobj;
  658. static int __init cert_store_init(void)
  659. {
  660. int rc = -ENOMEM;
  661. cert_store_dbf = debug_register("cert_store_msg", 10, 1, 64);
  662. if (!cert_store_dbf)
  663. goto cleanup_dbf;
  664. cert_store_hexdump = debug_register("cert_store_hexdump", 3, 1, 128);
  665. if (!cert_store_hexdump)
  666. goto cleanup_dbf;
  667. debug_register_view(cert_store_hexdump, &debug_hex_ascii_view);
  668. debug_register_view(cert_store_dbf, &debug_sprintf_view);
  669. /* Create directory /sys/firmware/cert_store. */
  670. cert_store_kobj = kobject_create_and_add("cert_store", firmware_kobj);
  671. if (!cert_store_kobj)
  672. goto cleanup_dbf;
  673. rc = sysfs_create_files(cert_store_kobj, cert_store_attrs);
  674. if (rc)
  675. goto cleanup_kobj;
  676. register_key_type(&key_type_cert_store_key);
  677. return rc;
  678. cleanup_kobj:
  679. kobject_put(cert_store_kobj);
  680. cleanup_dbf:
  681. debug_unregister(cert_store_dbf);
  682. debug_unregister(cert_store_hexdump);
  683. return rc;
  684. }
  685. device_initcall(cert_store_init);