Blame SOURCES/libarchive-3.3.3-CVE-2021-23177.patch

e4ce1a
From fba4f123cc456d2b2538f811bb831483bf336bad Mon Sep 17 00:00:00 2001
e4ce1a
From: Martin Matuska <martin@matuska.org>
e4ce1a
Date: Sat, 21 Aug 2021 20:51:07 +0200
e4ce1a
Subject: [PATCH 1/2] Fix handling of symbolic link ACLs
e4ce1a
e4ce1a
On Linux ACLs on symbolic links are not supported.
e4ce1a
We must avoid calling acl_set_file() on symbolic links as their
e4ce1a
targets are modified instead.
e4ce1a
e4ce1a
While here, do not try to set default ACLs on non-directories.
e4ce1a
e4ce1a
Fixes #1565
e4ce1a
---
e4ce1a
 libarchive/archive_disk_acl_freebsd.c | 20 +++++++++++++++-----
e4ce1a
 libarchive/archive_disk_acl_linux.c   | 23 ++++++++++++++++++++---
e4ce1a
 libarchive/archive_disk_acl_sunos.c   | 13 +++++++++----
e4ce1a
 3 files changed, 44 insertions(+), 12 deletions(-)
e4ce1a
e4ce1a
diff --git a/libarchive/archive_disk_acl_freebsd.c b/libarchive/archive_disk_acl_freebsd.c
e4ce1a
index aba41e5d..ed4e7a78 100644
e4ce1a
--- a/libarchive/archive_disk_acl_freebsd.c
e4ce1a
+++ b/libarchive/archive_disk_acl_freebsd.c
e4ce1a
@@ -319,7 +319,7 @@ translate_acl(struct archive_read_disk *a,
e4ce1a
 
e4ce1a
 static int
e4ce1a
 set_acl(struct archive *a, int fd, const char *name,
e4ce1a
-    struct archive_acl *abstract_acl,
e4ce1a
+    struct archive_acl *abstract_acl, __LA_MODE_T mode,
e4ce1a
     int ae_requested_type, const char *tname)
e4ce1a
 {
e4ce1a
 	int		 acl_type = 0;
e4ce1a
@@ -364,6 +364,13 @@ set_acl(struct archive *a, int fd, const char *name,
e4ce1a
 		return (ARCHIVE_FAILED);
e4ce1a
 	}
e4ce1a
 
e4ce1a
+	if (acl_type == ACL_TYPE_DEFAULT && !S_ISDIR(mode)) {
e4ce1a
+		errno = EINVAL;
e4ce1a
+		archive_set_error(a, errno,
e4ce1a
+		    "Cannot set default ACL on non-directory");
e4ce1a
+		return (ARCHIVE_WARN);
e4ce1a
+	}
e4ce1a
+
e4ce1a
 	acl = acl_init(entries);
e4ce1a
 	if (acl == (acl_t)NULL) {
e4ce1a
 		archive_set_error(a, errno,
e4ce1a
@@ -542,7 +549,10 @@ set_acl(struct archive *a, int fd, const char *name,
e4ce1a
 	else if (acl_set_link_np(name, acl_type, acl) != 0)
e4ce1a
 #else
e4ce1a
 	/* FreeBSD older than 8.0 */
e4ce1a
-	else if (acl_set_file(name, acl_type, acl) != 0)
e4ce1a
+	else if (S_ISLNK(mode)) {
e4ce1a
+	    /* acl_set_file() follows symbolic links, skip */
e4ce1a
+	    ret = ARCHIVE_OK;
e4ce1a
+	} else if (acl_set_file(name, acl_type, acl) != 0)
e4ce1a
 #endif
e4ce1a
 	{
e4ce1a
 		if (errno == EOPNOTSUPP) {
e4ce1a
@@ -677,14 +687,14 @@ archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
e4ce1a
 	    & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
e4ce1a
 		if ((archive_acl_types(abstract_acl)
e4ce1a
 		    & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
e4ce1a
-			ret = set_acl(a, fd, name, abstract_acl,
e4ce1a
+			ret = set_acl(a, fd, name, abstract_acl, mode,
e4ce1a
 			    ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
e4ce1a
 			if (ret != ARCHIVE_OK)
e4ce1a
 				return (ret);
e4ce1a
 		}
e4ce1a
 		if ((archive_acl_types(abstract_acl)
e4ce1a
 		    & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
e4ce1a
-			ret = set_acl(a, fd, name, abstract_acl,
e4ce1a
+			ret = set_acl(a, fd, name, abstract_acl, mode,
e4ce1a
 			    ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
e4ce1a
 
e4ce1a
 		/* Simultaneous POSIX.1e and NFSv4 is not supported */
e4ce1a
@@ -693,7 +703,7 @@ archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
e4ce1a
 #if ARCHIVE_ACL_FREEBSD_NFS4
e4ce1a
 	else if ((archive_acl_types(abstract_acl) &
e4ce1a
 	    ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
e4ce1a
-		ret = set_acl(a, fd, name, abstract_acl,
e4ce1a
+		ret = set_acl(a, fd, name, abstract_acl, mode,
e4ce1a
 		    ARCHIVE_ENTRY_ACL_TYPE_NFS4, "nfs4");
e4ce1a
 	}
e4ce1a
 #endif
e4ce1a
diff --git a/libarchive/archive_disk_acl_linux.c b/libarchive/archive_disk_acl_linux.c
e4ce1a
index 3928f3d6..31d27053 100644
e4ce1a
--- a/libarchive/archive_disk_acl_linux.c
e4ce1a
+++ b/libarchive/archive_disk_acl_linux.c
e4ce1a
@@ -343,6 +343,11 @@ set_richacl(struct archive *a, int fd, const char *name,
e4ce1a
 		return (ARCHIVE_FAILED);
e4ce1a
 	}
e4ce1a
 
e4ce1a
+	if (S_ISLNK(mode)) {
e4ce1a
+		/* Linux does not support RichACLs on symbolic links */
e4ce1a
+		return (ARCHIVE_OK);
e4ce1a
+	}
e4ce1a
+
e4ce1a
 	richacl = richacl_alloc(entries);
e4ce1a
 	if (richacl == NULL) {
e4ce1a
 		archive_set_error(a, errno,
e4ce1a
@@ -455,7 +460,7 @@ exit_free:
e4ce1a
 #if ARCHIVE_ACL_LIBACL
e4ce1a
 static int
e4ce1a
 set_acl(struct archive *a, int fd, const char *name,
e4ce1a
-    struct archive_acl *abstract_acl,
e4ce1a
+    struct archive_acl *abstract_acl, __LA_MODE_T mode,
e4ce1a
     int ae_requested_type, const char *tname)
e4ce1a
 {
e4ce1a
 	int		 acl_type = 0;
e4ce1a
@@ -488,6 +493,18 @@ set_acl(struct archive *a, int fd, const char *name,
e4ce1a
 		return (ARCHIVE_FAILED);
e4ce1a
 	}
e4ce1a
 
e4ce1a
+	if (S_ISLNK(mode)) {
e4ce1a
+		/* Linux does not support ACLs on symbolic links */
e4ce1a
+		return (ARCHIVE_OK);
e4ce1a
+	}
e4ce1a
+
e4ce1a
+	if (acl_type == ACL_TYPE_DEFAULT && !S_ISDIR(mode)) {
e4ce1a
+		errno = EINVAL;
e4ce1a
+		archive_set_error(a, errno,
e4ce1a
+		    "Cannot set default ACL on non-directory");
e4ce1a
+		return (ARCHIVE_WARN);
e4ce1a
+	}
e4ce1a
+
e4ce1a
 	acl = acl_init(entries);
e4ce1a
 	if (acl == (acl_t)NULL) {
e4ce1a
 		archive_set_error(a, errno,
e4ce1a
@@ -727,14 +744,14 @@ archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
e4ce1a
 	    & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
e4ce1a
 		if ((archive_acl_types(abstract_acl)
e4ce1a
 		    & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
e4ce1a
-			ret = set_acl(a, fd, name, abstract_acl,
e4ce1a
+			ret = set_acl(a, fd, name, abstract_acl, mode,
e4ce1a
 			    ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
e4ce1a
 			if (ret != ARCHIVE_OK)
e4ce1a
 				return (ret);
e4ce1a
 		}
e4ce1a
 		if ((archive_acl_types(abstract_acl)
e4ce1a
 		    & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
e4ce1a
-			ret = set_acl(a, fd, name, abstract_acl,
e4ce1a
+			ret = set_acl(a, fd, name, abstract_acl, mode,
e4ce1a
 			    ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
e4ce1a
 	}
e4ce1a
 #endif	/* ARCHIVE_ACL_LIBACL */
e4ce1a
diff --git a/libarchive/archive_disk_acl_sunos.c b/libarchive/archive_disk_acl_sunos.c
e4ce1a
index b0f5dfad..0ef3ad52 100644
e4ce1a
--- a/libarchive/archive_disk_acl_sunos.c
e4ce1a
+++ b/libarchive/archive_disk_acl_sunos.c
e4ce1a
@@ -443,7 +443,7 @@ translate_acl(struct archive_read_disk *a,
e4ce1a
 
e4ce1a
 static int
e4ce1a
 set_acl(struct archive *a, int fd, const char *name,
e4ce1a
-    struct archive_acl *abstract_acl,
e4ce1a
+    struct archive_acl *abstract_acl, __LA_MODE_T mode,
e4ce1a
     int ae_requested_type, const char *tname)
e4ce1a
 {
e4ce1a
 	aclent_t	 *aclent;
e4ce1a
@@ -467,7 +467,6 @@ set_acl(struct archive *a, int fd, const char *name,
e4ce1a
 	if (entries == 0)
e4ce1a
 		return (ARCHIVE_OK);
e4ce1a
 
e4ce1a
-
e4ce1a
 	switch (ae_requested_type) {
e4ce1a
 	case ARCHIVE_ENTRY_ACL_TYPE_POSIX1E:
e4ce1a
 		cmd = SETACL;
e4ce1a
@@ -492,6 +491,12 @@ set_acl(struct archive *a, int fd, const char *name,
e4ce1a
 		return (ARCHIVE_FAILED);
e4ce1a
 	}
e4ce1a
 
e4ce1a
+        if (S_ISLNK(mode)) {
e4ce1a
+                /* Skip ACLs on symbolic links */
e4ce1a
+		ret = ARCHIVE_OK;
e4ce1a
+		goto exit_free;
e4ce1a
+        }
e4ce1a
+
e4ce1a
 	e = 0;
e4ce1a
 
e4ce1a
 	while (archive_acl_next(a, abstract_acl, ae_requested_type, &ae_type,
e4ce1a
@@ -801,7 +806,7 @@ archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
e4ce1a
 	if ((archive_acl_types(abstract_acl)
e4ce1a
 	    & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
e4ce1a
 		/* Solaris writes POSIX.1e access and default ACLs together */
e4ce1a
-		ret = set_acl(a, fd, name, abstract_acl,
e4ce1a
+		ret = set_acl(a, fd, name, abstract_acl, mode,
e4ce1a
 		    ARCHIVE_ENTRY_ACL_TYPE_POSIX1E, "posix1e");
e4ce1a
 
e4ce1a
 		/* Simultaneous POSIX.1e and NFSv4 is not supported */
e4ce1a
@@ -810,7 +815,7 @@ archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
e4ce1a
 #if ARCHIVE_ACL_SUNOS_NFS4
e4ce1a
 	else if ((archive_acl_types(abstract_acl) &
e4ce1a
 	    ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
e4ce1a
-		ret = set_acl(a, fd, name, abstract_acl,
e4ce1a
+		ret = set_acl(a, fd, name, abstract_acl, mode,
e4ce1a
 		    ARCHIVE_ENTRY_ACL_TYPE_NFS4, "nfs4");
e4ce1a
 	}
e4ce1a
 #endif
e4ce1a
-- 
e4ce1a
2.31.1
e4ce1a