Merge commit 'v2.6.30' into for-2.6.31
[safe/jmp/linux-2.6] / fs / ecryptfs / crypto.c
index e935a22..b91851f 100644 (file)
@@ -483,15 +483,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;
@@ -620,16 +612,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;
@@ -946,6 +929,8 @@ static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
        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);
@@ -1322,14 +1307,13 @@ 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);
+                                 0, virt_len);
        if (rc)
                printk(KERN_ERR "%s: Error attempting to write header "
                       "information to lower file; rc = [%d]\n", __func__,
@@ -1339,7 +1323,6 @@ ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
 
 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 +1332,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 +1359,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 +1377,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;
 }
@@ -1716,7 +1714,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 +1724,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;
 }
@@ -1931,7 +1929,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 +2010,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 +2067,7 @@ int ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
        }
        (*dst_size) = dst_byte_offset;
 out:
-       return rc;
+       return;
 }
 
 /**
@@ -2192,32 +2204,26 @@ 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 "
@@ -2226,17 +2232,8 @@ int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
                        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,