Skip to content

Commit

Permalink
sysenc: refuse to unmarshal into undersized dst
Browse files Browse the repository at this point in the history
Unmarshal currently doesn't check that unmarshaling actually
consumes to full buffer. This means that looking uint16 from an
uint32 value doesn't return an error.

Fixes #1157

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Oct 20, 2023
1 parent 0d47e51 commit 741a2e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/sysenc/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ func Unmarshal(data interface{}, buf []byte) error {

rd.Reset(buf)

return binary.Read(rd, internal.NativeEndian, value)
if err := binary.Read(rd, internal.NativeEndian, value); err != nil {
return err
}

if rd.Len() != 0 {
return fmt.Errorf("unmarshaling %T doesn't consume all data", data)
}

return nil
}
}

Expand Down
8 changes: 8 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ func TestBatchAPIHash(t *testing.T) {
}
}

func TestMapLookupKeyTooSmall(t *testing.T) {
m := createArray(t)

var small uint16
qt.Assert(t, m.Put(uint32(0), uint32(1234)), qt.IsNil)
qt.Assert(t, m.Lookup(uint32(0), &small), qt.IsNotNil)
}

func TestBatchAPIMapDelete(t *testing.T) {
if err := haveBatchAPI(); err != nil {
t.Skipf("batch api not available: %v", err)
Expand Down

0 comments on commit 741a2e9

Please sign in to comment.