chancloser: legacy closer accepts empty shutdown script

Open Beginner friendly
#11,674 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
Half a day
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
networking

Research direction

Inspect lnwallet/chancloser/chancloser.go around validateShutdownScript, where the legacy closer conditionally validates the peer script. Update the validation and adjust lnwallet/chancloser/chancloser_test.go for TestMaybeMatchScript and TestTaprootFastClose; the reproducer test can be added as described. Run go test ./lnwallet/chancloser -run TestReceiveShutdownEmptyScript -count=1 and then the full package tests.

Written by the indexing model from the issue text.

Description

BOLT 2 requires shutdown's scriptpubkey to be P2WPKH, P2WSH, a witness program of version 1 to 16 (with option_shutdown_anysegwit), or an OP_RETURN (with option_simple_close).

But the legacy closer only checks the peer's script if it isn't empty:

https://github.com/lightningnetwork/lnd/blob/d5651c62990282fae6930e9e926b463bb74aaee0/lnwallet/chancloser/chancloser.go#L663-L673

If the peer set no upfront shutdown script, LND accepts an empty scriptpubkey, replies with its own shutdown, and negotiates a closing transaction that pays the peer's balance to an empty PkScript:

https://github.com/lightningnetwork/lnd/blob/d5651c62990282fae6930e9e926b463bb74aaee0/lnwallet/channel.go#L10261-L10274

TestMaybeMatchScript actually asserts that the empty script is accepted:

https://github.com/lightningnetwork/lnd/blob/d5651c62990282fae6930e9e926b463bb74aaee0/lnwallet/chancloser/chancloser_test.go#L85-L90

The RBF closer has rejected it since #11019, which added an explicit check before calling the same function:

https://github.com/lightningnetwork/lnd/blob/d5651c62990282fae6930e9e926b463bb74aaee0/lnwallet/chancloser/rbf_coop_transitions.go#L215-L231

Impact

A transaction with an empty output script is non-standard, so the closing transaction never relays. The operator has to notice and force close.

Reproducer

lnwallet/chancloser/empty_shutdown_script_test.go:

package chancloser

import (
	"testing"

	"github.com/btcsuite/btcd/chaincfg/v2"
	"github.com/btcsuite/btcd/wire/v2"
	"github.com/lightningnetwork/lnd/lntypes"
	"github.com/lightningnetwork/lnd/lnwallet/chainfee"
	"github.com/lightningnetwork/lnd/lnwire"
	"github.com/stretchr/testify/require"
)

// TestReceiveShutdownEmptyScript checks that the legacy closer rejects a
// shutdown with an empty scriptpubkey from a peer that set no upfront shutdown
// script. BOLT 2 doesn't allow an empty scriptpubkey, and a closing
// transaction paying to one isn't standard.
func TestReceiveShutdownEmptyScript(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		script  lnwire.DeliveryAddress
		wantErr bool
	}{
		{
			name:   "p2tr",
			script: remoteAddr,
		},
		{
			name:    "empty",
			script:  lnwire.DeliveryAddress{},
			wantErr: true,
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			// The peer opened the channel without an upfront
			// shutdown script, and now starts a cooperative close.
			channel := &mockChannel{}
			closer := NewChanCloser(
				ChanCloseCfg{
					Channel:     channel,
					ChainParams: &chaincfg.SimNetParams,
					DisableChannel: func(wire.OutPoint) error {
						return nil
					},
				}, DeliveryAddrWithKey{
					DeliveryAddress: localAddr,
				}, chainfee.SatPerKWeight(506), 0, nil,
				lntypes.Remote,
			)

			_, err := closer.ReceiveShutdown(lnwire.Shutdown{
				ChannelID: lnwire.NewChanIDFromOutPoint(
					channel.chanPoint,
				),
				Address: tc.script,
			})
			if tc.wantErr {
				require.Error(t, err, "accepted an empty "+
					"shutdown script")

				return
			}
			require.NoError(t, err)
		})
	}
}
$ go test ./lnwallet/chancloser -run TestReceiveShutdownEmptyScript -count=1
--- FAIL: TestReceiveShutdownEmptyScript (0.00s)
    --- FAIL: TestReceiveShutdownEmptyScript/empty (0.00s)
        empty_shutdown_script_test.go:64:
            	Error:      	An error is expected but got nil.
            	Messages:   	accepted an empty shutdown script
FAIL

Suggested fix

Check every peer script. ValidateUpfrontShutdown already rejects an empty one:

--- a/lnwallet/chancloser/chancloser.go
+++ b/lnwallet/chancloser/chancloser.go
@@ -660,10 +660,10 @@ func validateShutdownScript(upfrontScript, peerScript lnwire.DeliveryAddress,
 			return ErrInvalidShutdownScript
 		}
 	}
-	if len(peerScript) != 0 {
-		if !lnwallet.ValidateUpfrontShutdown(peerScript, netParams) {
-			return ErrInvalidShutdownScript
-		}
+	// BOLT 2 requires the peer's scriptpubkey to be one of the standard
+	// forms, so an empty one is invalid too.
+	if !lnwallet.ValidateUpfrontShutdown(peerScript, netParams) {
+		return ErrInvalidShutdownScript
 	}
 
 	// If no upfront shutdown script was set, return early because we do

Two existing tests also need to be updated for the new behavior:

  • TestMaybeMatchScript
  • TestTaprootFastClose

Discovery

Found incidentally during LLM-assisted analysis of artifacts from a smite fuzzing campaign.

Dominant language
Go
Stars
8.2k
Forks
2.3k
Avg merge
1d 11h
Merged PRs (30d)
103

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lightningnetwork/lnd

All issues in lightningnetwork/lnd

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.