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

Fix invalid memory access in CScript::operator+= (guidovranken, ajtowns) #11284

Merged
merged 1 commit into from Oct 2, 2017

Conversation

ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Sep 8, 2017

This is a fix for #11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

@practicalswift
Copy link
Contributor

Concept ACK. Nice!

@donaloconnor
Copy link
Contributor

ACK - good spot.

Copy link
Member

@sipa sipa left a comment

Choose a reason for hiding this comment

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

utACK 84a8b8ab28cf379f1909f0bfc63c9eb25fba4dbd

I was going to suggest just outlawing adding a CScript to itself, but this fix is so small there's no point doing anything else.

@@ -420,6 +420,7 @@ class CScript : public CScriptBase

CScript& operator+=(const CScript& b)
{
reserve(size()+b.size());
Copy link
Member

Choose a reason for hiding this comment

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

Ultranit: spaces around +.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooops. Fixed. Ran the clang-format snippet over it; also changed the declaration of "hex" in the test code.

@ajtowns
Copy link
Contributor Author

ajtowns commented Sep 11, 2017

Testing with the updated script_tests.cpp -- without reserve():

$ valgrind ./test_bitcoin -t script_tests -p
[...]
==26488== Invalid read of size 1
==26488==    at 0x305BE8: insert<prevector<28, unsigned char>::const_iterator> (prevector.h:379)
==26488==    by 0x305BE8: CScript::operator+=(CScript const&) (script.h:423)
==26488==    by 0x2D8BF2: script_tests::script_c_append_self::test_method() (script_tests.cpp:1467)

with reserve():

$ valgrind ./test_bitcoin -t script_tests -p
[...]
Running 12 test cases...

0%   10   20   30   40   50   60   70   80   90   100%
|----|----|----|----|----|----|----|----|----|----|
***************************************************

*** No errors detected
[...]

@jnewbery
Copy link
Contributor

I believe common practice would be to credit the reporter (@guidovranken) in the commit message.

@maflcko maflcko changed the title Fix invalid memory access in CScript::operator+= Fix invalid memory access in CScript::operator+= (guidovranken, ajtowns) Sep 18, 2017
@laanwj
Copy link
Member

laanwj commented Oct 2, 2017

utACK d601f16

@laanwj laanwj merged commit d601f16 into bitcoin:master Oct 2, 2017
laanwj added a commit that referenced this pull request Oct 2, 2017
…vranken, ajtowns)

d601f16 Fix invalid memory access in CScript::operator+= (Anthony Towns)

Pull request description:

  This is a fix for #11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

  The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

  A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

Tree-SHA512: 4720d0c17463fdc43b344c45fe603423d20b30d48da1b9d85eeedc505d7f34db1ed5495ef1556459ae962a94717e3c6e8fc441763771901efea210d01322b7ef
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 17, 2020
… (guidovranken, ajtowns)

d601f16 Fix invalid memory access in CScript::operator+= (Anthony Towns)

Pull request description:

  This is a fix for bitcoin#11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

  The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

  A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

Tree-SHA512: 4720d0c17463fdc43b344c45fe603423d20b30d48da1b9d85eeedc505d7f34db1ed5495ef1556459ae962a94717e3c6e8fc441763771901efea210d01322b7ef
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 22, 2020
… (guidovranken, ajtowns)

d601f16 Fix invalid memory access in CScript::operator+= (Anthony Towns)

Pull request description:

  This is a fix for bitcoin#11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

  The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

  A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

Tree-SHA512: 4720d0c17463fdc43b344c45fe603423d20b30d48da1b9d85eeedc505d7f34db1ed5495ef1556459ae962a94717e3c6e8fc441763771901efea210d01322b7ef
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 22, 2020
… (guidovranken, ajtowns)

d601f16 Fix invalid memory access in CScript::operator+= (Anthony Towns)

Pull request description:

  This is a fix for bitcoin#11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

  The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

  A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

Tree-SHA512: 4720d0c17463fdc43b344c45fe603423d20b30d48da1b9d85eeedc505d7f34db1ed5495ef1556459ae962a94717e3c6e8fc441763771901efea210d01322b7ef
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 29, 2020
… (guidovranken, ajtowns)

d601f16 Fix invalid memory access in CScript::operator+= (Anthony Towns)

Pull request description:

  This is a fix for bitcoin#11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

  The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

  A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

Tree-SHA512: 4720d0c17463fdc43b344c45fe603423d20b30d48da1b9d85eeedc505d7f34db1ed5495ef1556459ae962a94717e3c6e8fc441763771901efea210d01322b7ef
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 29, 2020
… (guidovranken, ajtowns)

d601f16 Fix invalid memory access in CScript::operator+= (Anthony Towns)

Pull request description:

  This is a fix for bitcoin#11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

  The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

  A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

Tree-SHA512: 4720d0c17463fdc43b344c45fe603423d20b30d48da1b9d85eeedc505d7f34db1ed5495ef1556459ae962a94717e3c6e8fc441763771901efea210d01322b7ef
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 31, 2020
… (guidovranken, ajtowns)

d601f16 Fix invalid memory access in CScript::operator+= (Anthony Towns)

Pull request description:

  This is a fix for bitcoin#11114 -- invoking "s += s" gets turned into "s.insert(s.end(), s.begin(), s.end())" which can result in an invalid memory access is s.capacity() < 2*s.size() (because s gets resized and possibly moved, so s.begin() and s.end() become invalid references when reading the values to be appended).

  The fix is straightforward: reserve enough space in advance, so that insert() doesn't need to resize and thus its arguments remain valid.

  A simple test case is added as well; though you probably need to run it via valgrind to actually catch the problem when it's not fixed...

Tree-SHA512: 4720d0c17463fdc43b344c45fe603423d20b30d48da1b9d85eeedc505d7f34db1ed5495ef1556459ae962a94717e3c6e8fc441763771901efea210d01322b7ef
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants