Skip to content

Taken pointer in lightning_gossipd triggered by sendinvoiceless plugin #3757

Closed

Description

Issue and Steps to Reproduce

EDIT: showing the contents of the output file instead of the link:

2020-05-31T19:40:02.694Z INFO plugin-sendinvoiceless.py:     - 02ad6fb8d693dc1e4569bcedefadf5f72a931ae027dc0f0c544b34c1c6f3b9a02b   623064x1516x0  1004msat
2020-05-31T19:40:02.694Z INFO plugin-sendinvoiceless.py:     - 0231eccc6510eb2e1c97c8a190d6ea096784aa7c358355442055aac8b20654f932   615503x2435x0  1002msat
2020-05-31T19:40:02.694Z INFO plugin-sendinvoiceless.py:     - 02888244029c5909593038ab19f269947c720de3423e491791b46c7c92f76279b6   587187x1846x1  1000msat
2020-05-31T19:40:02.696Z INFO lightningd: Sending 39999046msat over 6 hops to deliver 1000msat
2020-05-31T19:40:31.300Z INFO 0231eccc6510eb2e1c97c8a190d6ea096784aa7c358355442055aac8b20654f932-chan#16782: htlc 1346 failed from 2th node with code 0x1007 (WIRE_TEMPORARY_CHANNEL_FAILURE)
2020-05-31T19:40:32.614Z INFO plugin-sendinvoiceless.py: RpcError: RPC call failed: method: waitsendpay, payload: {'payment_hash': '23b2a502c626dc752f91a42a8aa10ae1be905e8e2a8d55a49bbb0cb030be7841', 'timeout': 6}, error: {'code': 204, 'message': 'failed: WIRE_TEMPORARY_CHANNEL_FAILURE (reply from remote)', 'data': {'id': 190, 'payment_hash': '23b2a502c626dc752f91a42a8aa10ae1be905e8e2a8d55a49bbb0cb030be7841', 'destination': '02888244029c5909593038ab19f269947c720de3423e491791b46c7c92f76279b6', 'msatoshi': 1000, 'amount_msat': 1000msat, 'msatoshi_sent': 39999046, 'amount_sent_msat': 39999046msat, 'created_at': 1590954025, 'status': 'pending', 'erring_index': 3, 'failcode': 4103, 'failcodename': 'WIRE_TEMPORARY_CHANNEL_FAILURE', 'erring_node': '021f7430cce1b1ec01675bd575c0785ee2150d1d3167588b7e52a8050d2df2d28e', 'erring_channel': '623064x1516x0', 'erring_direction': 0, 'raw_message': '10070000'}}
lightning_gossipd: Outstanding taken pointers: (nil)
lightningd: gossipd failed (exit status 1), exiting.
pay: Lost connection to the RPC socket.
autoclean: Lost connection to the RPC socket.

EDIT: lightning_gossipd: Outstanding taken pointers: (nil)
doesn't sound good to me ;-)

getinfo output

"version": "v0.8.2-178-g8d98b99"

Activity

  1. changed the title [-]C-lightning crashes (again) with sendinvoiceless[/-] [+]Taken pointer in lightning_gossipd[/+] on Jun 19, 2020
  2. changed the title [-]Taken pointer in lightning_gossipd[/-] [+]Taken pointer in lightning_gossipd triggered by sendinvoiceless plugin[/+] on Jun 19, 2020
  3. cdecker commented on Jun 19, 2020

    @cdecker
    Member

    This is very strange indeed. The code that is reporting an error is this:

    int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout)
    {
    const char *t;
    t = taken_any();
    if (t)
    errx(1, "Outstanding taken pointers: %s", t);
    clean_tmpctx();
    return poll(fds, nfds, timeout);
    }

    (permalink to the commit you reported, but it is unchanged since then)

    The code tests whether we have any taken pointers, assigning to t, checks whether t is NULL, and if not it aborts with an error message. From the error message it seems that the pointer is NULL / (nil), so it should not have triggered the abort, but it does.

    I have no idea how that could happen. Paging Dr @rustyrussell :-)

  4. ZmnSCPxj commented on Jun 19, 2020

    @ZmnSCPxj
    Contributor

    It could be taken_any returns a non-NULL pointer to the string "(nil)". So possibly we may need to look inside at taken_any as well.

  5. ZmnSCPxj commented on Jun 19, 2020

    @ZmnSCPxj
    Contributor

    Looking at taken_any, it does a sprintf(buffer, "%p", takenarr[0]). So it could indeed end up pointing to a string "(nil)", if takenarr[0] is NULL, num_taken is not 0, and there is no labelarr. This implies somebody did a take on a NULL, I think.

  6. ZmnSCPxj commented on Jun 22, 2020

    @ZmnSCPxj
    Contributor

    A part that might be the culprit:

    return patch_channel_update(ctx, take(channel_update));

    The channel_update is loaded from this:

    lightning/gossipd/gossipd.c

    Lines 1481 to 1497 in 5db69f1

    if (!fromwire_temporary_channel_failure(ctx,
    onion_message,
    &channel_update) &&
    !fromwire_amount_below_minimum(ctx,
    onion_message, &unused_msat,
    &channel_update) &&
    !fromwire_fee_insufficient(ctx,
    onion_message, &unused_msat,
    &channel_update) &&
    !fromwire_incorrect_cltv_expiry(ctx,
    onion_message, &unused32,
    &channel_update) &&
    !fromwire_expiry_too_soon(ctx,
    onion_message,
    &channel_update))
    /* No channel update. */
    return NULL;

    Those fromwire_ functions will load NULL into channel_update if the length of the channel update is 0 (which should not happen, but shrug it comes from remote node, which might be buggy or whatev).

    The interesting bit is your logs:

    2020-06-15T04:12:33.734Z INFO plugin-sendinvoiceless.py: RpcError: RPC call failed: method: waitsendpay, payload: {'payment_hash': '271163d996bb2fda734a70ea2f5ee08a5ed89992289f81b2bd93a5dfabb8a2e2', 'timeout': 46}, error: {'code': 204, 'message': 'failed: WIRE_TEMPORARY_CHANNEL_FAILURE (reply from remote)', 'data': {'id': 245, 'payment_hash': '271163d996bb2fda734a70ea2f5ee08a5ed89992289f81b2bd93a5dfabb8a2e2', 'destination': '02888244029c5909593038ab19f269947c720de3423e491791b46c7c92f76279b6', 'msatoshi': 1000, 'amount_msat': 1000msat, 'msatoshi_sent': 40009003, 'amount_sent_msat': 40009003msat, 'created_at': 1592194346, 'status': 'pending', 'erring_index': 3, 'failcode': 4103, 'failcodename': 'WIRE_TEMPORARY_CHANNEL_FAILURE', 'erring_node': '021f7430cce1b1ec01675bd575c0785ee2150d1d3167588b7e52a8050d2df2d28e', 'erring_channel': '623356x854x1', 'erring_direction': 0, 'raw_message': '10070000'}}
    

    See the raw_message bit? It says 10070000. That is hex: two bytes of message ID (0x1007) followed by two 0 bytes of length --- precisely what would cause fromwire_temporary_channel_failure to set channel_update to NULL.

    Note in particular 0x1007 is:

    msgtype,temporary_channel_failure,UPDATE|7
    msgdata,temporary_channel_failure,len,u16,
    msgdata,temporary_channel_failure,channel_update,byte,len

    UPDATE is defined here:

    #define UPDATE 0x1000

    Thus WIRE_TEMPORARY_CHANNEL_FAILURE is 0x1007 (UPDATE|7).

    We see as well that the bytes after the message code is the length of the channel update, which the remote node gave as 0, which causes the generated fromwire_ function to set channel_update to NULL, whih gets passed into take and possibly utterly destroys the take system (maybe).

    So what we can do is try to add this code:

    if (!channel_update)
            return NULL;
    

    before:

    return patch_channel_update(ctx, take(channel_update));

    And try to see if sendinvoiceless triggers the bug again or not.

  7. ZmnSCPxj commented on Jun 22, 2020

    @ZmnSCPxj
    Contributor

    This seems further corroborated by inspecting patch_channel_update:

    lightning/gossipd/gossipd.c

    Lines 1450 to 1466 in 5db69f1

    static u8 *patch_channel_update(const tal_t *ctx, u8 *channel_update TAKES)
    {
    u8 *fixed;
    if (channel_update != NULL &&
    fromwire_peektype(channel_update) != WIRE_CHANNEL_UPDATE) {
    /* This should be a channel_update, prefix with the
    * WIRE_CHANNEL_UPDATE type, but isn't. Let's prefix it. */
    fixed = tal_arr(ctx, u8, 0);
    towire_u16(&fixed, WIRE_CHANNEL_UPDATE);
    towire(&fixed, channel_update, tal_bytelen(channel_update));
    if (taken(channel_update))
    tal_free(channel_update);
    return fixed;
    } else {
    return tal_dup_talarr(ctx, u8, channel_update);
    }
    }

    If channel_update is a taken pointer to NULL, then it goes into the else branch, which just calls into tal_dup_talarr:

    lightning/gossipd/gossipd.c

    Lines 1463 to 1465 in 5db69f1

    } else {
    return tal_dup_talarr(ctx, u8, channel_update);
    }

    The implementation of tal_dup_talarr is:

    lightning/common/utils.c

    Lines 145 to 150 in 5db69f1

    void *tal_dup_talarr_(const tal_t *ctx, const tal_t *src, const char *label)
    {
    if (!src)
    return NULL;
    return tal_dup_(ctx, src, 1, tal_bytelen(src), 0, label);
    }

    The culprit is this:

    lightning/common/utils.c

    Lines 147 to 148 in 5db69f1

    if (!src)
    return NULL;

    If the input pointer is NULL we return NULL as well, BUT if it is a taken NULL pointer, we do not pass it to the taken function, which is needed in order to clear the taken-ness of NULL.

    So a better fix would be:

    void *tal_dup_talarr_(const tal_t *ctx, const tal_t *src, const char *label)   
    {       
            if (!src) {
                    (void) taken(src);
                    return NULL; 
            }
            return tal_dup_(ctx, src, 1, tal_bytelen(src), 0, label);              
    }

    This makes tal_dup_talarr correctly take NULL inputs.

  8. added 3 commits that reference this issue on Jun 22, 2020
    19381a4
    cece06a
    d5d69ea
  9. ZmnSCPxj commented on Jun 22, 2020

    @ZmnSCPxj
    Contributor

    Hmm. Well it "should" be possible to write a plugin used in testing that will force an onion failure of WIRE_TEMPORARY_CHANNEL_FAILURE with a length-0 channel_update in htlc_accepted hook, which should tickle this bug as well. Let me try to see if I can tickle that bug this way.

  10. 2 remaining items

  11. ZmnSCPxj commented on Jun 22, 2020

    @ZmnSCPxj
    Contributor
  12. ZmnSCPxj commented on Jun 23, 2020

    @ZmnSCPxj
    Contributor

    I think we have the conservative policy that any crash of lightning_gossipd might corrupt the gossip store, so we assume the gossip store is corrupted. It might not necessarily be corrupted. Though do note that the code I pointed out is what extracts a new channel_update from a payment failure, and we generally store channel_updates into the store, so the abort at that point might also be what lead to corrupted gossip stores (e.g. we queue up some updates into the gossip store, then we crash while processing one of them because of this "taken NULL" bug, preventing us from properly writing out the correct data to the gossip store). Not certain; speculation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions