Blame SOURCES/CVE-2021-0920.patch

336690
From 10bde99768d3c92f5fde1ec51f74e926fc4cf779 Mon Sep 17 00:00:00 2001
336690
From: Artem Savkov <asavkov@redhat.com>
336690
Date: Thu, 27 Jan 2022 11:44:06 +0100
336690
Subject: [KPATCH CVE-2021-0920] af_unix: fix garbage collect vs MSG_PEEK
336690
336690
Kernels:
336690
- 3.10.0-1160.15.2.el7
336690
- 3.10.0-1160.21.1.el7
336690
- 3.10.0-1160.24.1.el7
336690
- 3.10.0-1160.25.1.el7
336690
- 3.10.0-1160.31.1.el7
336690
- 3.10.0-1160.36.2.el7
336690
- 3.10.0-1160.41.1.el7
336690
- 3.10.0-1160.42.2.el7
336690
- 3.10.0-1160.45.1.el7
336690
- 3.10.0-1160.49.1.el7
336690
- 3.10.0-1160.53.1.el7
336690
336690
Changes since last build:
336690
336690
arches: x86_64 ppc64le
336690
- af_unix.o: changed function: unix_dgram_recvmsg
336690
- af_unix.o: changed function: unix_stream_read_generic
336690
- garbage.o: new function: unix_peek_fds
336690
----
336690
336690
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/18
336690
Approved-by: Joe Lawrence (@joe.lawrence)
336690
Approved-by: Yannick Cote (@ycote1)
336690
Kernels:
336690
3.10.0-1160.21.1.el7
336690
3.10.0-1160.24.1.el7
336690
3.10.0-1160.25.1.el7
336690
3.10.0-1160.31.1.el7
336690
3.10.0-1160.36.2.el7
336690
3.10.0-1160.41.1.el7
336690
3.10.0-1160.42.2.el7
336690
3.10.0-1160.45.1.el7
336690
3.10.0-1160.49.1.el7
336690
3.10.0-1160.53.1.el7
336690
336690
Modifications: moved unix_peek_fds() to net/unix/garbage.c to avoid
336690
changing unix_gc_lock scope.
336690
336690
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2031986
336690
336690
Z-MR: https://gitlab.com/redhat/rhel/src/kernel/rhel-7/-/merge_requests/379
336690
336690
commit ab0fd1713f1efeb2c859d32721625ea98cd9e663
336690
Author: William Zhao <wizhao@redhat.com>
336690
Date:   Wed Jan 19 09:29:17 2022 -0500
336690
336690
    af_unix: fix garbage collect vs MSG_PEEK
336690
336690
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2031970
336690
    Upstream Status: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
336690
    CVE: CVE-2021-0920
336690
    Conflicts: The code still uses the old "sock_iocb" structure since rhel-7
336690
    does not have the commit 7cc05662682da ("net: remove sock_iocb"). Thus
336690
    passing the "scm_cookie" pointer to the "unix_peek_fds" was slightly
336690
    modified to use "siocb->scm" instead of just "&scm". Additionally
336690
    the "unix_gc_lock" is not exposed to "af_unix.c" via a header file since
336690
    rhel-7 does not have the commit f4e65870e5ce ("net: split out functions
336690
    related to registering inflight socket files"). Bringing in the commit
336690
    will add a new UNIX_SCM kernel config; this can be avoided by adding
336690
    extern-ing the "unix_gc_lock".
336690
336690
    commit cbcf01128d0a92e131bd09f1688fe032480b65ca
336690
    Author: Miklos Szeredi <mszeredi@redhat.com>
336690
    Date:   Wed Jul 28 14:47:20 2021 +0200
336690
336690
        af_unix: fix garbage collect vs MSG_PEEK
336690
336690
        unix_gc() assumes that candidate sockets can never gain an external
336690
        reference (i.e.  be installed into an fd) while the unix_gc_lock is
336690
        held.  Except for MSG_PEEK this is guaranteed by modifying inflight
336690
        count under the unix_gc_lock.
336690
336690
        MSG_PEEK does not touch any variable protected by unix_gc_lock (file
336690
        count is not), yet it needs to be serialized with garbage collection.
336690
        Do this by locking/unlocking unix_gc_lock:
336690
336690
         1) increment file count
336690
336690
         2) lock/unlock barrier to make sure incremented file count is visible
336690
            to garbage collection
336690
336690
         3) install file into fd
336690
336690
        This is a lock barrier (unlike smp_mb()) that ensures that garbage
336690
        collection is run completely before or completely after the barrier.
336690
336690
        Cc: <stable@vger.kernel.org>
336690
        Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
336690
        Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
336690
        Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
336690
    Signed-off-by: William Zhao <wizhao@redhat.com>
336690
336690
Signed-off-by: Artem Savkov <asavkov@redhat.com>
336690
---
336690
 net/unix/af_unix.c |  7 +++++--
336690
 net/unix/garbage.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++
336690
 2 files changed, 52 insertions(+), 2 deletions(-)
336690
336690
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
336690
index bcb0ad54b9b3..a264b4598872 100644
336690
--- a/net/unix/af_unix.c
336690
+++ b/net/unix/af_unix.c
336690
@@ -1468,6 +1468,8 @@ out:
336690
 	return err;
336690
 }
336690
 
336690
+void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb);
336690
+
336690
 static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
336690
 {
336690
 	int i;
336690
@@ -2182,7 +2184,8 @@ static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
336690
 		sk_peek_offset_fwd(sk, size);
336690
 
336690
 		if (UNIXCB(skb).fp)
336690
-			siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
336690
+			unix_peek_fds(siocb->scm, skb);
336690
+
336690
 	}
336690
 	err = (flags & MSG_TRUNC) ? skb->len - skip : size;
336690
 
336690
@@ -2432,7 +2435,7 @@ unlock:
336690
 			/* It is questionable, see note in unix_dgram_recvmsg.
336690
 			 */
336690
 			if (UNIXCB(skb).fp)
336690
-				siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp);
336690
+				unix_peek_fds(siocb->scm, skb);
336690
 
336690
 			sk_peek_offset_fwd(sk, chunk);
336690
 
336690
diff --git a/net/unix/garbage.c b/net/unix/garbage.c
336690
index c36757e72844..f242268477ba 100644
336690
--- a/net/unix/garbage.c
336690
+++ b/net/unix/garbage.c
336690
@@ -374,3 +374,50 @@ void unix_gc(void)
336690
  out:
336690
 	spin_unlock(&unix_gc_lock);
336690
 }
336690
+
336690
+void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
336690
+{
336690
+	scm->fp = scm_fp_dup(UNIXCB(skb).fp);
336690
+
336690
+	/*
336690
+	 * Garbage collection of unix sockets starts by selecting a set of
336690
+	 * candidate sockets which have reference only from being in flight
336690
+	 * (total_refs == inflight_refs).  This condition is checked once during
336690
+	 * the candidate collection phase, and candidates are marked as such, so
336690
+	 * that non-candidates can later be ignored.  While inflight_refs is
336690
+	 * protected by unix_gc_lock, total_refs (file count) is not, hence this
336690
+	 * is an instantaneous decision.
336690
+	 *
336690
+	 * Once a candidate, however, the socket must not be reinstalled into a
336690
+	 * file descriptor while the garbage collection is in progress.
336690
+	 *
336690
+	 * If the above conditions are met, then the directed graph of
336690
+	 * candidates (*) does not change while unix_gc_lock is held.
336690
+	 *
336690
+	 * Any operations that changes the file count through file descriptors
336690
+	 * (dup, close, sendmsg) does not change the graph since candidates are
336690
+	 * not installed in fds.
336690
+	 *
336690
+	 * Dequeing a candidate via recvmsg would install it into an fd, but
336690
+	 * that takes unix_gc_lock to decrement the inflight count, so it's
336690
+	 * serialized with garbage collection.
336690
+	 *
336690
+	 * MSG_PEEK is special in that it does not change the inflight count,
336690
+	 * yet does install the socket into an fd.  The following lock/unlock
336690
+	 * pair is to ensure serialization with garbage collection.  It must be
336690
+	 * done between incrementing the file count and installing the file into
336690
+	 * an fd.
336690
+	 *
336690
+	 * If garbage collection starts after the barrier provided by the
336690
+	 * lock/unlock, then it will see the elevated refcount and not mark this
336690
+	 * as a candidate.  If a garbage collection is already in progress
336690
+	 * before the file count was incremented, then the lock/unlock pair will
336690
+	 * ensure that garbage collection is finished before progressing to
336690
+	 * installing the fd.
336690
+	 *
336690
+	 * (*) A -> B where B is on the queue of A or B is on the queue of C
336690
+	 * which is on the queue of listening socket A.
336690
+	 */
336690
+	spin_lock(&unix_gc_lock);
336690
+	spin_unlock(&unix_gc_lock);
336690
+}
336690
-- 
336690
2.26.3
336690
336690