From owner-dev-commits-src-branches@freebsd.org Fri Sep 10 08:34:11 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 4978A6A943A; Fri, 10 Sep 2021 08:34:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4H5TgR1YSLz4lGh; Fri, 10 Sep 2021 08:34:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 165FC1ABA; Fri, 10 Sep 2021 08:34:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 18A8YBPE086241; Fri, 10 Sep 2021 08:34:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 18A8YBVd086240; Fri, 10 Sep 2021 08:34:11 GMT (envelope-from git) Date: Fri, 10 Sep 2021 08:34:11 GMT Message-Id: <202109100834.18A8YBVd086240@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Martin Matuska Subject: git: 85cff46114b5 - stable/11 - libarchive: import bugfix from upstream MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mm X-Git-Repository: src X-Git-Refname: refs/heads/stable/11 X-Git-Reftype: branch X-Git-Commit: 85cff46114b507a378461269d345ec7292fd87dd Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Sep 2021 08:34:11 -0000 The branch stable/11 has been updated by mm: URL: https://cgit.FreeBSD.org/src/commit/?id=85cff46114b507a378461269d345ec7292fd87dd commit 85cff46114b507a378461269d345ec7292fd87dd Author: Martin Matuska AuthorDate: 2021-08-27 10:51:01 +0000 Commit: Martin Matuska CommitDate: 2021-09-10 08:33:59 +0000 libarchive: import bugfix from upstream Reworked bugfix for upstream issue #1566: Do not follow symlinks when processing the fixup list (cherry picked from commit c577bdfce6b4451ab897bfe5013543e78a7f9b62) --- .../libarchive/archive_write_disk_posix.c | 62 +++++++++++++++------- .../libarchive/test/test_write_disk_fixup.c | 44 +++++++++++---- 2 files changed, 78 insertions(+), 28 deletions(-) diff --git a/contrib/libarchive/libarchive/archive_write_disk_posix.c b/contrib/libarchive/libarchive/archive_write_disk_posix.c index bcd152d9454e..a554679bfd10 100644 --- a/contrib/libarchive/libarchive/archive_write_disk_posix.c +++ b/contrib/libarchive/libarchive/archive_write_disk_posix.c @@ -2462,6 +2462,7 @@ _archive_write_disk_close(struct archive *_a) struct archive_write_disk *a = (struct archive_write_disk *)_a; struct fixup_entry *next, *p; struct stat st; + char *c; int fd, ret; archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, @@ -2475,24 +2476,49 @@ _archive_write_disk_close(struct archive *_a) while (p != NULL) { fd = -1; a->pst = NULL; /* Mark stat cache as out-of-date. */ - if (p->fixup & - (TODO_TIMES | TODO_MODE_BASE | TODO_ACLS | TODO_FFLAGS)) { - fd = open(p->name, - O_WRONLY | O_BINARY | O_NOFOLLOW | O_CLOEXEC); + + /* We must strip trailing slashes from the path to avoid + dereferencing symbolic links to directories */ + c = p->name; + while (*c != '\0') + c++; + while (c != p->name && *(c - 1) == '/') { + c--; + *c = '\0'; + } + + if (p->fixup == 0) + goto skip_fixup_entry; + else { + fd = open(p->name, O_BINARY | O_NOFOLLOW | O_RDONLY +#if defined(O_DIRECTORY) + | O_DIRECTORY +#endif + | O_CLOEXEC); + /* + ` * If we don't support O_DIRECTORY, + * or open() has failed, we must stat() + * to verify that we are opening a directory + */ +#if defined(O_DIRECTORY) if (fd == -1) { - /* If we cannot lstat, skip entry */ - if (lstat(p->name, &st) != 0) + if (lstat(p->name, &st) != 0 || + !S_ISDIR(st.st_mode)) { goto skip_fixup_entry; - /* - * If we deal with a symbolic link, mark - * it in the fixup mode to ensure no - * modifications are made to its target. - */ - if (S_ISLNK(st.st_mode)) { - p->mode &= ~S_IFMT; - p->mode |= S_IFLNK; } } +#else +#if HAVE_FSTAT + if (fd > 0 && ( + fstat(fd, &st) != 0 || !S_ISDIR(st.st_mode))) { + goto skip_fixup_entry; + } else +#endif + if (lstat(p->name, &st) != 0 || + !S_ISDIR(st.st_mode)) { + goto skip_fixup_entry; + } +#endif } if (p->fixup & TODO_TIMES) { set_times(a, fd, p->mode, p->name, @@ -2504,14 +2530,13 @@ _archive_write_disk_close(struct archive *_a) if (p->fixup & TODO_MODE_BASE) { #ifdef HAVE_FCHMOD if (fd >= 0) - fchmod(fd, p->mode); + fchmod(fd, p->mode & 07777); else #endif #ifdef HAVE_LCHMOD - lchmod(p->name, p->mode); + lchmod(p->name, p->mode & 07777); #else - if (!S_ISLNK(p->mode)) - chmod(p->name, p->mode); + chmod(p->name, p->mode & 07777); #endif } if (p->fixup & TODO_ACLS) @@ -2664,7 +2689,6 @@ new_fixup(struct archive_write_disk *a, const char *pathname) fe->next = a->fixup_list; a->fixup_list = fe; fe->fixup = 0; - fe->mode = 0; fe->name = strdup(pathname); return (fe); } diff --git a/contrib/libarchive/libarchive/test/test_write_disk_fixup.c b/contrib/libarchive/libarchive/test/test_write_disk_fixup.c index c399c9842e46..b83b73079290 100644 --- a/contrib/libarchive/libarchive/test/test_write_disk_fixup.c +++ b/contrib/libarchive/libarchive/test/test_write_disk_fixup.c @@ -47,26 +47,50 @@ DEFINE_TEST(test_write_disk_fixup) /* * Create a file */ - assertMakeFile("victim", 0600, "a"); + assertMakeFile("file", 0600, "a"); + + /* + * Create a directory + */ + assertMakeDir("dir", 0700); /* * Create a directory and a symlink with the same name */ - /* Directory: dir */ + /* Directory: dir1 */ + assert((ae = archive_entry_new()) != NULL); + archive_entry_copy_pathname(ae, "dir1/"); + archive_entry_set_mode(ae, AE_IFDIR | 0555); + assertEqualIntA(ad, 0, archive_write_header(ad, ae)); + assertEqualIntA(ad, 0, archive_write_finish_entry(ad)); + archive_entry_free(ae); + + /* Directory: dir2 */ assert((ae = archive_entry_new()) != NULL); - archive_entry_copy_pathname(ae, "dir"); - archive_entry_set_mode(ae, AE_IFDIR | 0606); + archive_entry_copy_pathname(ae, "dir2/"); + archive_entry_set_mode(ae, AE_IFDIR | 0555); assertEqualIntA(ad, 0, archive_write_header(ad, ae)); assertEqualIntA(ad, 0, archive_write_finish_entry(ad)); archive_entry_free(ae); - /* Symbolic Link: dir -> foo */ + /* Symbolic Link: dir1 -> dir */ + assert((ae = archive_entry_new()) != NULL); + archive_entry_copy_pathname(ae, "dir1"); + archive_entry_set_mode(ae, AE_IFLNK | 0777); + archive_entry_set_size(ae, 0); + archive_entry_copy_symlink(ae, "dir"); + assertEqualIntA(ad, 0, r = archive_write_header(ad, ae)); + if (r >= ARCHIVE_WARN) + assertEqualIntA(ad, 0, archive_write_finish_entry(ad)); + archive_entry_free(ae); + + /* Symbolic Link: dir2 -> file */ assert((ae = archive_entry_new()) != NULL); - archive_entry_copy_pathname(ae, "dir"); + archive_entry_copy_pathname(ae, "dir2"); archive_entry_set_mode(ae, AE_IFLNK | 0777); archive_entry_set_size(ae, 0); - archive_entry_copy_symlink(ae, "victim"); + archive_entry_copy_symlink(ae, "file"); assertEqualIntA(ad, 0, r = archive_write_header(ad, ae)); if (r >= ARCHIVE_WARN) assertEqualIntA(ad, 0, archive_write_finish_entry(ad)); @@ -75,7 +99,9 @@ DEFINE_TEST(test_write_disk_fixup) assertEqualInt(ARCHIVE_OK, archive_write_free(ad)); /* Test the entries on disk. */ - assertIsSymlink("dir", "victim", 0); - assertFileMode("victim", 0600); + assertIsSymlink("dir1", "dir", 0); + assertIsSymlink("dir2", "file", 0); + assertFileMode("dir", 0700); + assertFileMode("file", 0600); #endif }