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

Trap OOB on externalcodecopy #272

Merged
merged 1 commit into from
Jul 29, 2018
Merged

Conversation

lrettig
Copy link
Member

@lrettig lrettig commented Jul 27, 2018

Per ewasm/tests#34 and #256 (comment) we want codecopy and extcodecopy to trap on OOB rather than zero padding.

src/eei.cpp Outdated
@@ -395,7 +395,8 @@ inline int64_t maxCallGas(int64_t gas) {
// FIXME: optimise this so no vector needs to be created
vector<uint8_t> codeBuffer(length);
size_t numCopied = context->fn_table->copy_code(context, &address, codeOffset, codeBuffer.data(), codeBuffer.size());
fill_n(&codeBuffer[numCopied], length - numCopied, 0);
// Make sure we didn't try to copy beyond the end of the code
ensureCondition(numCopied == length, InvalidMemoryAccess, "Out of bounds memory copy");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually can you change the message to Out of bounds (source) memory copy to be in line with the rest? (Please squash it into this)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does the "source" and "destination" here mean? I couldn't understand it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

source memory vs. destination memory

Copy link
Member Author

@lrettig lrettig Jul 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I figured out what's bothering me. While the "destination" checks make sense, the "source" checks in the loadMemory methods only effectively check that the offset is non-negative. E.g. on line 899:

ensureCondition((srcOffset + length) >= srcOffset, InvalidMemoryAccess, "Out of bounds (source) memory copy.");

No check is actually made here that we're not attempting to copy beyond the bounds of memory. I added a sample test for this. Do you agree this is an issue? If so I'll fix it and PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does seem to check for both:

    ensureCondition((srcOffset + length) >= srcOffset, InvalidMemoryAccess, "Out of bounds (source) memory copy.");
    ensureCondition(src.size() >= (srcOffset + length), InvalidMemoryAccess, "Out of bounds (source) memory copy.");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you mean loadMemoy. That should check against memory.size().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. Okay, I'll fix it, and this, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #273

@axic
Copy link
Member

axic commented Jul 28, 2018

Hm, seems that #128 also fixed this same bug.

@lrettig
Copy link
Member Author

lrettig commented Jul 28, 2018

If that will be merged soon and this is redundant feel free to close this, up to you.

@lrettig lrettig force-pushed the extcodecopy-trap-oob branch from 1ed2f7d to 048f550 Compare July 29, 2018 01:37
@axic axic force-pushed the extcodecopy-trap-oob branch from 048f550 to f93e8dd Compare July 29, 2018 10:48
Current behavior is to zero pad. Bring behavior in line with codecopy by
trapping.
@axic axic force-pushed the extcodecopy-trap-oob branch from f93e8dd to d7244f3 Compare July 29, 2018 10:48
@axic
Copy link
Member

axic commented Jul 29, 2018

If that will be merged soon and this is redundant feel free to close this, up to you.

No it is just funny we had it for so long, but wasn't noticed properly.

@axic axic merged commit d78295b into ewasm:master Jul 29, 2018
@axic axic removed the in progress label Jul 29, 2018
jakelang added a commit that referenced this pull request Jul 29, 2018
@lrettig lrettig deleted the extcodecopy-trap-oob branch July 30, 2018 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants