Skip to content

Commit f91202c

Browse files
committed
fix(*) redefine callback functions to a style FFI will not overflow
Though in C function pointers are flexible, FFI seems to only accept certain combination of usage: ```lua -- as signature void (*cb)(...); -- when used void consumer(cb c); ``` Some combination will result in FFI parser error, one will result in FFI doing implict magic and result in overflow: ```lua local ffi = require("ffi") ffi.cdef[[ typedef int cb(); ]] for i=0, 60000 do local pok, pp = pcall(ffi.cast, "cb*", function() end) if not pok then print(i, ", ", pp) break end if pp then pp:free() end end ```
1 parent 5d9e7a4 commit f91202c

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

lib/resty/openssl/include/pem.lua

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@ local ffi = require "ffi"
44
require "resty.openssl.include.ossl_typ"
55

66
ffi.cdef [[
7-
typedef int pem_password_cb (char *buf, int size, int rwflag, void *userdata);
7+
// all pem_password_cb* has been modified to pem_password_cb to avoid a table overflow issue
8+
typedef int (*pem_password_cb)(char *buf, int size, int rwflag, void *userdata);
89
EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
910
// the following signature has been modified to avoid ffi.cast
10-
pem_password_cb *cb, const char *u);
11+
pem_password_cb cb, const char *u);
1112
// pem_password_cb *cb, void *u);
1213
EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
1314
// the following signature has been modified to avoid ffi.cast
14-
pem_password_cb *cb, const char *u);
15+
pem_password_cb cb, const char *u);
1516
// pem_password_cb *cb, void *u);
1617
int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
1718
unsigned char *kstr, int klen,
18-
pem_password_cb *cb, void *u);
19+
pem_password_cb cb, void *u);
1920
int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
2021

21-
X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x, pem_password_cb *cb, void *u);
22+
X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x, pem_password_cb cb, void *u);
2223
int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
2324

24-
X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x, pem_password_cb *cb, void *u);
25+
X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x, pem_password_cb cb, void *u);
2526
int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
2627

27-
X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
28+
X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb cb, void *u);
2829
int PEM_write_bio_X509(BIO *bp, X509 *x);
2930

30-
DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
31+
DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb cb, void *u);
3132
int PEM_write_bio_DHparams(BIO *bp, DH *x);
3233

33-
EC_GROUP *PEM_read_bio_ECPKParameters(BIO *bp, EC_GROUP **x, pem_password_cb *cb, void *u);
34+
EC_GROUP *PEM_read_bio_ECPKParameters(BIO *bp, EC_GROUP **x, pem_password_cb cb, void *u);
3435
int PEM_write_bio_ECPKParameters(BIO *bp, const EC_GROUP *x);
3536

3637
]]

lib/resty/openssl/include/ssl.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ffi.cdef [[
5252
/*STACK_OF(SSL_CIPHER)*/ OPENSSL_STACK *SSL_CTX_get_ciphers(const SSL_CTX *ctx);
5353
OPENSSL_STACK *SSL_get_peer_cert_chain(const SSL *ssl);
5454

55-
typedef int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx);
55+
typedef int (*verify_callback)(int preverify_ok, X509_STORE_CTX *x509_ctx);
5656
void SSL_set_verify(SSL *s, int mode,
5757
int (*verify_callback)(int, X509_STORE_CTX *));
5858

lib/resty/openssl/pkey.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ local function load_pem_der(txt, opts, funcs)
8888
end
8989
arg = { null, nil, passphrase }
9090
elseif opts.passphrase_cb then
91-
passphrase_cb = ffi_cast("pem_password_cb*", function(buf, size)
91+
passphrase_cb = ffi_cast("pem_password_cb", function(buf, size)
9292
local p = opts.passphrase_cb()
9393
local len = #p -- 1 byte for \0
9494
if len > size then

lib/resty/openssl/ssl.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function _M:get_timeout()
216216
return tonumber(C.SSL_SESSION_get_timeout(session))
217217
end
218218

219-
local ssl_verify_default_cb = ffi_cast("verify_callback*", function()
219+
local ssl_verify_default_cb = ffi_cast("verify_callback", function()
220220
return 1
221221
end)
222222

@@ -226,7 +226,7 @@ function _M:set_verify(mode, cb)
226226
end
227227

228228
if cb then
229-
cb = ffi_cast("verify_callback*", cb)
229+
cb = ffi_cast("verify_callback", cb)
230230
self._verify_cb = cb
231231
end
232232

0 commit comments

Comments
 (0)