Skip to contentSkip to conversation

vma: Detect infinite loop in vma tree

PATCH Superseded Liam R. Howlett 3 participants 6 messages Oct 31, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vma: Detect infinite loop in vma tree

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vma.c +2 -1
index 68138e8c153e..60ed8cc187ad 100644
@@ -615,7 +615,8 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count)
+			break;
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

I should add:

This patch does have the downside of making the count difference less useful (we will only see one extra as apposed to what may exist).

Quoted 26 lines
Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vma.c +2 -1
index 68138e8c153e..60ed8cc187ad 100644
@@ -615,7 +615,8 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count)
+			break;
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
Signature · 1 line
2.43.0
#2 lore raw
In reply to Liam R. Howlett
Quoted 23 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vma.c +0 -0
index 68138e8c153e..60ed8cc187ad 100644

mm/vma.c void validate_mm(struct mm_struct *mm) · line 619

Quoted 6 lines
mm/vma.c +2 -1
@@ -615,7 +615,8 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count)
+			break;

Would it make sense to allow some slack so that the error below can distinguish better between off-by-one/few error from a complete corruption?

And in that case assign some special value to "i" (-1?) to make it clear this was triggered?

Quoted 3 lines
mm/vma.c +0 -0
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
#3 lore raw
Quoted 34 lines · Vlastimil Babka
Quoted 23 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vma.c +0 -0
index 68138e8c153e..60ed8cc187ad 100644

mm/vma.c void validate_mm(struct mm_struct *mm) · line 619

Quoted 6 lines
mm/vma.c +2 -1
@@ -615,7 +615,8 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count)
+			break;

Would it make sense to allow some slack so that the error below can distinguish better between off-by-one/few error from a complete corruption?

Quoted 2 lines

And in that case assign some special value to "i" (-1?) to make it clear this was triggered?

Yes, probably. 10 would be plenty. In recent memory I cannot think of an example that we exceeded 7 munmap()'s in a single operation - although it is easily possible to do.

I like the idea of -1 too, at least someone would come to inspect where it came from at that point.

Quoted 5 lines
Quoted 4 lines
mm/vma.c +0 -0
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
#4 lore raw
Quoted 38 lines · Liam R. Howlett
Quoted 34 lines · Vlastimil Babka
Quoted 23 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vma.c +0 -0
index 68138e8c153e..60ed8cc187ad 100644

mm/vma.c void validate_mm(struct mm_struct *mm) · line 619

Quoted 6 lines
mm/vma.c +2 -1
@@ -615,7 +615,8 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count)
+			break;

Would it make sense to allow some slack so that the error below can distinguish better between off-by-one/few error from a complete corruption?

Quoted 2 lines

And in that case assign some special value to "i" (-1?) to make it clear this was triggered?

Quoted 6 lines

Yes, probably. 10 would be plenty. In recent memory I cannot think of an example that we exceeded 7 munmap()'s in a single operation - although it is easily possible to do.

I like the idea of -1 too, at least someone would come to inspect where it came from at that point.

Hm this feels a little arbitrary though... I mean can we race with map_count at this point or is everything locked down such that no munmaps() can happen?

Otherwise it feels a bit whack-a-mole.

I agree with Vlastimil though it'd be nice to sort of differentiate, but if we _absolutely can only iterate mm->map_count times_ here, it might be worth letting a few more go, then in the next bit of code...

Quoted 6 lines
Quoted 6 lines

mm/vma.c void validate_mm(struct mm_struct *mm) · line 622

Quoted 5 lines
mm/vma.c +0 -0
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);

...here which does indeed imply that i literally cannot be anything but mm->map_count, I mean I guess we already get to see here how far off we are.

So yeah something like letting it go 10 more times (maybe like #define UNUSUALLY_BAD_CORRUPTION_COUNT 10 or something I don't know naming is hard) just so we can pick that out would be nice.

But I do like the general idea here though!

#5 lore raw
Quoted 50 lines · Lorenzo Stoakes
Quoted 38 lines · Liam R. Howlett
Quoted 34 lines · Vlastimil Babka
Quoted 23 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vma.c +0 -0
index 68138e8c153e..60ed8cc187ad 100644

mm/vma.c void validate_mm(struct mm_struct *mm) · line 619

Quoted 6 lines
mm/vma.c +2 -1
@@ -615,7 +615,8 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count)
+			break;

Would it make sense to allow some slack so that the error below can distinguish better between off-by-one/few error from a complete corruption?

Quoted 2 lines

And in that case assign some special value to "i" (-1?) to make it clear this was triggered?

Quoted 6 lines

Yes, probably. 10 would be plenty. In recent memory I cannot think of an example that we exceeded 7 munmap()'s in a single operation - although it is easily possible to do.

I like the idea of -1 too, at least someone would come to inspect where it came from at that point.

Hm this feels a little arbitrary though... I mean can we race with map_count at this point or is everything locked down such that no munmaps() can happen?

Quoted 5 lines

Otherwise it feels a bit whack-a-mole.

I agree with Vlastimil though it'd be nice to sort of differentiate, but if we _absolutely can only iterate mm->map_count times_ here, it might be worth letting a few more go, then in the next bit of code...

this is done under the write lock, otherwise it would vary already.

Quoted 12 lines
Quoted 7 lines
Quoted 7 lines

mm/vma.c void validate_mm(struct mm_struct *mm) · line 622

Quoted 6 lines
mm/vma.c +0 -0
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);

...here which does indeed imply that i literally cannot be anything but mm->map_count, I mean I guess we already get to see here how far off we are.

Quoted 5 lines

So yeah something like letting it go 10 more times (maybe like #define UNUSUALLY_BAD_CORRUPTION_COUNT 10 or something I don't know naming is hard) just so we can pick that out would be nice.

But I do like the general idea here though!

This may not ever produce anything - if we are in the maple tree code and never reaching a leaf to return anything then we will still loop forever. But it is something that is easy enough to detect and stop - which may make a syzbot report a bit easier to swallow.