Commit ea0b3e81 authored by Darrick J. Wong's avatar Darrick J. Wong
Browse files

xfs: enforce one namespace per attribute



Create a standardized helper function to enforce one namespace bit per
extended attribute, and refactor all the open-coded hweight logic.  This
function is not a static inline to avoid porting hassles in userspace.

Signed-off-by: default avatarDarrick J. Wong <djwong@kernel.org>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
parent ffdcc3b8
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -1532,12 +1532,23 @@ xfs_attr_node_get(
	return error;
}

/* Enforce that there is at most one namespace bit per attr. */
inline bool xfs_attr_check_namespace(unsigned int attr_flags)
{
	return hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) < 2;
}

/* Returns true if the attribute entry name is valid. */
bool
xfs_attr_namecheck(
	unsigned int	attr_flags,
	const void	*name,
	size_t		length)
{
	/* Only one namespace bit allowed. */
	if (!xfs_attr_check_namespace(attr_flags))
		return false;

	/*
	 * MAXNAMELEN includes the trailing null, but (name/length) leave it
	 * out, so use >= for the length check.
+3 −1
Original line number Diff line number Diff line
@@ -560,7 +560,9 @@ enum xfs_attr_update {
int xfs_attr_set(struct xfs_da_args *args, enum xfs_attr_update op);
int xfs_attr_set_iter(struct xfs_attr_intent *attr);
int xfs_attr_remove_iter(struct xfs_attr_intent *attr);
bool xfs_attr_namecheck(const void *name, size_t length);
bool xfs_attr_check_namespace(unsigned int attr_flags);
bool xfs_attr_namecheck(unsigned int attr_flags, const void *name,
		size_t length);
int xfs_attr_calc_size(struct xfs_da_args *args, int *local);
void xfs_init_attr_trans(struct xfs_da_args *args, struct xfs_trans_res *tres,
			 unsigned int *total);
+6 −1
Original line number Diff line number Diff line
@@ -950,6 +950,11 @@ xfs_attr_shortform_to_leaf(
		nargs.hashval = xfs_da_hashname(sfe->nameval,
						sfe->namelen);
		nargs.attr_filter = sfe->flags & XFS_ATTR_NSP_ONDISK_MASK;
		if (!xfs_attr_check_namespace(sfe->flags)) {
			xfs_da_mark_sick(args);
			error = -EFSCORRUPTED;
			goto out;
		}
		error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
		ASSERT(error == -ENOATTR);
		error = xfs_attr3_leaf_add(bp, &nargs);
@@ -1063,7 +1068,7 @@ xfs_attr_shortform_verify(
		 * one namespace flag per xattr, so we can just count the
		 * bits (i.e. hweight) here.
		 */
		if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
		if (!xfs_attr_check_namespace(sfep->flags))
			return __this_address;

		sfep = next_sfep;
+5 −7
Original line number Diff line number Diff line
@@ -203,14 +203,8 @@ xchk_xattr_actor(
		return 0;
	}

	/* Only one namespace bit allowed. */
	if (hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) > 1) {
		xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno);
		return -ECANCELED;
	}

	/* Does this name make sense? */
	if (!xfs_attr_namecheck(name, namelen)) {
	if (!xfs_attr_namecheck(attr_flags, name, namelen)) {
		xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno);
		return -ECANCELED;
	}
@@ -519,6 +513,10 @@ xchk_xattr_rec(
		xchk_da_set_corrupt(ds, level);
		return 0;
	}
	if (!xfs_attr_check_namespace(ent->flags)) {
		xchk_da_set_corrupt(ds, level);
		return 0;
	}

	if (ent->flags & XFS_ATTR_LOCAL) {
		lentry = (struct xfs_attr_leaf_name_local *)
+1 −3
Original line number Diff line number Diff line
@@ -123,12 +123,10 @@ xrep_xattr_want_salvage(
		return false;
	if (namelen > XATTR_NAME_MAX || namelen <= 0)
		return false;
	if (!xfs_attr_namecheck(name, namelen))
	if (!xfs_attr_namecheck(attr_flags, name, namelen))
		return false;
	if (valuelen > XATTR_SIZE_MAX || valuelen < 0)
		return false;
	if (hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
		return false;
	return true;
}

Loading