[IA64] export smp_send_reschedule
[safe/jmp/linux-2.6] / fs / ecryptfs / crypto.c
index 18c78ab..8b65f28 100644 (file)
@@ -924,6 +924,15 @@ static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
                crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
        if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
                crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
+       if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
+               crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
+               if (mount_crypt_stat->flags
+                   & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
+                       crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
+               else if (mount_crypt_stat->flags
+                        & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
+                       crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
+       }
 }
 
 static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
@@ -937,6 +946,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);
@@ -1060,7 +1071,8 @@ struct ecryptfs_flag_map_elem {
 static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
        {0x00000001, ECRYPTFS_ENABLE_HMAC},
        {0x00000002, ECRYPTFS_ENCRYPTED},
-       {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
+       {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
+       {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
 };
 
 /**
@@ -1213,6 +1225,8 @@ int ecryptfs_read_and_validate_header_region(char *data,
                &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
        int rc;
 
+       if (crypt_stat->extent_size == 0)
+               crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
        rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
                                 ecryptfs_inode);
        if (rc) {
@@ -1222,7 +1236,6 @@ int ecryptfs_read_and_validate_header_region(char *data,
        }
        if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
                rc = -EINVAL;
-               ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
        }
 out:
        return rc;
@@ -1311,14 +1324,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__,
@@ -1328,7 +1340,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;
@@ -1338,6 +1349,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
@@ -1354,7 +1376,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;
 
@@ -1370,33 +1394,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;
 }
@@ -1629,98 +1655,6 @@ out:
 }
 
 /**
- * ecryptfs_encode_filename - converts a plaintext file name to cipher text
- * @crypt_stat: The crypt_stat struct associated with the file anem to encode
- * @name: The plaintext name
- * @length: The length of the plaintext
- * @encoded_name: The encypted name
- *
- * Encrypts and encodes a filename into something that constitutes a
- * valid filename for a filesystem, with printable characters.
- *
- * We assume that we have a properly initialized crypto context,
- * pointed to by crypt_stat->tfm.
- *
- * TODO: Implement filename decoding and decryption here, in place of
- * memcpy. We are keeping the framework around for now to (1)
- * facilitate testing of the components needed to implement filename
- * encryption and (2) to provide a code base from which other
- * developers in the community can easily implement this feature.
- *
- * Returns the length of encoded filename; negative if error
- */
-int
-ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
-                        const char *name, int length, char **encoded_name)
-{
-       int error = 0;
-
-       (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
-       if (!(*encoded_name)) {
-               error = -ENOMEM;
-               goto out;
-       }
-       /* TODO: Filename encryption is a scheduled feature for a
-        * future version of eCryptfs. This function is here only for
-        * the purpose of providing a framework for other developers
-        * to easily implement filename encryption. Hint: Replace this
-        * memcpy() with a call to encrypt and encode the
-        * filename, the set the length accordingly. */
-       memcpy((void *)(*encoded_name), (void *)name, length);
-       (*encoded_name)[length] = '\0';
-       error = length + 1;
-out:
-       return error;
-}
-
-/**
- * ecryptfs_decode_filename - converts the cipher text name to plaintext
- * @crypt_stat: The crypt_stat struct associated with the file
- * @name: The filename in cipher text
- * @length: The length of the cipher text name
- * @decrypted_name: The plaintext name
- *
- * Decodes and decrypts the filename.
- *
- * We assume that we have a properly initialized crypto context,
- * pointed to by crypt_stat->tfm.
- *
- * TODO: Implement filename decoding and decryption here, in place of
- * memcpy. We are keeping the framework around for now to (1)
- * facilitate testing of the components needed to implement filename
- * encryption and (2) to provide a code base from which other
- * developers in the community can easily implement this feature.
- *
- * Returns the length of decoded filename; negative if error
- */
-int
-ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
-                        const char *name, int length, char **decrypted_name)
-{
-       int error = 0;
-
-       (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
-       if (!(*decrypted_name)) {
-               error = -ENOMEM;
-               goto out;
-       }
-       /* TODO: Filename encryption is a scheduled feature for a
-        * future version of eCryptfs. This function is here only for
-        * the purpose of providing a framework for other developers
-        * to easily implement filename encryption. Hint: Replace this
-        * memcpy() with a call to decode and decrypt the
-        * filename, the set the length accordingly. */
-       memcpy((void *)(*decrypted_name), (void *)name, length);
-       (*decrypted_name)[length + 1] = '\0';   /* Only for convenience
-                                                * in printing out the
-                                                * string in debug
-                                                * messages */
-       error = length;
-out:
-       return error;
-}
-
-/**
  * ecryptfs_encrypt_filename - encrypt filename
  *
  * CBC-encrypts the filename. We do not want to encrypt the same
@@ -1760,7 +1694,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;
@@ -1797,7 +1731,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;
@@ -1807,7 +1741,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;
 }
@@ -1833,7 +1767,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;
        }
@@ -1858,7 +1792,7 @@ 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 "
+               printk(KERN_ERR "Error attempting to set key of size [%zd] for "
                       "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
                rc = -EINVAL;
                goto out;
@@ -2012,7 +1946,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 */
@@ -2093,16 +2027,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;
        }
@@ -2136,7 +2084,7 @@ int ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
        }
        (*dst_size) = dst_byte_offset;
 out:
-       return rc;
+       return;
 }
 
 /**
@@ -2174,7 +2122,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;
@@ -2208,7 +2156,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);
@@ -2273,51 +2221,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,