From 328196a29c5091d2358a5efd2a59ab029c2165c5 Mon Sep 17 00:00:00 2001 From: Martin Matuska Date: Sun, 26 Jan 2020 00:35:50 +0100 Subject: [PATCH] PAX writer: fix entry uname or gname longer than 32 characters Fixes #1319 --- libarchive/archive_write_set_format_ustar.c | 16 ++++--- libarchive/test/test_write_format_pax.c | 46 +++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/libarchive/archive_write_set_format_ustar.c b/libarchive/archive_write_set_format_ustar.c index e1fe974a1..d1a06bc4f 100644 --- a/libarchive/archive_write_set_format_ustar.c +++ b/libarchive/archive_write_set_format_ustar.c @@ -513,9 +513,11 @@ __archive_write_format_header_ustar(struct archive_write *a, char h[512], } if (copy_length > 0) { if (copy_length > USTAR_uname_size) { - archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, - "Username too long"); - ret = ARCHIVE_FAILED; + if (tartype != 'x') { + archive_set_error(&a->archive, + ARCHIVE_ERRNO_MISC, "Username too long"); + ret = ARCHIVE_FAILED; + } copy_length = USTAR_uname_size; } memcpy(h + USTAR_uname_offset, p, copy_length); @@ -536,9 +538,11 @@ __archive_write_format_header_ustar(struct archive_write *a, char h[512], } if (copy_length > 0) { if (strlen(p) > USTAR_gname_size) { - archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, - "Group name too long"); - ret = ARCHIVE_FAILED; + if (tartype != 'x') { + archive_set_error(&a->archive, + ARCHIVE_ERRNO_MISC, "Group name too long"); + ret = ARCHIVE_FAILED; + } copy_length = USTAR_gname_size; } memcpy(h + USTAR_gname_offset, p, copy_length); diff --git a/libarchive/test/test_write_format_pax.c b/libarchive/test/test_write_format_pax.c index 41a423a96..4538aac82 100644 --- a/libarchive/test/test_write_format_pax.c +++ b/libarchive/test/test_write_format_pax.c @@ -104,6 +104,28 @@ DEFINE_TEST(test_write_format_pax) assertEqualIntA(a, 1024, archive_write_data(a, nulls, 1024)); assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9)); + /* + * "file4" is similar to "file1" but has a large uid, large gid, + * uname and gname are longer than 32 characters + */ + assert((ae = archive_entry_new()) != NULL); + archive_entry_set_atime(ae, 2, 20); + archive_entry_set_birthtime(ae, 3, 30); + archive_entry_set_ctime(ae, 4, 40); + archive_entry_set_mtime(ae, 5, 50); + archive_entry_copy_pathname(ae, "file4"); + archive_entry_set_mode(ae, S_IFREG | 0755); + archive_entry_set_size(ae, 8); + archive_entry_copy_uname(ae, + "long-uname123456789012345678901234567890"); + archive_entry_copy_gname(ae, + "long-gname123456789012345678901234567890"); + archive_entry_set_uid(ae, 536870912); + archive_entry_set_gid(ae, 536870913); + assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); + archive_entry_free(ae); + assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9)); + /* * XXX TODO XXX Archive directory, other file types. * Archive extended attributes, ACLs, other metadata. @@ -198,6 +220,30 @@ DEFINE_TEST(test_write_format_pax) assertEqualIntA(a, 8, archive_read_data(a, buff2, 10)); assertEqualMem(buff2, "12345678", 8); + /* + * Read "file4 + */ + assertEqualIntA(a, 0, archive_read_next_header(a, &ae)); + assertEqualInt(2, archive_entry_atime(ae)); + assertEqualInt(20, archive_entry_atime_nsec(ae)); + assertEqualInt(3, archive_entry_birthtime(ae)); + assertEqualInt(30, archive_entry_birthtime_nsec(ae)); + assertEqualInt(4, archive_entry_ctime(ae)); + assertEqualInt(40, archive_entry_ctime_nsec(ae)); + assertEqualInt(5, archive_entry_mtime(ae)); + assertEqualInt(50, archive_entry_mtime_nsec(ae)); + assertEqualString("file4", archive_entry_pathname(ae)); + assertEqualString("long-uname123456789012345678901234567890", + archive_entry_uname(ae)); + assertEqualString("long-gname123456789012345678901234567890", + archive_entry_gname(ae)); + assertEqualInt(536870912, archive_entry_uid(ae)); + assertEqualInt(536870913, archive_entry_gid(ae)); + assert((S_IFREG | 0755) == archive_entry_mode(ae)); + assertEqualInt(8, archive_entry_size(ae)); + assertEqualIntA(a, 8, archive_read_data(a, buff2, 10)); + assertEqualMem(buff2, "12345678", 8); + /* * Verify the end of the archive. */ -- 2.47.2