Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid reading out of buffer limits in readWordFromBytes #402

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/EVM/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,17 @@ readWord i b = readWordFromBytes i b
-- returns an abstract ReadWord expression if a concrete word cannot be constructed
readWordFromBytes :: Expr EWord -> Expr Buf -> Expr EWord
readWordFromBytes (Lit idx) (ConcreteBuf bs) =
case toInt idx of
Nothing -> Lit 0
Just i -> Lit $ word $ padRight 32 $ BS.take 32 $ BS.drop i bs
if idx <= (maxBound :: W256) - 31
-- the word we are trying to read is within the address space of the buffer
then case toInt idx of
Nothing -> Lit 0
Just i -> Lit $ word $ padRight 32 $ BS.take 32 $ BS.drop i bs
-- if the word we are trying to read goes outside of bounds, then
-- wraparound and take the remaing bytes from the beginning of the
-- buffer
else case toInt ((maxBound :: W256) - idx + 1) of
Nothing -> Lit 0 -- should never reach here because maxBound - idx + 1 < 32
Just i -> Lit $ word $ padLeft i $ BS.take (32 - i) bs
readWordFromBytes i@(Lit idx) buf = let
bytes = [readByte (Lit i') buf | i' <- [idx .. idx + 31]]
in if Prelude.and . (fmap isLitByte) $ bytes
Expand Down
Loading