Skip to contentSkip to conversation

xfsdump: avoid false-positive __strcpy_chk abort on long dirent names

PATCH In review vmfunc 4 participants 8 messages Jun 25 – Jul 3

lore
Share & tools
lore

vmfunc

author
#0 lore raw

PATCH 1/1 xfsdump: avoid false-positive __strcpy_chk abort on long dirent names

direnthdr_t and direnthdr_v1_t end in dh_name[6], which is used as a trailing variable-length buffer rather than a fixed six-byte field. dump_dirent() allocates each record with malloc(sz), where

sz = offsetofmember(direnthdr*_t, dh_name) + namelen + 1

(rounded up to DIRENTHDR_ALIGN), and the "sz > direntbufsz" check ahead of the copy guarantees the allocation is large enough to hold the name. The declared [6] is part of the on-media record layout: DIRENTHDR_SZ and the arch translation paths depend on it, so it cannot simply be turned into a [] flexible array member.

When xfsdump is built with _FORTIFY_SOURCE together with -fstrict-flex-arrays (increasingly the default in distributions), the trailing dh_name[6] is no longer treated as a flexible array. __builtin_object_size() sizes it to 6, strcpy() is rewritten to __strcpy_chk(dst, src, 6), and dumping any directory entry whose name is longer than five bytes aborts at runtime:

	*** buffer overflow detected ***: terminated
	#4  __fortify_fail
	#5  __chk_fail
	#6  __strcpy_chk
	#7  dump_dirent

The copy is in fact safe; only fortify's notion of the destination size is wrong. Write the name through the byte offset dump_dirent() already computes, (char *)dhdrp + name_offset, instead of through the sized dh_name lvalue. The destination is then a plain char * into the malloc'd record, which fortify cannot sub-object-size, so the spurious check is dropped while the rest of the binary keeps its hardening.

---
Andrey Albershteyn (Cc'd) tested this with fstests on the downstream report and asked that it be sent upstream; the discussion and original backtrace are at https://github.com/NixOS/nixpkgs/pull/533325 .

 dump/content.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
dump/content.c +4 -4
index 6462267..8d482cc 100644
@@ -5151,14 +5151,14 @@ dump_dirent(drive_t *drivep,
 		dhdrp->dh_sz = (uint16_t)sz;
 		dhdrp->dh_gen = (uint16_t)(gen & DENTGENMASK);
 		if (name) {
-			strcpy(dhdrp->dh_name, name);
+			strcpy((char *)dhdrp + name_offset, name);
 		}
 
 		dhdrp->dh_checksum = calc_checksum(dhdrp, DIRENTHDR_SZ);
 
 		xlate_direnthdr_v1(dhdrp, tmpdhdrp, 1);
 		if (name) {
-			strcpy(tmpdhdrp->dh_name, name);
+			strcpy((char *)tmpdhdrp + name_offset, name);
 		}
 	} else {
 		direnthdr_t *dhdrp = (direnthdr_t *)contextp->cc_mdirentbufp;
@@ -5169,14 +5169,14 @@ dump_dirent(drive_t *drivep,
 		dhdrp->dh_gen = gen;
 		dhdrp->dh_sz = (uint16_t)sz;
 		if (name) {
-			strcpy(dhdrp->dh_name, name);
+			strcpy((char *)dhdrp + name_offset, name);
 		}
 
 		dhdrp->dh_checksum = calc_checksum(dhdrp, DIRENTHDR_SZ);
 
 		xlate_direnthdr(dhdrp, tmpdhdrp, 1);
 		if (name) {
-			strcpy(tmpdhdrp->dh_name, name);
+			strcpy((char *)tmpdhdrp + name_offset, name);
 		}
 	}
 
Signature · 1 line
2.54.0
#1 lore raw
Quoted 49 lines · vmfunc

direnthdr_t and direnthdr_v1_t end in dh_name[6], which is used as a trailing variable-length buffer rather than a fixed six-byte field. dump_dirent() allocates each record with malloc(sz), where

sz = offsetofmember(direnthdr*_t, dh_name) + namelen + 1

(rounded up to DIRENTHDR_ALIGN), and the "sz > direntbufsz" check ahead of the copy guarantees the allocation is large enough to hold the name. The declared [6] is part of the on-media record layout: DIRENTHDR_SZ and the arch translation paths depend on it, so it cannot simply be turned into a [] flexible array member.

When xfsdump is built with _FORTIFY_SOURCE together with -fstrict-flex-arrays (increasingly the default in distributions), the trailing dh_name[6] is no longer treated as a flexible array. __builtin_object_size() sizes it to 6, strcpy() is rewritten to __strcpy_chk(dst, src, 6), and dumping any directory entry whose name is longer than five bytes aborts at runtime:

	*** buffer overflow detected ***: terminated
	#4  __fortify_fail
	#5  __chk_fail
	#6  __strcpy_chk
	#7  dump_dirent

The copy is in fact safe; only fortify's notion of the destination size is wrong. Write the name through the byte offset dump_dirent() already computes, (char *)dhdrp + name_offset, instead of through the sized dh_name lvalue. The destination is then a plain char * into the malloc'd record, which fortify cannot sub-object-size, so the spurious check is dropped while the rest of the binary keeps its hardening.

---
Andrey Albershteyn (Cc'd) tested this with fstests on the downstream report and asked that it be sent upstream; the discussion and original backtrace are at https://github.com/NixOS/nixpkgs/pull/533325 .

 dump/content.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
dump/content.c +0 -0
index 6462267..8d482cc 100644
@@ -5151,14 +5151,14 @@ dump_dirent(drive_t *drivep,
 		dhdrp->dh_sz = (uint16_t)sz;
 		dhdrp->dh_gen = (uint16_t)(gen & DENTGENMASK);
 		if (name) {

dump/content.c dump_dirent(drive_t *drivep, · line 5161

Quoted 10 lines
dump/content.c +2 -2
-			strcpy(dhdrp->dh_name, name);
+			strcpy((char *)dhdrp + name_offset, name);
 		}
 
 		dhdrp->dh_checksum = calc_checksum(dhdrp, DIRENTHDR_SZ);
 
 		xlate_direnthdr_v1(dhdrp, tmpdhdrp, 1);
 		if (name) {
-			strcpy(tmpdhdrp->dh_name, name);
+			strcpy((char *)tmpdhdrp + name_offset, name);

Why wouldn't you fix the structure definitions to use the proper VLA syntax (dh_name[]) instead of making the code less readable?

--D

Quoted 24 lines
dump/content.c +2 -2
 		}
 	} else {
 		direnthdr_t *dhdrp = (direnthdr_t *)contextp->cc_mdirentbufp;
@@ -5169,14 +5169,14 @@ dump_dirent(drive_t *drivep,
 		dhdrp->dh_gen = gen;
 		dhdrp->dh_sz = (uint16_t)sz;
 		if (name) {
-			strcpy(dhdrp->dh_name, name);
+			strcpy((char *)dhdrp + name_offset, name);
 		}
 
 		dhdrp->dh_checksum = calc_checksum(dhdrp, DIRENTHDR_SZ);
 
 		xlate_direnthdr(dhdrp, tmpdhdrp, 1);
 		if (name) {
-			strcpy(tmpdhdrp->dh_name, name);
+			strcpy((char *)tmpdhdrp + name_offset, name);
 		}
 	}
 
Signature · 1 line
2.54.0
author
#2 lore raw
Quoted 2 lines · Darrick J. Wong

Why wouldn't you fix the structure definitions to use the proper VLA syntax (dh_name[]) instead of making the code less readable?

looked at that first but dh_name[] doesnt drop in cleanly here. the declared size isnt slack padding, its the count of name bytes carried inline in the fixed-size on-media header, n read_dirent() in restore/content.c reads the record back out using sizeof() of that member:

	namep = dhdrp->dh_name + sizeof(dhdrp->dh_name);
	memcpy(dhdrp->dh_name, dhdr_v1.dh_name, sizeof(dhdr_v1.dh_name));

sizeof() on a flex array member is a constraint violation, so dh_name[] wont compile without rewriting that reader. it also breaks assert(sizeof(direnthdr_v1_t) == DIRENTHDR_SZ): the v1 header is 16 bytes of fields n dh_name[8] is exactly the pad that brings it up to DIRENTHDR_SZ (24). as a [] member it goes to 16 n the record size moves with it.

so a proper [] conversion means reworking the v1 on-media reader + the record-size invariants, felt out of proportion for fixing the fortify abort, so i kept this to the minimal change that preserves the layout n the rest of the hardening. happy to do the struct cleanup as a separate patch if u'd rather, just flagging its a format change not a pure readability one.

thanks,
celeste

#3 lore raw
Quoted 11 lines · vmfunc
Quoted 2 lines · Darrick J. Wong

Why wouldn't you fix the structure definitions to use the proper VLA syntax (dh_name[]) instead of making the code less readable?

looked at that first but dh_name[] doesnt drop in cleanly here. the declared size isnt slack padding, its the count of name bytes carried inline in the fixed-size on-media header, n read_dirent() in restore/content.c reads the record back out using sizeof() of that member:

	namep = dhdrp->dh_name + sizeof(dhdrp->dh_name);
	memcpy(dhdrp->dh_name, dhdr_v1.dh_name, sizeof(dhdr_v1.dh_name));

To me it looks like the problem is that mix up the on-disk and in-memory structure with different semantics here. The somewhat more invasive fix would be to split the

direnthdr structure into an in-memory direnthdr one, and an on-disk direnthdr_v3. The former would use a VLA, and the latter the existing hard coded value. This would also allow to make the endian conversion in xlate_direnthdr/xlate_direnthdr_v1 type safe.

#4 lore raw
In reply to vmfunc
Quoted 26 lines · vmfunc
Quoted 2 lines · Darrick J. Wong

Why wouldn't you fix the structure definitions to use the proper VLA syntax (dh_name[]) instead of making the code less readable?

looked at that first but dh_name[] doesnt drop in cleanly here. the declared size isnt slack padding, its the count of name bytes carried inline in the fixed-size on-media header, n read_dirent() in restore/content.c reads the record back out using sizeof() of that member:

	namep = dhdrp->dh_name + sizeof(dhdrp->dh_name);
	memcpy(dhdrp->dh_name, dhdr_v1.dh_name, sizeof(dhdr_v1.dh_name));

sizeof() on a flex array member is a constraint violation, so dh_name[] wont compile without rewriting that reader. it also breaks assert(sizeof(direnthdr_v1_t) == DIRENTHDR_SZ): the v1 header is 16 bytes of fields n dh_name[8] is exactly the pad that brings it up to DIRENTHDR_SZ (24). as a [] member it goes to 16 n the record size moves with it.

so a proper [] conversion means reworking the v1 on-media reader + the record-size invariants, felt out of proportion for fixing the fortify abort, so i kept this to the minimal change that preserves the layout n the rest of the hardening. happy to do the struct cleanup as a separate patch if u'd rather, just flagging its a format change not a pure readability one.

Quoted 3 lines

thanks,
celeste

My thoughts too, seems like easy enough fix to make fortified version not crash.

Signature · 1 line
- Andrey
author
#5 lore raw

should we then assume that this is fine to merge?

- vmfunc

#6 lore raw
Quoted 1 line · Celeste

should we then assume that this is fine to merge?

I still don't think that just papering over the misuse of the same type for on-disk and in-memory uses with different characteristics is a good idea.

#7 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 1 line · Celeste

should we then assume that this is fine to merge?

I still don't think that just papering over the misuse of the same type for on-disk and in-memory uses with different characteristics is a good idea.

I agree.

--D