aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2018-01-11 23:30:08 -0500
committerTheodore Ts'o <tytso@mit.edu>2018-01-11 23:30:08 -0500
commit2cbadadcfdf0d8a538ce32ed12e18ef487773b07 (patch)
tree27e38f4642a9dd981e8cc7451b4f116cc678dea8
parent50c961de59ec841c1185c18457e6dab227f3bbf3 (diff)
fscrypt: define fscrypt_fname_alloc_buffer() to be for presented names
Previously fscrypt_fname_alloc_buffer() was used to allocate buffers for both presented (decrypted or encoded) and encrypted filenames. That was confusing, because it had to allocate the worst-case size for either, e.g. including NUL-padding even when it was meaningless. But now that fscrypt_setup_filename() no longer calls it, it is only used in the ->get_link() and ->readdir() paths, which specifically want a buffer for presented filenames. Therefore, switch the behavior over to allocating the buffer for presented filenames only. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
-rw-r--r--fs/crypto/fname.c29
-rw-r--r--include/linux/fscrypt_notsupp.h2
2 files changed, 15 insertions, 16 deletions
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c
index 65424b89a1d1..44ddd094b7c5 100644
--- a/fs/crypto/fname.c
+++ b/fs/crypto/fname.c
@@ -204,37 +204,36 @@ u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)
204EXPORT_SYMBOL(fscrypt_fname_encrypted_size); 204EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
205 205
206/** 206/**
207 * fscrypt_fname_crypto_alloc_obuff() - 207 * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames
208 * 208 *
209 * Allocates an output buffer that is sufficient for the crypto operation 209 * Allocate a buffer that is large enough to hold any decrypted or encoded
210 * specified by the context and the direction. 210 * filename (null-terminated), for the given maximum encrypted filename length.
211 *
212 * Return: 0 on success, -errno on failure
211 */ 213 */
212int fscrypt_fname_alloc_buffer(const struct inode *inode, 214int fscrypt_fname_alloc_buffer(const struct inode *inode,
213 u32 ilen, struct fscrypt_str *crypto_str) 215 u32 max_encrypted_len,
216 struct fscrypt_str *crypto_str)
214{ 217{
215 u32 olen = fscrypt_fname_encrypted_size(inode, ilen);
216 const u32 max_encoded_len = 218 const u32 max_encoded_len =
217 max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE), 219 max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
218 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name))); 220 1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
221 u32 max_presented_len;
219 222
220 crypto_str->len = olen; 223 max_presented_len = max(max_encoded_len, max_encrypted_len);
221 olen = max(olen, max_encoded_len);
222 224
223 /* 225 crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
224 * Allocated buffer can hold one more character to null-terminate the 226 if (!crypto_str->name)
225 * string
226 */
227 crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
228 if (!(crypto_str->name))
229 return -ENOMEM; 227 return -ENOMEM;
228 crypto_str->len = max_presented_len;
230 return 0; 229 return 0;
231} 230}
232EXPORT_SYMBOL(fscrypt_fname_alloc_buffer); 231EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
233 232
234/** 233/**
235 * fscrypt_fname_crypto_free_buffer() - 234 * fscrypt_fname_free_buffer - free the buffer for presented filenames
236 * 235 *
237 * Frees the buffer allocated for crypto operation. 236 * Free the buffer allocated by fscrypt_fname_alloc_buffer().
238 */ 237 */
239void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str) 238void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
240{ 239{
diff --git a/include/linux/fscrypt_notsupp.h b/include/linux/fscrypt_notsupp.h
index 0962f504aa91..c9592e307df5 100644
--- a/include/linux/fscrypt_notsupp.h
+++ b/include/linux/fscrypt_notsupp.h
@@ -140,7 +140,7 @@ static inline u32 fscrypt_fname_encrypted_size(const struct inode *inode,
140} 140}
141 141
142static inline int fscrypt_fname_alloc_buffer(const struct inode *inode, 142static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
143 u32 ilen, 143 u32 max_encrypted_len,
144 struct fscrypt_str *crypto_str) 144 struct fscrypt_str *crypto_str)
145{ 145{
146 return -EOPNOTSUPP; 146 return -EOPNOTSUPP;