summaryrefslogtreecommitdiffstats
path: root/fs/crypto
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2018-01-05 13:45:01 -0500
committerTheodore Ts'o <tytso@mit.edu>2018-01-11 22:06:19 -0500
commit76e81d6d50481144824237e6843122824b0a55c0 (patch)
treea0db01898ef78dce8778c83b3a729861d96e089a /fs/crypto
parenta575784c6c13b8f1bae05fbba873e326ec73e289 (diff)
fscrypt: new helper functions for ->symlink()
Currently, filesystems supporting fscrypt need to implement some tricky logic when creating encrypted symlinks, including handling a peculiar on-disk format (struct fscrypt_symlink_data) and correctly calculating the size of the encrypted symlink. Introduce helper functions to make things a bit easier: - fscrypt_prepare_symlink() computes and validates the size the symlink target will require on-disk. - fscrypt_encrypt_symlink() creates the encrypted target if needed. The new helpers actually fix some subtle bugs. First, when checking whether the symlink target was too long, filesystems didn't account for the fact that the NUL padding is meant to be truncated if it would cause the maximum length to be exceeded, as is done for filenames in directories. Consequently users would receive ENAMETOOLONG when creating symlinks close to what is supposed to be the maximum length. For example, with EXT4 with a 4K block size, the maximum symlink target length in an encrypted directory is supposed to be 4093 bytes (in comparison to 4095 in an unencrypted directory), but in FS_POLICY_FLAGS_PAD_32-mode only up to 4064 bytes were accepted. Second, symlink targets of "." and ".." were not being encrypted, even though they should be, as these names are special in *directory entries* but not in symlink targets. Fortunately, we can fix this simply by starting to encrypt them, as old kernels already accept them in encrypted form. Third, the output string length the filesystems were providing when doing the actual encryption was incorrect, as it was forgotten to exclude 'sizeof(struct fscrypt_symlink_data)'. Fortunately though, this bug didn't make a difference. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Diffstat (limited to 'fs/crypto')
-rw-r--r--fs/crypto/fname.c8
-rw-r--r--fs/crypto/fscrypt_private.h4
-rw-r--r--fs/crypto/hooks.c90
3 files changed, 99 insertions, 3 deletions
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c
index 52d4dbe1e8e7..62f13d533439 100644
--- a/fs/crypto/fname.c
+++ b/fs/crypto/fname.c
@@ -34,8 +34,8 @@ static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
34 * 34 *
35 * Return: 0 on success, -errno on failure 35 * Return: 0 on success, -errno on failure
36 */ 36 */
37static int fname_encrypt(struct inode *inode, 37int fname_encrypt(struct inode *inode,
38 const struct qstr *iname, struct fscrypt_str *oname) 38 const struct qstr *iname, struct fscrypt_str *oname)
39{ 39{
40 struct skcipher_request *req = NULL; 40 struct skcipher_request *req = NULL;
41 DECLARE_CRYPTO_WAIT(wait); 41 DECLARE_CRYPTO_WAIT(wait);
@@ -56,9 +56,11 @@ static int fname_encrypt(struct inode *inode,
56 * Copy the filename to the output buffer for encrypting in-place and 56 * Copy the filename to the output buffer for encrypting in-place and
57 * pad it with the needed number of NUL bytes. 57 * pad it with the needed number of NUL bytes.
58 */ 58 */
59 if (WARN_ON(oname->len < iname->len))
60 return -ENOBUFS;
59 cryptlen = max_t(unsigned int, iname->len, FS_CRYPTO_BLOCK_SIZE); 61 cryptlen = max_t(unsigned int, iname->len, FS_CRYPTO_BLOCK_SIZE);
60 cryptlen = round_up(cryptlen, padding); 62 cryptlen = round_up(cryptlen, padding);
61 cryptlen = min(cryptlen, lim); 63 cryptlen = min3(cryptlen, lim, oname->len);
62 memcpy(oname->name, iname->name, iname->len); 64 memcpy(oname->name, iname->name, iname->len);
63 memset(oname->name + iname->len, 0, cryptlen - iname->len); 65 memset(oname->name + iname->len, 0, cryptlen - iname->len);
64 66
diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index 2b848e7c92f0..6995bca5006b 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -98,6 +98,10 @@ extern int fscrypt_do_page_crypto(const struct inode *inode,
98extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx, 98extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx,
99 gfp_t gfp_flags); 99 gfp_t gfp_flags);
100 100
101/* fname.c */
102extern int fname_encrypt(struct inode *inode,
103 const struct qstr *iname, struct fscrypt_str *oname);
104
101/* keyinfo.c */ 105/* keyinfo.c */
102extern void __exit fscrypt_essiv_cleanup(void); 106extern void __exit fscrypt_essiv_cleanup(void);
103 107
diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c
index 9f5fb2eb9cf7..4b83e4af2e41 100644
--- a/fs/crypto/hooks.c
+++ b/fs/crypto/hooks.c
@@ -110,3 +110,93 @@ int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry)
110 return 0; 110 return 0;
111} 111}
112EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup); 112EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
113
114int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
115 unsigned int max_len,
116 struct fscrypt_str *disk_link)
117{
118 int err;
119
120 /*
121 * To calculate the size of the encrypted symlink target we need to know
122 * the amount of NUL padding, which is determined by the flags set in
123 * the encryption policy which will be inherited from the directory.
124 * The easiest way to get access to this is to just load the directory's
125 * fscrypt_info, since we'll need it to create the dir_entry anyway.
126 *
127 * Note: in test_dummy_encryption mode, @dir may be unencrypted.
128 */
129 err = fscrypt_get_encryption_info(dir);
130 if (err)
131 return err;
132 if (!fscrypt_has_encryption_key(dir))
133 return -ENOKEY;
134
135 /*
136 * Calculate the size of the encrypted symlink and verify it won't
137 * exceed max_len. Note that for historical reasons, encrypted symlink
138 * targets are prefixed with the ciphertext length, despite this
139 * actually being redundant with i_size. This decreases by 2 bytes the
140 * longest symlink target we can accept.
141 *
142 * We could recover 1 byte by not counting a null terminator, but
143 * counting it (even though it is meaningless for ciphertext) is simpler
144 * for now since filesystems will assume it is there and subtract it.
145 */
146 if (sizeof(struct fscrypt_symlink_data) + len > max_len)
147 return -ENAMETOOLONG;
148 disk_link->len = min_t(unsigned int,
149 sizeof(struct fscrypt_symlink_data) +
150 fscrypt_fname_encrypted_size(dir, len),
151 max_len);
152 disk_link->name = NULL;
153 return 0;
154}
155EXPORT_SYMBOL_GPL(__fscrypt_prepare_symlink);
156
157int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
158 unsigned int len, struct fscrypt_str *disk_link)
159{
160 int err;
161 struct qstr iname = { .name = target, .len = len };
162 struct fscrypt_symlink_data *sd;
163 unsigned int ciphertext_len;
164 struct fscrypt_str oname;
165
166 err = fscrypt_require_key(inode);
167 if (err)
168 return err;
169
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;
177 }
178 ciphertext_len = disk_link->len - sizeof(*sd);
179 sd->len = cpu_to_le16(ciphertext_len);
180
181 oname.name = sd->encrypted_path;
182 oname.len = ciphertext_len;
183 err = fname_encrypt(inode, &iname, &oname);
184 if (err) {
185 if (!disk_link->name)
186 kfree(sd);
187 return err;
188 }
189 BUG_ON(oname.len != ciphertext_len);
190
191 /*
192 * Null-terminating the ciphertext doesn't make sense, but we still
193 * count the null terminator in the length, so we might as well
194 * initialize it just in case the filesystem writes it out.
195 */
196 sd->encrypted_path[ciphertext_len] = '\0';
197
198 if (!disk_link->name)
199 disk_link->name = (unsigned char *)sd;
200 return 0;
201}
202EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);