hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/ecryptfs/crypto.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /**
23 * eCryptfs: Linux filesystem encryption layer
34 *
....@@ -6,21 +7,6 @@
67 * Copyright (C) 2004-2007 International Business Machines Corp.
78 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
89 * Michael C. Thompson <mcthomps@us.ibm.com>
9
- *
10
- * This program is free software; you can redistribute it and/or
11
- * modify it under the terms of the GNU General Public License as
12
- * published by the Free Software Foundation; either version 2 of the
13
- * License, or (at your option) any later version.
14
- *
15
- * This program is distributed in the hope that it will be useful, but
16
- * WITHOUT ANY WARRANTY; without even the implied warranty of
17
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
- * General Public License for more details.
19
- *
20
- * You should have received a copy of the GNU General Public License
21
- * along with this program; if not, write to the Free Software
22
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23
- * 02111-1307, USA.
2410 */
2511
2612 #include <crypto/hash.h>
....@@ -37,6 +23,7 @@
3723 #include <linux/slab.h>
3824 #include <asm/unaligned.h>
3925 #include <linux/kernel.h>
26
+#include <linux/xattr.h>
4027 #include "ecryptfs_kernel.h"
4128
4229 #define DECRYPT 0
....@@ -61,19 +48,6 @@
6148 }
6249 }
6350
64
-static int ecryptfs_hash_digest(struct crypto_shash *tfm,
65
- char *src, int len, char *dst)
66
-{
67
- SHASH_DESC_ON_STACK(desc, tfm);
68
- int err;
69
-
70
- desc->tfm = tfm;
71
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
72
- err = crypto_shash_digest(desc, src, len, dst);
73
- shash_desc_zero(desc);
74
- return err;
75
-}
76
-
7751 /**
7852 * ecryptfs_calculate_md5 - calculates the md5 of @src
7953 * @dst: Pointer to 16 bytes of allocated memory
....@@ -88,11 +62,8 @@
8862 struct ecryptfs_crypt_stat *crypt_stat,
8963 char *src, int len)
9064 {
91
- struct crypto_shash *tfm;
92
- int rc = 0;
65
+ int rc = crypto_shash_tfm_digest(crypt_stat->hash_tfm, src, len, dst);
9366
94
- tfm = crypt_stat->hash_tfm;
95
- rc = ecryptfs_hash_digest(tfm, src, len, dst);
9667 if (rc) {
9768 printk(KERN_ERR
9869 "%s: Error computing crypto hash; rc = [%d]\n",
....@@ -610,7 +581,8 @@
610581 full_alg_name);
611582 goto out_free;
612583 }
613
- crypto_skcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
584
+ crypto_skcipher_set_flags(crypt_stat->tfm,
585
+ CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
614586 rc = 0;
615587 out_free:
616588 kfree(full_alg_name);
....@@ -874,13 +846,10 @@
874846 * @crypt_stat: The cryptographic context
875847 * @page_virt: Source data to be parsed
876848 * @bytes_read: Updated with the number of bytes read
877
- *
878
- * Returns zero on success; non-zero if the flag set is invalid
879849 */
880
-static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
850
+static void ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
881851 char *page_virt, int *bytes_read)
882852 {
883
- int rc = 0;
884853 int i;
885854 u32 flags;
886855
....@@ -893,7 +862,6 @@
893862 /* Version is in top 8 bits of the 32-bit flag vector */
894863 crypt_stat->file_version = ((flags >> 24) & 0xFF);
895864 (*bytes_read) = 4;
896
- return rc;
897865 }
898866
899867 /**
....@@ -1131,9 +1099,21 @@
11311099 char *page_virt, size_t size)
11321100 {
11331101 int rc;
1102
+ struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
1103
+ struct inode *lower_inode = d_inode(lower_dentry);
11341104
1135
- rc = ecryptfs_setxattr(ecryptfs_dentry, ecryptfs_inode,
1136
- ECRYPTFS_XATTR_NAME, page_virt, size, 0);
1105
+ if (!(lower_inode->i_opflags & IOP_XATTR)) {
1106
+ rc = -EOPNOTSUPP;
1107
+ goto out;
1108
+ }
1109
+
1110
+ inode_lock(lower_inode);
1111
+ rc = __vfs_setxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
1112
+ page_virt, size, 0);
1113
+ if (!rc && ecryptfs_inode)
1114
+ fsstack_copy_attr_all(ecryptfs_inode, lower_inode);
1115
+ inode_unlock(lower_inode);
1116
+out:
11371117 return rc;
11381118 }
11391119
....@@ -1307,12 +1287,7 @@
13071287 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
13081288 ecryptfs_i_size_init(page_virt, d_inode(ecryptfs_dentry));
13091289 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1310
- rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1311
- &bytes_read);
1312
- if (rc) {
1313
- ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1314
- goto out;
1315
- }
1290
+ ecryptfs_process_flags(crypt_stat, (page_virt + offset), &bytes_read);
13161291 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
13171292 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
13181293 "file version [%d] is supported by this "
....@@ -1594,9 +1569,9 @@
15941569 "[%s]; rc = [%d]\n", full_alg_name, rc);
15951570 goto out;
15961571 }
1597
- crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1572
+ crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
15981573 if (*key_size == 0)
1599
- *key_size = crypto_skcipher_default_keysize(*key_tfm);
1574
+ *key_size = crypto_skcipher_max_keysize(*key_tfm);
16001575 get_random_bytes(dummy_key, *key_size);
16011576 rc = crypto_skcipher_setkey(*key_tfm, dummy_key, *key_size);
16021577 if (rc) {