Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adv.(Packet).Field method stuck in an endless loop, if a specific bytes sequence is passed #114

Open
MetalRex101 opened this issue Nov 6, 2024 · 1 comment

Comments

@MetalRex101
Copy link

MetalRex101 commented Nov 6, 2024

If I execute the code, it stuck in an endless loop.

func TestNewPacket(t *testing.T) {
	p := adv.NewRawPacket([]byte{0xff, 0x7f, 0xff, 0xff})
	p.ManufacturerData() // <- stuck here
}

Right now this happens, because

func (p *Packet) Field(typ byte) []byte {
	b := p.b
	for len(b) > 0 {
		if len(b) < 2 {
			return nil
		}
		l, t := b[0], b[1]
		if int(l) < 1 || len(b) < int(1+l) { // if l is 255, then uint8 255 + 1 will result in 0
			return nil
		}
		if t == typ {
			return b[2 : 2+l-1]
		}
		b = b[1+l:] // the same goes here. Since the sum is 0, it will take the whole package again
	}
	return nil
}
@rtoma
Copy link

rtoma commented Mar 15, 2025

Observing the same while parsing ble advertisements out in the wild.

The Field function is clearly missing an input sanity check, considering max length of p.b is 31 bytes, see

const MaxEIRPacketLength = 31

which is used in:

ble/linux/adv/packet.go

Lines 37 to 43 in 8c5522f

func NewRawPacket(bytes ...[]byte) *Packet {
p := &Packet{b: make([]byte, 0, MaxEIRPacketLength)}
for _, b := range bytes {
p.b = append(p.b, b...)
}
return p
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants