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

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