diff options
-rwxr-xr-x | regress/82-bn_prepops-null-deref.lua | 63 | ||||
-rw-r--r-- | regress/regress.lua | 1 |
2 files changed, 64 insertions, 0 deletions
diff --git a/regress/82-bn_prepops-null-deref.lua b/regress/82-bn_prepops-null-deref.lua new file mode 100755 index 0000000..6a1d617 --- /dev/null +++ b/regress/82-bn_prepops-null-deref.lua | |||
@@ -0,0 +1,63 @@ | |||
1 | #!/usr/bin/env lua | ||
2 | -- | ||
3 | -- The following code could trigger a NULL dereference. | ||
4 | -- | ||
5 | -- bn_prepops(lua_State *L, BIGNUM **r, BIGNUM **a, BIGNUM **b, _Bool commute) { | ||
6 | -- ... | ||
7 | -- *b = checkbig(L, 2, &lvalue); | ||
8 | -- ... | ||
9 | -- } | ||
10 | -- | ||
11 | -- bn_sqr(lua_State *L) { | ||
12 | -- BIGNUM *r, *a; | ||
13 | -- | ||
14 | -- bn_prepops(L, &r, &a, NULL, 1); | ||
15 | -- ... | ||
16 | -- } | ||
17 | -- | ||
18 | -- Caught by clang static analyzer. This was introduced with a patch adding | ||
19 | -- the :sqr method. This should have been caught sooner as the :sqr method | ||
20 | -- couldn't have possibly ever worked--a missing or non-numeric second | ||
21 | -- operand would have thrown a Lua error, and a numeric second operand | ||
22 | -- triggers the NULL dereference. | ||
23 | -- | ||
24 | require"regress".export".*" | ||
25 | |||
26 | local function N(i) return bignum.new(i) end | ||
27 | |||
28 | -- passing a second numeric operand triggered a NULL dereference | ||
29 | local r = N(4):sqr(0) | ||
30 | |||
31 | |||
32 | -- check minimal functionality of all our operators | ||
33 | local tests = { | ||
34 | { op = "add", a = 1, b = 1, r = 2 }, | ||
35 | { op = "sub", a = 2, b = 1, r = 1 }, | ||
36 | { op = "mul", a = 2, b = 2, r = 4 }, | ||
37 | { op = "idiv", a = 4, b = 2, r = 2 }, | ||
38 | { op = "mod", a = 4, b = 2, r = 0 }, | ||
39 | { op = "exp", a = 2, b = 2, r = 4 }, | ||
40 | { op = "sqr", a = 4, b = nil, r = 16 }, | ||
41 | { op = "gcd", a = 47, b = 3, r = 1 }, | ||
42 | } | ||
43 | |||
44 | local function tdescr(t) | ||
45 | return string.format("%s(%s, %s)", t.op, tostring(t.a), tostring(t.b)) | ||
46 | end | ||
47 | |||
48 | for i,t in ipairs(tests) do | ||
49 | local a = N(t.a) | ||
50 | local op = a[t.op] | ||
51 | local ok, r | ||
52 | |||
53 | if t.b then | ||
54 | ok, r = pcall(op, a, t.b) | ||
55 | else | ||
56 | ok, r = pcall(op, a) | ||
57 | end | ||
58 | |||
59 | check(ok, "failed test #%d (%s) (%s)", i, tdescr(t), r) | ||
60 | check(N(r) == N(t.r), "failed test #%d (%s) (expected %s, got %s)", i, tdescr(t), tostring(t.r), tostring(r)) | ||
61 | end | ||
62 | |||
63 | say"OK" | ||
diff --git a/regress/regress.lua b/regress/regress.lua index 8d955ea..4377db5 100644 --- a/regress/regress.lua +++ b/regress/regress.lua | |||
@@ -1,5 +1,6 @@ | |||
1 | local regress = { | 1 | local regress = { |
2 | openssl = require"openssl", | 2 | openssl = require"openssl", |
3 | bignum = require"openssl.bignum", | ||
3 | pkey = require"openssl.pkey", | 4 | pkey = require"openssl.pkey", |
4 | x509 = require"openssl.x509", | 5 | x509 = require"openssl.x509", |
5 | name = require"openssl.x509.name", | 6 | name = require"openssl.x509.name", |