Blame SOURCES/CVE-2021-0920.patch

c38cbc
From 18c390171a63c1be8df1a62172214efbd9e84813 Mon Sep 17 00:00:00 2001
c38cbc
From: Joe Lawrence <joe.lawrence@redhat.com>
c38cbc
Date: Thu, 20 Jan 2022 14:39:19 -0500
c38cbc
Subject: [KPATCH CVE-2021-0920] af_unix: kpatch fixes for CVE-2021-0920
c38cbc
c38cbc
Kernels:
c38cbc
4.18.0-348.el8
c38cbc
4.18.0-348.2.1.el8_5
c38cbc
4.18.0-348.7.1.el8_5
c38cbc
4.18.0-348.12.2.el8_5
c38cbc
c38cbc
Changes since last build:
c38cbc
arches: x86_64 ppc64le
c38cbc
af_unix.o: changed function: unix_dgram_recvmsg
c38cbc
af_unix.o: changed function: unix_stream_read_generic
c38cbc
---------------------------
c38cbc
c38cbc
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-8/-/merge_requests/17
c38cbc
Approved-by: Yannick Cote (@ycote1)
c38cbc
Approved-by: Artem Savkov (@artem.savkov)
c38cbc
Kernels:
c38cbc
4.18.0-348.el8
c38cbc
4.18.0-348.2.1.el8_5
c38cbc
4.18.0-348.7.1.el8_5
c38cbc
4.18.0-348.12.2.el8_5
c38cbc
c38cbc
Modifications: none
c38cbc
c38cbc
commit 49c79494c048e940be91a9454c2f507bc33680fc
c38cbc
Author: Patrick Talbert <ptalbert@redhat.com>
c38cbc
Date:   Mon Jan 10 13:13:05 2022 +0100
c38cbc
c38cbc
    af_unix: fix garbage collect vs MSG_PEEK
c38cbc
c38cbc
    Bugzilla: https://bugzilla.redhat.com/2031974
c38cbc
    CVE: CVE-2021-0920
c38cbc
    Y-Commit: 35c0f6eeb4644e87e7f3c1198a9f31b76220053d
c38cbc
c38cbc
    O-CVE: CVE-2021-0920
c38cbc
    O-Bugzilla: https://bugzilla.redhat.com/2031975
c38cbc
    Upstream status: main
c38cbc
    Testing: Sanity only
c38cbc
c38cbc
    commit cbcf01128d0a92e131bd09f1688fe032480b65ca
c38cbc
    Author: Miklos Szeredi <mszeredi@redhat.com>
c38cbc
    Date:   Wed Jul 28 14:47:20 2021 +0200
c38cbc
c38cbc
        af_unix: fix garbage collect vs MSG_PEEK
c38cbc
c38cbc
        unix_gc() assumes that candidate sockets can never gain an external
c38cbc
        reference (i.e.  be installed into an fd) while the unix_gc_lock is
c38cbc
        held.  Except for MSG_PEEK this is guaranteed by modifying inflight
c38cbc
        count under the unix_gc_lock.
c38cbc
c38cbc
        MSG_PEEK does not touch any variable protected by unix_gc_lock (file
c38cbc
        count is not), yet it needs to be serialized with garbage collection.
c38cbc
        Do this by locking/unlocking unix_gc_lock:
c38cbc
c38cbc
         1) increment file count
c38cbc
c38cbc
         2) lock/unlock barrier to make sure incremented file count is visible
c38cbc
            to garbage collection
c38cbc
c38cbc
         3) install file into fd
c38cbc
c38cbc
        This is a lock barrier (unlike smp_mb()) that ensures that garbage
c38cbc
        collection is run completely before or completely after the barrier.
c38cbc
c38cbc
        Cc: <stable@vger.kernel.org>
c38cbc
        Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
c38cbc
        Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
c38cbc
        Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
c38cbc
c38cbc
    Signed-off-by: Patrick Talbert <ptalbert@redhat.com>
c38cbc
c38cbc
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
c38cbc
---
c38cbc
 net/unix/af_unix.c | 51 ++++++++++++++++++++++++++++++++++++++++++++--
c38cbc
 1 file changed, 49 insertions(+), 2 deletions(-)
c38cbc
c38cbc
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
c38cbc
index 247e3138d1ef..d9c968caaf20 100644
c38cbc
--- a/net/unix/af_unix.c
c38cbc
+++ b/net/unix/af_unix.c
c38cbc
@@ -1498,6 +1498,53 @@ static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
c38cbc
 	return err;
c38cbc
 }
c38cbc
 
c38cbc
+static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
c38cbc
+{
c38cbc
+	scm->fp = scm_fp_dup(UNIXCB(skb).fp);
c38cbc
+
c38cbc
+	/*
c38cbc
+	 * Garbage collection of unix sockets starts by selecting a set of
c38cbc
+	 * candidate sockets which have reference only from being in flight
c38cbc
+	 * (total_refs == inflight_refs).  This condition is checked once during
c38cbc
+	 * the candidate collection phase, and candidates are marked as such, so
c38cbc
+	 * that non-candidates can later be ignored.  While inflight_refs is
c38cbc
+	 * protected by unix_gc_lock, total_refs (file count) is not, hence this
c38cbc
+	 * is an instantaneous decision.
c38cbc
+	 *
c38cbc
+	 * Once a candidate, however, the socket must not be reinstalled into a
c38cbc
+	 * file descriptor while the garbage collection is in progress.
c38cbc
+	 *
c38cbc
+	 * If the above conditions are met, then the directed graph of
c38cbc
+	 * candidates (*) does not change while unix_gc_lock is held.
c38cbc
+	 *
c38cbc
+	 * Any operations that changes the file count through file descriptors
c38cbc
+	 * (dup, close, sendmsg) does not change the graph since candidates are
c38cbc
+	 * not installed in fds.
c38cbc
+	 *
c38cbc
+	 * Dequeing a candidate via recvmsg would install it into an fd, but
c38cbc
+	 * that takes unix_gc_lock to decrement the inflight count, so it's
c38cbc
+	 * serialized with garbage collection.
c38cbc
+	 *
c38cbc
+	 * MSG_PEEK is special in that it does not change the inflight count,
c38cbc
+	 * yet does install the socket into an fd.  The following lock/unlock
c38cbc
+	 * pair is to ensure serialization with garbage collection.  It must be
c38cbc
+	 * done between incrementing the file count and installing the file into
c38cbc
+	 * an fd.
c38cbc
+	 *
c38cbc
+	 * If garbage collection starts after the barrier provided by the
c38cbc
+	 * lock/unlock, then it will see the elevated refcount and not mark this
c38cbc
+	 * as a candidate.  If a garbage collection is already in progress
c38cbc
+	 * before the file count was incremented, then the lock/unlock pair will
c38cbc
+	 * ensure that garbage collection is finished before progressing to
c38cbc
+	 * installing the fd.
c38cbc
+	 *
c38cbc
+	 * (*) A -> B where B is on the queue of A or B is on the queue of C
c38cbc
+	 * which is on the queue of listening socket A.
c38cbc
+	 */
c38cbc
+	spin_lock(&unix_gc_lock);
c38cbc
+	spin_unlock(&unix_gc_lock);
c38cbc
+}
c38cbc
+
c38cbc
 static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
c38cbc
 {
c38cbc
 	int err = 0;
c38cbc
@@ -2124,7 +2171,7 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
c38cbc
 		sk_peek_offset_fwd(sk, size);
c38cbc
 
c38cbc
 		if (UNIXCB(skb).fp)
c38cbc
-			scm.fp = scm_fp_dup(UNIXCB(skb).fp);
c38cbc
+			unix_peek_fds(&scm, skb);
c38cbc
 	}
c38cbc
 	err = (flags & MSG_TRUNC) ? skb->len - skip : size;
c38cbc
 
c38cbc
@@ -2365,7 +2412,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
c38cbc
 			/* It is questionable, see note in unix_dgram_recvmsg.
c38cbc
 			 */
c38cbc
 			if (UNIXCB(skb).fp)
c38cbc
-				scm.fp = scm_fp_dup(UNIXCB(skb).fp);
c38cbc
+				unix_peek_fds(&scm, skb);
c38cbc
 
c38cbc
 			sk_peek_offset_fwd(sk, chunk);
c38cbc
 
c38cbc
-- 
c38cbc
2.34.1
c38cbc
c38cbc