Re: [PATCH 1/1] xfs: check for possible overflow in xfs_ioc_trim

Dave Chinner
2012-07-30T21:49:53Z

On Mon, Jul 30, 2012 at 10:13:44AM +0200, Tomas Racek wrote:
> If range.start argument was between ULLONG_MAX - BBSIZE and ULLONG_MAX,
> BTOBB macro resulted in overflow which caused start to be set to 0.
> Now, invalid argument error is returned instead.
>
> Signed-off-by: Tomas Racek <tracek@redhat.com>
> ---
> fs/xfs/xfs_discard.c | 4 ++++
> 1 files changed, 4 insertions(+), 0 deletions(-)
>
> diff --git a/fs/xfs/xfs_discard.c b/fs/xfs/xfs_discard.c
> index f9c3fe3..0ef7dd4 100644
> --- a/fs/xfs/xfs_discard.c
> +++ b/fs/xfs/xfs_discard.c
> @@ -179,6 +179,10 @@ xfs_ioc_trim(
> * used by the fstrim application. In the end it really doesn't
> * matter as trimming blocks is an advisory interface.
> */
> +
> + if (range.start > ULLONG_MAX - BBSIZE)
> + return -XFS_ERROR(EINVAL);
> +

There's no point checking for overflow on the range start - what we
need to check is whether it is larger than the size of the
filesystem. We do that after the conversion of range.start to basic
blocks, so that check needs to be promoted to before this. i.e.

if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks))
return -XFS_ERROR(EINVAL);

> start = BTOBB(range.start);
> end = start + BTOBBT(range.len) - 1;
> minlen = BTOBB(max_t(u64, granularity, range.minlen));

And that will prevent the overflow in BTOBB() just as effectively...

Cheers,

Dave.
--
Dave Chinner
david@fromorbit.com