Skip to contentSkip to conversation

kasan: avoid overflowing quarantine size on low memory systems

PATCH Applied Alexander Potapenko 5 participants 10 messages Aug 1, 2016 – Aug 31, 2016

Subsystems (2)

v1 of 2 Applied 1/1 Latest is v2 →

lore
Share & tools
lore

Alexander Potapenko

author reviewer
#0 lore raw

PATCH 1/1 kasan: avoid overflowing quarantine size on low memory systems

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +10 -2
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-	size_t new_quarantine_size;
+	size_t new_quarantine_size, percpu_quarantines;
 	unsigned long flags;
 	struct qlist_head to_free = QLIST_INIT;
 	size_t size_to_free = 0;
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
 	 */
 	new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
 		QUARANTINE_FRACTION;
-	new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	if (new_quarantine_size < percpu_quarantines) {
+		WARN_ONCE(1,
+			"Too little memory, disabling global KASAN quarantine.\n",
+		);
+		new_quarantine_size = 0;
+	} else {
+		new_quarantine_size -= percpu_quarantines;
+	}
 	WRITE_ONCE(quarantine_size, new_quarantine_size);
 
 	last = global_quarantine.head;
Signature · 7 lines
2.8.0.rc3.226.g39d4020

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#1 lore raw
Quoted 13 lines · On Mon, 1 Aug 2016 16:59:23 +0200 Alexander Potapenko <[email protected]>…

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

mm/kasan/quarantine.c +2 -1
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
 	 */
 	new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
 		QUARANTINE_FRACTION;
-	new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	if (new_quarantine_size < percpu_quarantines) {

mm/kasan/quarantine.c void quarantine_reduce(void) · line 228

Quoted 10 lines
mm/kasan/quarantine.c +7 -0
+		WARN_ONCE(1,
+			"Too little memory, disabling global KASAN quarantine.\n",
+		);
+		new_quarantine_size = 0;
+	} else {
+		new_quarantine_size -= percpu_quarantines;
+	}
 	WRITE_ONCE(quarantine_size, new_quarantine_size);
 
 	last = global_quarantine.head;

This is a little tidier:

mm/kasan/quarantine.c renamed from mm/kasan/quarantine.c~kasan-avoid-overflowing-quarantine-size-on-low-memory-systems-fix +3 -6
@@ -217,14 +217,11 @@ void quarantine_reduce(void)
 	new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
 		QUARANTINE_FRACTION;
 	percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
-	if (new_quarantine_size < percpu_quarantines) {
-		WARN_ONCE(1,
-			"Too little memory, disabling global KASAN quarantine.\n",
-		);
+	if (WARN_ONCE(new_quarantine_size < percpu_quarantines,
+		"Too little memory, disabling global KASAN quarantine.\n"))
 		new_quarantine_size = 0;
-	} else {
+	else
 		new_quarantine_size -= percpu_quarantines;
-	}
 	WRITE_ONCE(quarantine_size, new_quarantine_size);
 
 	last = global_quarantine.head;

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
In reply to Alexander Potapenko
Quoted 4 lines · Alexander Potapenko

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Just curious, how did find this?
Overflow is possible if system has more than 32 cpus per GB of memory. AFIAK this quite unusual.

Quoted 22 lines
Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +1 -1
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-	size_t new_quarantine_size;
+	size_t new_quarantine_size, percpu_quarantines;
 	unsigned long flags;
 	struct qlist_head to_free = QLIST_INIT;
 	size_t size_to_free = 0;

mm/kasan/quarantine.c void quarantine_reduce(void) · line 221

Quoted 9 lines
mm/kasan/quarantine.c +5 -1
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
 	 */
 	new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
 		QUARANTINE_FRACTION;
-	new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	if (new_quarantine_size < percpu_quarantines) {
+		WARN_ONCE(1,
+			"Too little memory, disabling global KASAN quarantine.\n",
+		);

Why WARN? I'd suggest pr_warn_once();

mm/kasan/quarantine.c void quarantine_reduce(void) · line 228

Quoted 8 lines
mm/kasan/quarantine.c +4 -0
+		new_quarantine_size = 0;
+	} else {
+		new_quarantine_size -= percpu_quarantines;
+	}
 	WRITE_ONCE(quarantine_size, new_quarantine_size);
 
 	last = global_quarantine.head;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#3 lore raw
Quoted 10 lines · Andrey Ryabinin
Quoted 4 lines · Alexander Potapenko

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Just curious, how did find this?
Overflow is possible if system has more than 32 cpus per GB of memory. AFIAK this quite unusual.

We were just reading the quarantine code, and Dmitry spotted the problem. I agree this is quite unusual, but we'd better prevent this case.

Quoted 25 lines
Quoted 25 lines
Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +1 -1
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-     size_t new_quarantine_size;
+     size_t new_quarantine_size, percpu_quarantines;
      unsigned long flags;
      struct qlist_head to_free = QLIST_INIT;
      size_t size_to_free = 0;
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
       */
      new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
              QUARANTINE_FRACTION;
Quoted 8 lines

mm/kasan/quarantine.c void quarantine_reduce(void) · line 221

Quoted 6 lines
mm/kasan/quarantine.c +5 -1
-     new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     if (new_quarantine_size < percpu_quarantines) {
+             WARN_ONCE(1,
+                     "Too little memory, disabling global KASAN quarantine.\n",
+             );

Why WARN? I'd suggest pr_warn_once();

Agreed. I'll send the updated patch.
(Sorry, Andrew, I'll have to get back to the non-tidy version then, as pr_warn_once() doesn't return the predicate value)

Quoted 8 lines
Quoted 8 lines
mm/kasan/quarantine.c +4 -0
+             new_quarantine_size = 0;
+     } else {
+             new_quarantine_size -= percpu_quarantines;
+     }
      WRITE_ONCE(quarantine_size, new_quarantine_size);
 
      last = global_quarantine.head;
Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
reviewer
#4 lore raw
In reply to Andrey Ryabinin
Quoted 10 lines · Andrey Ryabinin
Quoted 4 lines · Alexander Potapenko

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Just curious, how did find this?
Overflow is possible if system has more than 32 cpus per GB of memory. AFIAK this quite unusual.

I was reading code for unrelated reason.

Quoted 25 lines
Quoted 25 lines
Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +1 -1
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-     size_t new_quarantine_size;
+     size_t new_quarantine_size, percpu_quarantines;
      unsigned long flags;
      struct qlist_head to_free = QLIST_INIT;
      size_t size_to_free = 0;
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
       */
      new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
              QUARANTINE_FRACTION;
Quoted 8 lines

mm/kasan/quarantine.c void quarantine_reduce(void) · line 221

Quoted 6 lines
mm/kasan/quarantine.c +5 -1
-     new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     if (new_quarantine_size < percpu_quarantines) {
+             WARN_ONCE(1,
+                     "Too little memory, disabling global KASAN quarantine.\n",
+             );

Why WARN? I'd suggest pr_warn_once();

I would suggest to just do something useful. Setting quarantine new_quarantine_size to 0 looks fine.
What would user do with this warning? Number of CPUs and amount of memory are generally fixed. Why is it an issue for end user at all? We still have some quarantine per-cpu. A WARNING means a [non-critical] kernel bug. E.g. syzkaller will catch each and every boot of such system as a bug.

Quoted 8 lines

mm/kasan/quarantine.c void quarantine_reduce(void) · line 228

Quoted 8 lines
mm/kasan/quarantine.c +4 -0
+             new_quarantine_size = 0;
+     } else {
+             new_quarantine_size -= percpu_quarantines;
+     }
      WRITE_ONCE(quarantine_size, new_quarantine_size);
 
      last = global_quarantine.head;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#5 lore raw
Quoted 50 lines · Dmitry Vyukov
Quoted 10 lines · Andrey Ryabinin
Quoted 4 lines · Alexander Potapenko

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Just curious, how did find this?
Overflow is possible if system has more than 32 cpus per GB of memory. AFIAK this quite unusual.

I was reading code for unrelated reason.

Quoted 25 lines
Quoted 25 lines
Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +1 -1
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-     size_t new_quarantine_size;
+     size_t new_quarantine_size, percpu_quarantines;
      unsigned long flags;
      struct qlist_head to_free = QLIST_INIT;
      size_t size_to_free = 0;
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
       */
      new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
              QUARANTINE_FRACTION;
Quoted 8 lines

mm/kasan/quarantine.c void quarantine_reduce(void) · line 221

Quoted 6 lines
mm/kasan/quarantine.c +5 -1
-     new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     if (new_quarantine_size < percpu_quarantines) {
+             WARN_ONCE(1,
+                     "Too little memory, disabling global KASAN quarantine.\n",
+             );

Why WARN? I'd suggest pr_warn_once();

Quoted 7 lines

I would suggest to just do something useful. Setting quarantine new_quarantine_size to 0 looks fine.
What would user do with this warning? Number of CPUs and amount of memory are generally fixed. Why is it an issue for end user at all? We still have some quarantine per-cpu. A WARNING means a [non-critical] kernel bug. E.g. syzkaller will catch each and every boot of such system as a bug.

How about printk_once then?
Silently setting the quarantine size to zero may puzzle the user.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
mm/kasan/quarantine.c +4 -0
+             new_quarantine_size = 0;
+     } else {
+             new_quarantine_size -= percpu_quarantines;
+     }
      WRITE_ONCE(quarantine_size, new_quarantine_size);
 
      last = global_quarantine.head;
Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
reviewer
#6 lore raw
Quoted 58 lines · Alexander Potapenko
Quoted 50 lines · Dmitry Vyukov
Quoted 10 lines · Andrey Ryabinin
Quoted 4 lines · Alexander Potapenko

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Just curious, how did find this?
Overflow is possible if system has more than 32 cpus per GB of memory. AFIAK this quite unusual.

I was reading code for unrelated reason.

Quoted 25 lines
Quoted 25 lines
Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +1 -1
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-     size_t new_quarantine_size;
+     size_t new_quarantine_size, percpu_quarantines;
      unsigned long flags;
      struct qlist_head to_free = QLIST_INIT;
      size_t size_to_free = 0;
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
       */
      new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
              QUARANTINE_FRACTION;
Quoted 8 lines

mm/kasan/quarantine.c void quarantine_reduce(void) · line 221

Quoted 6 lines
mm/kasan/quarantine.c +5 -1
-     new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+     if (new_quarantine_size < percpu_quarantines) {
+             WARN_ONCE(1,
+                     "Too little memory, disabling global KASAN quarantine.\n",
+             );

Why WARN? I'd suggest pr_warn_once();

Quoted 7 lines

I would suggest to just do something useful. Setting quarantine new_quarantine_size to 0 looks fine.
What would user do with this warning? Number of CPUs and amount of memory are generally fixed. Why is it an issue for end user at all? We still have some quarantine per-cpu. A WARNING means a [non-critical] kernel bug. E.g. syzkaller will catch each and every boot of such system as a bug.

Quoted 2 lines

How about printk_once then?
Silently setting the quarantine size to zero may puzzle the user.

We still have per-cpu quarantine.
new_quarantine_size==0 is not radically different from new_quarantine_size==1. Both limit KASAN ability to detect UAF. Why do we WARN in the former case but not in the latter?
We can print per-cpu/global quarantine sizes to console. Then if we got any complaints we can figure out what happens from the log.

Quoted 15 lines
Quoted 8 lines
Quoted 8 lines

mm/kasan/quarantine.c void quarantine_reduce(void) · line 228

Quoted 8 lines
mm/kasan/quarantine.c +4 -0
+             new_quarantine_size = 0;
+     } else {
+             new_quarantine_size -= percpu_quarantines;
+     }
      WRITE_ONCE(quarantine_size, new_quarantine_size);
 
      last = global_quarantine.head;

--
Alexander Potapenko
Software Engineer

Quoted 7 lines

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
In reply to Alexander Potapenko
Quoted 13 lines · Alexander Potapenko
Quoted 5 lines · Dmitry Vyukov

On Tue, Aug 2, 2016 at 12:00 PM, Andrey Ryabinin

Quoted 2 lines

Why WARN? I'd suggest pr_warn_once();

Quoted 7 lines

I would suggest to just do something useful. Setting quarantine new_quarantine_size to 0 looks fine.
What would user do with this warning? Number of CPUs and amount of memory are generally fixed. Why is it an issue for end user at all? We still have some quarantine per-cpu. A WARNING means a [non-critical] kernel bug. E.g. syzkaller will catch each and every boot of such system as a bug.

Quoted 3 lines

How about printk_once then?
Silently setting the quarantine size to zero may puzzle the user.

Nope, user will not notice anything. So keeping it silent would be better. Plus it's very unlikely that this will ever happen in real life.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#8 lore raw
Quoted 16 lines · Andrey Ryabinin
Quoted 13 lines · Alexander Potapenko
Quoted 12 lines · Dmitry Vyukov

On Tue, Aug 2, 2016 at 12:00 PM, Andrey Ryabinin

Quoted 2 lines

Why WARN? I'd suggest pr_warn_once();

I would suggest to just do something useful. Setting quarantine new_quarantine_size to 0 looks fine.
What would user do with this warning? Number of CPUs and amount of memory are generally fixed. Why is it an issue for end user at all? We still have some quarantine per-cpu. A WARNING means a [non-critical] kernel bug. E.g. syzkaller will catch each and every boot of such system as a bug.

Quoted 7 lines
Quoted 3 lines

How about printk_once then?
Silently setting the quarantine size to zero may puzzle the user.

Nope, user will not notice anything. So keeping it silent would be better. Plus it's very unlikely that this will ever happen in real life.

Ok, I've sent out v2, please take a look.

Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#9 lore raw
In reply to Alexander Potapenko
Quoted 44 lines · Alexander Potapenko

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +10 -2
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-	size_t new_quarantine_size;
+	size_t new_quarantine_size, percpu_quarantines;
 	unsigned long flags;
 	struct qlist_head to_free = QLIST_INIT;
 	size_t size_to_free = 0;
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
 	 */
 	new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
 		QUARANTINE_FRACTION;
-	new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	if (new_quarantine_size < percpu_quarantines) {
+		WARN_ONCE(1,
+			"Too little memory, disabling global KASAN quarantine.\n",
+		);
+		new_quarantine_size = 0;
+	} else {
+		new_quarantine_size -= percpu_quarantines;
+	}
 	WRITE_ONCE(quarantine_size, new_quarantine_size);
 
 	last = global_quarantine.head;
Signature · 1 line
2.8.0.rc3.226.g39d4020
Quoted 44 lines · Alexander Potapenko

If the total amount of memory assigned to quarantine is less than the amount of memory assigned to per-cpu quarantines, |new_quarantine_size| may overflow. Instead, set it to zero.

Fixes 55834c59098d ("mm: kasan: initial memory quarantine

implementation")

---
 mm/kasan/quarantine.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/kasan/quarantine.c +10 -2
index 65793f1..416d3b0 100644
@@ -196,7 +196,7 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache)
 
 void quarantine_reduce(void)
 {
-	size_t new_quarantine_size;
+	size_t new_quarantine_size, percpu_quarantines;
 	unsigned long flags;
 	struct qlist_head to_free = QLIST_INIT;
 	size_t size_to_free = 0;
@@ -214,7 +214,15 @@ void quarantine_reduce(void)
 	 */
 	new_quarantine_size = (READ_ONCE(totalram_pages) << PAGE_SHIFT) /
 		QUARANTINE_FRACTION;
-	new_quarantine_size -= QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	percpu_quarantines = QUARANTINE_PERCPU_SIZE * num_online_cpus();
+	if (new_quarantine_size < percpu_quarantines) {
+		WARN_ONCE(1,
+			"Too little memory, disabling global KASAN quarantine.\n",
+		);
+		new_quarantine_size = 0;
+	} else {
+		new_quarantine_size -= percpu_quarantines;
+	}
 	WRITE_ONCE(quarantine_size, new_quarantine_size);
 
 	last = global_quarantine.head;
Signature · 1 line
2.8.0.rc3.226.g39d4020