potential NULL pointer deref in XFS on failed mount
RFC PATCH 1/1 potential NULL pointer deref in XFS on failed mount
(got no reply on this when I originally send it on 20061031, so resending
now that a bit of time has passed. The patch still applies cleanly to Linus' git tree as of today.)
The Coverity checker spotted a potential problem in XFS.
The problem is that if, in xfs_mount(), this code triggers:
... if (!mp->m_logdev_targp) goto error0; ...
Then we'll end up calling xfs_unmountfs_close() with a NULL 'mp->m_logdev_targp'.
This in turn will result in a call to xfs_free_buftarg() with its 'btp' argument == NULL. xfs_free_buftarg() dereferences 'btp' leading to a NULL pointer dereference and crash.
I think this can happen, since the fatal call to xfs_free_buftarg() happens when 'm_logdev_targp != m_ddev_targp' and due to a check of 'm_ddev_targp' against NULL in xfs_mount() (and subsequent return if it is NULL) the two will never both be NULL when we hit the error0 label from the two lines cited above.
Comments welcome (please keep me on Cc: on replies).
Here's a proposed patch to fix this by testing 'btp' against NULL in xfs_free_buftarg().
---
fs/xfs/linux-2.6/xfs_buf.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
fs/xfs/linux-2.6/xfs_buf.c +3 -0
@@ -1450,6 +1450,9 @@ xfs_free_buftarg( xfs_buftarg_t *btp, int external) { +if (unlikely (!btp)) +return ; +xfs_flush_buftarg (btp,1 );if (external)xfs_blkdev_put (btp->bt_bdev);
Hey Jesper,
Rather, it can be done as below. Nothing to say that
your code wouldn't work. Just that catch it early, so that potential function call overhead to call xfs_free_buftarg can be avoided.
voidxfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
{
if (mp->m_logdev_targp && (mp->m_logdev_targp != mp->m_ddev_targp))
xfs_free_buftarg(mp->m_logdev_targp, 1);
if (mp->m_rtdev_targp)
xfs_free_buftarg(mp->m_rtdev_targp, 1);
xfs_free_buftarg(mp->m_ddev_targp, 0);
}Quoted 49 lines · Jesper Juhl
(got no reply on this when I originally send it on 20061031, so resending
now that a bit of time has passed. The patch still applies cleanly to Linus' git tree as of today.)The Coverity checker spotted a potential problem in XFS.
The problem is that if, in
xfs_mount(), this code triggers:... if (!mp->m_logdev_targp) goto error0; ...Then we'll end up calling
xfs_unmountfs_close()with a NULL 'mp->m_logdev_targp'. This in turn will result in a call toxfs_free_buftarg()with its 'btp' argument == NULL.xfs_free_buftarg()dereferences 'btp' leading to a NULL pointer dereference and crash.I think this can happen, since the fatal call to
xfs_free_buftarg()happens when 'm_logdev_targp!=m_ddev_targp' and due to a check of 'm_ddev_targp' against NULL inxfs_mount()(and subsequent return if it is NULL) the two will never both be NULL when we hit the error0 label from the two lines cited above.Comments welcome (please keep me on Cc: on replies).
Here's a proposed patch to fix this by testing 'btp' against NULL in
xfs_free_buftarg().Signed-off-by Jesper Juhl <[email protected]>---
fs/xfs/linux-2.6/xfs_buf.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)fs/xfs/linux-2.6/xfs_buf.c +3
-0@@ -1450,6 +1450,9 @@ xfs_free_buftarg( xfs_buftarg_t *btp, int external) { +if (unlikely (!btp)) +return ; +xfs_flush_buftarg (btp,1 );if (external)xfs_blkdev_put (btp->bt_bdev);
Quoted 5 lines · Shailendra Tripathi
Hey Jesper,
Rather, it can be done as below. Nothing to say that
your code wouldn't work. Just that catch it early, so that potential function call overhead to callxfs_free_buftargcan be avoided.
Hi Shailendra,
The reason I want to fix it in the freeing function is that many other functions in the kernel that free resources are safe to call with NULL pointers and this would make xfs_free_buftarg() follow that convention. This would perhaps also allow for some cleanups in other places that call the function since then there's no longer a need for explicit NULL checks any more (haven't checked if there's anything to gain there though).
I don't think the function call overhead matters much since this is in a case of a failed mount, so it should happen very rarely.
Quoted 10 lines
void
xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr){ if (mp->m_logdev_targp && (mp->m_logdev_targp != mp->m_ddev_targp)) xfs_free_buftarg(mp->m_logdev_targp, 1); if (mp->m_rtdev_targp) xfs_free_buftarg(mp->m_rtdev_targp, 1); xfs_free_buftarg(mp->m_ddev_targp, 0); }
Signature · 3 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html
Quoted 10 lines · Jesper Juhl
The reason I want to fix it in the freeing function is that many other functions in the kernel that free resources are safe to call with NULL pointers and this would make
xfs_free_buftarg()follow that convention. This would perhaps also allow for some cleanups in other places that call the function since then there's no longer a need for explicit NULL checks any more (haven't checked if there's anything to gain there though).
I don't think the function call overhead matters much since this is in a case of a failed mount, so it should happen very rarely.
I agree with you. However, cleanup functions should(/must?) check for NULL etc and
in this case it is already doing so for other cases. So, perhaps not required. Just a different viewpoint. Your choice.
Quoted 13 lines
Quoted 11 lines
void
xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr){ if (mp->m_logdev_targp && (mp->m_logdev_targp != mp->m_ddev_targp)) xfs_free_buftarg(mp->m_logdev_targp, 1); if (mp->m_rtdev_targp) xfs_free_buftarg(mp->m_rtdev_targp, 1); xfs_free_buftarg(mp->m_ddev_targp, 0); }
Quoted 14 lines · Jesper Juhl
(got no reply on this when I originally send it on 20061031, so resending
now that a bit of time has passed. The patch still applies cleanly to Linus' git tree as of today.)The Coverity checker spotted a potential problem in XFS.
The problem is that if, in
xfs_mount(), this code triggers:... if (!mp->m_logdev_targp) goto error0; ...
Quoted 5 lines
Then we'll end up calling
xfs_unmountfs_close()with a NULL 'mp->m_logdev_targp'.
This in turn will result in a call toxfs_free_buftarg()with its 'btp' argument == NULL.xfs_free_buftarg()dereferences 'btp' leading to a NULL pointer dereference and crash.
Interesting that coverity found that, but failed to find the other leaks in that function from exactly the same code and error case.....
Quoted 10 lines
I think this can happen, since the fatal call to
xfs_free_buftarg()happens when 'm_logdev_targp!=m_ddev_targp' and due to a check of 'm_ddev_targp' against NULL inxfs_mount()(and subsequent return if it is NULL) the two will never both be NULL when we hit the error0 label from the two lines cited above.Comments welcome (please keep me on Cc: on replies).
Here's a proposed patch to fix this by testing 'btp' against NULL in
xfs_free_buftarg().
Not the right fix - we should only be trying to free valid buftargs, which means xfs_unmountfs_close() is the correct place to fix this....
e.g:
- if (mp->m_logdev_targp != mp->m_ddev_targp)
+ if (mp->m_logdev_targp && (mp->m_logdev_targp != mp->m_ddev_targp))
As to the afore-mentioned leaks, if we fail to allocate a realtime buftarg, then we will leak a reference to both the rtdev and logdev, and if we fail to allocate an external log buftarg we'll leak a reference to the logdev. i.e., we fail to do one or both of:
xfs_blkdev_put(logdev); xfs_blkdev_put(rtdev);
To remove the bdev references we may have gained earlier. Normally, these references are released by xfs_free_buftarg(), but because we failed to allocate the buftarg, we can't drop the references via that method....
Cheers,
Dave.
Signature · 3 lines
Dave Chinner Principal Engineer SGI Australian Software Group
Quoted 31 lines · David Chinner
Quoted 14 lines · Jesper Juhl
(got no reply on this when I originally send it on 20061031, so resending
now that a bit of time has passed. The patch still applies cleanly to Linus' git tree as of today.)The Coverity checker spotted a potential problem in XFS.
The problem is that if, in
xfs_mount(), this code triggers:... if (!mp->m_logdev_targp) goto error0; ...Quoted 5 lines
Then we'll end up calling
xfs_unmountfs_close()with a NULL 'mp->m_logdev_targp'.
This in turn will result in a call toxfs_free_buftarg()with its 'btp' argument == NULL.xfs_free_buftarg()dereferences 'btp' leading to a NULL pointer dereference and crash.Interesting that coverity found that, but failed to find the other leaks in that function from exactly the same code and error case.....
Quoted 6 lines
I think this can happen, since the fatal call to
xfs_free_buftarg()happens when 'm_logdev_targp!=m_ddev_targp' and due to a check of 'm_ddev_targp' against NULL inxfs_mount()(and subsequent return if it is NULL) the two will never both be NULL when we hit the error0 label from the two lines cited above.
Quoted 9 lines
Quoted 4 lines
Comments welcome (please keep me on Cc: on replies).
Here's a proposed patch to fix this by testing 'btp' against NULL in
xfs_free_buftarg().Not the right fix - we should only be trying to free valid buftargs, which means
xfs_unmountfs_close()is the correct place to fix this....
Ok.
Quoted 10 lines
e.g:
(quoted diff) +1
-1- if (mp->m_logdev_targp != mp->m_ddev_targp) + if (mp->m_logdev_targp && (mp->m_logdev_targp != mp->m_ddev_targp))As to the afore-mentioned leaks, if we fail to allocate a realtime buftarg, then we will leak a reference to both the rtdev and logdev, and if we fail to allocate an external log buftarg we'll leak a reference to the logdev. i.e., we fail to do one or both of:
Quoted 8 lines
xfs_blkdev_put(logdev); xfs_blkdev_put(rtdev);To remove the bdev references we may have gained earlier. Normally, these references are released by
xfs_free_buftarg(), but because we failed to allocate the buftarg, we can't drop the references via that method....
Thanks a lot for commenting Dave.
I don't have time to look more at this tonight or tomorrow, but I'll try to cook up a proper fix covering all cases during the weekend.
Signature · 3 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html
Quoted 37 lines · Jesper Juhl
Quoted 36 lines · David Chinner
Quoted 14 lines · Jesper Juhl
(got no reply on this when I originally send it on 20061031, so resending
now that a bit of time has passed. The patch still applies cleanly to Linus' git tree as of today.)The Coverity checker spotted a potential problem in XFS.
The problem is that if, in
xfs_mount(), this code triggers:... if (!mp->m_logdev_targp) goto error0; ...Quoted 5 lines
Then we'll end up calling
xfs_unmountfs_close()with a NULL 'mp->m_logdev_targp'.
This in turn will result in a call toxfs_free_buftarg()with its 'btp' argument == NULL.xfs_free_buftarg()dereferences 'btp' leading to a NULL pointer dereference and crash.Interesting that coverity found that, but failed to find the other leaks in that function from exactly the same code and error case.....
Quoted 10 lines
I think this can happen, since the fatal call to
xfs_free_buftarg()happens when 'm_logdev_targp!=m_ddev_targp' and due to a check of 'm_ddev_targp' against NULL inxfs_mount()(and subsequent return if it is NULL) the two will never both be NULL when we hit the error0 label from the two lines cited above.Comments welcome (please keep me on Cc: on replies).
Here's a proposed patch to fix this by testing 'btp' against NULL in
xfs_free_buftarg().
Quoted 26 lines
Quoted 4 lines
Not the right fix - we should only be trying to free valid buftargs, which means
xfs_unmountfs_close()is the correct place to fix this....Ok.
Quoted 18 lines
e.g:
(quoted diff) +1
-1- if (mp->m_logdev_targp != mp->m_ddev_targp) + if (mp->m_logdev_targp && (mp->m_logdev_targp != mp->m_ddev_targp))As to the afore-mentioned leaks, if we fail to allocate a realtime buftarg, then we will leak a reference to both the rtdev and logdev, and if we fail to allocate an external log buftarg we'll leak a reference to the logdev. i.e., we fail to do one or both of:
xfs_blkdev_put(logdev); xfs_blkdev_put(rtdev);To remove the bdev references we may have gained earlier. Normally, these references are released by
xfs_free_buftarg(), but because we failed to allocate the buftarg, we can't drop the references via that method....
How about something like the attached patch ?
(sorry about the attachment, but I can't inline the patch from my current location - a whitespace damaged version is below though for easy review)
---
fs/xfs/xfs_mount.c | 2 +-fs/xfs/xfs_vfsops.c | 10 ++++++++--
2 files changed, 9 insertions(+), 3 deletions(-)
fs/xfs/xfs_mount.c +0 -0
@@ -1135,7 +1135,7 @@ #endif
voidxfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr) {
- if (mp->m_logdev_targp != mp->m_ddev_targp)
+ if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
xfs_free_buftarg(mp->m_logdev_targp, 1);
if (mp->m_rtdev_targp)
xfs_free_buftarg(mp->m_rtdev_targp, 1);fs/xfs/xfs_vfsops.c +8 -2
@@ -473,13 +473,19 @@ xfs_mount( }if (rtdev) { mp->m_rtdev_targp =xfs_alloc_buftarg (rtdev,1 ); -if (!mp->m_rtdev_targp) +if (!mp->m_rtdev_targp) { +xfs_blkdev_put (logdev); +xfs_blkdev_put (rtdev); goto error0; + } } mp->m_logdev_targp = (logdev && logdev != ddev) ?xfs_alloc_buftarg (logdev,1 ) : mp->m_ddev_targp; -if (!mp->m_logdev_targp) +if (!mp->m_logdev_targp) { +xfs_blkdev_put (logdev); +xfs_blkdev_put (rtdev); goto error0; + }/* * Setup flags based on mount(2) options and then the superblock
Signature · 3 lines
Jesper Juhl <[email protected]> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html
---
fs/xfs/xfs_mount.c | 2 +- fs/xfs/xfs_vfsops.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-)
fs/xfs/xfs_mount.c +1 -1
@@ -1135,7 +1135,7 @@ #endifvoid xfs_unmountfs_close (xfs_mount_t *mp,struct cred *cr) { -if (mp->m_logdev_targp != mp->m_ddev_targp) +if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)xfs_free_buftarg (mp->m_logdev_targp,1 );if (mp->m_rtdev_targp)xfs_free_buftarg (mp->m_rtdev_targp,1 );
fs/xfs/xfs_vfsops.c +8 -2
@@ -473,13 +473,19 @@ xfs_mount( }if (rtdev) { mp->m_rtdev_targp =xfs_alloc_buftarg (rtdev,1 ); -if (!mp->m_rtdev_targp) +if (!mp->m_rtdev_targp) { +xfs_blkdev_put (logdev); +xfs_blkdev_put (rtdev); goto error0; + } } mp->m_logdev_targp = (logdev && logdev != ddev) ?xfs_alloc_buftarg (logdev,1 ) : mp->m_ddev_targp; -if (!mp->m_logdev_targp) +if (!mp->m_logdev_targp) { +xfs_blkdev_put (logdev); +xfs_blkdev_put (rtdev); goto error0; + }/* * Setup flags based on mount(2) options and then the superblock