Skip to contentSkip to conversation

crypto: xor - avoid division by zero crash

PATCH In review Helge Deller 2 participants 3 messages Jan 24, 2021 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: xor - avoid division by zero crash

On some of my parisc machines, this patch c055e3eae0f1 ("crypto: xor - use ktime for template benchmarking") triggers a dividy-by-zero exception because "min" becomes zero, which then leads to a kernel crash.

It's not clear yet, why I see the issue only on some machines. At least on those the measured time to run the xor speed tests becomes zero nanoseconds. Maybe it's because gcc-10 optimizes the speed test out, or because of some other changes in the time keeping routines.

In either case, the kernel should not crash.

This patch adds a workaround by reporting such cases with a kernel warning and continues as if the xor tests would have run in 1 ns.

---

crypto/xor.c +2 -0
index eacbf4f93990..3639341bac7e 100644
@@ -100,6 +100,8 @@ do_xor_speed(struct xor_block_template *tmpl, void *b1, void *b2)
 			mb();
 		}
 		diff = ktime_sub(ktime_get(), start);
+		if (WARN_ON(diff == 0))
+			diff = 1;
 		if (diff < min)
 			min = diff;
 	}
#1 lore raw
Quoted 26 lines · Helge Deller

On some of my parisc machines, this patch c055e3eae0f1 ("crypto: xor - use ktime for template benchmarking") triggers a dividy-by-zero exception because "min" becomes zero, which then leads to a kernel crash.

It's not clear yet, why I see the issue only on some machines. At least on those the measured time to run the xor speed tests becomes zero nanoseconds. Maybe it's because gcc-10 optimizes the speed test out, or because of some other changes in the time keeping routines.

In either case, the kernel should not crash.

This patch adds a workaround by reporting such cases with a kernel warning and continues as if the xor tests would have run in 1 ns.

---

crypto/xor.c +0 -0
index eacbf4f93990..3639341bac7e 100644

crypto/xor.c do_xor_speed(struct xor_block_template *tmpl, void *b1, void *b2) · line 107

Quoted 8 lines
crypto/xor.c +2 -0
@@ -100,6 +100,8 @@ do_xor_speed(struct xor_block_template *tmpl, void *b1, void *b2)
                        mb();
                }
                diff = ktime_sub(ktime_get(), start);
+               if (WARN_ON(diff == 0))
+                       diff = 1;
                if (diff < min)
                        min = diff;
        }

This should already be fixed in mainline - please check whether that fix works for you.

#2 lore raw
Quoted 22 lines · Ard Biesheuvel
Quoted 21 lines · Helge Deller

On some of my parisc machines, this patch c055e3eae0f1 ("crypto: xor - use ktime for template benchmarking") triggers a dividy-by-zero exception because "min" becomes zero, which then leads to a kernel crash.

It's not clear yet, why I see the issue only on some machines. At least on those the measured time to run the xor speed tests becomes zero nanoseconds. Maybe it's because gcc-10 optimizes the speed test out, or because of some other changes in the time keeping routines.

In either case, the kernel should not crash.

This patch adds a workaround by reporting such cases with a kernel warning and continues as if the xor tests would have run in 1 ns.

---

Quoted 16 lines

crypto/xor.c do_xor_speed(struct xor_block_template *tmpl, void *b1, void *b2) · line 107

Quoted 13 lines
crypto/xor.c +2 -0
index eacbf4f93990..3639341bac7e 100644
@@ -100,6 +100,8 @@ do_xor_speed(struct xor_block_template *tmpl, void *b1, void *b2)
                        mb();
                }
                diff = ktime_sub(ktime_get(), start);
+               if (WARN_ON(diff == 0))
+                       diff = 1;
                if (diff < min)
                        min = diff;
        }

This should already be fixed in mainline - please check whether that fix works for you.

Yes, it's basically the same fix and a good step.
It fixes the kernel crash at least, but it's still strange that the timing calculation will be wrong.

Helge