You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
11 KiB

  1. package light
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "github.com/tendermint/tendermint/light/provider"
  9. "github.com/tendermint/tendermint/types"
  10. )
  11. // The detector component of the light client detect and handles attacks on the light client.
  12. // More info here:
  13. // tendermint/docs/architecture/adr-047-handling-evidence-from-light-client.md
  14. // detectDivergence is a second wall of defense for the light client.
  15. //
  16. // It takes the target verified header and compares it with the headers of a set of
  17. // witness providers that the light client is connected to. If a conflicting header
  18. // is returned it verifies and examines the conflicting header against the verified
  19. // trace that was produced from the primary. If successful it produces two sets of evidence
  20. // and sends them to the opposite provider before halting.
  21. //
  22. // If there are no conflictinge headers, the light client deems the verified target header
  23. // trusted and saves it to the trusted store.
  24. func (c *Client) detectDivergence(ctx context.Context, primaryTrace []*types.LightBlock, now time.Time) error {
  25. if primaryTrace == nil || len(primaryTrace) < 2 {
  26. return errors.New("nil or single block primary trace")
  27. }
  28. var (
  29. headerMatched bool
  30. lastVerifiedHeader = primaryTrace[len(primaryTrace)-1].SignedHeader
  31. witnessesToRemove = make([]int, 0)
  32. )
  33. c.logger.Debug("Running detector against trace", "endBlockHeight", lastVerifiedHeader.Height,
  34. "endBlockHash", lastVerifiedHeader.Hash, "length", len(primaryTrace))
  35. c.providerMutex.Lock()
  36. defer c.providerMutex.Unlock()
  37. if len(c.witnesses) == 0 {
  38. return errNoWitnesses{}
  39. }
  40. // launch one goroutine per witness to retrieve the light block of the target height
  41. // and compare it with the header from the primary
  42. errc := make(chan error, len(c.witnesses))
  43. for i, witness := range c.witnesses {
  44. go c.compareNewHeaderWithWitness(ctx, errc, lastVerifiedHeader, witness, i)
  45. }
  46. // handle errors from the header comparisons as they come in
  47. for i := 0; i < cap(errc); i++ {
  48. err := <-errc
  49. switch e := err.(type) {
  50. case nil: // at least one header matched
  51. headerMatched = true
  52. case errConflictingHeaders:
  53. // We have conflicting headers. This could possibly imply an attack on the light client.
  54. // First we need to verify the witness's header using the same skipping verification and then we
  55. // need to find the point that the headers diverge and examine this for any evidence of an attack.
  56. //
  57. // We combine these actions together, verifying the witnesses headers and outputting the trace
  58. // which captures the bifurcation point and if successful provides the information to create
  59. supportingWitness := c.witnesses[e.WitnessIndex]
  60. witnessTrace, primaryBlock, err := c.examineConflictingHeaderAgainstTrace(
  61. ctx,
  62. primaryTrace,
  63. e.Block.SignedHeader,
  64. supportingWitness,
  65. now,
  66. )
  67. if err != nil {
  68. c.logger.Info("Error validating witness's divergent header", "witness", supportingWitness, "err", err)
  69. witnessesToRemove = append(witnessesToRemove, e.WitnessIndex)
  70. continue
  71. }
  72. // We are suspecting that the primary is faulty, hence we hold the witness as the source of truth
  73. // and generate evidence against the primary that we can send to the witness
  74. primaryEv := newLightClientAttackEvidence(primaryBlock, witnessTrace[len(witnessTrace)-1], witnessTrace[0])
  75. c.logger.Error("Attempted attack detected. Sending evidence againt primary by witness", "ev", primaryEv,
  76. "primary", c.primary, "witness", supportingWitness)
  77. c.sendEvidence(ctx, primaryEv, supportingWitness)
  78. if primaryBlock.Commit.Round != witnessTrace[len(witnessTrace)-1].Commit.Round {
  79. c.logger.Info("The light client has detected, and prevented, an attempted amnesia attack." +
  80. " We think this attack is pretty unlikely, so if you see it, that's interesting to us." +
  81. " Can you let us know by opening an issue through https://github.com/tendermint/tendermint/issues/new?")
  82. }
  83. // This may not be valid because the witness itself is at fault. So now we reverse it, examining the
  84. // trace provided by the witness and holding the primary as the source of truth. Note: primary may not
  85. // respond but this is okay as we will halt anyway.
  86. primaryTrace, witnessBlock, err := c.examineConflictingHeaderAgainstTrace(
  87. ctx,
  88. witnessTrace,
  89. primaryBlock.SignedHeader,
  90. c.primary,
  91. now,
  92. )
  93. if err != nil {
  94. c.logger.Info("Error validating primary's divergent header", "primary", c.primary, "err", err)
  95. return ErrLightClientAttack
  96. }
  97. // We now use the primary trace to create evidence against the witness and send it to the primary
  98. witnessEv := newLightClientAttackEvidence(witnessBlock, primaryTrace[len(primaryTrace)-1], primaryTrace[0])
  99. c.logger.Error("Sending evidence against witness by primary", "ev", witnessEv,
  100. "primary", c.primary, "witness", supportingWitness)
  101. c.sendEvidence(ctx, witnessEv, c.primary)
  102. // We return the error and don't process anymore witnesses
  103. return ErrLightClientAttack
  104. case errBadWitness:
  105. c.logger.Info("Witness returned an error during header comparison", "witness", c.witnesses[e.WitnessIndex],
  106. "err", err)
  107. // if witness sent us an invalid header, then remove it. If it didn't respond or couldn't find the block, then we
  108. // ignore it and move on to the next witness
  109. if _, ok := e.Reason.(provider.ErrBadLightBlock); ok {
  110. c.logger.Info("Witness sent us invalid header / vals -> removing it", "witness", c.witnesses[e.WitnessIndex])
  111. witnessesToRemove = append(witnessesToRemove, e.WitnessIndex)
  112. }
  113. }
  114. }
  115. for _, idx := range witnessesToRemove {
  116. c.removeWitness(idx)
  117. }
  118. // 1. If we had at least one witness that returned the same header then we
  119. // conclude that we can trust the header
  120. if headerMatched {
  121. return nil
  122. }
  123. // 2. ELse all witnesses have either not responded, don't have the block or sent invalid blocks.
  124. return ErrFailedHeaderCrossReferencing
  125. }
  126. // compareNewHeaderWithWitness takes the verified header from the primary and compares it with a
  127. // header from a specified witness. The function can return one of three errors:
  128. //
  129. // 1: errConflictingHeaders -> there may have been an attack on this light client
  130. // 2: errBadWitness -> the witness has either not responded, doesn't have the header or has given us an invalid one
  131. // Note: In the case of an invalid header we remove the witness
  132. // 3: nil -> the hashes of the two headers match
  133. func (c *Client) compareNewHeaderWithWitness(ctx context.Context, errc chan error, h *types.SignedHeader,
  134. witness provider.Provider, witnessIndex int) {
  135. lightBlock, err := witness.LightBlock(ctx, h.Height)
  136. if err != nil {
  137. errc <- errBadWitness{Reason: err, WitnessIndex: witnessIndex}
  138. return
  139. }
  140. if !bytes.Equal(h.Hash(), lightBlock.Hash()) {
  141. errc <- errConflictingHeaders{Block: lightBlock, WitnessIndex: witnessIndex}
  142. }
  143. c.logger.Debug("Matching header received by witness", "height", h.Height, "witness", witnessIndex)
  144. errc <- nil
  145. }
  146. // sendEvidence sends evidence to a provider on a best effort basis.
  147. func (c *Client) sendEvidence(ctx context.Context, ev *types.LightClientAttackEvidence, receiver provider.Provider) {
  148. err := receiver.ReportEvidence(ctx, ev)
  149. if err != nil {
  150. c.logger.Error("Failed to report evidence to provider", "ev", ev, "provider", receiver)
  151. }
  152. }
  153. // examineConflictingHeaderAgainstTrace takes a trace from one provider and a divergent header that
  154. // it has received from another and preforms verifySkipping at the heights of each of the intermediate
  155. // headers in the trace until it reaches the divergentHeader. 1 of 2 things can happen.
  156. //
  157. // 1. The light client verifies a header that is different to the intermediate header in the trace. This
  158. // is the bifurcation point and the light client can create evidence from it
  159. // 2. The source stops responding, doesn't have the block or sends an invalid header in which case we
  160. // return the error and remove the witness
  161. func (c *Client) examineConflictingHeaderAgainstTrace(
  162. ctx context.Context,
  163. trace []*types.LightBlock,
  164. divergentHeader *types.SignedHeader,
  165. source provider.Provider, now time.Time) ([]*types.LightBlock, *types.LightBlock, error) {
  166. var previouslyVerifiedBlock *types.LightBlock
  167. for idx, traceBlock := range trace {
  168. // The first block in the trace MUST be the same to the light block that the source produces
  169. // else we cannot continue with verification.
  170. sourceBlock, err := source.LightBlock(ctx, traceBlock.Height)
  171. if err != nil {
  172. return nil, nil, err
  173. }
  174. if idx == 0 {
  175. if shash, thash := sourceBlock.Hash(), traceBlock.Hash(); !bytes.Equal(shash, thash) {
  176. return nil, nil, fmt.Errorf("trusted block is different to the source's first block (%X = %X)",
  177. thash, shash)
  178. }
  179. previouslyVerifiedBlock = sourceBlock
  180. continue
  181. }
  182. // we check that the source provider can verify a block at the same height of the
  183. // intermediate height
  184. trace, err := c.verifySkipping(ctx, source, previouslyVerifiedBlock, sourceBlock, now)
  185. if err != nil {
  186. return nil, nil, fmt.Errorf("verifySkipping of conflicting header failed: %w", err)
  187. }
  188. // check if the headers verified by the source has diverged from the trace
  189. if shash, thash := sourceBlock.Hash(), traceBlock.Hash(); !bytes.Equal(shash, thash) {
  190. // Bifurcation point found!
  191. return trace, traceBlock, nil
  192. }
  193. // headers are still the same. update the previouslyVerifiedBlock
  194. previouslyVerifiedBlock = sourceBlock
  195. }
  196. // We have reached the end of the trace without observing a divergence. The last header is thus different
  197. // from the divergent header that the source originally sent us, then we return an error.
  198. return nil, nil, fmt.Errorf("source provided different header to the original header it provided (%X != %X)",
  199. previouslyVerifiedBlock.Hash(), divergentHeader.Hash())
  200. }
  201. // newLightClientAttackEvidence determines the type of attack and then forms the evidence filling out
  202. // all the fields such that it is ready to be sent to a full node.
  203. func newLightClientAttackEvidence(conflicted, trusted, common *types.LightBlock) *types.LightClientAttackEvidence {
  204. ev := &types.LightClientAttackEvidence{ConflictingBlock: conflicted}
  205. // if this is an equivocation or amnesia attack, i.e. the validator sets are the same, then we
  206. // return the height of the conflicting block else if it is a lunatic attack and the validator sets
  207. // are not the same then we send the height of the common header.
  208. if ev.ConflictingHeaderIsInvalid(trusted.Header) {
  209. ev.CommonHeight = common.Height
  210. ev.Timestamp = common.Time
  211. ev.TotalVotingPower = common.ValidatorSet.TotalVotingPower()
  212. } else {
  213. ev.CommonHeight = trusted.Height
  214. ev.Timestamp = trusted.Time
  215. ev.TotalVotingPower = trusted.ValidatorSet.TotalVotingPower()
  216. }
  217. ev.ByzantineValidators = ev.GetByzantineValidators(common.ValidatorSet, trusted.SignedHeader)
  218. return ev
  219. }