@@ -163,53 +163,61 @@ static int name_size(const u8 *name)
163163 }
164164}
165165
166- static int tpm2_parse_read_public ( char * name , struct tpm_buf * buf )
166+ static int tpm2_read_public ( struct tpm_chip * chip , u32 handle , void * name )
167167{
168- struct tpm_header * head = ( struct tpm_header * ) buf -> data ;
168+ u32 mso = tpm2_handle_mso ( handle ) ;
169169 off_t offset = TPM_HEADER_SIZE ;
170- u32 tot_len = be32_to_cpu (head -> length );
171- int ret ;
172- u32 val ;
173-
174- /* we're starting after the header so adjust the length */
175- tot_len -= TPM_HEADER_SIZE ;
176-
177- /* skip public */
178- val = tpm_buf_read_u16 (buf , & offset );
179- if (val > tot_len )
180- return - EINVAL ;
181- offset += val ;
182- /* name */
183- val = tpm_buf_read_u16 (buf , & offset );
184- ret = name_size (& buf -> data [offset ]);
185- if (ret < 0 )
186- return ret ;
187-
188- if (val != ret )
189- return - EINVAL ;
190-
191- memcpy (name , & buf -> data [offset ], val );
192- /* forget the rest */
193- return 0 ;
194- }
195-
196- static int tpm2_read_public (struct tpm_chip * chip , u32 handle , char * name )
197- {
170+ int rc , name_size_alg ;
198171 struct tpm_buf buf ;
199- int rc ;
172+
173+ if (mso != TPM2_MSO_PERSISTENT && mso != TPM2_MSO_VOLATILE &&
174+ mso != TPM2_MSO_NVRAM ) {
175+ memcpy (name , & handle , sizeof (u32 ));
176+ return sizeof (u32 );
177+ }
200178
201179 rc = tpm_buf_init (& buf , TPM2_ST_NO_SESSIONS , TPM2_CC_READ_PUBLIC );
202180 if (rc )
203181 return rc ;
204182
205183 tpm_buf_append_u32 (& buf , handle );
206- rc = tpm_transmit_cmd (chip , & buf , 0 , "read public" );
207- if (rc == TPM2_RC_SUCCESS )
208- rc = tpm2_parse_read_public (name , & buf );
209184
210- tpm_buf_destroy (& buf );
185+ rc = tpm_transmit_cmd (chip , & buf , 0 , "TPM2_ReadPublic" );
186+ if (rc ) {
187+ tpm_buf_destroy (& buf );
188+ return tpm_ret_to_err (rc );
189+ }
211190
212- return rc ;
191+ /* Skip TPMT_PUBLIC: */
192+ offset += tpm_buf_read_u16 (& buf , & offset );
193+
194+ /*
195+ * Ensure space for the length field of TPM2B_NAME and hashAlg field of
196+ * TPMT_HA (the extra four bytes).
197+ */
198+ if (offset + 4 > tpm_buf_length (& buf )) {
199+ tpm_buf_destroy (& buf );
200+ return - EIO ;
201+ }
202+
203+ rc = tpm_buf_read_u16 (& buf , & offset );
204+ name_size_alg = name_size (& buf .data [offset ]);
205+
206+ if (name_size_alg < 0 )
207+ return name_size_alg ;
208+
209+ if (rc != name_size_alg ) {
210+ tpm_buf_destroy (& buf );
211+ return - EIO ;
212+ }
213+
214+ if (offset + rc > tpm_buf_length (& buf )) {
215+ tpm_buf_destroy (& buf );
216+ return - EIO ;
217+ }
218+
219+ memcpy (name , & buf .data [offset ], rc );
220+ return name_size_alg ;
213221}
214222#endif /* CONFIG_TCG_TPM2_HMAC */
215223
@@ -243,6 +251,7 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
243251#ifdef CONFIG_TCG_TPM2_HMAC
244252 enum tpm2_mso_type mso = tpm2_handle_mso (handle );
245253 struct tpm2_auth * auth ;
254+ u16 name_size_alg ;
246255 int slot ;
247256 int ret ;
248257#endif
@@ -273,8 +282,10 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
273282 mso == TPM2_MSO_NVRAM ) {
274283 if (!name ) {
275284 ret = tpm2_read_public (chip , handle , auth -> name [slot ]);
276- if (ret )
285+ if (ret < 0 )
277286 goto err ;
287+
288+ name_size_alg = ret ;
278289 }
279290 } else {
280291 if (name ) {
@@ -286,13 +297,8 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
286297 }
287298
288299 auth -> name_h [slot ] = handle ;
289- if (name ) {
290- ret = name_size (name );
291- if (ret < 0 )
292- goto err ;
293-
294- memcpy (auth -> name [slot ], name , ret );
295- }
300+ if (name )
301+ memcpy (auth -> name [slot ], name , name_size_alg );
296302#endif
297303 return 0 ;
298304
0 commit comments