blob: 6a678d0e956a50db612c98a29ac21aecb4ce60c9 [file] [log] [blame]
Linus Torvalds32190f02017-11-14 11:35:15 -08001/* SPDX-License-Identifier: GPL-2.0 */
Eric Biggers46f47e42017-01-24 10:58:06 -08002/*
Dave Chinner734f0d22017-10-09 12:15:34 -07003 * fscrypt.h: declarations for per-file encryption
4 *
5 * Filesystems that implement per-file encryption include this header
6 * file with the __FS_HAS_ENCRYPTION set according to whether that filesystem
7 * is being built with encryption support or not.
Eric Biggers46f47e42017-01-24 10:58:06 -08008 *
9 * Copyright (C) 2015, Google, Inc.
10 *
11 * Written by Michael Halcrow, 2015.
12 * Modified by Jaegeuk Kim, 2015.
13 */
Dave Chinner734f0d22017-10-09 12:15:34 -070014#ifndef _LINUX_FSCRYPT_H
15#define _LINUX_FSCRYPT_H
Eric Biggers46f47e42017-01-24 10:58:06 -080016
Eric Biggers46f47e42017-01-24 10:58:06 -080017#include <linux/fs.h>
Eric Biggers46f47e42017-01-24 10:58:06 -080018
19#define FS_CRYPTO_BLOCK_SIZE 16
20
Eric Biggers542060c2018-01-05 10:44:55 -080021struct fscrypt_ctx;
Eric Biggers46f47e42017-01-24 10:58:06 -080022struct fscrypt_info;
23
Eric Biggers46f47e42017-01-24 10:58:06 -080024/**
25 * For encrypted symlinks, the ciphertext length is stored at the beginning
26 * of the string in little-endian format.
27 */
28struct fscrypt_symlink_data {
29 __le16 len;
30 char encrypted_path[1];
31} __packed;
32
Eric Biggers46f47e42017-01-24 10:58:06 -080033struct fscrypt_str {
34 unsigned char *name;
35 u32 len;
36};
37
38struct fscrypt_name {
39 const struct qstr *usr_fname;
40 struct fscrypt_str disk_name;
41 u32 hash;
42 u32 minor_hash;
43 struct fscrypt_str crypto_buf;
44};
45
46#define FSTR_INIT(n, l) { .name = n, .len = l }
47#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
48#define fname_name(p) ((p)->disk_name.name)
49#define fname_len(p) ((p)->disk_name.len)
50
Tahsin Erdoganaf652072017-07-06 00:01:59 -040051/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
52#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
53
Dave Chinner734f0d22017-10-09 12:15:34 -070054#if __FS_HAS_ENCRYPTION
Dave Chinner734f0d22017-10-09 12:15:34 -070055#include <linux/fscrypt_supp.h>
Eric Biggers4fd4b152018-01-05 10:44:53 -080056#else
Dave Chinner734f0d22017-10-09 12:15:34 -070057#include <linux/fscrypt_notsupp.h>
Eric Biggers4fd4b152018-01-05 10:44:53 -080058#endif
Dave Chinner734f0d22017-10-09 12:15:34 -070059
Eric Biggersd293c3e2017-10-09 12:15:39 -070060/**
61 * fscrypt_require_key - require an inode's encryption key
62 * @inode: the inode we need the key for
63 *
64 * If the inode is encrypted, set up its encryption key if not already done.
65 * Then require that the key be present and return -ENOKEY otherwise.
66 *
67 * No locks are needed, and the key will live as long as the struct inode --- so
68 * it won't go away from under you.
69 *
70 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
71 * if a problem occurred while setting up the encryption key.
72 */
73static inline int fscrypt_require_key(struct inode *inode)
74{
75 if (IS_ENCRYPTED(inode)) {
76 int err = fscrypt_get_encryption_info(inode);
77
78 if (err)
79 return err;
80 if (!fscrypt_has_encryption_key(inode))
81 return -ENOKEY;
82 }
83 return 0;
84}
Dave Chinner734f0d22017-10-09 12:15:34 -070085
Eric Biggers0ea87a92017-10-09 12:15:41 -070086/**
87 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
88 * @old_dentry: an existing dentry for the inode being linked
89 * @dir: the target directory
90 * @dentry: negative dentry for the target filename
91 *
92 * A new link can only be added to an encrypted directory if the directory's
93 * encryption key is available --- since otherwise we'd have no way to encrypt
94 * the filename. Therefore, we first set up the directory's encryption key (if
95 * not already done) and return an error if it's unavailable.
96 *
97 * We also verify that the link will not violate the constraint that all files
98 * in an encrypted directory tree use the same encryption policy.
99 *
100 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
101 * -EPERM if the link would result in an inconsistent encryption policy, or
102 * another -errno code.
103 */
104static inline int fscrypt_prepare_link(struct dentry *old_dentry,
105 struct inode *dir,
106 struct dentry *dentry)
107{
108 if (IS_ENCRYPTED(dir))
109 return __fscrypt_prepare_link(d_inode(old_dentry), dir);
110 return 0;
111}
112
Eric Biggers94b26f32017-10-09 12:15:42 -0700113/**
114 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
115 * @old_dir: source directory
116 * @old_dentry: dentry for source file
117 * @new_dir: target directory
118 * @new_dentry: dentry for target location (may be negative unless exchanging)
119 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
120 *
121 * Prepare for ->rename() where the source and/or target directories may be
122 * encrypted. A new link can only be added to an encrypted directory if the
123 * directory's encryption key is available --- since otherwise we'd have no way
124 * to encrypt the filename. A rename to an existing name, on the other hand,
125 * *is* cryptographically possible without the key. However, we take the more
126 * conservative approach and just forbid all no-key renames.
127 *
128 * We also verify that the rename will not violate the constraint that all files
129 * in an encrypted directory tree use the same encryption policy.
130 *
131 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EPERM if the
132 * rename would cause inconsistent encryption policies, or another -errno code.
133 */
134static inline int fscrypt_prepare_rename(struct inode *old_dir,
135 struct dentry *old_dentry,
136 struct inode *new_dir,
137 struct dentry *new_dentry,
138 unsigned int flags)
139{
140 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
141 return __fscrypt_prepare_rename(old_dir, old_dentry,
142 new_dir, new_dentry, flags);
143 return 0;
144}
145
Eric Biggers32c3cf02017-10-09 12:15:43 -0700146/**
147 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
148 * @dir: directory being searched
149 * @dentry: filename being looked up
150 * @flags: lookup flags
151 *
152 * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be
153 * done with or without the directory's encryption key; without the key,
154 * filenames are presented in encrypted form. Therefore, we'll try to set up
155 * the directory's encryption key, but even without it the lookup can continue.
156 *
157 * To allow invalidating stale dentries if the directory's encryption key is
158 * added later, we also install a custom ->d_revalidate() method and use the
159 * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a
160 * plaintext name (flag set) or a ciphertext name (flag cleared).
161 *
162 * Return: 0 on success, -errno if a problem occurred while setting up the
163 * encryption key
164 */
165static inline int fscrypt_prepare_lookup(struct inode *dir,
166 struct dentry *dentry,
167 unsigned int flags)
168{
169 if (IS_ENCRYPTED(dir))
170 return __fscrypt_prepare_lookup(dir, dentry);
171 return 0;
172}
173
Eric Biggers815dac32017-10-09 12:15:44 -0700174/**
175 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
176 * @dentry: dentry through which the inode is being changed
177 * @attr: attributes to change
178 *
179 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
180 * most attribute changes are allowed even without the encryption key. However,
181 * without the encryption key we do have to forbid truncates. This is needed
182 * because the size being truncated to may not be a multiple of the filesystem
183 * block size, and in that case we'd have to decrypt the final block, zero the
184 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
185 * filesystem block boundary, but it's simpler to just forbid all truncates ---
186 * and we already forbid all other contents modifications without the key.)
187 *
188 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
189 * if a problem occurred while setting up the encryption key.
190 */
191static inline int fscrypt_prepare_setattr(struct dentry *dentry,
192 struct iattr *attr)
193{
194 if (attr->ia_valid & ATTR_SIZE)
195 return fscrypt_require_key(d_inode(dentry));
196 return 0;
197}
198
Eric Biggers76e81d62018-01-05 10:45:01 -0800199/**
200 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
201 * @dir: directory in which the symlink is being created
202 * @target: plaintext symlink target
203 * @len: length of @target excluding null terminator
204 * @max_len: space the filesystem has available to store the symlink target
205 * @disk_link: (out) the on-disk symlink target being prepared
206 *
207 * This function computes the size the symlink target will require on-disk,
208 * stores it in @disk_link->len, and validates it against @max_len. An
209 * encrypted symlink may be longer than the original.
210 *
211 * Additionally, @disk_link->name is set to @target if the symlink will be
212 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
213 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
214 * on-disk target later. (The reason for the two-step process is that some
215 * filesystems need to know the size of the symlink target before creating the
216 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
217 *
218 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
219 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
220 * occurred while setting up the encryption key.
221 */
222static inline int fscrypt_prepare_symlink(struct inode *dir,
223 const char *target,
224 unsigned int len,
225 unsigned int max_len,
226 struct fscrypt_str *disk_link)
227{
228 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
229 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
230
231 disk_link->name = (unsigned char *)target;
232 disk_link->len = len + 1;
233 if (disk_link->len > max_len)
234 return -ENAMETOOLONG;
235 return 0;
236}
237
238/**
239 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
240 * @inode: symlink inode
241 * @target: plaintext symlink target
242 * @len: length of @target excluding null terminator
243 * @disk_link: (in/out) the on-disk symlink target being prepared
244 *
245 * If the symlink target needs to be encrypted, then this function encrypts it
246 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
247 * previously to compute @disk_link->len. If the filesystem did not allocate a
248 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
249 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
250 *
251 * Return: 0 on success, -errno on failure
252 */
253static inline int fscrypt_encrypt_symlink(struct inode *inode,
254 const char *target,
255 unsigned int len,
256 struct fscrypt_str *disk_link)
257{
258 if (IS_ENCRYPTED(inode))
259 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
260 return 0;
261}
262
Dave Chinner734f0d22017-10-09 12:15:34 -0700263#endif /* _LINUX_FSCRYPT_H */