Skip to content

Commit

Permalink
bugfix: resty.aes: fixed memory overrun bug when user provided a salt…
Browse files Browse the repository at this point in the history
… 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) <agentzh@gmail.com>
  • Loading branch information
dndx authored and agentzh committed May 23, 2017
1 parent 154207e commit 82ff0e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/resty/aes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ function _M.new(self, key, salt, _cipher, _hash, hash_rounds)
ffi_copy(gen_iv, _hash.iv, 16)

else
if salt and #salt ~= 8 then
return nil, "salt must be 8 characters or nil"
end

if C.EVP_BytesToKey(_cipher.method, _hash, salt, key, #key,
hash_rounds, gen_key, gen_iv)
~= _cipherLength
Expand Down
19 changes: 7 additions & 12 deletions t/aes.t
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,24 @@ true



=== TEST 4: AES oversized 10-byte salt
=== TEST 4: AES oversized or too short salt
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua '
local aes = require "resty.aes"
local str = require "resty.string"
local aes_default = aes:new("secret","Oversized!")
local encrypted = aes_default:encrypt("hello")
ngx.say("AES-128 (oversized salt) CBC MD5: ", str.to_hex(encrypted))
local decrypted = aes_default:decrypt(encrypted)
ngx.say(decrypted == "hello")
local aes_check = aes:new("secret","Oversize")
local encrypted_check = aes_check:encrypt("hello")
ngx.say(encrypted_check == encrypted)
local res, err = aes:new("secret","Oversized!")
ngx.say(res, ", ", err)
res, err = aes:new("secret","abc")
ngx.say(res, ", ", err)
';
}
--- request
GET /t
--- response_body
AES-128 (oversized salt) CBC MD5: 90a9c9a96f06c597c8da99c37a6c689f
true
true
nil, salt must be 8 characters or nil
nil, salt must be 8 characters or nil
--- no_error_log
[error]

Expand Down

0 comments on commit 82ff0e3

Please sign in to comment.