Skip to content

Fix PSBT marshal/unmarshal functions. - #191

Closed
rustyrussell wants to merge 16 commits into
ElementsProject:masterfrom
rustyrussell:guilt/fix-psbt-uninit
Closed

Fix PSBT marshal/unmarshal functions.#191
rustyrussell wants to merge 16 commits into
ElementsProject:masterfrom
rustyrussell:guilt/fix-psbt-uninit

Conversation

@rustyrussell

Copy link
Copy Markdown
Contributor

Started with PSBT, will continue. But worth reviewing.

@rustyrussell
rustyrussell marked this pull request as draft May 20, 2020 05:38
Comment thread src/pullpush.c
memcpy(*cursor, src, *max);
/* From now on, max records the room we *needed* */
*max = len - *max;
*cursor = NULL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the purpose of doing this on failure?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

*cursor == NULL is the way you detect unmarshalling failure.

*max increasing is how you tell how many bytes short you are.

You'll see this explicitly used here:

    if (cursor == NULL) {
            /* Once cursor was NULL, max accumulates hm bytes we needed */
            *bytes_written = len + max;
            return WALLY_EINVAL;
    } else {
            *bytes_written = len - max;
    }

@achow101

Copy link
Copy Markdown
Contributor

Concept ACK

@rustyrussell
rustyrussell force-pushed the guilt/fix-psbt-uninit branch from 234e885 to e7023db Compare May 22, 2020 05:05
@rustyrussell
rustyrussell marked this pull request as ready for review May 22, 2020 05:06
@rustyrussell rustyrussell changed the title [RFC] Start using proper marshal/unmarshal functions. Fix PSBT marshal/unmarshal functions. May 22, 2020
@rustyrussell
rustyrussell force-pushed the guilt/fix-psbt-uninit branch from e7023db to 52d5ba8 Compare May 23, 2020 04:25
@rustyrussell

Copy link
Copy Markdown
Contributor Author

This still fails travis, but I can't tell why since the logs are insufficient :(

@glslang

glslang commented May 23, 2020

Copy link
Copy Markdown
Contributor

Look at tools/travis_build.sh and add your new test to it. you should have the output of it somewhere in the build output

@rustyrussell
rustyrussell force-pushed the guilt/fix-psbt-uninit branch from 52d5ba8 to 3931357 Compare May 25, 2020 01:40
@rustyrussell

Copy link
Copy Markdown
Contributor Author

Look at tools/travis_build.sh and add your new test to it. you should have the output of it somewhere in the build output

OK, I fixed it a different way in the first commit: let's see how that looks?

@rustyrussell
rustyrussell force-pushed the guilt/fix-psbt-uninit branch 2 times, most recently from 08a1f16 to e9100ff Compare May 25, 2020 02:04
@glslang

glslang commented May 25, 2020

Copy link
Copy Markdown
Contributor

Nice. #197 fixes the actual travis build so hopefully all current outstanding PRs will have a green build.

@rustyrussell
rustyrussell force-pushed the guilt/fix-psbt-uninit branch 2 times, most recently from 168a926 to a874af4 Compare May 27, 2020 04:08

@achow101 achow101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few comments

  • In 8484e97 "travis: don't explicitly list what tests in tools/travis_build.sh": Maybe don't @ mention people in commit messages. In Core, we have a policy that commit messages shouldn't do that as those people will get spammed when forks pick up those commits.
  • In 637e4c5 "psbt: add a simple test case, by copying some of BIP 0174.": src/test_psbt* needs to be added to .gitignore.
  • In e06ad79 "pullpush: add routines for safe marshal / unmarshal.": There are a few places where max and cursor are dereferenced without checking that they are not null.. This seems okay to me as the pushpull functions are internal and we would want to see a segfault as that indicates we are using them improperly. But maybe that's not the intention?

Comment thread src/pullpush.c Outdated

void push_u8(unsigned char **cursor, size_t *max, uint8_t v)
{
push_bytes(cursor, max, &v, 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In e06ad79 "pullpush: add routines for safe marshal / unmarshal."

nit: sizeof(v) instead of 1

AFAICT, the below does the same thing, and avoids glslang having
to tell me to add my test there.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
As revealed by:

	make && LD_LIBRARY_PATH=`pwd`/src/.libs valgrind  --leak-check=full src/.libs/test_psbt

There are more, but the next patches rework that code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
@rustyrussell

rustyrussell commented May 29, 2020

Copy link
Copy Markdown
Contributor Author

A few comments

* In [8484e97](https://github.com/ElementsProject/libwally-core/commit/8484e9760d89419fbd56e796685569150c7b6b36) "travis: don't explicitly list what tests in tools/travis_build.sh": Maybe don't @ mention people in commit messages. In Core, we have a policy that commit messages shouldn't do that as those people will get spammed when forks pick up those commits.

Ah, I gave up reading my GH notifications long ago, so I never noticed. Good idea, thanks!

* In [637e4c5](https://github.com/ElementsProject/libwally-core/commit/637e4c58a5f355c2d6a238cdf31676dd1dd2149e) "psbt: add a simple test case, by copying some of BIP 0174.": `src/test_psbt*` needs to be added to `.gitignore`.

Ack, and same with limits test. (EDIT: actually, they use such gross wildcards in .gitignore that they get covered too. This is bad practice, but that's a separate issue).

* In [e06ad79](https://github.com/ElementsProject/libwally-core/commit/e06ad79ea226348dcc7ef960fea88d475723ff97) "pullpush: add routines for safe marshal / unmarshal.": There are a few places where `max` and `cursor` are dereferenced without checking that they are not null.. This seems okay to me as the pushpull functions are internal and we would want to see a segfault as that indicates we are using them improperly. But maybe that's not the intention?

Yes, they're internal only, and have specific semantics; adding checks like this would seem to add nothing but clutter. In particular, there's no infra in libwally for "crash iff in dev mode" which would allow CI to be effective in this case. If we were to return WALLY_EINVAL on internal errors, we might not notice them in CI.

Manually checking for end of buffers is error prone: these pull & push
variants are similar to those used by c-lightning.

Importantly, if they fail you can still call them, meaning you can
check whether it overflowed at the end of all the work.  The push
function also can tell you exactly how many bytes short you were.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This also means we return WALLY_EINVAL instead of corrupting the stack
if they provide a witness output > 50 bytes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
As a special bonus, document that it now returns bytes_written as the
entire length, if the error was caused by bytes_len being insufficient.

This allows the caller to avoid calling bytes_len in the common case,
if they want to.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
…ytes.

This avoid (recently buggy!) duplication.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Now we pull the magic before calling count_psbt_parts(), it no longer
needs to skip over it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This currently fails (badly).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
valgrind is better, but this is simple and finds current leaks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
@rustyrussell
rustyrussell force-pushed the guilt/fix-psbt-uninit branch from a874af4 to 1cccf05 Compare May 29, 2020 01:17
@rustyrussell

Copy link
Copy Markdown
Contributor Author

Rebased with fixes from @achow101 (all minor).

@achow101

Copy link
Copy Markdown
Contributor

ACK 1cccf05

@achow101

Copy link
Copy Markdown
Contributor

@glslang @greenaddress Can this be merged?

This was referenced Jul 13, 2020
@jgriffiths

Copy link
Copy Markdown
Contributor

Closing this in favour of #210 which includes/supersedes it. Please re-open if something you need got missed in the merge.

@jgriffiths jgriffiths closed this Jul 16, 2020
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.

4 participants