btrfs-progs/convert
Qu Wenruo a927cb1b0a btrfs-progs: convert: fix the filename output when rolling back
[BUG]
When rolling back a converted btrfs, the filename output is corrupted:

  $ btrfs-convert -r  ~/test.img
  btrfs-convert from btrfs-progs v6.9.2

  Open filesystem for rollback:
    Label:
    UUID:            df54baf3-c91e-4956-96f9-99413a857576
    Restoring from:  ext2_saved0ƨy/image
                               ^^^ Corruption
  Rollback succeeded

[CAUSE]
The error is in how we handle the filename.  In btrfs all our strings
are not '\0' terminated, but with explicit length.

But in C, most strings are '\0' terminated, so after reading a filename
from btrfs, we need to manually terminate the string.

However the code adding the terminating '\0' looks like this:

	/* Get the filename length. */
	name_len = btrfs_root_ref_name_len(path.nodes[0], root_ref_item);

	/*
	 * This should not happen, but as an extra handling for possible
	 * corrupted btrfs.
	 */
	if (name_len > sizeof(dir_name))
		name_len = sizeof(dir_name) - 1;
	/* Got the real filename into our buffer. */
 	read_extent_buffer(path.nodes[0], dir_name, (unsigned long)(root_ref_item + 1), name_len);

	/* Terminate the string. */
	dir_name[sizeof(dir_name) - 1] = 0;

The problem is, the final termination is totally wrong, it always make
the last buffer char '\0', not using the @name_len we read before.

[FIX]
Use @name_len to terminate the string, as we have already updated it to
handle buffer overflow, it can handle both the regular and corrupted
case.

Fixes: dc29a5c51d ("btrfs-progs: convert: update default output")
Signed-off-by: Qu Wenruo <wqu@suse.com>
2024-07-30 19:59:07 +02:00
..
Makefile btrfs-progs: build: add stub makefile to convert 2017-03-08 13:00:46 +01:00
common.c btrfs-progs: rename and move __strncpy_null to string-utils 2024-06-24 19:18:46 +02:00
common.h btrfs-progs: convert: move simple_range into common.h 2022-10-11 09:08:08 +02:00
main.c btrfs-progs: convert: fix the filename output when rolling back 2024-07-30 19:59:07 +02:00
source-ext2.c btrfs-progs: do interactive fixing of some ambigous typos 2024-07-30 19:56:08 +02:00
source-ext2.h btrfs-progs: convert: update include lists 2022-10-11 09:06:12 +02:00
source-fs.c btrfs-progs: reorder key initializations 2024-04-30 21:49:15 +02:00
source-fs.h btrfs-progs: convert defined constants to enums 2024-02-08 08:30:37 +01:00
source-reiserfs.c btrfs-progs: move btrfs_record_file_extent and code into a new file 2023-10-03 01:11:56 +02:00
source-reiserfs.h btrfs-progs: convert: update include lists 2022-10-11 09:06:12 +02:00