Blame SOURCES/0004-logrotate-3.18.0-CVE-2022-1348.patch

f167cd
From 53e0dc4a8ddcb169b0ba36472de03f4366f45159 Mon Sep 17 00:00:00 2001
f167cd
From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= <cgzones@googlemail.com>
f167cd
Date: Tue, 29 Mar 2022 21:06:54 +0200
f167cd
Subject: [PATCH 1/3] skip locking if state file is world-readable
f167cd
f167cd
Fixes: CVE-2022-1348 - potential DoS from unprivileged users via the state file
f167cd
Bug: https://bugzilla.redhat.com/CVE-2022-1348
f167cd
f167cd
Upstream-commit: 1f76a381e2caa0603ae3dbc51ed0f1aa0d6658b9
f167cd
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
f167cd
---
f167cd
 logrotate.c            | 24 ++++++++++++++++++++++--
f167cd
 logrotate.spec.in      |  3 +--
f167cd
 test/Makefile.am       |  1 +
f167cd
 test/test-0087.sh      |  1 +
f167cd
 test/test-0092.sh      | 19 +++++++++++++++++++
f167cd
 test/test-config.92.in |  4 ++++
f167cd
 6 files changed, 48 insertions(+), 4 deletions(-)
f167cd
 create mode 100755 test/test-0092.sh
f167cd
 create mode 100644 test/test-config.92.in
f167cd
f167cd
diff --git a/logrotate.c b/logrotate.c
f167cd
index d3f2825..78153b3 100644
f167cd
--- a/logrotate.c
f167cd
+++ b/logrotate.c
f167cd
@@ -2565,6 +2565,9 @@ static int writeState(const char *stateFilename)
f167cd
 
f167cd
     close(fdcurr);
f167cd
 
f167cd
+    /* drop world-readable flag to prevent others from locking */
f167cd
+    sb.st_mode &= ~(mode_t)S_IROTH;
f167cd
+
f167cd
     fdsave = createOutputFile(tmpFilename, O_RDWR | O_CREAT | O_TRUNC, &sb, prev_acl, 0);
f167cd
 #ifdef WITH_ACL
f167cd
     if (prev_acl) {
f167cd
@@ -2898,15 +2901,16 @@ static int readState(const char *stateFilename)
f167cd
 
f167cd
 static int lockState(const char *stateFilename, int skip_state_lock)
f167cd
 {
f167cd
+    struct stat sb;
f167cd
     int lockFd = open(stateFilename, O_RDWR | O_CLOEXEC);
f167cd
     if (lockFd == -1) {
f167cd
         if (errno == ENOENT) {
f167cd
             message(MESS_DEBUG, "Creating stub state file: %s\n",
f167cd
                     stateFilename);
f167cd
 
f167cd
-            /* create a stub state file with mode 0644 */
f167cd
+            /* create a stub state file with mode 0640 */
f167cd
             lockFd = open(stateFilename, O_CREAT | O_EXCL | O_WRONLY,
f167cd
-                          S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
f167cd
+                          S_IWUSR | S_IRUSR | S_IRGRP);
f167cd
             if (lockFd == -1) {
f167cd
                 message(MESS_ERROR, "error creating stub state file %s: %s\n",
f167cd
                         stateFilename, strerror(errno));
f167cd
@@ -2926,6 +2930,22 @@ static int lockState(const char *stateFilename, int skip_state_lock)
f167cd
         return 0;
f167cd
     }
f167cd
 
f167cd
+    if (fstat(lockFd, &sb) == -1) {
f167cd
+        message(MESS_ERROR, "error stat()ing state file %s: %s\n",
f167cd
+                stateFilename, strerror(errno));
f167cd
+        close(lockFd);
f167cd
+        return 1;
f167cd
+    }
f167cd
+
f167cd
+    if (sb.st_mode & S_IROTH) {
f167cd
+        message(MESS_ERROR, "state file %s is world-readable and thus can"
f167cd
+                " be locked from other unprivileged users."
f167cd
+                " Skipping lock acquisition...\n",
f167cd
+                stateFilename);
f167cd
+        close(lockFd);
f167cd
+        return 0;
f167cd
+    }
f167cd
+
f167cd
     if (flock(lockFd, LOCK_EX | LOCK_NB) == -1) {
f167cd
         if (errno == EWOULDBLOCK) {
f167cd
             message(MESS_ERROR, "state file %s is already locked\n"
f167cd
diff --git a/logrotate.spec.in b/logrotate.spec.in
f167cd
index 92e1d97..3caabf2 100644
f167cd
--- a/logrotate.spec.in
f167cd
+++ b/logrotate.spec.in
f167cd
@@ -41,7 +41,6 @@ install -p -m 644 examples/logrotate.conf $RPM_BUILD_ROOT%{_sysconfdir}/logrotat
f167cd
 install -p -m 644 examples/btmp $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/btmp
f167cd
 install -p -m 644 examples/wtmp $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/wtmp
f167cd
 install -p -m 755 examples/logrotate.cron $RPM_BUILD_ROOT%{_sysconfdir}/cron.daily/logrotate
f167cd
-touch $RPM_BUILD_ROOT%{_localstatedir}/lib/logrotate.status
f167cd
 
f167cd
 %clean
f167cd
 rm -rf $RPM_BUILD_ROOT
f167cd
@@ -55,4 +54,4 @@ rm -rf $RPM_BUILD_ROOT
f167cd
 %attr(0755, root, root) %{_sysconfdir}/cron.daily/logrotate
f167cd
 %attr(0644, root, root) %config(noreplace) %{_sysconfdir}/logrotate.conf
f167cd
 %attr(0755, root, root) %{_sysconfdir}/logrotate.d
f167cd
-%attr(0644, root, root) %verify(not size md5 mtime) %config(noreplace) %{_localstatedir}/lib/logrotate.status
f167cd
+%ghost %attr(0640, root, root) %verify(not size md5 mtime) %{_localstatedir}/lib/logrotate.status
f167cd
diff --git a/test/Makefile.am b/test/Makefile.am
f167cd
index 914fe65..d6fb7c8 100644
f167cd
--- a/test/Makefile.am
f167cd
+++ b/test/Makefile.am
f167cd
@@ -87,6 +87,7 @@ TEST_CASES = \
f167cd
 	test-0086.sh \
f167cd
 	test-0087.sh \
f167cd
 	test-0088.sh \
f167cd
+	test-0092.sh \
f167cd
 	test-0100.sh \
f167cd
 	test-0101.sh
f167cd
 
f167cd
diff --git a/test/test-0087.sh b/test/test-0087.sh
f167cd
index 91e5266..aeff2c6 100755
f167cd
--- a/test/test-0087.sh
f167cd
+++ b/test/test-0087.sh
f167cd
@@ -8,6 +8,7 @@ cleanup 87
f167cd
 preptest test.log 87 1
f167cd
 
f167cd
 touch state
f167cd
+chmod 0640 state
f167cd
 
f167cd
 $RLR test-config.87 -f &
f167cd
 
f167cd
diff --git a/test/test-0092.sh b/test/test-0092.sh
f167cd
new file mode 100755
f167cd
index 0000000..be52e14
f167cd
--- /dev/null
f167cd
+++ b/test/test-0092.sh
f167cd
@@ -0,0 +1,19 @@
f167cd
+#!/bin/sh
f167cd
+
f167cd
+. ./test-common.sh
f167cd
+
f167cd
+# check state file locking
f167cd
+cleanup 92
f167cd
+
f167cd
+preptest test.log 92 1
f167cd
+
f167cd
+touch state
f167cd
+chmod 0644 state
f167cd
+flock state -c "sleep 10" &
f167cd
+
f167cd
+$RLR -f test-config.92 || exit 23
f167cd
+
f167cd
+checkoutput <
f167cd
+test.log 0
f167cd
+test.log.1 0 zero
f167cd
+EOF
f167cd
diff --git a/test/test-config.92.in b/test/test-config.92.in
f167cd
new file mode 100644
f167cd
index 0000000..ac93900
f167cd
--- /dev/null
f167cd
+++ b/test/test-config.92.in
f167cd
@@ -0,0 +1,4 @@
f167cd
+&DIR&/test.log {
f167cd
+    rotate 1
f167cd
+    create
f167cd
+}
f167cd
-- 
f167cd
2.35.3
f167cd
f167cd
f167cd
From 0d2d770cc5aa7bf14e84a2832249eeeb391b0b8a Mon Sep 17 00:00:00 2001
f167cd
From: Kamil Dudka <kdudka@redhat.com>
f167cd
Date: Wed, 25 May 2022 09:55:02 +0200
f167cd
Subject: [PATCH 2/3] drop world-readable permission on state file
f167cd
f167cd
... even when ACLs are enabled.  This is a follow-up to the fix
f167cd
of CVE-2022-1348.  It has no impact on security but makes the state
f167cd
file locking work again in more cases.
f167cd
f167cd
Closes: https://github.com/logrotate/logrotate/pull/446
f167cd
f167cd
Upstream-commit: addbd293242b0b78aa54f054e6c1d249451f137d
f167cd
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
f167cd
---
f167cd
 logrotate.c       | 10 +++++++---
f167cd
 test/test-0048.sh |  1 +
f167cd
 2 files changed, 8 insertions(+), 3 deletions(-)
f167cd
f167cd
diff --git a/logrotate.c b/logrotate.c
f167cd
index 78153b3..8d49f26 100644
f167cd
--- a/logrotate.c
f167cd
+++ b/logrotate.c
f167cd
@@ -2498,6 +2498,7 @@ static int writeState(const char *stateFilename)
f167cd
     struct tm now;
f167cd
     time_t now_time, last_time;
f167cd
     char *prevCtx;
f167cd
+    int force_mode = 0;
f167cd
 
f167cd
     localtime_r(&nowSecs, &now;;
f167cd
 
f167cd
@@ -2565,10 +2566,13 @@ static int writeState(const char *stateFilename)
f167cd
 
f167cd
     close(fdcurr);
f167cd
 
f167cd
-    /* drop world-readable flag to prevent others from locking */
f167cd
-    sb.st_mode &= ~(mode_t)S_IROTH;
f167cd
+    if (sb.st_mode & (mode_t)S_IROTH) {
f167cd
+        /* drop world-readable flag to prevent others from locking */
f167cd
+        sb.st_mode &= ~(mode_t)S_IROTH;
f167cd
+        force_mode = 1;
f167cd
+    }
f167cd
 
f167cd
-    fdsave = createOutputFile(tmpFilename, O_RDWR | O_CREAT | O_TRUNC, &sb, prev_acl, 0);
f167cd
+    fdsave = createOutputFile(tmpFilename, O_RDWR | O_CREAT | O_TRUNC, &sb, prev_acl, force_mode);
f167cd
 #ifdef WITH_ACL
f167cd
     if (prev_acl) {
f167cd
         acl_free(prev_acl);
f167cd
diff --git a/test/test-0048.sh b/test/test-0048.sh
f167cd
index 62d606b..06b255a 100755
f167cd
--- a/test/test-0048.sh
f167cd
+++ b/test/test-0048.sh
f167cd
@@ -18,6 +18,7 @@ cat > state << EOF
f167cd
 logrotate state -- version 2
f167cd
 EOF
f167cd
 
f167cd
+chmod 0640 state
f167cd
 setfacl -m u:nobody:rwx state
f167cd
 
f167cd
 $RLR test-config.48
f167cd
-- 
f167cd
2.35.3
f167cd
f167cd
f167cd
From 105ed9f433a3aaf1aec93318aa9c8811b59d7b23 Mon Sep 17 00:00:00 2001
f167cd
From: Kamil Dudka <kdudka@redhat.com>
f167cd
Date: Fri, 27 May 2022 09:56:07 +0200
f167cd
Subject: [PATCH 3/3] lockState: do not print `error:` when exit code is
f167cd
 unaffected
f167cd
f167cd
Closes: https://github.com/logrotate/logrotate/pull/448
f167cd
f167cd
Upstream-commit: 31cf1099ab8514dfcae5a980bc77352edd5292f8
f167cd
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
f167cd
---
f167cd
 logrotate.c | 4 ++--
f167cd
 1 file changed, 2 insertions(+), 2 deletions(-)
f167cd
f167cd
diff --git a/logrotate.c b/logrotate.c
f167cd
index 27deaf3..77db8c2 100644
f167cd
--- a/logrotate.c
f167cd
+++ b/logrotate.c
f167cd
@@ -2942,8 +2942,8 @@ static int lockState(const char *stateFilename, int skip_state_lock)
f167cd
     }
f167cd
 
f167cd
     if (sb.st_mode & S_IROTH) {
f167cd
-        message(MESS_ERROR, "state file %s is world-readable and thus can"
f167cd
-                " be locked from other unprivileged users."
f167cd
+        message(MESS_NORMAL, "warning: state file %s is world-readable"
f167cd
+                " and thus can be locked from other unprivileged users."
f167cd
                 " Skipping lock acquisition...\n",
f167cd
                 stateFilename);
f167cd
         close(lockFd);
f167cd
-- 
f167cd
2.35.3
f167cd