CVE-2015-0274 (C, published 2015-03-16T10:59Z)
Description: value: The XFS implementation in the Linux kernel before 3.15 improperly uses an old size value during remote attribute replacement, which allows local users to cause a denial of service (transaction overrun and data corruption) or possibly gain privileges by leveraging XFS filesystem access.
Severity: HIGH (CVSS v3 base score: )
Weakness: CWE-19 \u2014 Data Processing Errors
Weaknesses in this category are typically found in functionality that processes data. Data processing is the manipulation of input to retrieve or save information.
Repository: https://github.com/torvalds/linux
Files changed:
fs/xfs/xfs_attr.c
fs/xfs/xfs_attr_leaf.c
fs/xfs/xfs_attr_list.c
fs/xfs/xfs_attr_remote.c
fs/xfs/xfs_da_btree.h
Commit message: xfs: remote attribute overwrite causes transaction overrun

Commit e461fcb ("xfs: remote attribute lookups require the value
length") passes the remote attribute length in the xfs_da_args
structure on lookup so that CRC calculations and validity checking
can be performed correctly by related code. This, unfortunately has
the side effect of changing the args->valuelen parameter in cases
where it shouldn't.

That is, when we replace a remote attribute, the incoming
replacement stores the value and length in args->value and
args->valuelen, but then the lookup which finds the existing remote
attribute overwrites args->valuelen with the length of the remote
attribute being replaced. Hence when we go to create the new
attribute, we create it of the size of the existing remote
attribute, not the size it is supposed to be. When the new attribute
is much smaller than the old attribute, this results in a
transaction overrun and an ASSERT() failure on a debug kernel:

XFS: Assertion failed: tp->t_blk_res_used <= tp->t_blk_res, file: fs/xfs/xfs_trans.c, line: 331

Fix this by keeping the remote attribute value length separate to
the attribute value length in the xfs_da_args structure. The enables
us to pass the length of the remote attribute to be removed without
overwriting the new attribute's length.

Also, ensure that when we save remote block contexts for a later
rename we zero the original state variables so that we don't confuse
the state of the attribute to be removes with the state of the new
attribute that we just added. [Spotted by Brain Foster.]

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Dave Chinner <david@fromorbit.com>


Vulnerable code:
		uint	dblocks = XFS_B_TO_FSB(mp, valuelen);
			args->valuelen = be32_to_cpu(name_rmt->valuelen);
							args->valuelen);
		valuelen = be32_to_cpu(name_rmt->valuelen);
						       valuelen);
			args->valuelen = valuelen;
		if (args->valuelen < valuelen) {
			args->valuelen = valuelen;
		args->valuelen = valuelen;
		name_rmt->valuelen = cpu_to_be32(args->valuelen);
		name_rmt->valuelen = cpu_to_be32(args->valuelen);
	int			valuelen = args->valuelen;
	blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
	valuelen = args->valuelen;

Fixed code:
		uint	dblocks = xfs_attr3_rmt_blocks(mp, valuelen);
		/* save the attribute state for later removal*/
		args->rmtvaluelen2 = args->rmtvaluelen;

		/*
		 * clear the remote attr state now that it is saved so that the
		 * values reflect the state of the attribute we are about to
		 * add, not the attribute we just found and will remove later.
		 */
		args->rmtblkno = 0;
		args->rmtblkcnt = 0;
		args->rmtvaluelen = 0;
		args->rmtvaluelen = args->rmtvaluelen2;
		/* save the attribute state for later removal*/
		args->rmtvaluelen2 = args->rmtvaluelen;

		/*
		 * clear the remote attr state now that it is saved so that the
		 * values reflect the state of the attribute we are about to
		 * add, not the attribute we just found and will remove later.
		 */
		args->rmtvaluelen = 0;
		args->rmtvaluelen = args->rmtvaluelen2;
		args->rmtvaluelen = args->valuelen;
			args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
							args->rmtvaluelen);
		args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
						       args->rmtvaluelen);
			args->valuelen = args->rmtvaluelen;
		if (args->valuelen < args->rmtvaluelen) {
			args->valuelen = args->rmtvaluelen;
		args->valuelen = args->rmtvaluelen;
		name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
		name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
				args.rmtvaluelen = valuelen;
	int			valuelen;
	ASSERT(args->rmtvaluelen == args->valuelen);
	valuelen = args->rmtvaluelen;
	blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
	valuelen = args->rmtvaluelen;
	int		rmtvaluelen;	/* remote attr value length in bytes */
	int		rmtvaluelen2;	/* remote attr value length in bytes */