Blame SOURCES/CVE-2021-0920.patch

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