xref: /linux/fs/ext4/crypto.c (revision b82144c319f92784f3ac2ee8c989cf8e265c64c8)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/quotaops.h>
4 #include <linux/uuid.h>
5 
6 #include "ext4.h"
7 #include "xattr.h"
8 #include "ext4_jbd2.h"
9 
10 static void ext4_fname_from_fscrypt_name(struct ext4_filename *dst,
11 					 const struct fscrypt_name *src)
12 {
13 	memset(dst, 0, sizeof(*dst));
14 
15 	dst->usr_fname = src->usr_fname;
16 	dst->disk_name = src->disk_name;
17 	dst->hinfo.hash = src->hash;
18 	dst->hinfo.minor_hash = src->minor_hash;
19 	dst->crypto_buf = src->crypto_buf;
20 }
21 
22 int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
23 			      int lookup, struct ext4_filename *fname)
24 {
25 	struct fscrypt_name name;
26 	int err;
27 
28 	err = fscrypt_setup_filename(dir, iname, lookup, &name);
29 	if (err)
30 		return err;
31 
32 	ext4_fname_from_fscrypt_name(fname, &name);
33 
34 	err = ext4_fname_setup_ci_filename(dir, iname, fname);
35 	if (err)
36 		ext4_fname_free_filename(fname);
37 
38 	return err;
39 }
40 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fname_setup_filename);
41 
42 int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry,
43 			      struct ext4_filename *fname)
44 {
45 	struct fscrypt_name name;
46 	int err;
47 
48 	err = fscrypt_prepare_lookup(dir, dentry, &name);
49 	if (err)
50 		return err;
51 
52 	ext4_fname_from_fscrypt_name(fname, &name);
53 
54 	err = ext4_fname_setup_ci_filename(dir, &dentry->d_name, fname);
55 	if (err)
56 		ext4_fname_free_filename(fname);
57 	return err;
58 }
59 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fname_prepare_lookup);
60 
61 void ext4_fname_free_filename(struct ext4_filename *fname)
62 {
63 	struct fscrypt_name name;
64 
65 	name.crypto_buf = fname->crypto_buf;
66 	fscrypt_free_filename(&name);
67 
68 	fname->crypto_buf.name = NULL;
69 	fname->usr_fname = NULL;
70 	fname->disk_name.name = NULL;
71 
72 	ext4_fname_free_ci_filename(fname);
73 }
74 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fname_free_filename);
75 
76 static bool uuid_is_zero(__u8 u[16])
77 {
78 	int i;
79 
80 	for (i = 0; i < 16; i++)
81 		if (u[i])
82 			return false;
83 	return true;
84 }
85 
86 int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg)
87 {
88 	struct super_block *sb = file_inode(filp)->i_sb;
89 	struct ext4_sb_info *sbi = EXT4_SB(sb);
90 	int err, err2;
91 	handle_t *handle;
92 
93 	if (!ext4_has_feature_encrypt(sb))
94 		return -EOPNOTSUPP;
95 
96 	if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
97 		err = mnt_want_write_file(filp);
98 		if (err)
99 			return err;
100 		handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
101 		if (IS_ERR(handle)) {
102 			err = PTR_ERR(handle);
103 			goto pwsalt_err_exit;
104 		}
105 		err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
106 						    EXT4_JTR_NONE);
107 		if (err)
108 			goto pwsalt_err_journal;
109 		lock_buffer(sbi->s_sbh);
110 		generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
111 		ext4_superblock_csum_set(sb);
112 		unlock_buffer(sbi->s_sbh);
113 		err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
114 pwsalt_err_journal:
115 		err2 = ext4_journal_stop(handle);
116 		if (err2 && !err)
117 			err = err2;
118 pwsalt_err_exit:
119 		mnt_drop_write_file(filp);
120 		if (err)
121 			return err;
122 	}
123 
124 	if (copy_to_user(arg, sbi->s_es->s_encrypt_pw_salt, 16))
125 		return -EFAULT;
126 	return 0;
127 }
128 
129 static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
130 {
131 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
132 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
133 }
134 
135 static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
136 							void *fs_data)
137 {
138 	handle_t *handle = fs_data;
139 	int res, res2, credits, retries = 0;
140 
141 	/*
142 	 * Encrypting the root directory is not allowed because e2fsck expects
143 	 * lost+found to exist and be unencrypted, and encrypting the root
144 	 * directory would imply encrypting the lost+found directory as well as
145 	 * the filename "lost+found" itself.
146 	 */
147 	if (inode->i_ino == EXT4_ROOT_INO)
148 		return -EPERM;
149 
150 	/*
151 	 * For new encrypted inodes, S_DAX is never set in the first place.
152 	 *
153 	 * For existing inodes, this is called only on empty directories.  ext4
154 	 * never sets S_DAX on directories.
155 	 */
156 	if (WARN_ON_ONCE(IS_DAX(inode)))
157 		return -EINVAL;
158 
159 	if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
160 		return -EOPNOTSUPP;
161 
162 	/*
163 	 * Encryption is not supported when the block size is larger
164 	 * than the page size.  This is rejected at mount time by
165 	 * ext4_check_large_folio(), and this check here is for the case
166 	 * when the 'encrypt' feature is enabled on a mounted filesystem.
167 	 */
168 	if (inode->i_sb->s_blocksize > PAGE_SIZE)
169 		return -EOPNOTSUPP;
170 
171 	res = ext4_convert_inline_data(inode);
172 	if (res)
173 		return res;
174 
175 	/*
176 	 * If a journal handle was specified, then the encryption context is
177 	 * being set on a new inode via inheritance and is part of a larger
178 	 * transaction to create the inode.  Otherwise the encryption context is
179 	 * being set on an existing inode in its own transaction.  Only in the
180 	 * latter case should the "retry on ENOSPC" logic be used.
181 	 */
182 
183 	if (handle) {
184 		/*
185 		 * __ext4_new_inode() should have already set the encrypt flag
186 		 * on the inode and avoided enabling inline data.
187 		 */
188 		if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
189 			return -EINVAL;
190 		if (WARN_ON_ONCE(ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)))
191 			return -EINVAL;
192 		/*
193 		 * Since the inode is new it is ok to pass the
194 		 * XATTR_CREATE flag. This is necessary to match the
195 		 * remaining journal credits check in the set_handle
196 		 * function with the credits allocated for the new
197 		 * inode.
198 		 */
199 		return ext4_xattr_set_handle(handle, inode,
200 					     EXT4_XATTR_INDEX_ENCRYPTION,
201 					     EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
202 					     ctx, len, XATTR_CREATE);
203 	}
204 
205 	res = dquot_initialize(inode);
206 	if (res)
207 		return res;
208 retry:
209 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
210 				     &credits);
211 	if (res)
212 		return res;
213 
214 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
215 	if (IS_ERR(handle))
216 		return PTR_ERR(handle);
217 
218 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
219 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
220 				    ctx, len, 0);
221 	if (!res) {
222 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
223 		/* Update inode->i_flags to set S_ENCRYPTED. */
224 		ext4_set_inode_flags(inode, false);
225 		res = ext4_mark_inode_dirty(handle, inode);
226 		if (res)
227 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
228 	}
229 	res2 = ext4_journal_stop(handle);
230 
231 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
232 		goto retry;
233 	if (!res)
234 		res = res2;
235 	return res;
236 }
237 
238 static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
239 {
240 	return EXT4_SB(sb)->s_dummy_enc_policy.policy;
241 }
242 
243 static bool ext4_has_stable_inodes(struct super_block *sb)
244 {
245 	return ext4_has_feature_stable_inodes(sb);
246 }
247 
248 const struct fscrypt_operations ext4_cryptops = {
249 	.inode_info_offs	= (int)offsetof(struct ext4_inode_info, i_crypt_info) -
250 				  (int)offsetof(struct ext4_inode_info, vfs_inode),
251 	.is_block_based		= 1,
252 	.has_32bit_inodes	= 1,
253 	.supports_subblock_data_units = 1,
254 	.legacy_key_prefix	= "ext4:",
255 	.get_context		= ext4_get_context,
256 	.set_context		= ext4_set_context,
257 	.get_dummy_policy	= ext4_get_dummy_policy,
258 	.empty_dir		= ext4_empty_dir,
259 	.has_stable_inodes	= ext4_has_stable_inodes,
260 };
261 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_cryptops);
262