mac80211: Check port authorization in the ieee80211_tx_dequeue() case
[linux/fpc-iii.git] / fs / crypto / hooks.c
blob926e5df20ec31b783a5ac77dded7d0bd58bdc27b
1 /*
2 * fs/crypto/hooks.c
4 * Encryption hooks for higher-level filesystem operations.
5 */
7 #include <linux/ratelimit.h>
8 #include "fscrypt_private.h"
10 /**
11 * fscrypt_file_open - prepare to open a possibly-encrypted regular file
12 * @inode: the inode being opened
13 * @filp: the struct file being set up
15 * Currently, an encrypted regular file can only be opened if its encryption key
16 * is available; access to the raw encrypted contents is not supported.
17 * Therefore, we first set up the inode's encryption key (if not already done)
18 * and return an error if it's unavailable.
20 * We also verify that if the parent directory (from the path via which the file
21 * is being opened) is encrypted, then the inode being opened uses the same
22 * encryption policy. This is needed as part of the enforcement that all files
23 * in an encrypted directory tree use the same encryption policy, as a
24 * protection against certain types of offline attacks. Note that this check is
25 * needed even when opening an *unencrypted* file, since it's forbidden to have
26 * an unencrypted file in an encrypted directory.
28 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
30 int fscrypt_file_open(struct inode *inode, struct file *filp)
32 int err;
33 struct dentry *dir;
35 err = fscrypt_require_key(inode);
36 if (err)
37 return err;
39 dir = dget_parent(file_dentry(filp));
40 if (IS_ENCRYPTED(d_inode(dir)) &&
41 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
42 fscrypt_warn(inode->i_sb,
43 "inconsistent encryption contexts: %lu/%lu",
44 d_inode(dir)->i_ino, inode->i_ino);
45 err = -EPERM;
47 dput(dir);
48 return err;
50 EXPORT_SYMBOL_GPL(fscrypt_file_open);
52 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir)
54 int err;
56 err = fscrypt_require_key(dir);
57 if (err)
58 return err;
60 if (!fscrypt_has_permitted_context(dir, inode))
61 return -EPERM;
63 return 0;
65 EXPORT_SYMBOL_GPL(__fscrypt_prepare_link);
67 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
68 struct inode *new_dir, struct dentry *new_dentry,
69 unsigned int flags)
71 int err;
73 err = fscrypt_require_key(old_dir);
74 if (err)
75 return err;
77 err = fscrypt_require_key(new_dir);
78 if (err)
79 return err;
81 if (old_dir != new_dir) {
82 if (IS_ENCRYPTED(new_dir) &&
83 !fscrypt_has_permitted_context(new_dir,
84 d_inode(old_dentry)))
85 return -EPERM;
87 if ((flags & RENAME_EXCHANGE) &&
88 IS_ENCRYPTED(old_dir) &&
89 !fscrypt_has_permitted_context(old_dir,
90 d_inode(new_dentry)))
91 return -EPERM;
93 return 0;
95 EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
97 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry)
99 int err = fscrypt_get_encryption_info(dir);
101 if (err)
102 return err;
104 if (fscrypt_has_encryption_key(dir)) {
105 spin_lock(&dentry->d_lock);
106 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
107 spin_unlock(&dentry->d_lock);
110 d_set_d_op(dentry, &fscrypt_d_ops);
111 return 0;
113 EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
115 int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
116 unsigned int max_len,
117 struct fscrypt_str *disk_link)
119 int err;
122 * To calculate the size of the encrypted symlink target we need to know
123 * the amount of NUL padding, which is determined by the flags set in
124 * the encryption policy which will be inherited from the directory.
125 * The easiest way to get access to this is to just load the directory's
126 * fscrypt_info, since we'll need it to create the dir_entry anyway.
128 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
130 err = fscrypt_get_encryption_info(dir);
131 if (err)
132 return err;
133 if (!fscrypt_has_encryption_key(dir))
134 return -ENOKEY;
137 * Calculate the size of the encrypted symlink and verify it won't
138 * exceed max_len. Note that for historical reasons, encrypted symlink
139 * targets are prefixed with the ciphertext length, despite this
140 * actually being redundant with i_size. This decreases by 2 bytes the
141 * longest symlink target we can accept.
143 * We could recover 1 byte by not counting a null terminator, but
144 * counting it (even though it is meaningless for ciphertext) is simpler
145 * for now since filesystems will assume it is there and subtract it.
147 if (!fscrypt_fname_encrypted_size(dir, len,
148 max_len - sizeof(struct fscrypt_symlink_data),
149 &disk_link->len))
150 return -ENAMETOOLONG;
151 disk_link->len += sizeof(struct fscrypt_symlink_data);
153 disk_link->name = NULL;
154 return 0;
156 EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
158 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
159 unsigned int len, struct fscrypt_str *disk_link)
161 int err;
162 struct qstr iname = QSTR_INIT(target, len);
163 struct fscrypt_symlink_data *sd;
164 unsigned int ciphertext_len;
166 err = fscrypt_require_key(inode);
167 if (err)
168 return err;
170 if (disk_link->name) {
171 /* filesystem-provided buffer */
172 sd = (struct fscrypt_symlink_data *)disk_link->name;
173 } else {
174 sd = kmalloc(disk_link->len, GFP_NOFS);
175 if (!sd)
176 return -ENOMEM;
178 ciphertext_len = disk_link->len - sizeof(*sd);
179 sd->len = cpu_to_le16(ciphertext_len);
181 err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
182 if (err) {
183 if (!disk_link->name)
184 kfree(sd);
185 return err;
188 * Null-terminating the ciphertext doesn't make sense, but we still
189 * count the null terminator in the length, so we might as well
190 * initialize it just in case the filesystem writes it out.
192 sd->encrypted_path[ciphertext_len] = '\0';
194 if (!disk_link->name)
195 disk_link->name = (unsigned char *)sd;
196 return 0;
198 EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
201 * fscrypt_get_symlink - get the target of an encrypted symlink
202 * @inode: the symlink inode
203 * @caddr: the on-disk contents of the symlink
204 * @max_size: size of @caddr buffer
205 * @done: if successful, will be set up to free the returned target
207 * If the symlink's encryption key is available, we decrypt its target.
208 * Otherwise, we encode its target for presentation.
210 * This may sleep, so the filesystem must have dropped out of RCU mode already.
212 * Return: the presentable symlink target or an ERR_PTR()
214 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
215 unsigned int max_size,
216 struct delayed_call *done)
218 const struct fscrypt_symlink_data *sd;
219 struct fscrypt_str cstr, pstr;
220 int err;
222 /* This is for encrypted symlinks only */
223 if (WARN_ON(!IS_ENCRYPTED(inode)))
224 return ERR_PTR(-EINVAL);
227 * Try to set up the symlink's encryption key, but we can continue
228 * regardless of whether the key is available or not.
230 err = fscrypt_get_encryption_info(inode);
231 if (err)
232 return ERR_PTR(err);
235 * For historical reasons, encrypted symlink targets are prefixed with
236 * the ciphertext length, even though this is redundant with i_size.
239 if (max_size < sizeof(*sd))
240 return ERR_PTR(-EUCLEAN);
241 sd = caddr;
242 cstr.name = (unsigned char *)sd->encrypted_path;
243 cstr.len = le16_to_cpu(sd->len);
245 if (cstr.len == 0)
246 return ERR_PTR(-EUCLEAN);
248 if (cstr.len + sizeof(*sd) - 1 > max_size)
249 return ERR_PTR(-EUCLEAN);
251 err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
252 if (err)
253 return ERR_PTR(err);
255 err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
256 if (err)
257 goto err_kfree;
259 err = -EUCLEAN;
260 if (pstr.name[0] == '\0')
261 goto err_kfree;
263 pstr.name[pstr.len] = '\0';
264 set_delayed_call(done, kfree_link, pstr.name);
265 return pstr.name;
267 err_kfree:
268 kfree(pstr.name);
269 return ERR_PTR(err);
271 EXPORT_SYMBOL_GPL(fscrypt_get_symlink);