Skip to contentSkip to conversation

Revised: [PATCH] mdadm: detect too-small device: error rather than underflow/crash

PATCH Applied [email protected] 2 participants 3 messages May 23, 2020 – Jun 5, 2020 MD RAID

lore
Share & tools
lore
#0 lore raw

Forgive me, I didn't at first notice your additional comments on the code. I have now incorporated your comments on style; now I think I got it right and respectfully resubmit the patch.

Please let me know if you have any additional feedback.

Thanks,
David

#1 lore raw

PATCH 1/1 Detect too-small device: error rather than underflow/crash

From: David Favro <[email protected]>

For 1.x metadata, when the user requested creation of an array on component devices that were too small even to hold the superblock, an undetected integer wraparound (underflow) resulted in an enormous computed size which resulted in various follow-on errors such as floating-point exception.

This patch detects this condition, prints a reasonable diagnostic message, and refuses to continue.

---
 super1.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
super1.c +14 -5
index e0d80be1..76648835 100644
@@ -2753,6 +2753,7 @@ static int validate_geometry1(struct supertype *st, int level,
 	unsigned long long ldsize, devsize;
 	int bmspace;
 	unsigned long long headroom;
+	unsigned long long overhead;
 	int fd;
 
 	if (level == LEVEL_CONTAINER) {
@@ -2785,10 +2786,6 @@ static int validate_geometry1(struct supertype *st, int level,
 	close(fd);
 
 	devsize = ldsize >> 9;
-	if (devsize < 24) {
-		*freesize = 0;
-		return 0;
-	}
 
 	/* creating:  allow suitable space for bitmap or PPL */
 	if (consistency_policy == CONSISTENCY_POLICY_PPL)
@@ -2829,15 +2826,27 @@ static int validate_geometry1(struct supertype *st, int level,
 	case 0: /* metadata at end.  Round down and subtract space to reserve */
 		devsize = (devsize & ~(4ULL*2-1));
 		/* space for metadata, bblog, bitmap/ppl */
-		devsize -= 8*2 + 8 + bmspace;
+		overhead = 8*2 + 8 + bmspace;
+		if (devsize < overhead) /* detect underflow */
+			goto dev_too_small_err;
+		devsize -= overhead;
 		break;
 	case 1:
 	case 2:
+		if (devsize < data_offset) /* detect underflow */
+			goto dev_too_small_err;
 		devsize -= data_offset;
 		break;
 	}
 	*freesize = devsize;
 	return 1;
+
+/* Error condition, device cannot even hold the overhead. */
+dev_too_small_err:
+	fprintf(stderr, "device %s is too small (%lluK) for "
+			"required metadata!\n", subdev, devsize>>1);
+	*freesize = 0;
+	return 0;
 }
 
 void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0)
Signature · 1 line
2.26.2.593.gb9946226
Applied
#2 lore raw

Re: [PATCH] Detect too-small device: error rather than underflow/crash

Quoted 8 lines · On 5/23/20 8:24 AM, [email protected] wrote:

From: David Favro <[email protected]>

For 1.x metadata, when the user requested creation of an array on component devices that were too small even to hold the superblock, an undetected integer wraparound (underflow) resulted in an enormous computed size which resulted in various follow-on errors such as floating-point exception.

Quoted 7 lines

This patch detects this condition, prints a reasonable diagnostic message, and refuses to continue.

---
 super1.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

Applied!

Thanks,
Jes