Commit 82d0d3e7 authored by Theodore Ts'o's avatar Theodore Ts'o

ext4 crypto: clean up error handling in ext4_fname_setup_filename

Fix a potential memory leak where fname->crypto_buf.name wouldn't get
freed in some error paths, and also make the error handling easier to
understand/audit.
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
parent d87f6d78
...@@ -401,7 +401,7 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, ...@@ -401,7 +401,7 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
((iname->name[1] == '.') && (iname->len == 2))))) { ((iname->name[1] == '.') && (iname->len == 2))))) {
fname->disk_name.name = (unsigned char *) iname->name; fname->disk_name.name = (unsigned char *) iname->name;
fname->disk_name.len = iname->len; fname->disk_name.len = iname->len;
goto out; return 0;
} }
ret = ext4_get_encryption_info(dir); ret = ext4_get_encryption_info(dir);
if (ret) if (ret)
...@@ -411,19 +411,16 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, ...@@ -411,19 +411,16 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
ret = ext4_fname_crypto_alloc_buffer(dir, iname->len, ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
&fname->crypto_buf); &fname->crypto_buf);
if (ret < 0) if (ret < 0)
goto out; return ret;
ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf); ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
if (ret < 0) if (ret < 0)
goto out; goto errout;
fname->disk_name.name = fname->crypto_buf.name; fname->disk_name.name = fname->crypto_buf.name;
fname->disk_name.len = fname->crypto_buf.len; fname->disk_name.len = fname->crypto_buf.len;
ret = 0; return 0;
goto out;
}
if (!lookup) {
ret = -EACCES;
goto out;
} }
if (!lookup)
return -EACCES;
/* We don't have the key and we are doing a lookup; decode the /* We don't have the key and we are doing a lookup; decode the
* user-supplied name * user-supplied name
...@@ -431,19 +428,17 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, ...@@ -431,19 +428,17 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
if (iname->name[0] == '_') if (iname->name[0] == '_')
bigname = 1; bigname = 1;
if ((bigname && (iname->len != 33)) || if ((bigname && (iname->len != 33)) ||
(!bigname && (iname->len > 43))) { (!bigname && (iname->len > 43)))
ret = -ENOENT; return -ENOENT;
}
fname->crypto_buf.name = kmalloc(32, GFP_KERNEL); fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
if (fname->crypto_buf.name == NULL) { if (fname->crypto_buf.name == NULL)
ret = -ENOMEM; return -ENOMEM;
goto out;
}
ret = digest_decode(iname->name + bigname, iname->len - bigname, ret = digest_decode(iname->name + bigname, iname->len - bigname,
fname->crypto_buf.name); fname->crypto_buf.name);
if (ret < 0) { if (ret < 0) {
ret = -ENOENT; ret = -ENOENT;
goto out; goto errout;
} }
fname->crypto_buf.len = ret; fname->crypto_buf.len = ret;
if (bigname) { if (bigname) {
...@@ -453,8 +448,10 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, ...@@ -453,8 +448,10 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
fname->disk_name.name = fname->crypto_buf.name; fname->disk_name.name = fname->crypto_buf.name;
fname->disk_name.len = fname->crypto_buf.len; fname->disk_name.len = fname->crypto_buf.len;
} }
ret = 0; return 0;
out: errout:
kfree(fname->crypto_buf.name);
fname->crypto_buf.name = NULL;
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment