Skip to contentSkip to conversation

db/print: bad character cause illegal memory access

PATCH In review Zorro Lang 2 participants 3 messages Jan 9, 2018 – Jan 10, 2018

lore
Share & tools
lore

Zorro Lang

author
#0 lore raw

PATCH 1/1 db/print: bad character cause illegal memory access

xfs_db can print specified field values, likes:

  # xfs_db -c "inode $inum" -c "print core.nblocks" $dev

But when we give it some bad chararcters, the 'print' command will crash:

  # xfs_db -r -c "inode 67211167" -c "print core.*" /dev/mapper/rhel-root
  bad character in field *
  *** Error in `xfs_db': free(): invalid pointer: 0x00007fa116e937b8 ***
  ...
  (crash messages)
  ...
  # xfs_db -r -c "inode 67211167" -c "print core.\"" /dev/mapper/rhel-root
  missing closing quote
  *** Error in `xfs_db': free(): invalid pointer: 0x00007f6e8e3c37b8 ***
  ...
  (crash messages)
  ...

The reason is xfs_db call ftok_free() to free ftok_t list, but flist_split() forgets to set the tail to NULL. That cause ftok_free() trys to free illegal memory address.

---
 db/flist.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
db/flist.c +6 -1
index e11acbfc..1a875b95 100644
@@ -371,11 +371,14 @@ flist_split(
 	v = xmalloc(sizeof(*v));
 	v->tok = NULL;
 	while (*s) {
+		v = xrealloc(v, (nv + 1) * sizeof(*v));
 		/* need to add string handling */
 		if (*s == '\"') {
 			s++; /* skip first quote */
 			if ((a = strrchr(s, '\"')) == NULL) {
 				dbprintf(_("missing closing quote %s\n"), s);
+				v[nv].tok = NULL;
+				v[nv].tokty = TT_END;
 				ftok_free(v);
 				return NULL;
 			}
@@ -393,19 +396,21 @@ flist_split(
 			t = puncttypes[a - punctchars];
 		} else {
 			dbprintf(_("bad character in field %s\n"), s);
+			v[nv].tok = NULL;
+			v[nv].tokty = TT_END;
 			ftok_free(v);
 			return NULL;
 		}
 		a = xmalloc(l + 1);
 		strncpy(a, s, l);
 		a[l] = '\0';
-		v = xrealloc(v, (nv + 2) * sizeof(*v));
 		v[nv].tok = a;
 		v[nv].tokty = t;
 		nv++;
 		s += l + tailskip;
 		tailskip = 0;
 	}
+	v = xrealloc(v, (nv + 1) * sizeof(*v));
 	v[nv].tok = NULL;
 	v[nv].tokty = TT_END;
 	return v;
Signature · 1 line
2.13.6
#1 lore raw
Quoted 58 lines · Zorro Lang

xfs_db can print specified field values, likes:

  # xfs_db -c "inode $inum" -c "print core.nblocks" $dev

But when we give it some bad chararcters, the 'print' command will crash:

  # xfs_db -r -c "inode 67211167" -c "print core.*" /dev/mapper/rhel-root
  bad character in field *
  *** Error in `xfs_db': free(): invalid pointer: 0x00007fa116e937b8 ***
  ...
  (crash messages)
  ...
  # xfs_db -r -c "inode 67211167" -c "print core.\"" /dev/mapper/rhel-root
  missing closing quote
  *** Error in `xfs_db': free(): invalid pointer: 0x00007f6e8e3c37b8 ***
  ...
  (crash messages)
  ...

The reason is xfs_db call ftok_free() to free ftok_t list, but flist_split() forgets to set the tail to NULL. That cause ftok_free() trys to free illegal memory address.

---
 db/flist.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
db/flist.c +5 -0
index e11acbfc..1a875b95 100644
@@ -371,11 +371,14 @@ flist_split(
 	v = xmalloc(sizeof(*v));
 	v->tok = NULL;
 	while (*s) {
+		v = xrealloc(v, (nv + 1) * sizeof(*v));
 		/* need to add string handling */
 		if (*s == '\"') {
 			s++; /* skip first quote */
 			if ((a = strrchr(s, '\"')) == NULL) {
 				dbprintf(_("missing closing quote %s\n"), s);
+				v[nv].tok = NULL;
+				v[nv].tokty = TT_END;
 				ftok_free(v);
 				return NULL;
 			}
@@ -393,19 +396,21 @@ flist_split(
 			t = puncttypes[a - punctchars];
 		} else {
 			dbprintf(_("bad character in field %s\n"), s);
+			v[nv].tok = NULL;
+			v[nv].tokty = TT_END;
 			ftok_free(v);
 			return NULL;
 		}
 		a = xmalloc(l + 1);
 		strncpy(a, s, l);

db/flist.c flist_split( · line 414

Quoted 10 lines
db/flist.c +1 -1
 		a[l] = '\0';
-		v = xrealloc(v, (nv + 2) * sizeof(*v));
 		v[nv].tok = a;
 		v[nv].tokty = t;
 		nv++;
 		s += l + tailskip;
 		tailskip = 0;
 	}
+	v = xrealloc(v, (nv + 1) * sizeof(*v));
 	v[nv].tok = NULL;

Could we just move the above line into the loop (right after nv is bumped) to initialize .tok similar to the initial allocation before the loop?

Brian

Quoted 9 lines
db/flist.c +0 -0
 	v[nv].tokty = TT_END;
 	return v;
Signature · 6 lines
2.13.6

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#2 lore raw
Quoted 65 lines · Brian Foster
Quoted 64 lines · Zorro Lang

xfs_db can print specified field values, likes:

  # xfs_db -c "inode $inum" -c "print core.nblocks" $dev

But when we give it some bad chararcters, the 'print' command will crash:

  # xfs_db -r -c "inode 67211167" -c "print core.*" /dev/mapper/rhel-root
  bad character in field *
  *** Error in `xfs_db': free(): invalid pointer: 0x00007fa116e937b8 ***
  ...
  (crash messages)
  ...
  # xfs_db -r -c "inode 67211167" -c "print core.\"" /dev/mapper/rhel-root
  missing closing quote
  *** Error in `xfs_db': free(): invalid pointer: 0x00007f6e8e3c37b8 ***
  ...
  (crash messages)
  ...

The reason is xfs_db call ftok_free() to free ftok_t list, but flist_split() forgets to set the tail to NULL. That cause ftok_free() trys to free illegal memory address.

---
 db/flist.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
db/flist.c +5 -1
index e11acbfc..1a875b95 100644
@@ -371,11 +371,14 @@ flist_split(
 	v = xmalloc(sizeof(*v));
 	v->tok = NULL;
 	while (*s) {
+		v = xrealloc(v, (nv + 1) * sizeof(*v));
 		/* need to add string handling */
 		if (*s == '\"') {
 			s++; /* skip first quote */
 			if ((a = strrchr(s, '\"')) == NULL) {
 				dbprintf(_("missing closing quote %s\n"), s);
+				v[nv].tok = NULL;
+				v[nv].tokty = TT_END;
 				ftok_free(v);
 				return NULL;
 			}
@@ -393,19 +396,21 @@ flist_split(
 			t = puncttypes[a - punctchars];
 		} else {
 			dbprintf(_("bad character in field %s\n"), s);
+			v[nv].tok = NULL;
+			v[nv].tokty = TT_END;
 			ftok_free(v);
 			return NULL;
 		}
 		a = xmalloc(l + 1);
 		strncpy(a, s, l);
 		a[l] = '\0';
-		v = xrealloc(v, (nv + 2) * sizeof(*v));
 		v[nv].tok = a;
 		v[nv].tokty = t;
 		nv++;
 		s += l + tailskip;
Quoted 8 lines

db/flist.c flist_split( · line 414

Quoted 4 lines
db/flist.c +1 -0
 		tailskip = 0;
 	}
+	v = xrealloc(v, (nv + 1) * sizeof(*v));
 	v[nv].tok = NULL;

Could we just move the above line into the loop (right after nv is bumped) to initialize .tok similar to the initial allocation before the loop?

Thanks for you review, Brian. I found djwong@ has fixed this bug on xfsprogs for-next branch by:

commit 945e47e2fcc5d1cec693122286da06d8ab829c52

Author: Darrick J. Wong <[email protected]>
Date:   Thu Jan 4 13:58:29 2018 -0600

xfs_db: fix crash when field list selector string has trailing slash

Thanks for you help,
Zorro

Quoted 16 lines

Brian

Quoted 9 lines
db/flist.c +0 -0
 	v[nv].tokty = TT_END;
 	return v;
Signature · 6 lines
2.13.6

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html