aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/tpm/tpm-interface.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/char/tpm/tpm-interface.c')
-rw-r--r--drivers/char/tpm/tpm-interface.c231
1 files changed, 98 insertions, 133 deletions
diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 1d6729be4cd6..76df4fbcf089 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -30,9 +30,9 @@
30#include <linux/spinlock.h> 30#include <linux/spinlock.h>
31#include <linux/freezer.h> 31#include <linux/freezer.h>
32#include <linux/pm_runtime.h> 32#include <linux/pm_runtime.h>
33#include <linux/tpm_eventlog.h>
33 34
34#include "tpm.h" 35#include "tpm.h"
35#include "tpm_eventlog.h"
36 36
37#define TPM_MAX_ORDINAL 243 37#define TPM_MAX_ORDINAL 243
38#define TSC_MAX_ORDINAL 12 38#define TSC_MAX_ORDINAL 12
@@ -328,7 +328,7 @@ unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
328} 328}
329EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); 329EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
330 330
331static bool tpm_validate_command(struct tpm_chip *chip, 331static int tpm_validate_command(struct tpm_chip *chip,
332 struct tpm_space *space, 332 struct tpm_space *space,
333 const u8 *cmd, 333 const u8 *cmd,
334 size_t len) 334 size_t len)
@@ -340,10 +340,10 @@ static bool tpm_validate_command(struct tpm_chip *chip,
340 unsigned int nr_handles; 340 unsigned int nr_handles;
341 341
342 if (len < TPM_HEADER_SIZE) 342 if (len < TPM_HEADER_SIZE)
343 return false; 343 return -EINVAL;
344 344
345 if (!space) 345 if (!space)
346 return true; 346 return 0;
347 347
348 if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) { 348 if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
349 cc = be32_to_cpu(header->ordinal); 349 cc = be32_to_cpu(header->ordinal);
@@ -352,7 +352,7 @@ static bool tpm_validate_command(struct tpm_chip *chip,
352 if (i < 0) { 352 if (i < 0) {
353 dev_dbg(&chip->dev, "0x%04X is an invalid command\n", 353 dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
354 cc); 354 cc);
355 return false; 355 return -EOPNOTSUPP;
356 } 356 }
357 357
358 attrs = chip->cc_attrs_tbl[i]; 358 attrs = chip->cc_attrs_tbl[i];
@@ -362,11 +362,11 @@ static bool tpm_validate_command(struct tpm_chip *chip,
362 goto err_len; 362 goto err_len;
363 } 363 }
364 364
365 return true; 365 return 0;
366err_len: 366err_len:
367 dev_dbg(&chip->dev, 367 dev_dbg(&chip->dev,
368 "%s: insufficient command length %zu", __func__, len); 368 "%s: insufficient command length %zu", __func__, len);
369 return false; 369 return -EINVAL;
370} 370}
371 371
372/** 372/**
@@ -391,8 +391,20 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
391 unsigned long stop; 391 unsigned long stop;
392 bool need_locality; 392 bool need_locality;
393 393
394 if (!tpm_validate_command(chip, space, buf, bufsiz)) 394 rc = tpm_validate_command(chip, space, buf, bufsiz);
395 return -EINVAL; 395 if (rc == -EINVAL)
396 return rc;
397 /*
398 * If the command is not implemented by the TPM, synthesize a
399 * response with a TPM2_RC_COMMAND_CODE return for user-space.
400 */
401 if (rc == -EOPNOTSUPP) {
402 header->length = cpu_to_be32(sizeof(*header));
403 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
404 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
405 TSS2_RESMGR_TPM_RC_LAYER);
406 return bufsiz;
407 }
396 408
397 if (bufsiz > TPM_BUFSIZE) 409 if (bufsiz > TPM_BUFSIZE)
398 bufsiz = TPM_BUFSIZE; 410 bufsiz = TPM_BUFSIZE;
@@ -413,6 +425,9 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
413 if (chip->dev.parent) 425 if (chip->dev.parent)
414 pm_runtime_get_sync(chip->dev.parent); 426 pm_runtime_get_sync(chip->dev.parent);
415 427
428 if (chip->ops->clk_enable != NULL)
429 chip->ops->clk_enable(chip, true);
430
416 /* Store the decision as chip->locality will be changed. */ 431 /* Store the decision as chip->locality will be changed. */
417 need_locality = chip->locality == -1; 432 need_locality = chip->locality == -1;
418 433
@@ -489,6 +504,9 @@ out:
489 chip->locality = -1; 504 chip->locality = -1;
490 } 505 }
491out_no_locality: 506out_no_locality:
507 if (chip->ops->clk_enable != NULL)
508 chip->ops->clk_enable(chip, false);
509
492 if (chip->dev.parent) 510 if (chip->dev.parent)
493 pm_runtime_put_sync(chip->dev.parent); 511 pm_runtime_put_sync(chip->dev.parent);
494 512
@@ -809,19 +827,20 @@ int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
809} 827}
810 828
811/** 829/**
812 * tpm_is_tpm2 - is the chip a TPM2 chip? 830 * tpm_is_tpm2 - do we a have a TPM2 chip?
813 * @chip_num: tpm idx # or ANY 831 * @chip: a &struct tpm_chip instance, %NULL for the default chip
814 * 832 *
815 * Returns < 0 on error, and 1 or 0 on success depending whether the chip 833 * Return:
816 * is a TPM2 chip. 834 * 1 if we have a TPM2 chip.
835 * 0 if we don't have a TPM2 chip.
836 * A negative number for system errors (errno).
817 */ 837 */
818int tpm_is_tpm2(u32 chip_num) 838int tpm_is_tpm2(struct tpm_chip *chip)
819{ 839{
820 struct tpm_chip *chip;
821 int rc; 840 int rc;
822 841
823 chip = tpm_chip_find_get(chip_num); 842 chip = tpm_chip_find_get(chip);
824 if (chip == NULL) 843 if (!chip)
825 return -ENODEV; 844 return -ENODEV;
826 845
827 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 846 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
@@ -833,23 +852,19 @@ int tpm_is_tpm2(u32 chip_num)
833EXPORT_SYMBOL_GPL(tpm_is_tpm2); 852EXPORT_SYMBOL_GPL(tpm_is_tpm2);
834 853
835/** 854/**
836 * tpm_pcr_read - read a pcr value 855 * tpm_pcr_read - read a PCR value from SHA1 bank
837 * @chip_num: tpm idx # or ANY 856 * @chip: a &struct tpm_chip instance, %NULL for the default chip
838 * @pcr_idx: pcr idx to retrieve 857 * @pcr_idx: the PCR to be retrieved
839 * @res_buf: TPM_PCR value 858 * @res_buf: the value of the PCR
840 * size of res_buf is 20 bytes (or NULL if you don't care)
841 * 859 *
842 * The TPM driver should be built-in, but for whatever reason it 860 * Return: same as with tpm_transmit_cmd()
843 * isn't, protect against the chip disappearing, by incrementing
844 * the module usage count.
845 */ 861 */
846int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) 862int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
847{ 863{
848 struct tpm_chip *chip;
849 int rc; 864 int rc;
850 865
851 chip = tpm_chip_find_get(chip_num); 866 chip = tpm_chip_find_get(chip);
852 if (chip == NULL) 867 if (!chip)
853 return -ENODEV; 868 return -ENODEV;
854 if (chip->flags & TPM_CHIP_FLAG_TPM2) 869 if (chip->flags & TPM_CHIP_FLAG_TPM2)
855 rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 870 rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
@@ -889,25 +904,26 @@ static int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
889} 904}
890 905
891/** 906/**
892 * tpm_pcr_extend - extend pcr value with hash 907 * tpm_pcr_extend - extend a PCR value in SHA1 bank.
893 * @chip_num: tpm idx # or AN& 908 * @chip: a &struct tpm_chip instance, %NULL for the default chip
894 * @pcr_idx: pcr idx to extend 909 * @pcr_idx: the PCR to be retrieved
895 * @hash: hash value used to extend pcr value 910 * @hash: the hash value used to extend the PCR value
896 * 911 *
897 * The TPM driver should be built-in, but for whatever reason it 912 * Note: with TPM 2.0 extends also those banks with a known digest size to the
898 * isn't, protect against the chip disappearing, by incrementing 913 * cryto subsystem in order to prevent malicious use of those PCR banks. In the
899 * the module usage count. 914 * future we should dynamically determine digest sizes.
915 *
916 * Return: same as with tpm_transmit_cmd()
900 */ 917 */
901int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) 918int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
902{ 919{
903 int rc; 920 int rc;
904 struct tpm_chip *chip;
905 struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)]; 921 struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
906 u32 count = 0; 922 u32 count = 0;
907 int i; 923 int i;
908 924
909 chip = tpm_chip_find_get(chip_num); 925 chip = tpm_chip_find_get(chip);
910 if (chip == NULL) 926 if (!chip)
911 return -ENODEV; 927 return -ENODEV;
912 928
913 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 929 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
@@ -1019,82 +1035,29 @@ out:
1019 return rc; 1035 return rc;
1020} 1036}
1021 1037
1022int tpm_send(u32 chip_num, void *cmd, size_t buflen) 1038/**
1039 * tpm_send - send a TPM command
1040 * @chip: a &struct tpm_chip instance, %NULL for the default chip
1041 * @cmd: a TPM command buffer
1042 * @buflen: the length of the TPM command buffer
1043 *
1044 * Return: same as with tpm_transmit_cmd()
1045 */
1046int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
1023{ 1047{
1024 struct tpm_chip *chip;
1025 int rc; 1048 int rc;
1026 1049
1027 chip = tpm_chip_find_get(chip_num); 1050 chip = tpm_chip_find_get(chip);
1028 if (chip == NULL) 1051 if (!chip)
1029 return -ENODEV; 1052 return -ENODEV;
1030 1053
1031 rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0, 1054 rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0,
1032 "attempting tpm_cmd"); 1055 "attempting to a send a command");
1033 tpm_put_ops(chip); 1056 tpm_put_ops(chip);
1034 return rc; 1057 return rc;
1035} 1058}
1036EXPORT_SYMBOL_GPL(tpm_send); 1059EXPORT_SYMBOL_GPL(tpm_send);
1037 1060
1038static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
1039 bool check_cancel, bool *canceled)
1040{
1041 u8 status = chip->ops->status(chip);
1042
1043 *canceled = false;
1044 if ((status & mask) == mask)
1045 return true;
1046 if (check_cancel && chip->ops->req_canceled(chip, status)) {
1047 *canceled = true;
1048 return true;
1049 }
1050 return false;
1051}
1052
1053int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1054 wait_queue_head_t *queue, bool check_cancel)
1055{
1056 unsigned long stop;
1057 long rc;
1058 u8 status;
1059 bool canceled = false;
1060
1061 /* check current status */
1062 status = chip->ops->status(chip);
1063 if ((status & mask) == mask)
1064 return 0;
1065
1066 stop = jiffies + timeout;
1067
1068 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
1069again:
1070 timeout = stop - jiffies;
1071 if ((long)timeout <= 0)
1072 return -ETIME;
1073 rc = wait_event_interruptible_timeout(*queue,
1074 wait_for_tpm_stat_cond(chip, mask, check_cancel,
1075 &canceled),
1076 timeout);
1077 if (rc > 0) {
1078 if (canceled)
1079 return -ECANCELED;
1080 return 0;
1081 }
1082 if (rc == -ERESTARTSYS && freezing(current)) {
1083 clear_thread_flag(TIF_SIGPENDING);
1084 goto again;
1085 }
1086 } else {
1087 do {
1088 tpm_msleep(TPM_TIMEOUT);
1089 status = chip->ops->status(chip);
1090 if ((status & mask) == mask)
1091 return 0;
1092 } while (time_before(jiffies, stop));
1093 }
1094 return -ETIME;
1095}
1096EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
1097
1098#define TPM_ORD_SAVESTATE 152 1061#define TPM_ORD_SAVESTATE 152
1099#define SAVESTATE_RESULT_SIZE 10 1062#define SAVESTATE_RESULT_SIZE 10
1100 1063
@@ -1187,16 +1150,15 @@ static const struct tpm_input_header tpm_getrandom_header = {
1187}; 1150};
1188 1151
1189/** 1152/**
1190 * tpm_get_random() - Get random bytes from the tpm's RNG 1153 * tpm_get_random() - get random bytes from the TPM's RNG
1191 * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1154 * @chip: a &struct tpm_chip instance, %NULL for the default chip
1192 * @out: destination buffer for the random bytes 1155 * @out: destination buffer for the random bytes
1193 * @max: the max number of bytes to write to @out 1156 * @max: the max number of bytes to write to @out
1194 * 1157 *
1195 * Returns < 0 on error and the number of bytes read on success 1158 * Return: same as with tpm_transmit_cmd()
1196 */ 1159 */
1197int tpm_get_random(u32 chip_num, u8 *out, size_t max) 1160int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
1198{ 1161{
1199 struct tpm_chip *chip;
1200 struct tpm_cmd_t tpm_cmd; 1162 struct tpm_cmd_t tpm_cmd;
1201 u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength; 1163 u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
1202 int err, total = 0, retries = 5; 1164 int err, total = 0, retries = 5;
@@ -1205,8 +1167,8 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
1205 if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) 1167 if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
1206 return -EINVAL; 1168 return -EINVAL;
1207 1169
1208 chip = tpm_chip_find_get(chip_num); 1170 chip = tpm_chip_find_get(chip);
1209 if (chip == NULL) 1171 if (!chip)
1210 return -ENODEV; 1172 return -ENODEV;
1211 1173
1212 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 1174 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
@@ -1248,22 +1210,23 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
1248EXPORT_SYMBOL_GPL(tpm_get_random); 1210EXPORT_SYMBOL_GPL(tpm_get_random);
1249 1211
1250/** 1212/**
1251 * tpm_seal_trusted() - seal a trusted key 1213 * tpm_seal_trusted() - seal a trusted key payload
1252 * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1214 * @chip: a &struct tpm_chip instance, %NULL for the default chip
1253 * @options: authentication values and other options 1215 * @options: authentication values and other options
1254 * @payload: the key data in clear and encrypted form 1216 * @payload: the key data in clear and encrypted form
1255 * 1217 *
1256 * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1218 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
1257 * are supported. 1219 * the keyring subsystem.
1220 *
1221 * Return: same as with tpm_transmit_cmd()
1258 */ 1222 */
1259int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1223int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
1260 struct trusted_key_options *options) 1224 struct trusted_key_options *options)
1261{ 1225{
1262 struct tpm_chip *chip;
1263 int rc; 1226 int rc;
1264 1227
1265 chip = tpm_chip_find_get(chip_num); 1228 chip = tpm_chip_find_get(chip);
1266 if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1229 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
1267 return -ENODEV; 1230 return -ENODEV;
1268 1231
1269 rc = tpm2_seal_trusted(chip, payload, options); 1232 rc = tpm2_seal_trusted(chip, payload, options);
@@ -1275,21 +1238,23 @@ EXPORT_SYMBOL_GPL(tpm_seal_trusted);
1275 1238
1276/** 1239/**
1277 * tpm_unseal_trusted() - unseal a trusted key 1240 * tpm_unseal_trusted() - unseal a trusted key
1278 * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1241 * @chip: a &struct tpm_chip instance, %NULL for the default chip
1279 * @options: authentication values and other options 1242 * @options: authentication values and other options
1280 * @payload: the key data in clear and encrypted form 1243 * @payload: the key data in clear and encrypted form
1244 *
1245 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
1246 * the keyring subsystem.
1281 * 1247 *
1282 * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1248 * Return: same as with tpm_transmit_cmd()
1283 * are supported.
1284 */ 1249 */
1285int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1250int tpm_unseal_trusted(struct tpm_chip *chip,
1251 struct trusted_key_payload *payload,
1286 struct trusted_key_options *options) 1252 struct trusted_key_options *options)
1287{ 1253{
1288 struct tpm_chip *chip;
1289 int rc; 1254 int rc;
1290 1255
1291 chip = tpm_chip_find_get(chip_num); 1256 chip = tpm_chip_find_get(chip);
1292 if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1257 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
1293 return -ENODEV; 1258 return -ENODEV;
1294 1259
1295 rc = tpm2_unseal_trusted(chip, payload, options); 1260 rc = tpm2_unseal_trusted(chip, payload, options);