Skip to content

Commit

Permalink
xfs: share xattr name and value buffers when logging xattr updates
Browse files Browse the repository at this point in the history
While running xfs/297 and generic/642, I noticed a crash in
xfs_attri_item_relog when it tries to copy the attr name to the new
xattri log item.  I think what happened here was that we called
->iop_commit on the old attri item (which nulls out the pointers) as
part of a log force at the same time that a chained attr operation was
ongoing.  The system was busy enough that at some later point, the defer
ops operation decided it was necessary to relog the attri log item, but
as we've detached the name buffer from the old attri log item, we can't
copy it to the new one, and kaboom.

I think there's a broader refcounting problem with LARP mode -- the
setxattr code can return to userspace before the CIL actually formats
and commits the log item, which results in a UAF bug.  Therefore, the
xattr log item needs to be able to retain a reference to the name and
value buffers until the log items have completely cleared the log.
Furthermore, each time we create an intent log item, we allocate new
memory and (re)copy the contents; sharing here would be very useful.

Solve the UAF and the unnecessary memory allocations by having the log
code create a single refcounted buffer to contain the name and value
contents.  This buffer can be passed from old to new during a relog
operation, and the logging code can (optionally) attach it to the
xfs_attr_item for reuse when LARP mode is enabled.

This also fixes a problem where the xfs_attri_log_item objects weren't
being freed back to the same cache where they came from.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Dave Chinner <david@fromorbit.com>
  • Loading branch information
Darrick J. Wong authored and Dave Chinner committed May 22, 2022
1 parent 22a68ba commit 4183e4f
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 134 deletions.
8 changes: 8 additions & 0 deletions fs/xfs/libxfs/xfs_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ enum xfs_delattr_state {
{ XFS_DAS_NODE_REMOVE_ATTR, "XFS_DAS_NODE_REMOVE_ATTR" }, \
{ XFS_DAS_DONE, "XFS_DAS_DONE" }

struct xfs_attri_log_nameval;

/*
* Context used for keeping track of delayed attribute operations
*/
Expand All @@ -517,6 +519,12 @@ struct xfs_attr_intent {

struct xfs_da_args *xattri_da_args;

/*
* Shared buffer containing the attr name and value so that the logging
* code can share large memory buffers between log items.
*/
struct xfs_attri_log_nameval *xattri_nameval;

/*
* Used by xfs_attr_set to hold a leaf buffer across a transaction roll
*/
Expand Down
59 changes: 47 additions & 12 deletions fs/xfs/libxfs/xfs_defer.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,35 +191,56 @@ static const struct xfs_defer_op_type *defer_op_types[] = {
[XFS_DEFER_OPS_TYPE_ATTR] = &xfs_attr_defer_type,
};

static bool
/*
* Ensure there's a log intent item associated with this deferred work item if
* the operation must be restarted on crash. Returns 1 if there's a log item;
* 0 if there isn't; or a negative errno.
*/
static int
xfs_defer_create_intent(
struct xfs_trans *tp,
struct xfs_defer_pending *dfp,
bool sort)
{
const struct xfs_defer_op_type *ops = defer_op_types[dfp->dfp_type];
struct xfs_log_item *lip;

if (!dfp->dfp_intent)
dfp->dfp_intent = ops->create_intent(tp, &dfp->dfp_work,
dfp->dfp_count, sort);
return dfp->dfp_intent != NULL;
if (dfp->dfp_intent)
return 1;

lip = ops->create_intent(tp, &dfp->dfp_work, dfp->dfp_count, sort);
if (!lip)
return 0;
if (IS_ERR(lip))
return PTR_ERR(lip);

dfp->dfp_intent = lip;
return 1;
}

/*
* For each pending item in the intake list, log its intent item and the
* associated extents, then add the entire intake list to the end of
* the pending list.
*
* Returns 1 if at least one log item was associated with the deferred work;
* 0 if there are no log items; or a negative errno.
*/
static bool
static int
xfs_defer_create_intents(
struct xfs_trans *tp)
{
struct xfs_defer_pending *dfp;
bool ret = false;
int ret = 0;

list_for_each_entry(dfp, &tp->t_dfops, dfp_list) {
int ret2;

trace_xfs_defer_create_intent(tp->t_mountp, dfp);
ret |= xfs_defer_create_intent(tp, dfp, true);
ret2 = xfs_defer_create_intent(tp, dfp, true);
if (ret2 < 0)
return ret2;
ret |= ret2;
}
return ret;
}
Expand Down Expand Up @@ -457,6 +478,8 @@ xfs_defer_finish_one(
dfp->dfp_count--;
error = ops->finish_item(tp, dfp->dfp_done, li, &state);
if (error == -EAGAIN) {
int ret;

/*
* Caller wants a fresh transaction; put the work item
* back on the list and log a new log intent item to
Expand All @@ -467,7 +490,9 @@ xfs_defer_finish_one(
dfp->dfp_count++;
dfp->dfp_done = NULL;
dfp->dfp_intent = NULL;
xfs_defer_create_intent(tp, dfp, false);
ret = xfs_defer_create_intent(tp, dfp, false);
if (ret < 0)
error = ret;
}

if (error)
Expand Down Expand Up @@ -514,10 +539,14 @@ xfs_defer_finish_noroll(
* of time that any one intent item can stick around in memory,
* pinning the log tail.
*/
bool has_intents = xfs_defer_create_intents(*tp);
int has_intents = xfs_defer_create_intents(*tp);

list_splice_init(&(*tp)->t_dfops, &dop_pending);

if (has_intents < 0) {
error = has_intents;
goto out_shutdown;
}
if (has_intents || dfp) {
error = xfs_defer_trans_roll(tp);
if (error)
Expand Down Expand Up @@ -676,13 +705,15 @@ xfs_defer_ops_capture(
if (list_empty(&tp->t_dfops))
return NULL;

error = xfs_defer_create_intents(tp);
if (error < 0)
return ERR_PTR(error);

/* Create an object to capture the defer ops. */
dfc = kmem_zalloc(sizeof(*dfc), KM_NOFS);
INIT_LIST_HEAD(&dfc->dfc_list);
INIT_LIST_HEAD(&dfc->dfc_dfops);

xfs_defer_create_intents(tp);

/* Move the dfops chain and transaction state to the capture struct. */
list_splice_init(&tp->t_dfops, &dfc->dfc_dfops);
dfc->dfc_tpflags = tp->t_flags & XFS_TRANS_LOWMODE;
Expand Down Expand Up @@ -759,6 +790,10 @@ xfs_defer_ops_capture_and_commit(

/* If we don't capture anything, commit transaction and exit. */
dfc = xfs_defer_ops_capture(tp);
if (IS_ERR(dfc)) {
xfs_trans_cancel(tp);
return PTR_ERR(dfc);
}
if (!dfc)
return xfs_trans_commit(tp);

Expand Down
Loading

0 comments on commit 4183e4f

Please sign in to comment.