80
80
/* maximum number of names the TPM must remember for authorization */
81
81
#define AUTH_MAX_NAMES 3
82
82
83
+ static int tpm2_create_primary (struct tpm_chip * chip , u32 hierarchy ,
84
+ u32 * handle , u8 * name );
85
+
83
86
/*
84
87
* This is the structure that carries all the auth information (like
85
88
* session handle, nonces, session key and auth) from use to use it is
@@ -851,6 +854,37 @@ static int tpm2_parse_start_auth_session(struct tpm2_auth *auth,
851
854
return 0 ;
852
855
}
853
856
857
+ static int tpm2_load_null (struct tpm_chip * chip , u32 * null_key )
858
+ {
859
+ int rc ;
860
+ unsigned int offset = 0 ; /* dummy offset for null seed context */
861
+ u8 name [SHA256_DIGEST_SIZE + 2 ];
862
+
863
+ rc = tpm2_load_context (chip , chip -> null_key_context , & offset ,
864
+ null_key );
865
+ if (rc != - EINVAL )
866
+ return rc ;
867
+
868
+ /* an integrity failure may mean the TPM has been reset */
869
+ dev_err (& chip -> dev , "NULL key integrity failure!\n" );
870
+ /* check the null name against what we know */
871
+ tpm2_create_primary (chip , TPM2_RH_NULL , NULL , name );
872
+ if (memcmp (name , chip -> null_key_name , sizeof (name )) == 0 )
873
+ /* name unchanged, assume transient integrity failure */
874
+ return rc ;
875
+ /*
876
+ * Fatal TPM failure: the NULL seed has actually changed, so
877
+ * the TPM must have been illegally reset. All in-kernel TPM
878
+ * operations will fail because the NULL primary can't be
879
+ * loaded to salt the sessions, but disable the TPM anyway so
880
+ * userspace programmes can't be compromised by it.
881
+ */
882
+ dev_err (& chip -> dev , "NULL name has changed, disabling TPM due to interference\n" );
883
+ chip -> flags |= TPM_CHIP_FLAG_DISABLE ;
884
+
885
+ return rc ;
886
+ }
887
+
854
888
/**
855
889
* tpm2_start_auth_session() - create a HMAC authentication session with the TPM
856
890
* @chip: the TPM chip structure to create the session with
@@ -868,12 +902,9 @@ int tpm2_start_auth_session(struct tpm_chip *chip)
868
902
struct tpm_buf buf ;
869
903
struct tpm2_auth * auth = chip -> auth ;
870
904
int rc ;
871
- /* null seed context has no offset, but we must provide one */
872
- unsigned int offset = 0 ;
873
- u32 nullkey ;
905
+ u32 null_key ;
874
906
875
- rc = tpm2_load_context (chip , chip -> null_key_context , & offset ,
876
- & nullkey );
907
+ rc = tpm2_load_null (chip , & null_key );
877
908
if (rc )
878
909
goto out ;
879
910
@@ -884,7 +915,7 @@ int tpm2_start_auth_session(struct tpm_chip *chip)
884
915
goto out ;
885
916
886
917
/* salt key handle */
887
- tpm_buf_append_u32 (& buf , nullkey );
918
+ tpm_buf_append_u32 (& buf , null_key );
888
919
/* bind key handle */
889
920
tpm_buf_append_u32 (& buf , TPM2_RH_NULL );
890
921
/* nonce caller */
@@ -908,7 +939,7 @@ int tpm2_start_auth_session(struct tpm_chip *chip)
908
939
tpm_buf_append_u16 (& buf , TPM_ALG_SHA256 );
909
940
910
941
rc = tpm_transmit_cmd (chip , & buf , 0 , "start auth session" );
911
- tpm2_flush_context (chip , nullkey );
942
+ tpm2_flush_context (chip , null_key );
912
943
913
944
if (rc == TPM2_RC_SUCCESS )
914
945
rc = tpm2_parse_start_auth_session (auth , & buf );
@@ -930,22 +961,28 @@ EXPORT_SYMBOL(tpm2_start_auth_session);
930
961
* @buf: The response buffer from the chip
931
962
* @handle: pointer to be filled in with the return handle of the primary
932
963
* @hierarchy: The hierarchy the primary was created for
964
+ * @name: pointer to be filled in with the primary key name
933
965
*
934
966
* Return:
935
967
* * 0 - OK
936
968
* * -errno - A system error
937
969
* * TPM_RC - A TPM error
938
970
*/
939
971
static int tpm2_parse_create_primary (struct tpm_chip * chip , struct tpm_buf * buf ,
940
- u32 * handle , u32 hierarchy )
972
+ u32 * handle , u32 hierarchy , u8 * name )
941
973
{
942
974
struct tpm_header * head = (struct tpm_header * )buf -> data ;
943
975
off_t offset_r = TPM_HEADER_SIZE , offset_t ;
944
976
u16 len = TPM_HEADER_SIZE ;
945
977
u32 total_len = be32_to_cpu (head -> length );
946
- u32 val , param_len ;
978
+ u32 val , param_len , keyhandle ;
979
+
980
+ keyhandle = tpm_buf_read_u32 (buf , & offset_r );
981
+ if (handle )
982
+ * handle = keyhandle ;
983
+ else
984
+ tpm2_flush_context (chip , keyhandle );
947
985
948
- * handle = tpm_buf_read_u32 (buf , & offset_r );
949
986
param_len = tpm_buf_read_u32 (buf , & offset_r );
950
987
/*
951
988
* param_len doesn't include the header, but all the other
@@ -958,9 +995,14 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
958
995
return - EINVAL ;
959
996
len = tpm_buf_read_u16 (buf , & offset_r );
960
997
offset_t = offset_r ;
961
- /* now we have the public area, compute the name of the object */
962
- put_unaligned_be16 (TPM_ALG_SHA256 , chip -> null_key_name );
963
- sha256 (& buf -> data [offset_r ], len , chip -> null_key_name + 2 );
998
+ if (name ) {
999
+ /*
1000
+ * now we have the public area, compute the name of
1001
+ * the object
1002
+ */
1003
+ put_unaligned_be16 (TPM_ALG_SHA256 , name );
1004
+ sha256 (& buf -> data [offset_r ], len , name + 2 );
1005
+ }
964
1006
965
1007
/* validate the public key */
966
1008
val = tpm_buf_read_u16 (buf , & offset_t );
@@ -1089,6 +1131,7 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
1089
1131
* @chip: the TPM chip to create under
1090
1132
* @hierarchy: The hierarchy handle to create under
1091
1133
* @handle: The returned volatile handle on success
1134
+ * @name: The name of the returned key
1092
1135
*
1093
1136
* For platforms that might not have a persistent primary, this can be
1094
1137
* used to create one quickly on the fly (it uses Elliptic Curve not
@@ -1103,7 +1146,7 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
1103
1146
* * TPM_RC - A TPM error
1104
1147
*/
1105
1148
static int tpm2_create_primary (struct tpm_chip * chip , u32 hierarchy ,
1106
- u32 * handle )
1149
+ u32 * handle , u8 * name )
1107
1150
{
1108
1151
int rc ;
1109
1152
struct tpm_buf buf ;
@@ -1193,7 +1236,8 @@ static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
1193
1236
"attempting to create NULL primary" );
1194
1237
1195
1238
if (rc == TPM2_RC_SUCCESS )
1196
- rc = tpm2_parse_create_primary (chip , & buf , handle , hierarchy );
1239
+ rc = tpm2_parse_create_primary (chip , & buf , handle , hierarchy ,
1240
+ name );
1197
1241
1198
1242
tpm_buf_destroy (& buf );
1199
1243
@@ -1205,7 +1249,8 @@ static int tpm2_create_null_primary(struct tpm_chip *chip)
1205
1249
u32 null_key ;
1206
1250
int rc ;
1207
1251
1208
- rc = tpm2_create_primary (chip , TPM2_RH_NULL , & null_key );
1252
+ rc = tpm2_create_primary (chip , TPM2_RH_NULL , & null_key ,
1253
+ chip -> null_key_name );
1209
1254
1210
1255
if (rc == TPM2_RC_SUCCESS ) {
1211
1256
unsigned int offset = 0 ; /* dummy offset for null key context */
0 commit comments