Skip to contentSkip to conversation

debugfs: avoid ambiguity when printing filenames

PATCH Applied Eric Biggers 2 participants 2 messages Apr 22, 2019 – Apr 29, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 debugfs: avoid ambiguity when printing filenames

From: Eric Biggers <[email protected]>

The way debugfs escapes filenames is ambiguous because a sequence like M-A can mean either the byte 'A' + 128 == 0xc1 or the three bytes {'M', '-', 'A'}. Similarly, ^A can mean either the byte 'A' ^ 0x40 == 0x01 or the two bytes {'^', 'A'}.

Fix this and simplify the code by switching to a simpler strategy where all bytes < 32, all bytes >= 127, and backslash are encoded with C-style hex escape sequences. E.g., the byte 0xc1 will now be encoded as \xc1 rather than M-A as it was before, while a filename consisting of the three bytes {'M', '-', 'A'} will continue to be shown as M-A.

I want to fix this mainly because I want to use debugfs to retrieve raw encrypted filenames for ciphertext verification tests. But this doesn't work if the returned filenames are ambiguous.

Fixes 68a1de3df340 ("debugfs: pretty print encrypted filenames in the ls command")
---
 debugfs/ls.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
debugfs/ls.c +5 -11
index 41af15d2..4f63bd3f 100644
@@ -62,21 +62,15 @@ static int print_filename(FILE *f, struct ext2_dir_entry *dirent, int options)
 	}
 	while (len--) {
 		ch = *cp++;
-		if (ch > 128) {
+		if (ch < 32 || ch >= 127 || ch == '\\') {
 			if (f)
-				fputs("M-", f);
-			ch -= 128;
-			retlen += 2;
-		}
-		if ((ch < 32) || (ch == 0x7f)) {
+				fprintf(f, "\\x%02x", ch);
+			retlen += 4;
+		} else {
 			if (f)
-				fputc('^', f);
-			ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
+				fputc(ch, f);
 			retlen++;
 		}
-		if (f)
-			fputc(ch, f);
-		retlen++;
 	}
 	return retlen;
 }
Signature · 1 line
2.21.0.593.g511ec345e18-goog
#1 lore raw
Quoted 13 lines · Eric Biggers

From: Eric Biggers <[email protected]>

The way debugfs escapes filenames is ambiguous because a sequence like M-A can mean either the byte 'A' + 128 == 0xc1 or the three bytes {'M', '-', 'A'}. Similarly, ^A can mean either the byte 'A' ^ 0x40 == 0x01 or the two bytes {'^', 'A'}.

Fix this and simplify the code by switching to a simpler strategy where all bytes < 32, all bytes >= 127, and backslash are encoded with C-style hex escape sequences. E.g., the byte 0xc1 will now be encoded as \xc1 rather than M-A as it was before, while a filename consisting of the three bytes {'M', '-', 'A'} will continue to be shown as M-A.

Quoted 6 lines

I want to fix this mainly because I want to use debugfs to retrieve raw encrypted filenames for ciphertext verification tests. But this doesn't work if the returned filenames are ambiguous.

Fixes 68a1de3df340 ("debugfs: pretty print encrypted filenames in the ls command")

Thanks, applied.

- Ted