All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] CacheFiles: Fix memory leak in cachefiles_check_auxdata error paths
@ 2013-09-10 14:19 Josh Boyer
  0 siblings, 0 replies; 2+ messages in thread
From: Josh Boyer @ 2013-09-10 14:19 UTC (permalink / raw)
  To: David Howells; +Cc: linux-cachefs, linux-kernel

We kmalloc auxbuf but fail to kfree it if we get errors in various places.
Rework the error paths to avoid the resource leak.

Signed-off-by: Josh Boyer <jwboyer@fedoraproject.org>
---
 fs/cachefiles/xattr.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/fs/cachefiles/xattr.c b/fs/cachefiles/xattr.c
index 34c88b8..f929ab6 100644
--- a/fs/cachefiles/xattr.c
+++ b/fs/cachefiles/xattr.c
@@ -176,20 +176,28 @@ int cachefiles_check_auxdata(struct cachefiles_object *object)
 
 	auxbuf->len = vfs_getxattr(dentry, cachefiles_xattr_cache,
 				   &auxbuf->type, 512 + 1);
-	if (auxbuf->len < 1)
-		return -ESTALE;
+	if (auxbuf->len < 1) {
+		ret = -ESTALE;
+		goto error;
+	}
 
-	if (auxbuf->type != object->fscache.cookie->def->type)
-		return -ESTALE;
+	if (auxbuf->type != object->fscache.cookie->def->type) {
+		ret = -ESTALE;
+		goto error;
+	}
 
 	dlen = auxbuf->len - 1;
 	ret = fscache_check_aux(&object->fscache, &auxbuf->data, dlen);
 
-	kfree(auxbuf);
-	if (ret != FSCACHE_CHECKAUX_OKAY)
-		return -ESTALE;
+	if (ret != FSCACHE_CHECKAUX_OKAY) {
+		ret = -ESTALE;
+		goto error;
+	}
 
-	return 0;
+	ret = 0;
+error:
+	kfree(auxbuf);
+	return ret;
 }
 
 /*
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] CacheFiles: Fix memory leak in cachefiles_check_auxdata error paths
@ 2013-09-11 15:38 David Howells
  0 siblings, 0 replies; 2+ messages in thread
From: David Howells @ 2013-09-11 15:38 UTC (permalink / raw)
  To: milosz; +Cc: ceph-devel, linux-cachefs, linux-kernel, Hongyi Jia, Josh Boyer

From: Josh Boyer <jwboyer@redhat.com>

In cachefiles_check_auxdata(), we allocate auxbuf but fail to free it if we get
determine there's an error or that the data is stale.

Further, assigning the output of vfs_getxattr() to auxbuf->len gives problems
with checking for errors as auxbuf->len is a u16.  We don't actually need to
set auxbuf->len, so keep the length in a variable for now.  We shouldn't need
to check the upper limit of the buffer as an overflow there should be
indicated by -ERANGE.

Whilst we're at it, fscache_check_aux() returns an enum value, not an int, so
assign it to an appropriately typed variable rather than to ret.

Signed-off-by: Josh Boyer <jwboyer@fedoraproject.org>
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Hongyi Jia <jiayisuse@gmail.com>
cc: Milosz Tanski <milosz@adfin.com>
---

 fs/cachefiles/xattr.c |   29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/fs/cachefiles/xattr.c b/fs/cachefiles/xattr.c
index 34c88b8..12b0eef 100644
--- a/fs/cachefiles/xattr.c
+++ b/fs/cachefiles/xattr.c
@@ -162,8 +162,9 @@ int cachefiles_update_object_xattr(struct cachefiles_object *object,
 int cachefiles_check_auxdata(struct cachefiles_object *object)
 {
 	struct cachefiles_xattr *auxbuf;
+	enum fscache_checkaux validity;
 	struct dentry *dentry = object->dentry;
-	unsigned int dlen;
+	ssize_t xlen;
 	int ret;
 
 	ASSERT(dentry);
@@ -174,22 +175,22 @@ int cachefiles_check_auxdata(struct cachefiles_object *object)
 	if (!auxbuf)
 		return -ENOMEM;
 
-	auxbuf->len = vfs_getxattr(dentry, cachefiles_xattr_cache,
-				   &auxbuf->type, 512 + 1);
-	if (auxbuf->len < 1)
-		return -ESTALE;
-
-	if (auxbuf->type != object->fscache.cookie->def->type)
-		return -ESTALE;
+	xlen = vfs_getxattr(dentry, cachefiles_xattr_cache,
+			    &auxbuf->type, 512 + 1);
+	ret = -ESTALE;
+	if (xlen < 1 ||
+	    auxbuf->type != object->fscache.cookie->def->type)
+		goto error;
 
-	dlen = auxbuf->len - 1;
-	ret = fscache_check_aux(&object->fscache, &auxbuf->data, dlen);
+	xlen--;
+	validity = fscache_check_aux(&object->fscache, &auxbuf->data, xlen);
+	if (validity != FSCACHE_CHECKAUX_OKAY)
+		goto error;
 
+	ret = 0;
+error:
 	kfree(auxbuf);
-	if (ret != FSCACHE_CHECKAUX_OKAY)
-		return -ESTALE;
-
-	return 0;
+	return ret;
 }
 
 /*


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-09-11 15:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-10 14:19 [PATCH] CacheFiles: Fix memory leak in cachefiles_check_auxdata error paths Josh Boyer
2013-09-11 15:38 David Howells

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.