Blame SOURCES/CVE-2021-0920.patch

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