[RFC 1/1 linux-next] fs/hfsplus: use kasprintf instead of kmalloc/strcpy.

From: Fabian Frederick
Date: Mon Mar 09 2015 - 15:12:07 EST


This patch removes prefixlen from all getxattr/setxattr callsites and
maximum attribute preallocation.

It's an RFC because it could bring a memory leak somewhere if attribute
is stored on the old preallocation basis and I don't know the code enough
to evaluate the risk.

Suggested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Fabian Frederick <fabf@xxxxxxxxx>
---
fs/hfsplus/xattr.c | 21 +++++++--------------
fs/hfsplus/xattr.h | 4 ++--
fs/hfsplus/xattr_security.c | 6 ++----
fs/hfsplus/xattr_trusted.c | 5 ++---
fs/hfsplus/xattr_user.c | 4 ++--
5 files changed, 15 insertions(+), 25 deletions(-)

diff --git a/fs/hfsplus/xattr.c b/fs/hfsplus/xattr.c
index 16f545d..99d84fa 100644
--- a/fs/hfsplus/xattr.c
+++ b/fs/hfsplus/xattr.c
@@ -426,7 +426,7 @@ static int copy_name(char *buffer, const char *xattr_name, int name_len)

int hfsplus_setxattr(struct dentry *dentry, const char *name,
const void *value, size_t size, int flags,
- const char *prefix, size_t prefixlen)
+ const char *prefix)
{
char *xattr_name;
int res;
@@ -434,12 +434,10 @@ int hfsplus_setxattr(struct dentry *dentry, const char *name,
if (!strcmp(name, ""))
return -EINVAL;

- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
- GFP_KERNEL);
+ xattr_name = kasprintf(GFP_KERNEL, "%s%s", prefix, name);
if (!xattr_name)
return -ENOMEM;
- strcpy(xattr_name, prefix);
- strcpy(xattr_name + prefixlen, name);
+
res = __hfsplus_setxattr(dentry->d_inode, xattr_name, value, size,
flags);
kfree(xattr_name);
@@ -584,7 +582,7 @@ failed_getxattr_init:

ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
void *value, size_t size,
- const char *prefix, size_t prefixlen)
+ const char *prefix)
{
int res;
char *xattr_name;
@@ -592,14 +590,10 @@ ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
if (!strcmp(name, ""))
return -EINVAL;

- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
- GFP_KERNEL);
+ xattr_name = kasprintf(GFP_KERNEL, "%s%s", prefix, name);
if (!xattr_name)
return -ENOMEM;

- strcpy(xattr_name, prefix);
- strcpy(xattr_name + prefixlen, name);
-
res = __hfsplus_getxattr(dentry->d_inode, xattr_name, value, size);
kfree(xattr_name);
return res;
@@ -863,7 +857,7 @@ static int hfsplus_osx_getxattr(struct dentry *dentry, const char *name,
return -EOPNOTSUPP;

return hfsplus_getxattr(dentry, name, buffer, size,
- XATTR_MAC_OSX_PREFIX, XATTR_MAC_OSX_PREFIX_LEN);
+ XATTR_MAC_OSX_PREFIX);
}

static int hfsplus_osx_setxattr(struct dentry *dentry, const char *name,
@@ -880,8 +874,7 @@ static int hfsplus_osx_setxattr(struct dentry *dentry, const char *name,
return -EOPNOTSUPP;

return hfsplus_setxattr(dentry, name, buffer, size, flags,
- XATTR_MAC_OSX_PREFIX,
- XATTR_MAC_OSX_PREFIX_LEN);
+ XATTR_MAC_OSX_PREFIX);
}

static size_t hfsplus_osx_listxattr(struct dentry *dentry, char *list,
diff --git a/fs/hfsplus/xattr.h b/fs/hfsplus/xattr.h
index f9b0955..fa637ab 100644
--- a/fs/hfsplus/xattr.h
+++ b/fs/hfsplus/xattr.h
@@ -23,14 +23,14 @@ int __hfsplus_setxattr(struct inode *inode, const char *name,

int hfsplus_setxattr(struct dentry *dentry, const char *name,
const void *value, size_t size, int flags,
- const char *prefix, size_t prefixlen);
+ const char *prefix);

ssize_t __hfsplus_getxattr(struct inode *inode, const char *name,
void *value, size_t size);

ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
void *value, size_t size,
- const char *prefix, size_t prefixlen);
+ const char *prefix);

ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size);

diff --git a/fs/hfsplus/xattr_security.c b/fs/hfsplus/xattr_security.c
index aacff00..d19d626 100644
--- a/fs/hfsplus/xattr_security.c
+++ b/fs/hfsplus/xattr_security.c
@@ -17,16 +17,14 @@ static int hfsplus_security_getxattr(struct dentry *dentry, const char *name,
void *buffer, size_t size, int type)
{
return hfsplus_getxattr(dentry, name, buffer, size,
- XATTR_SECURITY_PREFIX,
- XATTR_SECURITY_PREFIX_LEN);
+ XATTR_SECURITY_PREFIX);
}

static int hfsplus_security_setxattr(struct dentry *dentry, const char *name,
const void *buffer, size_t size, int flags, int type)
{
return hfsplus_setxattr(dentry, name, buffer, size, flags,
- XATTR_SECURITY_PREFIX,
- XATTR_SECURITY_PREFIX_LEN);
+ XATTR_SECURITY_PREFIX);
}

static size_t hfsplus_security_listxattr(struct dentry *dentry, char *list,
diff --git a/fs/hfsplus/xattr_trusted.c b/fs/hfsplus/xattr_trusted.c
index bcf6508..6aad192 100644
--- a/fs/hfsplus/xattr_trusted.c
+++ b/fs/hfsplus/xattr_trusted.c
@@ -15,15 +15,14 @@ static int hfsplus_trusted_getxattr(struct dentry *dentry, const char *name,
void *buffer, size_t size, int type)
{
return hfsplus_getxattr(dentry, name, buffer, size,
- XATTR_TRUSTED_PREFIX,
- XATTR_TRUSTED_PREFIX_LEN);
+ XATTR_TRUSTED_PREFIX);
}

static int hfsplus_trusted_setxattr(struct dentry *dentry, const char *name,
const void *buffer, size_t size, int flags, int type)
{
return hfsplus_setxattr(dentry, name, buffer, size, flags,
- XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
+ XATTR_TRUSTED_PREFIX);
}

static size_t hfsplus_trusted_listxattr(struct dentry *dentry, char *list,
diff --git a/fs/hfsplus/xattr_user.c b/fs/hfsplus/xattr_user.c
index 5aa0e6d..8c86183 100644
--- a/fs/hfsplus/xattr_user.c
+++ b/fs/hfsplus/xattr_user.c
@@ -16,14 +16,14 @@ static int hfsplus_user_getxattr(struct dentry *dentry, const char *name,
{

return hfsplus_getxattr(dentry, name, buffer, size,
- XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
+ XATTR_USER_PREFIX);
}

static int hfsplus_user_setxattr(struct dentry *dentry, const char *name,
const void *buffer, size_t size, int flags, int type)
{
return hfsplus_setxattr(dentry, name, buffer, size, flags,
- XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
+ XATTR_USER_PREFIX);
}

static size_t hfsplus_user_listxattr(struct dentry *dentry, char *list,
--
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/