You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Taken pointer in lightning_gossipd triggered by sendinvoiceless plugin #3757
changed the title [-]Taken pointer in lightning_gossipd[/-][+]Taken pointer in lightning_gossipd triggered by sendinvoiceless plugin[/+]on Jun 19, 2020
(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 :-)
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.
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).
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.
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).
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.
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.
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.
Activity
[-]C-lightning crashes (again) with sendinvoiceless[/-][+]Taken pointer in lightning_gossipd[/+][-]Taken pointer in lightning_gossipd[/-][+]Taken pointer in lightning_gossipd triggered by sendinvoiceless plugin[/+]cdecker commentedon Jun 19, 2020
This is very strange indeed. The code that is reporting an error is this:
lightning/common/daemon.c
Lines 81 to 92 in 8d98b99
(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 whethertisNULL, and if not it aborts with an error message. From the error message it seems that the pointer isNULL/(nil), so it should not have triggered the abort, but it does.I have no idea how that could happen. Paging Dr @rustyrussell :-)
ZmnSCPxj commentedon Jun 19, 2020
It could be
taken_anyreturns a non-NULLpointer to the string"(nil)". So possibly we may need to look inside attaken_anyas well.ZmnSCPxj commentedon Jun 19, 2020
Looking at
taken_any, it does asprintf(buffer, "%p", takenarr[0]). So it could indeed end up pointing to a string"(nil)", iftakenarr[0]is NULL,num_takenis not 0, and there is nolabelarr. This implies somebody did atakeon aNULL, I think.ZmnSCPxj commentedon Jun 22, 2020
A part that might be the culprit:
lightning/gossipd/gossipd.c
Line 1499 in 5db69f1
The
channel_updateis loaded from this:lightning/gossipd/gossipd.c
Lines 1481 to 1497 in 5db69f1
Those
fromwire_functions will loadNULLintochannel_updateif 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:
See the
raw_messagebit? It says10070000. That is hex: two bytes of message ID (0x1007) followed by two 0 bytes of length --- precisely what would causefromwire_temporary_channel_failureto setchannel_updatetoNULL.Note in particular 0x1007 is:
lightning/wire/extracted_onion_wire_csv
Lines 21 to 23 in 5db69f1
UPDATEis defined here:lightning/wire/onion_defs.h
Line 17 in 5db69f1
Thus
WIRE_TEMPORARY_CHANNEL_FAILUREis 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 setchannel_updatetoNULL, whih gets passed intotakeand possibly utterly destroys thetakesystem (maybe).So what we can do is try to add this code:
before:
lightning/gossipd/gossipd.c
Line 1499 in 5db69f1
And try to see if
sendinvoicelesstriggers the bug again or not.ZmnSCPxj commentedon Jun 22, 2020
This seems further corroborated by inspecting
patch_channel_update:lightning/gossipd/gossipd.c
Lines 1450 to 1466 in 5db69f1
If
channel_updateis ataken pointer toNULL, then it goes into theelsebranch, which just calls intotal_dup_talarr:lightning/gossipd/gossipd.c
Lines 1463 to 1465 in 5db69f1
The implementation of
tal_dup_talarris:lightning/common/utils.c
Lines 145 to 150 in 5db69f1
The culprit is this:
lightning/common/utils.c
Lines 147 to 148 in 5db69f1
If the input pointer is
NULLwe returnNULLas well, BUT if it is atakenNULLpointer, we do not pass it to thetakenfunction, which is needed in order to clear thetaken-ness ofNULL.So a better fix would be:
This makes
tal_dup_talarrcorrectly takeNULLinputs.common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
ZmnSCPxj commentedon Jun 22, 2020
Hmm. Well it "should" be possible to write a plugin used in testing that will force an onion failure of
WIRE_TEMPORARY_CHANNEL_FAILUREwith a length-0channel_updateinhtlc_acceptedhook, which should tickle this bug as well. Let me try to see if I can tickle that bug this way.tests/test_pay.py: Add test to replicate ElementsProject#3757.
2 remaining items
tests/test_pay.py: Add test to replicate ElementsProject#3757.
ZmnSCPxj commentedon Jun 22, 2020
Got a similar message with the proposed test. https://travis-ci.org/github/ZmnSCPxj/lightning/jobs/700837757#L2572
tests/test_pay.py: Add test to replicate ElementsProject#3757.
common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
tests/test_pay.py: Add test to replicate ElementsProject#3757.
common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
tests/test_pay.py: Add test to replicate ElementsProject#3757.
common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
tests/test_pay.py: Add test to replicate ElementsProject#3757.
common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
tests/test_pay.py: Add test to replicate #3757.
common/utils.c: Correctly handle NULL `take`n pointer in `tal_dup_tal…
ZmnSCPxj commentedon Jun 23, 2020
I think we have the conservative policy that any crash of
lightning_gossipdmight 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 newchannel_updatefrom a payment failure, and we generally storechannel_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.