Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
d3f056bd
Commit
d3f056bd
authored
Sep 21, 2018
by
Ferenc Szabo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
swarm/network/stream: fix DoS invalid hash length (#927)
parent
81080bf8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
9 deletions
+82
-9
messages.go
swarm/network/stream/messages.go
+10
-4
streamer_test.go
swarm/network/stream/streamer_test.go
+72
-5
No files found.
swarm/network/stream/messages.go
View file @
d3f056bd
...
...
@@ -26,7 +26,7 @@ import (
bv
"github.com/ethereum/go-ethereum/swarm/network/bitvector"
"github.com/ethereum/go-ethereum/swarm/spancontext"
"github.com/ethereum/go-ethereum/swarm/storage"
opentracing
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go"
)
var
syncBatchTimeout
=
30
*
time
.
Second
...
...
@@ -195,10 +195,16 @@ func (p *Peer) handleOfferedHashesMsg(ctx context.Context, req *OfferedHashesMsg
if
err
!=
nil
{
return
err
}
hashes
:=
req
.
Hashes
want
,
err
:=
bv
.
New
(
len
(
hashes
)
/
HashSize
)
lenHashes
:=
len
(
hashes
)
if
lenHashes
%
HashSize
!=
0
{
return
fmt
.
Errorf
(
"error invalid hashes length (len: %v)"
,
lenHashes
)
}
want
,
err
:=
bv
.
New
(
lenHashes
/
HashSize
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"error initiaising bitvector of length %v: %v"
,
len
(
hashes
)
/
HashSize
,
err
)
return
fmt
.
Errorf
(
"error initiaising bitvector of length %v: %v"
,
len
Hashes
/
HashSize
,
err
)
}
ctr
:=
0
...
...
@@ -206,7 +212,7 @@ func (p *Peer) handleOfferedHashesMsg(ctx context.Context, req *OfferedHashesMsg
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
syncBatchTimeout
)
ctx
=
context
.
WithValue
(
ctx
,
"source"
,
p
.
ID
()
.
String
())
for
i
:=
0
;
i
<
len
(
hashes
)
;
i
+=
HashSize
{
for
i
:=
0
;
i
<
len
Hashes
;
i
+=
HashSize
{
hash
:=
hashes
[
i
:
i
+
HashSize
]
if
wait
:=
c
.
NeedData
(
ctx
,
hash
);
wait
!=
nil
{
...
...
swarm/network/stream/streamer_test.go
View file @
d3f056bd
...
...
@@ -19,6 +19,7 @@ package stream
import
(
"bytes"
"context"
"errors"
"testing"
"time"
...
...
@@ -55,11 +56,12 @@ func TestStreamerRequestSubscription(t *testing.T) {
}
var
(
hash0
=
sha3
.
Sum256
([]
byte
{
0
})
hash1
=
sha3
.
Sum256
([]
byte
{
1
})
hash2
=
sha3
.
Sum256
([]
byte
{
2
})
hashesTmp
=
append
(
hash0
[
:
],
hash1
[
:
]
...
)
hashes
=
append
(
hashesTmp
,
hash2
[
:
]
...
)
hash0
=
sha3
.
Sum256
([]
byte
{
0
})
hash1
=
sha3
.
Sum256
([]
byte
{
1
})
hash2
=
sha3
.
Sum256
([]
byte
{
2
})
hashesTmp
=
append
(
hash0
[
:
],
hash1
[
:
]
...
)
hashes
=
append
(
hashesTmp
,
hash2
[
:
]
...
)
corruptHashes
=
append
(
hashes
[
:
40
])
)
type
testClient
struct
{
...
...
@@ -459,6 +461,71 @@ func TestStreamerUpstreamSubscribeLiveAndHistory(t *testing.T) {
}
}
func
TestStreamerDownstreamCorruptHashesMsgExchange
(
t
*
testing
.
T
)
{
tester
,
streamer
,
_
,
teardown
,
err
:=
newStreamerTester
(
t
)
defer
teardown
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
stream
:=
NewStream
(
"foo"
,
""
,
true
)
var
tc
*
testClient
streamer
.
RegisterClientFunc
(
"foo"
,
func
(
p
*
Peer
,
t
string
,
live
bool
)
(
Client
,
error
)
{
tc
=
newTestClient
(
t
)
return
tc
,
nil
})
peerID
:=
tester
.
IDs
[
0
]
err
=
streamer
.
Subscribe
(
peerID
,
stream
,
NewRange
(
5
,
8
),
Top
)
if
err
!=
nil
{
t
.
Fatalf
(
"Expected no error, got %v"
,
err
)
}
err
=
tester
.
TestExchanges
(
p2ptest
.
Exchange
{
Label
:
"Subscribe message"
,
Expects
:
[]
p2ptest
.
Expect
{
{
Code
:
4
,
Msg
:
&
SubscribeMsg
{
Stream
:
stream
,
History
:
NewRange
(
5
,
8
),
Priority
:
Top
,
},
Peer
:
peerID
,
},
},
},
p2ptest
.
Exchange
{
Label
:
"Corrupt offered hash message"
,
Triggers
:
[]
p2ptest
.
Trigger
{
{
Code
:
1
,
Msg
:
&
OfferedHashesMsg
{
HandoverProof
:
&
HandoverProof
{
Handover
:
&
Handover
{},
},
Hashes
:
corruptHashes
,
From
:
5
,
To
:
8
,
Stream
:
stream
,
},
Peer
:
peerID
,
},
},
})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
expectedError
:=
errors
.
New
(
"Message handler error: (msg code 1): error invalid hashes length (len: 40)"
)
if
err
:=
tester
.
TestDisconnected
(
&
p2ptest
.
Disconnect
{
Peer
:
tester
.
IDs
[
0
],
Error
:
expectedError
});
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
func
TestStreamerDownstreamOfferedHashesMsgExchange
(
t
*
testing
.
T
)
{
tester
,
streamer
,
_
,
teardown
,
err
:=
newStreamerTester
(
t
)
defer
teardown
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment