chancloser: legacy closer accepts empty shutdown script

Aperta Adatta ai principianti
#11,674 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
Mezza giornata
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
networking

Direzione di ricerca

Ispeziona lnwallet/chancloser/chancloser.go intorno a validateShutdownScript, dove il closer legacy convalida condizionalmente lo script del peer. Aggiorna la validazione e adatta lnwallet/chancloser/chancloser_test.go per TestMaybeMatchScript e TestTaprootFastClose; il test di riproduzione può essere aggiunto come descritto. Esegui go test ./lnwallet/chancloser -run TestReceiveShutdownEmptyScript -count=1 e poi i test completi del package.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Go
Stelle
8.2k
Fork
2.3k
Merge medio
1g 11h
PR unite (30g)
103

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di lightningnetwork/lnd

Tutte le issue di lightningnetwork/lnd

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.