Skip to content

Commit f3d4ce0

Browse files
committed
core/state, ethdb, trie: test intermediate secure key leak, fix memdb bug
1 parent 886478b commit f3d4ce0

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎core/state/statedb_test.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,71 @@ func TestUpdateLeaks(t *testing.T) {
5050
t.Errorf("State leaked into database: %x -> %x", key, value)
5151
}
5252
}
53+
54+
// Tests that no intermediate state of an object is stored into the database,
55+
// only the one right before the commit.
56+
func TestIntermediateLeaks(t *testing.T) {
57+
// Create two state databases, one transitioning to the final state, the other final from the beginning
58+
transDb, _ := ethdb.NewMemDatabase()
59+
finalDb, _ := ethdb.NewMemDatabase()
60+
transState, _ := New(common.Hash{}, transDb)
61+
finalState, _ := New(common.Hash{}, finalDb)
62+
63+
// Update the states with some objects
64+
for i := byte(0); i < 255; i++ {
65+
// Create a new state object with some data into the transition database
66+
obj := transState.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
67+
obj.SetBalance(big.NewInt(int64(11 * i)))
68+
obj.SetNonce(uint64(42 * i))
69+
if i%2 == 0 {
70+
obj.SetState(common.BytesToHash([]byte{i, i, i, 0}), common.BytesToHash([]byte{i, i, i, i, 0}))
71+
}
72+
if i%3 == 0 {
73+
obj.SetCode([]byte{i, i, i, i, i, 0})
74+
}
75+
transState.UpdateStateObject(obj)
76+
77+
// Overwrite all the data with new values in the transition database
78+
obj.SetBalance(big.NewInt(int64(11*i + 1)))
79+
obj.SetNonce(uint64(42*i + 1))
80+
if i%2 == 0 {
81+
obj.SetState(common.BytesToHash([]byte{i, i, i, 0}), common.Hash{})
82+
obj.SetState(common.BytesToHash([]byte{i, i, i, 1}), common.BytesToHash([]byte{i, i, i, i, 1}))
83+
}
84+
if i%3 == 0 {
85+
obj.SetCode([]byte{i, i, i, i, i, 1})
86+
}
87+
transState.UpdateStateObject(obj)
88+
89+
// Create the final state object directly in the final database
90+
obj = finalState.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
91+
obj.SetBalance(big.NewInt(int64(11*i + 1)))
92+
obj.SetNonce(uint64(42*i + 1))
93+
if i%2 == 0 {
94+
obj.SetState(common.BytesToHash([]byte{i, i, i, 1}), common.BytesToHash([]byte{i, i, i, i, 1}))
95+
}
96+
if i%3 == 0 {
97+
obj.SetCode([]byte{i, i, i, i, i, 1})
98+
}
99+
finalState.UpdateStateObject(obj)
100+
}
101+
if _, err := transState.Commit(); err != nil {
102+
t.Fatalf("failed to commit transition state: %v", err)
103+
}
104+
if _, err := finalState.Commit(); err != nil {
105+
t.Fatalf("failed to commit final state: %v", err)
106+
}
107+
// Cross check the databases to ensure they are the same
108+
for _, key := range finalDb.Keys() {
109+
if _, err := transDb.Get(key); err != nil {
110+
val, _ := finalDb.Get(key)
111+
t.Errorf("entry missing from the transition database: %x -> %x", key, val)
112+
}
113+
}
114+
for _, key := range transDb.Keys() {
115+
if _, err := finalDb.Get(key); err != nil {
116+
val, _ := transDb.Get(key)
117+
t.Errorf("extra entry in the transition database: %x -> %x", key, val)
118+
}
119+
}
120+
}

‎ethdb/memory_database.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (b *memBatch) Put(key, value []byte) error {
107107
b.lock.Lock()
108108
defer b.lock.Unlock()
109109

110-
b.writes = append(b.writes, kv{key, common.CopyBytes(value)})
110+
b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)})
111111
return nil
112112
}
113113

‎trie/secure_trie.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (t *SecureTrie) TryUpdate(key, value []byte) error {
109109
if err != nil {
110110
return err
111111
}
112-
t.secKeyCache[string(hk)] = key
112+
t.secKeyCache[string(hk)] = common.CopyBytes(key)
113113
return nil
114114
}
115115

@@ -123,7 +123,9 @@ func (t *SecureTrie) Delete(key []byte) {
123123
// TryDelete removes any existing value for key from the trie.
124124
// If a node was not found in the database, a MissingNodeError is returned.
125125
func (t *SecureTrie) TryDelete(key []byte) error {
126-
return t.Trie.TryDelete(t.hashKey(key))
126+
hk := t.hashKey(key)
127+
delete(t.secKeyCache, string(hk))
128+
return t.Trie.TryDelete(hk)
127129
}
128130

129131
// GetKey returns the sha3 preimage of a hashed key that was

0 commit comments

Comments
 (0)