From 7e53611e3a087518e027285d1ef4d6719780e86e Mon Sep 17 00:00:00 2001 From: Peter Oberparleiter Date: Tue, 18 Jul 2023 13:41:28 +0200 Subject: [PATCH] dump2tar: fix truncated paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When creating a tar archive, dump2tar incorrectly truncates the last character of file paths that are exactly 100 characters long. Paths up to 100 characters can be represented in the 100-byte name field of a tar header entry, while longer paths are handled via an additional tar data block. For 100-character paths, dump2tar determines that a single header is sufficient, but then uses util_strlcpy() to store the path into the name field. Since util_strlcpy() ensures nul-terminated strings, the final character of the path is overwritten. Fix this by using strncpy() instead of util_strlcpy(). Also mark the affected name fields as "nonstring" to prevent associated compiler warnings. Reported-by: Steffen Maier Fixes: d85cf20981cb ("dump2tar: Change SET_STR_FIELD to copy strings correctly") Reviewed-by: Jan Höppner Signed-off-by: Peter Oberparleiter Signed-off-by: Jan Höppner --- dump2tar/src/tar.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/dump2tar/src/tar.c b/dump2tar/src/tar.c index bc5649a0..b2ebe68c 100644 --- a/dump2tar/src/tar.c +++ b/dump2tar/src/tar.c @@ -23,9 +23,16 @@ #define BLOCKSIZE 512 +#if __has_attribute(nonstring) +# define __nonstring __attribute__ ((nonstring)) +#else +# define __nonstring +#endif + + /* Basic TAR header */ struct tar_header { - char name[100]; + char name[100] __nonstring; char mode[8]; char uid[8]; char gid[8]; @@ -33,7 +40,7 @@ struct tar_header { char mtime[12]; char chksum[8]; char typeflag; - char linkname[100]; + char linkname[100] __nonstring; char magic[6]; char version[2]; char uname[32]; @@ -78,7 +85,7 @@ static void set_time(char *dest, size_t len, time_t value) #define SET_TIME_FIELD(obj, name, value) \ set_time((obj)->name, sizeof((obj)->name), (time_t) (value)) #define SET_STR_FIELD(obj, name, value) \ - util_strlcpy((obj)->name, (value), sizeof((obj)->name)) + strncpy((obj)->name, (value), sizeof((obj)->name)) /* Initialize the tar file @header with the provided data */ static void init_header(struct tar_header *header, const char *filename,