@@ -1871,11 +1871,6 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
18711871 * just one will be sufficient to decrypt to get the FEK. */
18721872find_next_matching_auth_tok :
18731873 found_auth_tok = 0 ;
1874- if (auth_tok_key ) {
1875- up_write (& (auth_tok_key -> sem ));
1876- key_put (auth_tok_key );
1877- auth_tok_key = NULL ;
1878- }
18791874 list_for_each_entry (auth_tok_list_item , & auth_tok_list , list ) {
18801875 candidate_auth_tok = & auth_tok_list_item -> auth_tok ;
18811876 if (unlikely (ecryptfs_verbosity > 0 )) {
@@ -1912,14 +1907,22 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
19121907 memcpy (& (candidate_auth_tok -> token .private_key ),
19131908 & (matching_auth_tok -> token .private_key ),
19141909 sizeof (struct ecryptfs_private_key ));
1910+ up_write (& (auth_tok_key -> sem ));
1911+ key_put (auth_tok_key );
19151912 rc = decrypt_pki_encrypted_session_key (candidate_auth_tok ,
19161913 crypt_stat );
19171914 } else if (candidate_auth_tok -> token_type == ECRYPTFS_PASSWORD ) {
19181915 memcpy (& (candidate_auth_tok -> token .password ),
19191916 & (matching_auth_tok -> token .password ),
19201917 sizeof (struct ecryptfs_password ));
1918+ up_write (& (auth_tok_key -> sem ));
1919+ key_put (auth_tok_key );
19211920 rc = decrypt_passphrase_encrypted_session_key (
19221921 candidate_auth_tok , crypt_stat );
1922+ } else {
1923+ up_write (& (auth_tok_key -> sem ));
1924+ key_put (auth_tok_key );
1925+ rc = - EINVAL ;
19231926 }
19241927 if (rc ) {
19251928 struct ecryptfs_auth_tok_list_item * auth_tok_list_item_tmp ;
@@ -1959,15 +1962,12 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
19591962out_wipe_list :
19601963 wipe_auth_tok_list (& auth_tok_list );
19611964out :
1962- if (auth_tok_key ) {
1963- up_write (& (auth_tok_key -> sem ));
1964- key_put (auth_tok_key );
1965- }
19661965 return rc ;
19671966}
19681967
19691968static int
1970- pki_encrypt_session_key (struct ecryptfs_auth_tok * auth_tok ,
1969+ pki_encrypt_session_key (struct key * auth_tok_key ,
1970+ struct ecryptfs_auth_tok * auth_tok ,
19711971 struct ecryptfs_crypt_stat * crypt_stat ,
19721972 struct ecryptfs_key_record * key_rec )
19731973{
@@ -1982,6 +1982,8 @@ pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
19821982 crypt_stat -> cipher ,
19831983 crypt_stat -> key_size ),
19841984 crypt_stat , & payload , & payload_len );
1985+ up_write (& (auth_tok_key -> sem ));
1986+ key_put (auth_tok_key );
19851987 if (rc ) {
19861988 ecryptfs_printk (KERN_ERR , "Error generating tag 66 packet\n" );
19871989 goto out ;
@@ -2011,6 +2013,8 @@ pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
20112013 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
20122014 * @dest: Buffer into which to write the packet
20132015 * @remaining_bytes: Maximum number of bytes that can be writtn
2016+ * @auth_tok_key: The authentication token key to unlock and put when done with
2017+ * @auth_tok
20142018 * @auth_tok: The authentication token used for generating the tag 1 packet
20152019 * @crypt_stat: The cryptographic context
20162020 * @key_rec: The key record struct for the tag 1 packet
@@ -2021,7 +2025,7 @@ pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
20212025 */
20222026static int
20232027write_tag_1_packet (char * dest , size_t * remaining_bytes ,
2024- struct ecryptfs_auth_tok * auth_tok ,
2028+ struct key * auth_tok_key , struct ecryptfs_auth_tok * auth_tok ,
20252029 struct ecryptfs_crypt_stat * crypt_stat ,
20262030 struct ecryptfs_key_record * key_rec , size_t * packet_size )
20272031{
@@ -2042,12 +2046,15 @@ write_tag_1_packet(char *dest, size_t *remaining_bytes,
20422046 memcpy (key_rec -> enc_key ,
20432047 auth_tok -> session_key .encrypted_key ,
20442048 auth_tok -> session_key .encrypted_key_size );
2049+ up_write (& (auth_tok_key -> sem ));
2050+ key_put (auth_tok_key );
20452051 goto encrypted_session_key_set ;
20462052 }
20472053 if (auth_tok -> session_key .encrypted_key_size == 0 )
20482054 auth_tok -> session_key .encrypted_key_size =
20492055 auth_tok -> token .private_key .key_size ;
2050- rc = pki_encrypt_session_key (auth_tok , crypt_stat , key_rec );
2056+ rc = pki_encrypt_session_key (auth_tok_key , auth_tok , crypt_stat ,
2057+ key_rec );
20512058 if (rc ) {
20522059 printk (KERN_ERR "Failed to encrypt session key via a key "
20532060 "module; rc = [%d]\n" , rc );
@@ -2424,6 +2431,8 @@ ecryptfs_generate_key_packet_set(char *dest_base,
24242431 & max , auth_tok ,
24252432 crypt_stat , key_rec ,
24262433 & written );
2434+ up_write (& (auth_tok_key -> sem ));
2435+ key_put (auth_tok_key );
24272436 if (rc ) {
24282437 ecryptfs_printk (KERN_WARNING , "Error "
24292438 "writing tag 3 packet\n" );
@@ -2441,8 +2450,8 @@ ecryptfs_generate_key_packet_set(char *dest_base,
24412450 }
24422451 (* len ) += written ;
24432452 } else if (auth_tok -> token_type == ECRYPTFS_PRIVATE_KEY ) {
2444- rc = write_tag_1_packet (dest_base + (* len ),
2445- & max , auth_tok ,
2453+ rc = write_tag_1_packet (dest_base + (* len ), & max ,
2454+ auth_tok_key , auth_tok ,
24462455 crypt_stat , key_rec , & written );
24472456 if (rc ) {
24482457 ecryptfs_printk (KERN_WARNING , "Error "
@@ -2451,14 +2460,13 @@ ecryptfs_generate_key_packet_set(char *dest_base,
24512460 }
24522461 (* len ) += written ;
24532462 } else {
2463+ up_write (& (auth_tok_key -> sem ));
2464+ key_put (auth_tok_key );
24542465 ecryptfs_printk (KERN_WARNING , "Unsupported "
24552466 "authentication token type\n" );
24562467 rc = - EINVAL ;
24572468 goto out_free ;
24582469 }
2459- up_write (& (auth_tok_key -> sem ));
2460- key_put (auth_tok_key );
2461- auth_tok_key = NULL ;
24622470 }
24632471 if (likely (max > 0 )) {
24642472 dest_base [(* len )] = 0x00 ;
@@ -2471,11 +2479,6 @@ ecryptfs_generate_key_packet_set(char *dest_base,
24712479out :
24722480 if (rc )
24732481 (* len ) = 0 ;
2474- if (auth_tok_key ) {
2475- up_write (& (auth_tok_key -> sem ));
2476- key_put (auth_tok_key );
2477- }
2478-
24792482 mutex_unlock (& crypt_stat -> keysig_list_mutex );
24802483 return rc ;
24812484}
0 commit comments