Re: [PATCH] Fix out-of-bounds access in xfs_bmapi_allocate by validating whichfork

From: Christoph Hellwig
Date: Mon Oct 28 2024 - 04:46:16 EST


On Mon, Oct 28, 2024 at 01:05:27AM +0530, MottiKumar Babu wrote:
> 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

How is this supposed to happen?