linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][RFC] XFS: Fix mem leak and possible NULL deref in xfs_setattr_nonsize()
@ 2012-02-05 21:23 Jesper Juhl
  2012-02-06  6:23 ` Dave Chinner
  2012-02-06  9:11 ` Raghavendra D Prabhu
  0 siblings, 2 replies; 8+ messages in thread
From: Jesper Juhl @ 2012-02-05 21:23 UTC (permalink / raw)
  To: xfs; +Cc: xfs-masters, linux-kernel, Ben Myers, Alex Elder

In xfs_setattr_nonsize(), xfs_trans_alloc() gets its memory from
_xfs_trans_alloc() which gets it from kmem_zone_zalloc() which may
fail and return NULL. So this:

	tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);

may result in a NULL 'tp'.
If it does, then the call:

	error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);

with a NULL 'tp' will explode, since xfs_trans_reserve() dereferences
its first argument unconditionally.

And if the memory allocation for 'tp' goes well (and thus
xfs_trans_reserve() does not explode) then we may leak the memory
allocated to 'tp' if xfs_trans_reserve() returns error.

I believe this patch should fix both issues, but I'm not intimate with
the XFS code at all, so there can easily be something I overlooked or
something that should be done differently than what I did.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 fs/xfs/xfs_iops.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

 Note:
  Please review carefully before applying.
  Especially since I don't currently have any XFS filesystems to test
  this on, nor any clear idea of a good way to actually test this if I
  had. So this patch is compile tested only on my end.

diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c
index ab30253..194c9d7 100644
--- a/fs/xfs/xfs_iops.c
+++ b/fs/xfs/xfs_iops.c
@@ -575,9 +575,14 @@ xfs_setattr_nonsize(
 	}
 
 	tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
+	if (!tp)
+		goto out_dqrele;
+
 	error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
-	if (error)
+	if (error) {
+		xfs_trans_cancel(tp, 0);
 		goto out_dqrele;
+	}
 
 	xfs_ilock(ip, XFS_ILOCK_EXCL);
 
-- 
1.7.9


Please CC me on replies.

-- 
Jesper Juhl <jj@chaosbits.net>       http://www.chaosbits.net/
Don't top-post http://www.catb.org/jargon/html/T/top-post.html
Plain text mails only, please.


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

end of thread, other threads:[~2012-07-20 19:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-05 21:23 [PATCH][RFC] XFS: Fix mem leak and possible NULL deref in xfs_setattr_nonsize() Jesper Juhl
2012-02-06  6:23 ` Dave Chinner
2012-02-06 20:44   ` Jesper Juhl
2012-07-20 19:28     ` Jesper Juhl
2012-02-06  9:11 ` Raghavendra D Prabhu
2012-02-06 20:51   ` Jesper Juhl
2012-02-06 21:27     ` Dave Chinner
2012-02-07 22:41     ` Raghavendra D Prabhu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).