nfsd4: Fix filp leak
[pandora-kernel.git] / fs / ecryptfs / keystore.c
index 3c4039c..03e609c 100644 (file)
@@ -516,10 +516,11 @@ ecryptfs_find_global_auth_tok_for_sig(
                        goto out_invalid_auth_tok;
                }
 
+               down_write(&(walker->global_auth_tok_key->sem));
                rc = ecryptfs_verify_auth_tok_from_key(
                                walker->global_auth_tok_key, auth_tok);
                if (rc)
-                       goto out_invalid_auth_tok;
+                       goto out_invalid_auth_tok_unlock;
 
                (*auth_tok_key) = walker->global_auth_tok_key;
                key_get(*auth_tok_key);
@@ -527,6 +528,8 @@ ecryptfs_find_global_auth_tok_for_sig(
        }
        rc = -ENOENT;
        goto out;
+out_invalid_auth_tok_unlock:
+       up_write(&(walker->global_auth_tok_key->sem));
 out_invalid_auth_tok:
        printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig);
        walker->flags |= ECRYPTFS_AUTH_TOK_INVALID;
@@ -635,6 +638,16 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
        }
        s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
        (*packet_size) = 0;
+       rc = ecryptfs_find_auth_tok_for_sig(
+               &auth_tok_key,
+               &s->auth_tok, mount_crypt_stat,
+               mount_crypt_stat->global_default_fnek_sig);
+       if (rc) {
+               printk(KERN_ERR "%s: Error attempting to find auth tok for "
+                      "fnek sig [%s]; rc = [%d]\n", __func__,
+                      mount_crypt_stat->global_default_fnek_sig, rc);
+               goto out;
+       }
        rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
                &s->desc.tfm,
                &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
@@ -720,16 +733,6 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
                goto out_free_unlock;
        }
        dest[s->i++] = s->cipher_code;
-       rc = ecryptfs_find_auth_tok_for_sig(
-               &auth_tok_key,
-               &s->auth_tok, mount_crypt_stat,
-               mount_crypt_stat->global_default_fnek_sig);
-       if (rc) {
-               printk(KERN_ERR "%s: Error attempting to find auth tok for "
-                      "fnek sig [%s]; rc = [%d]\n", __func__,
-                      mount_crypt_stat->global_default_fnek_sig, rc);
-               goto out_free_unlock;
-       }
        /* TODO: Support other key modules than passphrase for
         * filename encryption */
        if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
@@ -869,8 +872,10 @@ out_free_unlock:
 out_unlock:
        mutex_unlock(s->tfm_mutex);
 out:
-       if (auth_tok_key)
+       if (auth_tok_key) {
+               up_write(&(auth_tok_key->sem));
                key_put(auth_tok_key);
+       }
        kfree(s);
        return rc;
 }
@@ -983,6 +988,15 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
                       __func__, s->cipher_code);
                goto out;
        }
+       rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
+                                           &s->auth_tok, mount_crypt_stat,
+                                           s->fnek_sig_hex);
+       if (rc) {
+               printk(KERN_ERR "%s: Error attempting to find auth tok for "
+                      "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
+                      rc);
+               goto out;
+       }
        rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm,
                                                        &s->tfm_mutex,
                                                        s->cipher_string);
@@ -1029,15 +1043,6 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
         * >= ECRYPTFS_MAX_IV_BYTES. */
        memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
        s->desc.info = s->iv;
-       rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
-                                           &s->auth_tok, mount_crypt_stat,
-                                           s->fnek_sig_hex);
-       if (rc) {
-               printk(KERN_ERR "%s: Error attempting to find auth tok for "
-                      "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
-                      rc);
-               goto out_free_unlock;
-       }
        /* TODO: Support other key modules than passphrase for
         * filename encryption */
        if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
@@ -1106,8 +1111,10 @@ out:
                (*filename_size) = 0;
                (*filename) = NULL;
        }
-       if (auth_tok_key)
+       if (auth_tok_key) {
+               up_write(&(auth_tok_key->sem));
                key_put(auth_tok_key);
+       }
        kfree(s);
        return rc;
 }
@@ -1638,9 +1645,10 @@ int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
                (*auth_tok_key) = NULL;
                goto out;
        }
-
+       down_write(&(*auth_tok_key)->sem);
        rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok);
        if (rc) {
+               up_write(&(*auth_tok_key)->sem);
                key_put(*auth_tok_key);
                (*auth_tok_key) = NULL;
                goto out;
@@ -1865,6 +1873,7 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
 find_next_matching_auth_tok:
        found_auth_tok = 0;
        if (auth_tok_key) {
+               up_write(&(auth_tok_key->sem));
                key_put(auth_tok_key);
                auth_tok_key = NULL;
        }
@@ -1951,8 +1960,10 @@ found_matching_auth_tok:
 out_wipe_list:
        wipe_auth_tok_list(&auth_tok_list);
 out:
-       if (auth_tok_key)
+       if (auth_tok_key) {
+               up_write(&(auth_tok_key->sem));
                key_put(auth_tok_key);
+       }
        return rc;
 }
 
@@ -2446,6 +2457,7 @@ ecryptfs_generate_key_packet_set(char *dest_base,
                        rc = -EINVAL;
                        goto out_free;
                }
+               up_write(&(auth_tok_key->sem));
                key_put(auth_tok_key);
                auth_tok_key = NULL;
        }
@@ -2460,8 +2472,10 @@ out_free:
 out:
        if (rc)
                (*len) = 0;
-       if (auth_tok_key)
+       if (auth_tok_key) {
+               up_write(&(auth_tok_key->sem));
                key_put(auth_tok_key);
+       }
 
        mutex_unlock(&crypt_stat->keysig_list_mutex);
        return rc;