Commit 4183e4f2 authored by Darrick J. Wong's avatar Darrick J. Wong Committed by Dave Chinner

xfs: share xattr name and value buffers when logging xattr updates

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: default avatarDarrick J. Wong <djwong@kernel.org>
Reviewed-by: default avatarDave Chinner <dchinner@redhat.com>
Signed-off-by: default avatarDave Chinner <david@fromorbit.com>
parent 22a68ba7
...@@ -502,6 +502,8 @@ enum xfs_delattr_state { ...@@ -502,6 +502,8 @@ enum xfs_delattr_state {
{ XFS_DAS_NODE_REMOVE_ATTR, "XFS_DAS_NODE_REMOVE_ATTR" }, \ { XFS_DAS_NODE_REMOVE_ATTR, "XFS_DAS_NODE_REMOVE_ATTR" }, \
{ XFS_DAS_DONE, "XFS_DAS_DONE" } { XFS_DAS_DONE, "XFS_DAS_DONE" }
struct xfs_attri_log_nameval;
/* /*
* Context used for keeping track of delayed attribute operations * Context used for keeping track of delayed attribute operations
*/ */
...@@ -517,6 +519,12 @@ struct xfs_attr_intent { ...@@ -517,6 +519,12 @@ struct xfs_attr_intent {
struct xfs_da_args *xattri_da_args; 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 * Used by xfs_attr_set to hold a leaf buffer across a transaction roll
*/ */
......
...@@ -191,35 +191,56 @@ static const struct xfs_defer_op_type *defer_op_types[] = { ...@@ -191,35 +191,56 @@ static const struct xfs_defer_op_type *defer_op_types[] = {
[XFS_DEFER_OPS_TYPE_ATTR] = &xfs_attr_defer_type, [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( xfs_defer_create_intent(
struct xfs_trans *tp, struct xfs_trans *tp,
struct xfs_defer_pending *dfp, struct xfs_defer_pending *dfp,
bool sort) bool sort)
{ {
const struct xfs_defer_op_type *ops = defer_op_types[dfp->dfp_type]; const struct xfs_defer_op_type *ops = defer_op_types[dfp->dfp_type];
struct xfs_log_item *lip;
if (!dfp->dfp_intent) if (dfp->dfp_intent)
dfp->dfp_intent = ops->create_intent(tp, &dfp->dfp_work, return 1;
dfp->dfp_count, sort);
return dfp->dfp_intent != NULL; 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 * 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 * associated extents, then add the entire intake list to the end of
* the pending list. * 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( xfs_defer_create_intents(
struct xfs_trans *tp) struct xfs_trans *tp)
{ {
struct xfs_defer_pending *dfp; struct xfs_defer_pending *dfp;
bool ret = false; int ret = 0;
list_for_each_entry(dfp, &tp->t_dfops, dfp_list) { list_for_each_entry(dfp, &tp->t_dfops, dfp_list) {
int ret2;
trace_xfs_defer_create_intent(tp->t_mountp, dfp); 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; return ret;
} }
...@@ -457,6 +478,8 @@ xfs_defer_finish_one( ...@@ -457,6 +478,8 @@ xfs_defer_finish_one(
dfp->dfp_count--; dfp->dfp_count--;
error = ops->finish_item(tp, dfp->dfp_done, li, &state); error = ops->finish_item(tp, dfp->dfp_done, li, &state);
if (error == -EAGAIN) { if (error == -EAGAIN) {
int ret;
/* /*
* Caller wants a fresh transaction; put the work item * Caller wants a fresh transaction; put the work item
* back on the list and log a new log intent item to * back on the list and log a new log intent item to
...@@ -467,7 +490,9 @@ xfs_defer_finish_one( ...@@ -467,7 +490,9 @@ xfs_defer_finish_one(
dfp->dfp_count++; dfp->dfp_count++;
dfp->dfp_done = NULL; dfp->dfp_done = NULL;
dfp->dfp_intent = 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) if (error)
...@@ -514,10 +539,14 @@ xfs_defer_finish_noroll( ...@@ -514,10 +539,14 @@ xfs_defer_finish_noroll(
* of time that any one intent item can stick around in memory, * of time that any one intent item can stick around in memory,
* pinning the log tail. * 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); list_splice_init(&(*tp)->t_dfops, &dop_pending);
if (has_intents < 0) {
error = has_intents;
goto out_shutdown;
}
if (has_intents || dfp) { if (has_intents || dfp) {
error = xfs_defer_trans_roll(tp); error = xfs_defer_trans_roll(tp);
if (error) if (error)
...@@ -676,13 +705,15 @@ xfs_defer_ops_capture( ...@@ -676,13 +705,15 @@ xfs_defer_ops_capture(
if (list_empty(&tp->t_dfops)) if (list_empty(&tp->t_dfops))
return NULL; return NULL;
error = xfs_defer_create_intents(tp);
if (error < 0)
return ERR_PTR(error);
/* Create an object to capture the defer ops. */ /* Create an object to capture the defer ops. */
dfc = kmem_zalloc(sizeof(*dfc), KM_NOFS); dfc = kmem_zalloc(sizeof(*dfc), KM_NOFS);
INIT_LIST_HEAD(&dfc->dfc_list); INIT_LIST_HEAD(&dfc->dfc_list);
INIT_LIST_HEAD(&dfc->dfc_dfops); INIT_LIST_HEAD(&dfc->dfc_dfops);
xfs_defer_create_intents(tp);
/* Move the dfops chain and transaction state to the capture struct. */ /* Move the dfops chain and transaction state to the capture struct. */
list_splice_init(&tp->t_dfops, &dfc->dfc_dfops); list_splice_init(&tp->t_dfops, &dfc->dfc_dfops);
dfc->dfc_tpflags = tp->t_flags & XFS_TRANS_LOWMODE; dfc->dfc_tpflags = tp->t_flags & XFS_TRANS_LOWMODE;
...@@ -759,6 +790,10 @@ xfs_defer_ops_capture_and_commit( ...@@ -759,6 +790,10 @@ xfs_defer_ops_capture_and_commit(
/* If we don't capture anything, commit transaction and exit. */ /* If we don't capture anything, commit transaction and exit. */
dfc = xfs_defer_ops_capture(tp); dfc = xfs_defer_ops_capture(tp);
if (IS_ERR(dfc)) {
xfs_trans_cancel(tp);
return PTR_ERR(dfc);
}
if (!dfc) if (!dfc)
return xfs_trans_commit(tp); return xfs_trans_commit(tp);
......
This diff is collapsed.
...@@ -11,6 +11,14 @@ ...@@ -11,6 +11,14 @@
struct xfs_mount; struct xfs_mount;
struct kmem_zone; struct kmem_zone;
struct xfs_attri_log_nameval {
struct xfs_log_iovec name;
struct xfs_log_iovec value;
refcount_t refcount;
/* name and value follow the end of this struct */
};
/* /*
* This is the "attr intention" log item. It is used to log the fact that some * This is the "attr intention" log item. It is used to log the fact that some
* extended attribute operations need to be processed. An operation is * extended attribute operations need to be processed. An operation is
...@@ -26,10 +34,7 @@ struct kmem_zone; ...@@ -26,10 +34,7 @@ struct kmem_zone;
struct xfs_attri_log_item { struct xfs_attri_log_item {
struct xfs_log_item attri_item; struct xfs_log_item attri_item;
atomic_t attri_refcount; atomic_t attri_refcount;
int attri_name_len; struct xfs_attri_log_nameval *attri_nameval;
int attri_value_len;
void *attri_name;
void *attri_value;
struct xfs_attri_log_format attri_format; struct xfs_attri_log_format attri_format;
}; };
......
...@@ -86,6 +86,13 @@ xlog_copy_iovec(struct xfs_log_vec *lv, struct xfs_log_iovec **vecp, ...@@ -86,6 +86,13 @@ xlog_copy_iovec(struct xfs_log_vec *lv, struct xfs_log_iovec **vecp,
return buf; return buf;
} }
static inline void *
xlog_copy_from_iovec(struct xfs_log_vec *lv, struct xfs_log_iovec **vecp,
const struct xfs_log_iovec *src)
{
return xlog_copy_iovec(lv, vecp, src->i_type, src->i_addr, src->i_len);
}
/* /*
* By comparing each component, we don't have to worry about extra * By comparing each component, we don't have to worry about extra
* endian issues in treating two 32 bit numbers as one 64 bit number * endian issues in treating two 32 bit numbers as one 64 bit number
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment