All of lore.kernel.org
 help / color / mirror / Atom feed
From: Allison Henderson <allison.henderson@oracle.com>
To: "djwong@kernel.org" <djwong@kernel.org>
Cc: "david@fromorbit.com" <david@fromorbit.com>,
	"linux-xfs@vger.kernel.org" <linux-xfs@vger.kernel.org>
Subject: Re: [PATCH 2/6] xfs: fix memcpy fortify errors in BUI log format copying
Date: Tue, 25 Oct 2022 20:47:36 +0000	[thread overview]
Message-ID: <754869a8f39d7b54380c3cc895ce37612c147ec7.camel@oracle.com> (raw)
In-Reply-To: <166664716290.2688790.11246233896801948595.stgit@magnolia>

On Mon, 2022-10-24 at 14:32 -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <djwong@kernel.org>
> 
> Starting in 6.1, CONFIG_FORTIFY_SOURCE checks the length parameter of
> memcpy.  Unfortunately, it doesn't handle flex arrays correctly:
> 
> ------------[ cut here ]------------
> memcpy: detected field-spanning write (size 48) of single field
> "dst_bui_fmt" at fs/xfs/xfs_bmap_item.c:628 (size 16)
> 
> Fix this by refactoring the xfs_bui_copy_format function to handle
> the
> copying of the head and the flex array members separately.  While
> we're
> at it, fix a minor validation deficiency in the recovery function.
> 
> Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Ok, makes sense:
Reviewed-by: Allison Henderson <allison.henderson@oracle.com>
> ---
>  fs/xfs/xfs_bmap_item.c |   46 ++++++++++++++++++++++----------------
> --------
>  fs/xfs/xfs_ondisk.h    |    5 +++++
>  2 files changed, 27 insertions(+), 24 deletions(-)
> 
> 
> diff --git a/fs/xfs/xfs_bmap_item.c b/fs/xfs/xfs_bmap_item.c
> index 51f66e982484..a1da6205252b 100644
> --- a/fs/xfs/xfs_bmap_item.c
> +++ b/fs/xfs/xfs_bmap_item.c
> @@ -608,28 +608,18 @@ static const struct xfs_item_ops
> xfs_bui_item_ops = {
>         .iop_relog      = xfs_bui_item_relog,
>  };
>  
> -/*
> - * Copy an BUI format buffer from the given buf, and into the
> destination
> - * BUI format structure.  The BUI/BUD items were designed not to
> need any
> - * special alignment handling.
> - */
> -static int
> +static inline void
>  xfs_bui_copy_format(
> -       struct xfs_log_iovec            *buf,
> -       struct xfs_bui_log_format       *dst_bui_fmt)
> +       struct xfs_bui_log_format       *dst,
> +       const struct xfs_bui_log_format *src)
>  {
> -       struct xfs_bui_log_format       *src_bui_fmt;
> -       uint                            len;
> +       unsigned int                    i;
>  
> -       src_bui_fmt = buf->i_addr;
> -       len = xfs_bui_log_format_sizeof(src_bui_fmt->bui_nextents);
> +       memcpy(dst, src, offsetof(struct xfs_bui_log_format,
> bui_extents));
>  
> -       if (buf->i_len == len) {
> -               memcpy(dst_bui_fmt, src_bui_fmt, len);
> -               return 0;
> -       }
> -       XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
> -       return -EFSCORRUPTED;
> +       for (i = 0; i < src->bui_nextents; i++)
> +               memcpy(&dst->bui_extents[i], &src->bui_extents[i],
> +                               sizeof(struct xfs_map_extent));
>  }
>  
>  /*
> @@ -646,23 +636,31 @@ xlog_recover_bui_commit_pass2(
>         struct xlog_recover_item        *item,
>         xfs_lsn_t                       lsn)
>  {
> -       int                             error;
>         struct xfs_mount                *mp = log->l_mp;
>         struct xfs_bui_log_item         *buip;
>         struct xfs_bui_log_format       *bui_formatp;
> +       size_t                          len;
>  
>         bui_formatp = item->ri_buf[0].i_addr;
>  
> +       if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) {
> +               XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log-
> >l_mp);
> +               return -EFSCORRUPTED;
> +       }
> +
>         if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
>                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log-
> >l_mp);
>                 return -EFSCORRUPTED;
>         }
> +
> +       len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
> +       if (item->ri_buf[0].i_len != len) {
> +               XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log-
> >l_mp);
> +               return -EFSCORRUPTED;
> +       }
> +
>         buip = xfs_bui_init(mp);
> -       error = xfs_bui_copy_format(&item->ri_buf[0], &buip-
> >bui_format);
> -       if (error) {
> -               xfs_bui_item_free(buip);
> -               return error;
> -       }
> +       xfs_bui_copy_format(&buip->bui_format, bui_formatp);
>         atomic_set(&buip->bui_next_extent, bui_formatp-
> >bui_nextents);
>         /*
>          * Insert the intent into the AIL directly and drop one
> reference so
> diff --git a/fs/xfs/xfs_ondisk.h b/fs/xfs/xfs_ondisk.h
> index 758702b9495f..56917e236370 100644
> --- a/fs/xfs/xfs_ondisk.h
> +++ b/fs/xfs/xfs_ondisk.h
> @@ -134,6 +134,11 @@ xfs_check_ondisk_structs(void)
>         XFS_CHECK_STRUCT_SIZE(struct xfs_trans_header,          16);
>         XFS_CHECK_STRUCT_SIZE(struct xfs_attri_log_format,      40);
>         XFS_CHECK_STRUCT_SIZE(struct xfs_attrd_log_format,      16);
> +       XFS_CHECK_STRUCT_SIZE(struct xfs_bui_log_format,        16);
> +       XFS_CHECK_STRUCT_SIZE(struct xfs_bud_log_format,        16);
> +       XFS_CHECK_STRUCT_SIZE(struct xfs_map_extent,            32);
> +
> +       XFS_CHECK_OFFSET(struct xfs_bui_log_format,
> bui_extents,        16);
>  
>         /*
>          * The v5 superblock format extended several v4 header
> structures with
> 


  parent reply	other threads:[~2022-10-25 20:47 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-24 21:32 [PATCHSET 0/6] xfs: fix various problems with log intent item recovery Darrick J. Wong
2022-10-24 21:32 ` [PATCH 1/6] xfs: fix validation in attr log " Darrick J. Wong
2022-10-25 18:50   ` Kees Cook
2022-10-25 20:42   ` Allison Henderson
2022-10-25 21:19   ` Dave Chinner
2022-10-25 22:05     ` Darrick J. Wong
2022-10-24 21:32 ` [PATCH 2/6] xfs: fix memcpy fortify errors in BUI log format copying Darrick J. Wong
2022-10-25 18:52   ` Kees Cook
2022-10-25 20:47   ` Allison Henderson [this message]
2022-10-25 21:34   ` Dave Chinner
2022-10-24 21:32 ` [PATCH 3/6] xfs: fix memcpy fortify errors in CUI " Darrick J. Wong
2022-10-25 20:47   ` Allison Henderson
2022-10-25 21:36   ` Dave Chinner
2022-10-24 21:32 ` [PATCH 4/6] xfs: fix memcpy fortify errors in RUI " Darrick J. Wong
2022-10-25 20:49   ` Allison Henderson
2022-10-25 21:37   ` Dave Chinner
2022-10-24 21:32 ` [PATCH 5/6] xfs: fix memcpy fortify errors in EFI " Darrick J. Wong
2022-10-25 19:08   ` Kees Cook
2022-10-25 20:54   ` Allison Henderson
2022-10-25 21:17     ` Darrick J. Wong
2022-10-25 21:47   ` Dave Chinner
2022-10-24 21:33 ` [PATCH 6/6] xfs: refactor all the EFI/EFD log item sizeof logic Darrick J. Wong
2022-10-25 19:14   ` Kees Cook
2022-10-25 20:56   ` Allison Henderson
2022-10-25 22:05   ` Dave Chinner
2022-10-25 22:08     ` Darrick J. Wong
2022-10-25 22:22       ` Dave Chinner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=754869a8f39d7b54380c3cc895ce37612c147ec7.camel@oracle.com \
    --to=allison.henderson@oracle.com \
    --cc=david@fromorbit.com \
    --cc=djwong@kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.