Blame SOURCES/CVE-2021-0920.patch

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