Skip to content

Commit b287487

Browse files
committed
fix(encoding): ensure input is well-formed before allocation
Perform dm.Wellformed() check on the input at the beginning of structFieldsCBOR.FromCBOR(). This prevents, among other things, random byte sequences from being interpreted as map headers with a large size and then waiting on the allocation. Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
1 parent 53b3014 commit b287487

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

corim/signedcorim_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func TestSignedCorim_FromCOSE_fail_corim_bad_cbor(t *testing.T) {
256256
var actual SignedCorim
257257
err := actual.FromCOSE(tv)
258258

259-
assert.EqualError(t, err, "failed CBOR decoding of unsigned CoRIM: unexpected EOF")
259+
assert.EqualError(t, err, "failed CBOR decoding of unsigned CoRIM: malformed CBOR: unexpected EOF")
260260
}
261261

262262
func TestSignedCorim_FromCOSE_fail_invalid_corim(t *testing.T) {

encoding/cbor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ func (o *structFieldsCBOR) ToCBOR(em cbor.EncMode) ([]byte, error) {
301301
}
302302

303303
func (o *structFieldsCBOR) FromCBOR(dm cbor.DecMode, data []byte) error {
304-
if len(data) == 0 {
305-
return errors.New("empty input")
304+
if err := dm.Wellformed(data); err != nil {
305+
return fmt.Errorf("malformed CBOR: %w", err)
306306
}
307307

308308
header := data[0]

encoding/cbor_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,30 @@ func Test_structFieldsCBOR_CBOR_decode_indefinite(t *testing.T) {
279279
assert.Equal(t, []int{0, 1, 2, 3, 4}, sfOut.Keys)
280280
}
281281

282+
func Test_structFieldsCBOR_CBOR_decode_malformed(t *testing.T) {
283+
data := []byte{
284+
0xcb, 0xba, 0x5a, 0x47, 0x42, 0xe0, 0xa5, 0x4e,
285+
}
286+
287+
dm, err := cbor.DecOptions{}.DecMode()
288+
require.NoError(t, err)
289+
290+
sfOut := newStructFieldsCBOR()
291+
err = sfOut.FromCBOR(dm, data)
292+
assert.ErrorContains(t, err, "malformed CBOR")
293+
}
294+
282295
func Test_structFieldsCBOR_CBOR_decode_negative(t *testing.T) {
283296
dm, err := cbor.DecOptions{}.DecMode()
284297
require.NoError(t, err)
285298

286299
sfOut := newStructFieldsCBOR()
287300
err = sfOut.FromCBOR(dm, []byte{0xa1, 0xff, 0x00})
288-
assert.EqualError(t, err, `map item 0: could not unmarshal key: cbor: unexpected "break" code`)
301+
assert.EqualError(t, err, `malformed CBOR: cbor: unexpected "break" code`)
289302
err = sfOut.FromCBOR(dm, []byte{0xbf, 0x00, 0x00})
290-
assert.EqualError(t, err, `unexpected EOF`)
303+
assert.EqualError(t, err, `malformed CBOR: unexpected EOF`)
291304
err = sfOut.FromCBOR(dm, []byte{0xa1, 0x00, 0xff})
292-
assert.EqualError(t, err, `map item 0: could not unmarshal value: cbor: unexpected "break" code`)
305+
assert.EqualError(t, err, `malformed CBOR: cbor: unexpected "break" code`)
293306

294307
err = sfOut.FromCBOR(dm, []byte{0x00})
295308
assert.EqualError(t, err, `expected map (CBOR Major Type 5), found Major Type 0`)

0 commit comments

Comments
 (0)