nommu: ramfs: remove unused local var
[safe/jmp/linux-2.6] / fs / ecryptfs / crypto.c
index ea2afd2..fbb6e5e 100644 (file)
@@ -245,13 +245,11 @@ void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
                crypto_free_blkcipher(crypt_stat->tfm);
        if (crypt_stat->hash_tfm)
                crypto_free_hash(crypt_stat->hash_tfm);
-       mutex_lock(&crypt_stat->keysig_list_mutex);
        list_for_each_entry_safe(key_sig, key_sig_tmp,
                                 &crypt_stat->keysig_list, crypt_stat_list) {
                list_del(&key_sig->crypt_stat_list);
                kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
        }
-       mutex_unlock(&crypt_stat->keysig_list_mutex);
        memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
 }
 
@@ -483,15 +481,7 @@ int ecryptfs_encrypt_page(struct page *page)
        ecryptfs_inode = page->mapping->host;
        crypt_stat =
                &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
-       if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
-               rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
-                                                      0, PAGE_CACHE_SIZE);
-               if (rc)
-                       printk(KERN_ERR "%s: Error attempting to copy "
-                              "page at index [%ld]\n", __func__,
-                              page->index);
-               goto out;
-       }
+       BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
        enc_extent_page = alloc_page(GFP_USER);
        if (!enc_extent_page) {
                rc = -ENOMEM;
@@ -519,13 +509,14 @@ int ecryptfs_encrypt_page(struct page *page)
                                  + extent_offset), crypt_stat);
                rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
                                          offset, crypt_stat->extent_size);
-               if (rc) {
+               if (rc < 0) {
                        ecryptfs_printk(KERN_ERR, "Error attempting "
                                        "to write lower page; rc = [%d]"
                                        "\n", rc);
                        goto out;
                }
        }
+       rc = 0;
 out:
        if (enc_extent_page) {
                kunmap(enc_extent_page);
@@ -620,16 +611,7 @@ int ecryptfs_decrypt_page(struct page *page)
        ecryptfs_inode = page->mapping->host;
        crypt_stat =
                &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
-       if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
-               rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
-                                                     PAGE_CACHE_SIZE,
-                                                     ecryptfs_inode);
-               if (rc)
-                       printk(KERN_ERR "%s: Error attempting to copy "
-                              "page at index [%ld]\n", __func__,
-                              page->index);
-               goto out;
-       }
+       BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
        enc_extent_page = alloc_page(GFP_USER);
        if (!enc_extent_page) {
                rc = -ENOMEM;
@@ -650,7 +632,7 @@ int ecryptfs_decrypt_page(struct page *page)
                rc = ecryptfs_read_lower(enc_extent_virt, offset,
                                         crypt_stat->extent_size,
                                         ecryptfs_inode);
-               if (rc) {
+               if (rc < 0) {
                        ecryptfs_printk(KERN_ERR, "Error attempting "
                                        "to read lower page; rc = [%d]"
                                        "\n", rc);
@@ -814,6 +796,7 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
        kfree(full_alg_name);
        if (IS_ERR(crypt_stat->tfm)) {
                rc = PTR_ERR(crypt_stat->tfm);
+               crypt_stat->tfm = NULL;
                ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
                                "Error initializing cipher [%s]\n",
                                crypt_stat->cipher);
@@ -942,20 +925,24 @@ static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
        struct ecryptfs_global_auth_tok *global_auth_tok;
        int rc = 0;
 
+       mutex_lock(&crypt_stat->keysig_list_mutex);
        mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
+
        list_for_each_entry(global_auth_tok,
                            &mount_crypt_stat->global_auth_tok_list,
                            mount_crypt_stat_list) {
+               if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
+                       continue;
                rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
                if (rc) {
                        printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
-                       mutex_unlock(
-                               &mount_crypt_stat->global_auth_tok_list_mutex);
                        goto out;
                }
        }
-       mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
+
 out:
+       mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
+       mutex_unlock(&crypt_stat->keysig_list_mutex);
        return rc;
 }
 
@@ -1227,14 +1214,15 @@ int ecryptfs_read_and_validate_header_region(char *data,
                crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
        rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
                                 ecryptfs_inode);
-       if (rc) {
+       if (rc < 0) {
                printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
                       __func__, rc);
                goto out;
        }
        if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
                rc = -EINVAL;
-       }
+       } else
+               rc = 0;
 out:
        return rc;
 }
@@ -1322,24 +1310,23 @@ static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
 }
 
 static int
-ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
-                                   struct dentry *ecryptfs_dentry,
-                                   char *virt)
+ecryptfs_write_metadata_to_contents(struct dentry *ecryptfs_dentry,
+                                   char *virt, size_t virt_len)
 {
        int rc;
 
        rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt,
-                                 0, crypt_stat->num_header_bytes_at_front);
-       if (rc)
+                                 0, virt_len);
+       if (rc < 0)
                printk(KERN_ERR "%s: Error attempting to write header "
-                      "information to lower file; rc = [%d]\n", __func__,
-                      rc);
+                      "information to lower file; rc = [%d]\n", __func__, rc);
+       else
+               rc = 0;
        return rc;
 }
 
 static int
 ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
-                                struct ecryptfs_crypt_stat *crypt_stat,
                                 char *page_virt, size_t size)
 {
        int rc;
@@ -1349,6 +1336,17 @@ ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
        return rc;
 }
 
+static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
+                                              unsigned int order)
+{
+       struct page *page;
+
+       page = alloc_pages(gfp_mask | __GFP_ZERO, order);
+       if (page)
+               return (unsigned long) page_address(page);
+       return 0;
+}
+
 /**
  * ecryptfs_write_metadata
  * @ecryptfs_dentry: The eCryptfs dentry
@@ -1365,7 +1363,9 @@ int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
 {
        struct ecryptfs_crypt_stat *crypt_stat =
                &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
+       unsigned int order;
        char *virt;
+       size_t virt_len;
        size_t size = 0;
        int rc = 0;
 
@@ -1381,33 +1381,35 @@ int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
                rc = -EINVAL;
                goto out;
        }
+       virt_len = crypt_stat->num_header_bytes_at_front;
+       order = get_order(virt_len);
        /* Released in this function */
-       virt = (char *)get_zeroed_page(GFP_KERNEL);
+       virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
        if (!virt) {
                printk(KERN_ERR "%s: Out of memory\n", __func__);
                rc = -ENOMEM;
                goto out;
        }
-       rc = ecryptfs_write_headers_virt(virt, PAGE_CACHE_SIZE, &size,
-                                        crypt_stat, ecryptfs_dentry);
+       rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
+                                        ecryptfs_dentry);
        if (unlikely(rc)) {
                printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
                       __func__, rc);
                goto out_free;
        }
        if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
-               rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
-                                                     crypt_stat, virt, size);
+               rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
+                                                     size);
        else
-               rc = ecryptfs_write_metadata_to_contents(crypt_stat,
-                                                        ecryptfs_dentry, virt);
+               rc = ecryptfs_write_metadata_to_contents(ecryptfs_dentry, virt,
+                                                        virt_len);
        if (rc) {
                printk(KERN_ERR "%s: Error writing metadata out to lower file; "
                       "rc = [%d]\n", __func__, rc);
                goto out_free;
        }
 out_free:
-       free_page((unsigned long)virt);
+       free_pages((unsigned long)virt, order);
 out:
        return rc;
 }
@@ -1599,7 +1601,7 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
        }
        rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
                                 ecryptfs_inode);
-       if (!rc)
+       if (rc >= 0)
                rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
                                                ecryptfs_dentry,
                                                ECRYPTFS_VALIDATE_HEADER_SIZE);
@@ -1679,7 +1681,7 @@ ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
                        kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
                if (!filename->encrypted_filename) {
                        printk(KERN_ERR "%s: Out of memory whilst attempting "
-                              "to kmalloc [%Zd] bytes\n", __func__,
+                              "to kmalloc [%zd] bytes\n", __func__,
                               filename->encrypted_filename_size);
                        rc = -ENOMEM;
                        goto out;
@@ -1704,7 +1706,7 @@ ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
        } else {
                printk(KERN_ERR "%s: No support for requested filename "
                       "encryption method in this release\n", __func__);
-               rc = -ENOTSUPP;
+               rc = -EOPNOTSUPP;
                goto out;
        }
 out:
@@ -1716,7 +1718,7 @@ static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
 {
        int rc = 0;
 
-       (*copied_name) = kmalloc((name_size + 2), GFP_KERNEL);
+       (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
        if (!(*copied_name)) {
                rc = -ENOMEM;
                goto out;
@@ -1726,7 +1728,7 @@ static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
                                                 * in printing out the
                                                 * string in debug
                                                 * messages */
-       (*copied_name_size) = (name_size + 1);
+       (*copied_name_size) = name_size;
 out:
        return rc;
 }
@@ -1752,7 +1754,7 @@ ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
        *key_tfm = NULL;
        if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
                rc = -EINVAL;
-               printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
+               printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
                      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
                goto out;
        }
@@ -1765,7 +1767,7 @@ ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
        if (IS_ERR(*key_tfm)) {
                rc = PTR_ERR(*key_tfm);
                printk(KERN_ERR "Unable to allocate crypto cipher with name "
-                      "[%s]; rc = [%d]\n", cipher_name, rc);
+                      "[%s]; rc = [%d]\n", full_alg_name, rc);
                goto out;
        }
        crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
@@ -1777,8 +1779,9 @@ ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
        get_random_bytes(dummy_key, *key_size);
        rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
        if (rc) {
-               printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
-                      "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
+               printk(KERN_ERR "Error attempting to set key of size [%zd] for "
+                      "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
+                      rc);
                rc = -EINVAL;
                goto out;
        }
@@ -1931,7 +1934,7 @@ static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
 
 /* We could either offset on every reverse map or just pad some 0x00's
  * at the front here */
-static unsigned char filename_rev_map[] = {
+static const unsigned char filename_rev_map[] = {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
@@ -2012,16 +2015,30 @@ out:
        return;
 }
 
-int ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
-                                 const unsigned char *src, size_t src_size)
+/**
+ * ecryptfs_decode_from_filename
+ * @dst: If NULL, this function only sets @dst_size and returns. If
+ *       non-NULL, this function decodes the encoded octets in @src
+ *       into the memory that @dst points to.
+ * @dst_size: Set to the size of the decoded string.
+ * @src: The encoded set of octets to decode.
+ * @src_size: The size of the encoded set of octets to decode.
+ */
+static void
+ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
+                             const unsigned char *src, size_t src_size)
 {
        u8 current_bit_offset = 0;
        size_t src_byte_offset = 0;
        size_t dst_byte_offset = 0;
-       int rc = 0;
 
        if (dst == NULL) {
-               /* Not exact; conservatively long */
+               /* Not exact; conservatively long. Every block of 4
+                * encoded characters decodes into a block of 3
+                * decoded characters. This segment of code provides
+                * the caller with the maximum amount of allocated
+                * space that @dst will need to point to in a
+                * subsequent call. */
                (*dst_size) = (((src_size + 1) * 3) / 4);
                goto out;
        }
@@ -2055,7 +2072,7 @@ int ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
        }
        (*dst_size) = dst_byte_offset;
 out:
-       return rc;
+       return;
 }
 
 /**
@@ -2093,7 +2110,7 @@ int ecryptfs_encrypt_and_encode_filename(
                filename = kzalloc(sizeof(*filename), GFP_KERNEL);
                if (!filename) {
                        printk(KERN_ERR "%s: Out of memory whilst attempting "
-                              "to kzalloc [%d] bytes\n", __func__,
+                              "to kzalloc [%zd] bytes\n", __func__,
                               sizeof(*filename));
                        rc = -ENOMEM;
                        goto out;
@@ -2127,7 +2144,7 @@ int ecryptfs_encrypt_and_encode_filename(
                (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
                if (!(*encoded_name)) {
                        printk(KERN_ERR "%s: Out of memory whilst attempting "
-                              "to kzalloc [%d] bytes\n", __func__,
+                              "to kzalloc [%zd] bytes\n", __func__,
                               (*encoded_name_size));
                        rc = -ENOMEM;
                        kfree(filename->encrypted_filename);
@@ -2154,7 +2171,7 @@ int ecryptfs_encrypt_and_encode_filename(
                        (*encoded_name)[(*encoded_name_size)] = '\0';
                        (*encoded_name_size)++;
                } else {
-                       rc = -ENOTSUPP;
+                       rc = -EOPNOTSUPP;
                }
                if (rc) {
                        printk(KERN_ERR "%s: Error attempting to encode "
@@ -2192,51 +2209,36 @@ int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
                                         struct dentry *ecryptfs_dir_dentry,
                                         const char *name, size_t name_size)
 {
+       struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
+               &ecryptfs_superblock_to_private(
+                       ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
        char *decoded_name;
        size_t decoded_name_size;
        size_t packet_size;
        int rc = 0;
 
-       if ((name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
+       if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
+           && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
+           && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
            && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
                        ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
-               struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
-                       &ecryptfs_superblock_to_private(
-                               ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
                const char *orig_name = name;
                size_t orig_name_size = name_size;
 
                name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
                name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
-               rc = ecryptfs_decode_from_filename(NULL, &decoded_name_size,
-                                                  name, name_size);
-               if (rc) {
-                       printk(KERN_ERR "%s: Error attempting to decode "
-                              "filename; rc = [%d]\n", __func__, rc);
-                       rc = ecryptfs_copy_filename(plaintext_name,
-                                                   plaintext_name_size,
-                                                   orig_name, orig_name_size);
-                       goto out;
-               }
+               ecryptfs_decode_from_filename(NULL, &decoded_name_size,
+                                             name, name_size);
                decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
                if (!decoded_name) {
                        printk(KERN_ERR "%s: Out of memory whilst attempting "
-                              "to kmalloc [%Zd] bytes\n", __func__,
+                              "to kmalloc [%zd] bytes\n", __func__,
                               decoded_name_size);
                        rc = -ENOMEM;
                        goto out;
                }
-               rc = ecryptfs_decode_from_filename(decoded_name,
-                                                  &decoded_name_size,
-                                                  name, name_size);
-               if (rc) {
-                       printk(KERN_ERR "%s: Error attempting to decode "
-                              "filename; rc = [%d]\n", __func__, rc);
-                       rc = ecryptfs_copy_filename(plaintext_name,
-                                                   plaintext_name_size,
-                                                   orig_name, orig_name_size);
-                       goto out_free;
-               }
+               ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
+                                             name, name_size);
                rc = ecryptfs_parse_tag_70_packet(plaintext_name,
                                                  plaintext_name_size,
                                                  &packet_size,