@@ -144,16 +144,23 @@ struct tpm2_auth {
144144/*
145145 * Name Size based on TPM algorithm (assumes no hash bigger than 255)
146146 */
147- static u8 name_size (const u8 * name )
147+ static int name_size (const u8 * name )
148148{
149- static u8 size_map [] = {
150- [TPM_ALG_SHA1 ] = SHA1_DIGEST_SIZE ,
151- [TPM_ALG_SHA256 ] = SHA256_DIGEST_SIZE ,
152- [TPM_ALG_SHA384 ] = SHA384_DIGEST_SIZE ,
153- [TPM_ALG_SHA512 ] = SHA512_DIGEST_SIZE ,
154- };
155- u16 alg = get_unaligned_be16 (name );
156- return size_map [alg ] + 2 ;
149+ u16 hash_alg = get_unaligned_be16 (name );
150+
151+ switch (hash_alg ) {
152+ case TPM_ALG_SHA1 :
153+ return SHA1_DIGEST_SIZE + 2 ;
154+ case TPM_ALG_SHA256 :
155+ return SHA256_DIGEST_SIZE + 2 ;
156+ case TPM_ALG_SHA384 :
157+ return SHA384_DIGEST_SIZE + 2 ;
158+ case TPM_ALG_SHA512 :
159+ return SHA512_DIGEST_SIZE + 2 ;
160+ default :
161+ pr_warn ("tpm: unsupported name algorithm: 0x%04x\n" , hash_alg );
162+ return - EINVAL ;
163+ }
157164}
158165
159166static int tpm2_read_public (struct tpm_chip * chip , u32 handle , void * name )
@@ -234,9 +241,11 @@ static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
234241 * As with most tpm_buf operations, success is assumed because failure
235242 * will be caused by an incorrect programming model and indicated by a
236243 * kernel message.
244+ *
245+ * Ends the authorization session on failure.
237246 */
238- void tpm_buf_append_name (struct tpm_chip * chip , struct tpm_buf * buf ,
239- u32 handle , u8 * name )
247+ int tpm_buf_append_name (struct tpm_chip * chip , struct tpm_buf * buf ,
248+ u32 handle , u8 * name )
240249{
241250#ifdef CONFIG_TCG_TPM2_HMAC
242251 enum tpm2_mso_type mso = tpm2_handle_mso (handle );
@@ -247,18 +256,22 @@ void tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
247256
248257 if (!tpm2_chip_auth (chip )) {
249258 tpm_buf_append_handle (chip , buf , handle );
250- return ;
259+ return 0 ;
251260 }
252261
253262#ifdef CONFIG_TCG_TPM2_HMAC
254263 slot = (tpm_buf_length (buf ) - TPM_HEADER_SIZE ) / 4 ;
255264 if (slot >= AUTH_MAX_NAMES ) {
256- dev_err (& chip -> dev , "TPM: too many handles\n" );
257- return ;
265+ dev_err (& chip -> dev , "too many handles\n" );
266+ ret = - EIO ;
267+ goto err ;
258268 }
259269 auth = chip -> auth ;
260- WARN (auth -> session != tpm_buf_length (buf ),
261- "name added in wrong place\n" );
270+ if (auth -> session != tpm_buf_length (buf )) {
271+ dev_err (& chip -> dev , "session state malformed" );
272+ ret = - EIO ;
273+ goto err ;
274+ }
262275 tpm_buf_append_u32 (buf , handle );
263276 auth -> session += 4 ;
264277
@@ -271,17 +284,29 @@ void tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
271284 goto err ;
272285 }
273286 } else {
274- if (name )
275- dev_err (& chip -> dev , "TPM: Handle does not require name but one is specified\n" );
287+ if (name ) {
288+ dev_err (& chip -> dev , "handle 0x%08x does not use a name\n" ,
289+ handle );
290+ ret = - EIO ;
291+ goto err ;
292+ }
276293 }
277294
278295 auth -> name_h [slot ] = handle ;
279- if (name )
280- memcpy (auth -> name [slot ], name , name_size (name ));
281- return ;
296+ if (name ) {
297+ ret = name_size (name );
298+ if (ret < 0 )
299+ goto err ;
300+
301+ memcpy (auth -> name [slot ], name , ret );
302+ }
303+ #endif
304+ return 0 ;
282305
306+ #ifdef CONFIG_TCG_TPM2_HMAC
283307err :
284308 tpm2_end_auth_session (chip );
309+ return tpm_ret_to_err (ret );
285310#endif
286311}
287312EXPORT_SYMBOL_GPL (tpm_buf_append_name );
@@ -599,11 +624,9 @@ static void tpm_buf_append_salt(struct tpm_buf *buf, struct tpm_chip *chip,
599624 * encryption key and encrypts the first parameter of the command
600625 * buffer with it.
601626 *
602- * As with most tpm_buf operations, success is assumed because failure
603- * will be caused by an incorrect programming model and indicated by a
604- * kernel message.
627+ * Ends the authorization session on failure.
605628 */
606- void tpm_buf_fill_hmac_session (struct tpm_chip * chip , struct tpm_buf * buf )
629+ int tpm_buf_fill_hmac_session (struct tpm_chip * chip , struct tpm_buf * buf )
607630{
608631 u32 cc , handles , val ;
609632 struct tpm2_auth * auth = chip -> auth ;
@@ -614,9 +637,12 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
614637 u32 attrs ;
615638 u8 cphash [SHA256_DIGEST_SIZE ];
616639 struct sha256_state sctx ;
640+ int ret ;
617641
618- if (!auth )
619- return ;
642+ if (!auth ) {
643+ ret = - EIO ;
644+ goto err ;
645+ }
620646
621647 /* save the command code in BE format */
622648 auth -> ordinal = head -> ordinal ;
@@ -625,9 +651,11 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
625651
626652 i = tpm2_find_cc (chip , cc );
627653 if (i < 0 ) {
628- dev_err (& chip -> dev , "Command 0x%x not found in TPM\n" , cc );
629- return ;
654+ dev_err (& chip -> dev , "command 0x%08x not found\n" , cc );
655+ ret = - EIO ;
656+ goto err ;
630657 }
658+
631659 attrs = chip -> cc_attrs_tbl [i ];
632660
633661 handles = (attrs >> TPM2_CC_ATTR_CHANDLES ) & GENMASK (2 , 0 );
@@ -641,9 +669,9 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
641669 u32 handle = tpm_buf_read_u32 (buf , & offset_s );
642670
643671 if (auth -> name_h [i ] != handle ) {
644- dev_err (& chip -> dev , "TPM: handle %d wrong for name \n" ,
645- i ) ;
646- return ;
672+ dev_err (& chip -> dev , "invalid handle 0x%08x \n" , handle );
673+ ret = - EIO ;
674+ goto err ;
647675 }
648676 }
649677 /* point offset_s to the start of the sessions */
@@ -674,12 +702,14 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
674702 offset_s += len ;
675703 }
676704 if (offset_s != offset_p ) {
677- dev_err (& chip -> dev , "TPM session length is incorrect\n" );
678- return ;
705+ dev_err (& chip -> dev , "session length is incorrect\n" );
706+ ret = - EIO ;
707+ goto err ;
679708 }
680709 if (!hmac ) {
681- dev_err (& chip -> dev , "TPM could not find HMAC session\n" );
682- return ;
710+ dev_err (& chip -> dev , "could not find HMAC session\n" );
711+ ret = - EIO ;
712+ goto err ;
683713 }
684714
685715 /* encrypt before HMAC */
@@ -711,8 +741,11 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
711741 if (mso == TPM2_MSO_PERSISTENT ||
712742 mso == TPM2_MSO_VOLATILE ||
713743 mso == TPM2_MSO_NVRAM ) {
714- sha256_update (& sctx , auth -> name [i ],
715- name_size (auth -> name [i ]));
744+ ret = name_size (auth -> name [i ]);
745+ if (ret < 0 )
746+ goto err ;
747+
748+ sha256_update (& sctx , auth -> name [i ], ret );
716749 } else {
717750 __be32 h = cpu_to_be32 (auth -> name_h [i ]);
718751
@@ -733,6 +766,11 @@ void tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
733766 sha256_update (& sctx , & auth -> attrs , 1 );
734767 tpm2_hmac_final (& sctx , auth -> session_key , sizeof (auth -> session_key )
735768 + auth -> passphrase_len , hmac );
769+ return 0 ;
770+
771+ err :
772+ tpm2_end_auth_session (chip );
773+ return ret ;
736774}
737775EXPORT_SYMBOL (tpm_buf_fill_hmac_session );
738776
0 commit comments