Blame SOURCES/0034-mdadm-Grow-Fix-use-after-close-bug-by-closing-after-.patch

cd8c44
From 548e9b916f86c06e2cdb50d8f49633f9bec66c7e Mon Sep 17 00:00:00 2001
cd8c44
From: Logan Gunthorpe <logang@deltatee.com>
cd8c44
Date: Wed, 22 Jun 2022 14:25:09 -0600
cd8c44
Subject: [PATCH 34/52] mdadm/Grow: Fix use after close bug by closing after
cd8c44
 fork
cd8c44
cd8c44
The test 07reshape-grow fails most of the time. But it succeeds around
cd8c44
1 in 5 times. When it does succeed, it causes the tests to die because
cd8c44
mdadm has segfaulted.
cd8c44
cd8c44
The segfault was caused by mdadm attempting to repoen a file
cd8c44
descriptor that was already closed. The backtrace of the segfault
cd8c44
was:
cd8c44
cd8c44
  #0  __strncmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:101
cd8c44
  #1  0x000056146e31d44b in devnm2devid (devnm=0x0) at util.c:956
cd8c44
  #2  0x000056146e31dab4 in open_dev_flags (devnm=0x0, flags=0)
cd8c44
                         at util.c:1072
cd8c44
  #3  0x000056146e31db22 in open_dev (devnm=0x0) at util.c:1079
cd8c44
  #4  0x000056146e3202e8 in reopen_mddev (mdfd=4) at util.c:2244
cd8c44
  #5  0x000056146e329f36 in start_array (mdfd=4,
cd8c44
              mddev=0x7ffc55342450 "/dev/md0", content=0x7ffc55342860,
cd8c44
              st=0x56146fc78660, ident=0x7ffc55342f70, best=0x56146fc6f5d0,
cd8c44
              bestcnt=10, chosen_drive=0, devices=0x56146fc706b0, okcnt=5,
cd8c44
	      sparecnt=0,  rebuilding_cnt=0, journalcnt=0, c=0x7ffc55342e90,
cd8c44
	      clean=1,  avail=0x56146fc78720 "\001\001\001\001\001",
cd8c44
	      start_partial_ok=0, err_ok=0, was_forced=0)
cd8c44
	                  at Assemble.c:1206
cd8c44
  #6  0x000056146e32c36e in Assemble (st=0x56146fc78660,
cd8c44
               mddev=0x7ffc55342450 "/dev/md0", ident=0x7ffc55342f70,
cd8c44
	       devlist=0x56146fc6e2d0, c=0x7ffc55342e90)
cd8c44
	                 at Assemble.c:1914
cd8c44
  #7  0x000056146e312ac9 in main (argc=11, argv=0x7ffc55343238)
cd8c44
                         at mdadm.c:1510
cd8c44
cd8c44
The file descriptor was closed early in Grow_continue(). The noted commit
cd8c44
moved the close() call to close the fd above the fork which caused the
cd8c44
parent process to return with a closed fd.
cd8c44
cd8c44
This meant reshape_array() and Grow_continue() would return in the parent
cd8c44
with the fd forked. The fd would eventually be passed to reopen_mddev()
cd8c44
which returned an unhandled NULL from fd2devnm() which would then be
cd8c44
dereferenced in devnm2devid.
cd8c44
cd8c44
Fix this by moving the close() call below the fork. This appears to
cd8c44
fix the 07revert-grow test. While we're at it, switch to using
cd8c44
close_fd() to invalidate the file descriptor.
cd8c44
cd8c44
Fixes: 77b72fa82813 ("mdadm/Grow: prevent md's fd from being occupied during delayed time")
cd8c44
Cc: Alex Wu <alexwu@synology.com>
cd8c44
Cc: BingJing Chang <bingjingc@synology.com>
cd8c44
Cc: Danny Shih <dannyshih@synology.com>
cd8c44
Cc: ChangSyun Peng <allenpeng@synology.com>
cd8c44
Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
cd8c44
Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
cd8c44
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
cd8c44
---
cd8c44
 Grow.c | 4 +++-
cd8c44
 1 file changed, 3 insertions(+), 1 deletion(-)
cd8c44
cd8c44
diff --git a/Grow.c b/Grow.c
cd8c44
index 8c520d42..97f22c75 100644
cd8c44
--- a/Grow.c
cd8c44
+++ b/Grow.c
cd8c44
@@ -3514,7 +3514,6 @@ started:
cd8c44
 			return 0;
cd8c44
 		}
cd8c44
 
cd8c44
-	close(fd);
cd8c44
 	/* Now we just need to kick off the reshape and watch, while
cd8c44
 	 * handling backups of the data...
cd8c44
 	 * This is all done by a forked background process.
cd8c44
@@ -3535,6 +3534,9 @@ started:
cd8c44
 		break;
cd8c44
 	}
cd8c44
 
cd8c44
+	/* Close unused file descriptor in the forked process */
cd8c44
+	close_fd(&fd;;
cd8c44
+
cd8c44
 	/* If another array on the same devices is busy, the
cd8c44
 	 * reshape will wait for them.  This would mean that
cd8c44
 	 * the first section that we suspend will stay suspended
cd8c44
-- 
cd8c44
2.31.1
cd8c44