[PATCH] Fix out-of-bounds access in xfs_bmapi_allocate by validating whichfork
From: MottiKumar Babu
Date: Sun Oct 27 2024 - 15:36:06 EST
This issue was reported by Coverity Scan.
Report:
CID 1633175 Out-of-bounds access - Access of memory not owned by this buffer may cause crashes or incorrect computations.
In xfs_bmapi_allocate: Out-of-bounds access to a buffer (CWE-119)
Signed-off-by: MottiKumar Babu <mottikumarbabu@xxxxxxxxx>
---
fs/xfs/libxfs/xfs_bmap.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c
index 36dd08d13293..6ff378d2d3d9 100644
--- a/fs/xfs/libxfs/xfs_bmap.c
+++ b/fs/xfs/libxfs/xfs_bmap.c
@@ -4169,6 +4169,10 @@ xfs_bmapi_allocate(
* is not on the busy list.
*/
bma->datatype = XFS_ALLOC_NOBUSY;
+ // Ensure whichfork is valid (0 or 1) before further checks
+ if (whichfork < 0 || whichfork > 1) {
+ return -EINVAL; // Invalid fork
+ }
if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
bma->datatype |= XFS_ALLOC_USERDATA;
if (bma->offset == 0)
--
2.43.0