From 45571fd5885d00520a5c1d3bb743634267920c19 Mon Sep 17 00:00:00 2001
From: Eric Sandeen <sandeen@redhat.com>
Date: Mon, 28 Jan 2019 13:03:03 -0600
Subject: [PATCH] xfs_repair: allow '/' in attribute names
Git-commit: 45571fd5885d00520a5c1d3bb743634267920c19
Patch-mainline: v4.20.0-rc1
References: bsc#1138237

For some reason, since the earliest days of XFS, a '/' character
in an extended attribute name has been treated as corruption by
xfs_repair.  This despite nothing in other userspace tools or the
kernel having this restriction.

My best guess is that this was an unintentional leftover from
common code between dirs & attrs in the "da" code, and there has
never been a good reason for it.

Since userspace and kernelspace allow such a name to be set,
listed, and read, it seems wrong to flag it as corruption.
So, make this test conditional on whether we're validating a name
in a dir, as opposed to the name of an attr.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
Acked-by: Anthony Iliopoulos <ailiop@suse.com>

---
 repair/attr_repair.c | 20 +++++++++++++-------
 repair/da_util.c     | 20 +++++++++++++-------
 repair/da_util.h     |  3 ++-
 repair/dir2.c        | 12 ++++++++++--
 4 files changed, 38 insertions(+), 17 deletions(-)

Index: xfsprogs-4.15.0/repair/attr_repair.c
===================================================================
--- xfsprogs-4.15.0.orig/repair/attr_repair.c
+++ xfsprogs-4.15.0/repair/attr_repair.c
@@ -135,6 +135,14 @@ set_da_freemap(xfs_mount_t *mp, da_freem
  * fork being emptied and put in shortform format.
  */
 
+static int
+attr_namecheck(
+	uint8_t	*name,
+	int	length)
+{
+	return namecheck((char *)name, length, false);
+}
+
 /*
  * This routine just checks what security needs are for attribute values
  * only called when root flag is set, otherwise these names could exist in
@@ -305,11 +313,9 @@ process_shortform_attr(
 			}
 		}
 
-		/* namecheck checks for / and null terminated for file names.
-		 * attributes names currently follow the same rules.
-		*/
-		if (namecheck((char *)&currententry->nameval[0],
-						currententry->namelen))  {
+		/* namecheck checks for null chars in attr names. */
+		if (attr_namecheck(currententry->nameval,
+						currententry->namelen)) {
 			do_warn(
 	_("entry contains illegal character in shortform attribute name\n"));
 			junkit = 1;
@@ -470,7 +476,7 @@ process_leaf_attr_local(
 	xfs_attr_leaf_name_local_t *local;
 
 	local = xfs_attr3_leaf_name_local(leaf, i);
-	if (local->namelen == 0 || namecheck((char *)&local->nameval[0],
+	if (local->namelen == 0 || attr_namecheck(local->nameval,
 							local->namelen)) {
 		do_warn(
 	_("attribute entry %d in attr block %u, inode %" PRIu64 " has bad name (namelen = %d)\n"),
@@ -525,7 +531,7 @@ process_leaf_attr_remote(
 
 	remotep = xfs_attr3_leaf_name_remote(leaf, i);
 
-	if (remotep->namelen == 0 || namecheck((char *)&remotep->name[0],
+	if (remotep->namelen == 0 || attr_namecheck(remotep->name,
 						remotep->namelen) ||
 			be32_to_cpu(entry->hashval) !=
 				libxfs_da_hashname((unsigned char *)&remotep->name[0],
Index: xfsprogs-4.15.0/repair/da_util.c
===================================================================
--- xfsprogs-4.15.0.orig/repair/da_util.c
+++ xfsprogs-4.15.0/repair/da_util.c
@@ -24,20 +24,26 @@
 #include "da_util.h"
 
 /*
- * takes a name and length (name need not be null-terminated)
- * and returns 1 if the name contains a '/' or a \0, returns 0
- * otherwise
+ * Takes a name and length (name need not be null-terminated) and whether
+ * we are checking a dir (as opposed to an attr).
+ * Returns 1 if the name contains a NUL or if a directory entry contains a '/'.
+ * Returns 0 if the name checks out.
  */
 int
-namecheck(char *name, int length)
+namecheck(
+	char	*name,
+	int	length,
+	bool	isadir)
 {
-	char *c;
-	int i;
+	char	*c;
+	int	i;
 
 	ASSERT(length < MAXNAMELEN);
 
 	for (c = name, i = 0; i < length; i++, c++) {
-		if (*c == '/' || *c == '\0')
+		if (isadir && *c == '/')
+			return 0;
+		if (*c == '\0')
 			return 1;
 	}
 
Index: xfsprogs-4.15.0/repair/da_util.h
===================================================================
--- xfsprogs-4.15.0.orig/repair/da_util.h
+++ xfsprogs-4.15.0/repair/da_util.h
@@ -39,7 +39,8 @@ typedef struct da_bt_cursor {
 int
 namecheck(
 	char		*name,
-	int		length);
+	int		length,
+	bool		isadir);
 
 struct xfs_buf *
 da_read_buf(
Index: xfsprogs-4.15.0/repair/dir2.c
===================================================================
--- xfsprogs-4.15.0.orig/repair/dir2.c
+++ xfsprogs-4.15.0/repair/dir2.c
@@ -56,6 +56,14 @@ _("malloc failed (%zu bytes) dir2_add_ba
 	l->ino = ino;
 }
 
+static int
+dir_namecheck(
+	uint8_t	*name,
+	int	length)
+{
+	return namecheck((char *)name, length, true);
+}
+
 int
 dir2_is_badino(
 	xfs_ino_t	ino)
@@ -322,7 +330,7 @@ _("entry #%d %s in shortform dir %" PRIu
 		 * the length value is stored in a byte
 		 * so it can't be too big, it can only wrap
 		 */
-		if (namecheck((char *)&sfep->name[0], namelen))  {
+		if (dir_namecheck(sfep->name, namelen)) {
 			/*
 			 * junk entry
 			 */
@@ -791,7 +799,7 @@ _("\twould clear inode number in entry a
 		 * during phase 4.
 		 */
 		junkit = dep->name[0] == '/';
-		nm_illegal = namecheck((char *)dep->name, dep->namelen);
+		nm_illegal = dir_namecheck(dep->name, dep->namelen);
 		if (ino_discovery && nm_illegal) {
 			do_warn(
 _("entry at block %u offset %" PRIdPTR " in directory inode %" PRIu64 " has illegal name \"%*.*s\": "),
