Blame SOURCES/CVE-2021-4083.patch

d44c23
From 80c7e812dffa734599aadde93cb8e30b34f0983d Mon Sep 17 00:00:00 2001
d44c23
From: Joe Lawrence <joe.lawrence@redhat.com>
d44c23
Date: Mon, 21 Mar 2022 15:45:03 -0400
d44c23
Subject: [KPATCH CVE-2021-4083] fget: kpatch fixes for CVE-2021-4083
d44c23
d44c23
Kernels:
d44c23
3.10.0-1160.24.1.el7
d44c23
3.10.0-1160.25.1.el7
d44c23
3.10.0-1160.31.1.el7
d44c23
3.10.0-1160.36.2.el7
d44c23
3.10.0-1160.41.1.el7
d44c23
3.10.0-1160.42.2.el7
d44c23
3.10.0-1160.45.1.el7
d44c23
3.10.0-1160.49.1.el7
d44c23
3.10.0-1160.53.1.el7
d44c23
3.10.0-1160.59.1.el7
d44c23
d44c23
Changes since last build:
d44c23
arches: x86_64 ppc64le
d44c23
file.o: changed function: SyS_dup
d44c23
file.o: changed function: dup_fd
d44c23
file.o: changed function: fget
d44c23
file.o: changed function: fget_light
d44c23
file.o: changed function: fget_raw
d44c23
file.o: changed function: fget_raw_light
d44c23
file.o: changed function: put_files_struct
d44c23
file.o: new function: __fget
d44c23
file.o: new function: __fget_light
d44c23
---------------------------
d44c23
d44c23
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/34
d44c23
Approved-by: Yannick Cote (@ycote1)
d44c23
Modifications:
d44c23
- include/linux/rcupdate.h, kernel/rcupdate.c: leave exported
d44c23
  rcu_my_thread_group_empty() intact
d44c23
- fs/file.c: use fput() instead of fputs_many() since we skipped commit
d44c23
  ("fs: add fget_many() and fput_many()")
d44c23
- fs/file.c: use fcheck_files() instead of files_lookup_fd_raw() since
d44c23
  we are skipping subsequent commit ("fget: clarify and improve
d44c23
  __fget_files() implementation") that provides it.
d44c23
- Set __attribute__((optimize("-fno-optimize-sibling-calls"))) for
d44c23
  fget() and fget_raw() on ppc64le
d44c23
d44c23
commit c2207a235113315ad696b06eb96ccd36d1f5fdeb
d44c23
Author: Miklos Szeredi <mszeredi@redhat.com>
d44c23
Date:   Fri Jan 21 10:22:29 2022 +0100
d44c23
d44c23
    introduce __fcheck_files() to fix rcu_dereference_check_fdtable(), kill rcu_my_thread_group_empty()
d44c23
d44c23
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2032478
d44c23
    Upstream status: Linus
d44c23
    Testing: xfstests
d44c23
    CVE: CVE-2021-4083
d44c23
    Conflicts:
d44c23
            - context difference due to backport of later patch
d44c23
            - target file difference due to missing backport of rcu source code
d44c23
              move
d44c23
d44c23
    commit a8d4b8345e0ee48b732126d980efaf0dc373e2b0
d44c23
    Author: Oleg Nesterov <oleg@redhat.com>
d44c23
    Date:   Sat Jan 11 19:19:32 2014 +0100
d44c23
d44c23
        introduce __fcheck_files() to fix rcu_dereference_check_fdtable(), kill rcu_my_thread_group_empty()
d44c23
d44c23
        rcu_dereference_check_fdtable() looks very wrong,
d44c23
d44c23
        1. rcu_my_thread_group_empty() was added by 844b9a8707f1 "vfs: fix
d44c23
           RCU-lockdep false positive due to /proc" but it doesn't really
d44c23
           fix the problem. A CLONE_THREAD (without CLONE_FILES) task can
d44c23
           hit the same race with get_files_struct().
d44c23
d44c23
           And otoh rcu_my_thread_group_empty() can suppress the correct
d44c23
           warning if the caller is the CLONE_FILES (without CLONE_THREAD)
d44c23
           task.
d44c23
d44c23
        2. files->count == 1 check is not really right too. Even if this
d44c23
           files_struct is not shared it is not safe to access it lockless
d44c23
           unless the caller is the owner.
d44c23
d44c23
           Otoh, this check is sub-optimal. files->count == 0 always means
d44c23
           it is safe to use it lockless even if files != current->files,
d44c23
           but put_files_struct() has to take rcu_read_lock(). See the next
d44c23
           patch.
d44c23
d44c23
        This patch removes the buggy checks and turns fcheck_files() into
d44c23
        __fcheck_files() which uses rcu_dereference_raw(), the "unshared"
d44c23
        callers, fget_light() and fget_raw_light(), can use it to avoid
d44c23
        the warning from RCU-lockdep.
d44c23
d44c23
        fcheck_files() is trivially reimplemented as rcu_lockdep_assert()
d44c23
        plus __fcheck_files().
d44c23
d44c23
        Signed-off-by: Oleg Nesterov <oleg@redhat.com>
d44c23
        Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
d44c23
d44c23
    Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
d44c23
d44c23
commit ec06bac02991edcfdeb148ab2fe7f3e2d7d3ceaa
d44c23
Author: Miklos Szeredi <mszeredi@redhat.com>
d44c23
Date:   Fri Jan 21 10:22:30 2022 +0100
d44c23
d44c23
    fs: factor out common code in fget() and fget_raw()
d44c23
d44c23
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2032478
d44c23
    Upstream status: Linus
d44c23
    Testing: xfstests
d44c23
    CVE: CVE-2021-4083
d44c23
    Conflicts:
d44c23
            - difference due to backport of later patch
d44c23
d44c23
    commit 1deb46e2562561255c34075825fd00f22a858bb3
d44c23
    Author: Oleg Nesterov <oleg@redhat.com>
d44c23
    Date:   Mon Jan 13 16:48:19 2014 +0100
d44c23
d44c23
        fs: factor out common code in fget() and fget_raw()
d44c23
d44c23
        Apart from FMODE_PATH check fget() and fget_raw() are identical,
d44c23
        shift the code into the new simple helper, __fget(fd, mask). Saves
d44c23
        160 bytes.
d44c23
d44c23
        Signed-off-by: Oleg Nesterov <oleg@redhat.com>
d44c23
        Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
d44c23
d44c23
    Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
d44c23
d44c23
commit ac43fab520f6836e2a7d3d20dd64d6328233ccbe
d44c23
Author: Miklos Szeredi <mszeredi@redhat.com>
d44c23
Date:   Fri Jan 21 10:22:30 2022 +0100
d44c23
d44c23
    fs: factor out common code in fget_light() and fget_raw_light()
d44c23
d44c23
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2032478
d44c23
    Upstream status: Linus
d44c23
    Testing: xfstests
d44c23
    CVE: CVE-2021-4083
d44c23
d44c23
    commit ad46183445043b562856c60b74db664668fb364b
d44c23
    Author: Oleg Nesterov <oleg@redhat.com>
d44c23
    Date:   Mon Jan 13 16:48:40 2014 +0100
d44c23
d44c23
        fs: factor out common code in fget_light() and fget_raw_light()
d44c23
d44c23
        Apart from FMODE_PATH check fget_light() and fget_raw_light() are
d44c23
        identical, shift the code into the new helper, __fget_light(fd, mask).
d44c23
        Saves 208 bytes.
d44c23
d44c23
        Signed-off-by: Oleg Nesterov <oleg@redhat.com>
d44c23
        Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
d44c23
d44c23
    Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
d44c23
d44c23
commit 9e24c8894f5df488a336f0c848f15a7d2f78d163
d44c23
Author: Miklos Szeredi <mszeredi@redhat.com>
d44c23
Date:   Fri Jan 21 10:22:30 2022 +0100
d44c23
d44c23
    fs: __fget_light() can use __fget() in slow path
d44c23
d44c23
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2032478
d44c23
    Upstream status: Linus
d44c23
    Testing: xfstests
d44c23
    CVE: CVE-2021-4083
d44c23
d44c23
    commit e6ff9a9fa4e05c1c03dec63cdc6a87d6dea02755
d44c23
    Author: Oleg Nesterov <oleg@redhat.com>
d44c23
    Date:   Mon Jan 13 16:49:06 2014 +0100
d44c23
d44c23
        fs: __fget_light() can use __fget() in slow path
d44c23
d44c23
        The slow path in __fget_light() can use __fget() to avoid the
d44c23
        code duplication. Saves 232 bytes.
d44c23
d44c23
        Signed-off-by: Oleg Nesterov <oleg@redhat.com>
d44c23
        Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
d44c23
d44c23
    Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
d44c23
d44c23
commit d63fb584ae2d7d9a1620e23e59072cb6929f3833
d44c23
Author: Miklos Szeredi <mszeredi@redhat.com>
d44c23
Date:   Fri Jan 21 10:22:30 2022 +0100
d44c23
d44c23
    fs/file.c: __fget() and dup2() atomicity rules
d44c23
d44c23
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2032478
d44c23
    Upstream status: Linus
d44c23
    Testing: xfstests
d44c23
    CVE: CVE-2021-4083
d44c23
d44c23
    commit 5ba97d2832f87943c43bb69cb1ef86dbc59df5bc
d44c23
    Author: Eric Dumazet <edumazet@google.com>
d44c23
    Date:   Mon Jun 29 17:10:30 2015 +0200
d44c23
d44c23
        fs/file.c: __fget() and dup2() atomicity rules
d44c23
d44c23
        __fget() does lockless fetch of pointer from the descriptor
d44c23
        table, attempts to grab a reference and treats "it was already
d44c23
        zero" as "it's already gone from the table, we just hadn't
d44c23
        seen the store, let's fail".  Unfortunately, that breaks the
d44c23
        atomicity of dup2() - __fget() might see the old pointer,
d44c23
        notice that it's been already dropped and treat that as
d44c23
        "it's closed".  What we should be getting is either the
d44c23
        old file or new one, depending whether we come before or after
d44c23
        dup2().
d44c23
d44c23
        Dmitry had following test failing sometimes :
d44c23
d44c23
        int fd;
d44c23
        void *Thread(void *x) {
d44c23
          char buf;
d44c23
          int n = read(fd, &buf, 1);
d44c23
          if (n != 1)
d44c23
            exit(printf("read failed: n=%d errno=%d\n", n, errno));
d44c23
          return 0;
d44c23
        }
d44c23
d44c23
        int main()
d44c23
        {
d44c23
          fd = open("/dev/urandom", O_RDONLY);
d44c23
          int fd2 = open("/dev/urandom", O_RDONLY);
d44c23
          if (fd == -1 || fd2 == -1)
d44c23
            exit(printf("open failed\n"));
d44c23
          pthread_t th;
d44c23
          pthread_create(&th, 0, Thread, 0);
d44c23
          if (dup2(fd2, fd) == -1)
d44c23
            exit(printf("dup2 failed\n"));
d44c23
          pthread_join(th, 0);
d44c23
          if (close(fd) == -1)
d44c23
            exit(printf("close failed\n"));
d44c23
          if (close(fd2) == -1)
d44c23
            exit(printf("close failed\n"));
d44c23
          printf("DONE\n");
d44c23
          return 0;
d44c23
        }
d44c23
d44c23
        Signed-off-by: Eric Dumazet <edumazet@google.com>
d44c23
        Reported-by: Dmitry Vyukov <dvyukov@google.com>
d44c23
        Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
d44c23
d44c23
    Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
d44c23
d44c23
commit bc04a10c9303dcd9a6305a0452361537257fa0c1
d44c23
Author: Miklos Szeredi <mszeredi@redhat.com>
d44c23
Date:   Fri Jan 21 10:22:31 2022 +0100
d44c23
d44c23
    fget: check that the fd still exists after getting a ref to it
d44c23
d44c23
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2032478
d44c23
    Upstream status: Linus
d44c23
    Testing: xfstests
d44c23
    CVE: CVE-2021-4083
d44c23
d44c23
    commit 054aa8d439b9185d4f5eb9a90282d1ce74772969
d44c23
    Author: Linus Torvalds <torvalds@linux-foundation.org>
d44c23
    Date:   Wed Dec 1 10:06:14 2021 -0800
d44c23
d44c23
        fget: check that the fd still exists after getting a ref to it
d44c23
d44c23
        Jann Horn points out that there is another possible race wrt Unix domain
d44c23
        socket garbage collection, somewhat reminiscent of the one fixed in
d44c23
        commit cbcf01128d0a ("af_unix: fix garbage collect vs MSG_PEEK").
d44c23
d44c23
        See the extended comment about the garbage collection requirements added
d44c23
        to unix_peek_fds() by that commit for details.
d44c23
d44c23
        The race comes from how we can locklessly look up a file descriptor just
d44c23
        as it is in the process of being closed, and with the right artificial
d44c23
        timing (Jann added a few strategic 'mdelay(500)' calls to do that), the
d44c23
        Unix domain socket garbage collector could see the reference count
d44c23
        decrement of the close() happen before fget() took its reference to the
d44c23
        file and the file was attached onto a new file descriptor.
d44c23
d44c23
        This is all (intentionally) correct on the 'struct file *' side, with
d44c23
        RCU lookups and lockless reference counting very much part of the
d44c23
        design.  Getting that reference count out of order isn't a problem per
d44c23
        se.
d44c23
d44c23
        But the garbage collector can get confused by seeing this situation of
d44c23
        having seen a file not having any remaining external references and then
d44c23
        seeing it being attached to an fd.
d44c23
d44c23
        In commit cbcf01128d0a ("af_unix: fix garbage collect vs MSG_PEEK") the
d44c23
        fix was to serialize the file descriptor install with the garbage
d44c23
        collector by taking and releasing the unix_gc_lock.
d44c23
d44c23
        That's not really an option here, but since this all happens when we are
d44c23
        in the process of looking up a file descriptor, we can instead simply
d44c23
        just re-check that the file hasn't been closed in the meantime, and just
d44c23
        re-do the lookup if we raced with a concurrent close() of the same file
d44c23
        descriptor.
d44c23
d44c23
        Reported-and-tested-by: Jann Horn <jannh@google.com>
d44c23
        Acked-by: Miklos Szeredi <mszeredi@redhat.com>
d44c23
        Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
d44c23
d44c23
    Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
d44c23
d44c23
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
d44c23
---
d44c23
 fs/file.c               | 86 +++++++++++++++--------------------------
d44c23
 include/linux/fdtable.h | 35 ++++++++++-------
d44c23
 2 files changed, 53 insertions(+), 68 deletions(-)
d44c23
d44c23
diff --git a/fs/file.c b/fs/file.c
d44c23
index 44bd634b636a..564d60bf0fda 100644
d44c23
--- a/fs/file.c
d44c23
+++ b/fs/file.c
d44c23
@@ -718,42 +718,43 @@ void do_close_on_exec(struct files_struct *files)
d44c23
 	spin_unlock(&files->file_lock);
d44c23
 }
d44c23
 
d44c23
-struct file *fget(unsigned int fd)
d44c23
+static struct file *__fget(unsigned int fd, fmode_t mask)
d44c23
 {
d44c23
-	struct file *file;
d44c23
 	struct files_struct *files = current->files;
d44c23
+	struct file *file;
d44c23
 
d44c23
 	rcu_read_lock();
d44c23
+loop:
d44c23
 	file = fcheck_files(files, fd);
d44c23
 	if (file) {
d44c23
-		/* File object ref couldn't be taken */
d44c23
-		if (file->f_mode & FMODE_PATH || !get_file_rcu(file))
d44c23
+		/* File object ref couldn't be taken.
d44c23
+		 * dup2() atomicity guarantee is the reason
d44c23
+		 * we loop to catch the new file (or NULL pointer)
d44c23
+		 */
d44c23
+		if (file->f_mode & mask)
d44c23
 			file = NULL;
d44c23
+		else if (!get_file_rcu(file))
d44c23
+			goto loop;
d44c23
+		else if (fcheck_files(files, fd) != file) {
d44c23
+			fput(file);
d44c23
+			goto loop;
d44c23
+		}
d44c23
 	}
d44c23
 	rcu_read_unlock();
d44c23
 
d44c23
 	return file;
d44c23
 }
d44c23
 
d44c23
+__attribute__((optimize("-fno-optimize-sibling-calls"))) struct file *fget(unsigned int fd)
d44c23
+{
d44c23
+	return __fget(fd, FMODE_PATH);
d44c23
+}
d44c23
 EXPORT_SYMBOL(fget);
d44c23
 
d44c23
-struct file *fget_raw(unsigned int fd)
d44c23
+__attribute__((optimize("-fno-optimize-sibling-calls"))) struct file *fget_raw(unsigned int fd)
d44c23
 {
d44c23
-	struct file *file;
d44c23
-	struct files_struct *files = current->files;
d44c23
-
d44c23
-	rcu_read_lock();
d44c23
-	file = fcheck_files(files, fd);
d44c23
-	if (file) {
d44c23
-		/* File object ref couldn't be taken */
d44c23
-		if (!atomic_long_inc_not_zero(&file->f_count))
d44c23
-			file = NULL;
d44c23
-	}
d44c23
-	rcu_read_unlock();
d44c23
-
d44c23
-	return file;
d44c23
+	return __fget(fd, 0);
d44c23
 }
d44c23
-
d44c23
 EXPORT_SYMBOL(fget_raw);
d44c23
 
d44c23
 /*
d44c23
@@ -772,56 +773,33 @@ EXPORT_SYMBOL(fget_raw);
d44c23
  * The fput_needed flag returned by fget_light should be passed to the
d44c23
  * corresponding fput_light.
d44c23
  */
d44c23
-struct file *fget_light(unsigned int fd, int *fput_needed)
d44c23
+struct file *__fget_light(unsigned int fd, fmode_t mask, int *fput_needed)
d44c23
 {
d44c23
-	struct file *file;
d44c23
 	struct files_struct *files = current->files;
d44c23
+	struct file *file;
d44c23
 
d44c23
 	*fput_needed = 0;
d44c23
 	if (atomic_read(&files->count) == 1) {
d44c23
-		file = fcheck_files(files, fd);
d44c23
-		if (file && (file->f_mode & FMODE_PATH))
d44c23
+		file = __fcheck_files(files, fd);
d44c23
+		if (file && (file->f_mode & mask))
d44c23
 			file = NULL;
d44c23
 	} else {
d44c23
-		rcu_read_lock();
d44c23
-		file = fcheck_files(files, fd);
d44c23
-		if (file) {
d44c23
-			if (!(file->f_mode & FMODE_PATH) &&
d44c23
-			    atomic_long_inc_not_zero(&file->f_count))
d44c23
-				*fput_needed = 1;
d44c23
-			else
d44c23
-				/* Didn't get the reference, someone's freed */
d44c23
-				file = NULL;
d44c23
-		}
d44c23
-		rcu_read_unlock();
d44c23
+		file = __fget(fd, mask);
d44c23
+		if (file)
d44c23
+			*fput_needed = 1;
d44c23
 	}
d44c23
 
d44c23
 	return file;
d44c23
 }
d44c23
+struct file *fget_light(unsigned int fd, int *fput_needed)
d44c23
+{
d44c23
+	return __fget_light(fd, FMODE_PATH, fput_needed);
d44c23
+}
d44c23
 EXPORT_SYMBOL(fget_light);
d44c23
 
d44c23
 struct file *fget_raw_light(unsigned int fd, int *fput_needed)
d44c23
 {
d44c23
-	struct file *file;
d44c23
-	struct files_struct *files = current->files;
d44c23
-
d44c23
-	*fput_needed = 0;
d44c23
-	if (atomic_read(&files->count) == 1) {
d44c23
-		file = fcheck_files(files, fd);
d44c23
-	} else {
d44c23
-		rcu_read_lock();
d44c23
-		file = fcheck_files(files, fd);
d44c23
-		if (file) {
d44c23
-			if (atomic_long_inc_not_zero(&file->f_count))
d44c23
-				*fput_needed = 1;
d44c23
-			else
d44c23
-				/* Didn't get the reference, someone's freed */
d44c23
-				file = NULL;
d44c23
-		}
d44c23
-		rcu_read_unlock();
d44c23
-	}
d44c23
-
d44c23
-	return file;
d44c23
+	return __fget_light(fd, 0, fput_needed);
d44c23
 }
d44c23
 
d44c23
 void set_close_on_exec(unsigned int fd, int flag)
d44c23
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
d44c23
index 88d74ca9418f..95bcca7c1a0f 100644
d44c23
--- a/include/linux/fdtable.h
d44c23
+++ b/include/linux/fdtable.h
d44c23
@@ -70,29 +70,36 @@ struct files_struct {
d44c23
 	RH_KABI_EXTEND(wait_queue_head_t resize_wait)
d44c23
 };
d44c23
 
d44c23
-#define rcu_dereference_check_fdtable(files, fdtfd) \
d44c23
-	(rcu_dereference_check((fdtfd), \
d44c23
-			       lockdep_is_held(&(files)->file_lock) || \
d44c23
-			       atomic_read(&(files)->count) == 1 || \
d44c23
-			       rcu_my_thread_group_empty()))
d44c23
-
d44c23
-#define files_fdtable(files) \
d44c23
-		(rcu_dereference_check_fdtable((files), (files)->fdt))
d44c23
-
d44c23
 struct file_operations;
d44c23
 struct vfsmount;
d44c23
 struct dentry;
d44c23
 
d44c23
-static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
d44c23
+#define rcu_dereference_check_fdtable(files, fdtfd) \
d44c23
+	rcu_dereference_check((fdtfd), lockdep_is_held(&(files)->file_lock))
d44c23
+
d44c23
+#define files_fdtable(files) \
d44c23
+	rcu_dereference_check_fdtable((files), (files)->fdt)
d44c23
+
d44c23
+/*
d44c23
+ * The caller must ensure that fd table isn't shared or hold rcu or file lock
d44c23
+ */
d44c23
+static inline struct file *__fcheck_files(struct files_struct *files, unsigned int fd)
d44c23
 {
d44c23
-	struct file * file = NULL;
d44c23
-	struct fdtable *fdt = files_fdtable(files);
d44c23
+	struct fdtable *fdt = rcu_dereference_raw(files->fdt);
d44c23
 
d44c23
 	if (fd < fdt->max_fds) {
d44c23
 		fd = array_index_nospec(fd, fdt->max_fds);
d44c23
-		file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
d44c23
+		return rcu_dereference_raw(fdt->fd[fd]);
d44c23
 	}
d44c23
-	return file;
d44c23
+	return NULL;
d44c23
+}
d44c23
+
d44c23
+static inline struct file *fcheck_files(struct files_struct *files, unsigned int fd)
d44c23
+{
d44c23
+	rcu_lockdep_assert(rcu_read_lock_held() ||
d44c23
+			   lockdep_is_held(&files->file_lock),
d44c23
+			   "suspicious rcu_dereference_check() usage");
d44c23
+	return __fcheck_files(files, fd);
d44c23
 }
d44c23
 
d44c23
 /*
d44c23
-- 
d44c23
2.26.3
d44c23
d44c23