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

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