Skip to content

Commit 82ff0e3

Browse files
dndxagentzh
authored andcommitted
bugfix: resty.aes: fixed memory overrun bug when user provided a salt of less than 8 characters but EVP_BytesToKey() expects more.
disallows salt strings longer than 8 characters to avoid false sense of security. Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent 154207e commit 82ff0e3

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lib/resty/aes.lua

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ function _M.new(self, key, salt, _cipher, _hash, hash_rounds)
159159
ffi_copy(gen_iv, _hash.iv, 16)
160160

161161
else
162+
if salt and #salt ~= 8 then
163+
return nil, "salt must be 8 characters or nil"
164+
end
165+
162166
if C.EVP_BytesToKey(_cipher.method, _hash, salt, key, #key,
163167
hash_rounds, gen_key, gen_iv)
164168
~= _cipherLength

t/aes.t

+7-12
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,24 @@ true
8989
9090
9191
92-
=== TEST 4: AES oversized 10-byte salt
92+
=== TEST 4: AES oversized or too short salt
9393
--- http_config eval: $::HttpConfig
9494
--- config
9595
location /t {
9696
content_by_lua '
9797
local aes = require "resty.aes"
9898
local str = require "resty.string"
99-
local aes_default = aes:new("secret","Oversized!")
100-
local encrypted = aes_default:encrypt("hello")
101-
ngx.say("AES-128 (oversized salt) CBC MD5: ", str.to_hex(encrypted))
102-
local decrypted = aes_default:decrypt(encrypted)
103-
ngx.say(decrypted == "hello")
104-
local aes_check = aes:new("secret","Oversize")
105-
local encrypted_check = aes_check:encrypt("hello")
106-
ngx.say(encrypted_check == encrypted)
99+
local res, err = aes:new("secret","Oversized!")
100+
ngx.say(res, ", ", err)
101+
res, err = aes:new("secret","abc")
102+
ngx.say(res, ", ", err)
107103
';
108104
}
109105
--- request
110106
GET /t
111107
--- response_body
112-
AES-128 (oversized salt) CBC MD5: 90a9c9a96f06c597c8da99c37a6c689f
113-
true
114-
true
108+
nil, salt must be 8 characters or nil
109+
nil, salt must be 8 characters or nil
115110
--- no_error_log
116111
[error]
117112

0 commit comments

Comments
 (0)