Blame SOURCES/CVE-2021-4083.patch

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