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

Forbid negative uvarint values #226

Merged
merged 4 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/main/kotlin/io/libp2p/etc/types/ByteArrayExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,20 @@ fun ByteArray.readUvarint(): Pair<Long, ByteArray>? {

var index = 0
var result: Long? = null
for (i in 0..9) {
for (i in 0..8) {
val b = this.get(index++).toUByte().toShort()
if (b < 0x80) {
if (i == 9 && b > 1) {
return null
}
result = x or (b.toLong() shl s)
break
}
x = x or (b.toLong() and 0x7f shl s)
s += 7
}

if (result != null && result <= size) {
// Check that we didn't break early.
if (result != null && index <= size) {
return Pair(result, slice(IntRange(index, size - 1)).toByteArray())
}

return null
throw IllegalStateException("uvarint too long")
}
9 changes: 5 additions & 4 deletions src/main/kotlin/io/libp2p/etc/types/ByteBufExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import java.lang.Integer.min
fun ByteBuf.writeUvarint(value: Int): ByteBuf = writeUvarint(value.toLong())

fun ByteBuf.writeUvarint(value: Long): ByteBuf {
if (value < 0) {
throw IllegalArgumentException("uvarint value must be positive")
}

var v = value
while (v >= 0x80) {
this.writeByte((v or 0x80).toInt())
Expand All @@ -29,17 +33,14 @@ fun ByteBuf.readUvarint(): Long {
var s = 0

val originalReaderIndex = readerIndex()
for (i in 0..9) {
for (i in 0..8) {
if (!this.isReadable) {
// buffer contains just a fragment of uint
readerIndex(originalReaderIndex)
return -1
}
val b = this.readUnsignedByte()
if (b < 0x80) {
if (i == 9 && b > 1) {
throw IllegalStateException("Overflow reading uvarint")
}
return x or (b.toLong() shl s)
}
x = x or (b.toLong() and 0x7f shl s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ class MultiaddrTest {
fun invalidSerializations() = listOf(
// Invalid var lengths
Arguments.of(Protocol.DNS4.encoded + 1L.toVarInt()),
Arguments.of(Protocol.DNS4.encoded + (-1L).toVarInt()),
Arguments.of(Protocol.DNS4.encoded + 10L.toVarInt() + ByteArray(9, { 0 })),
Arguments.of(Protocol.DNS4.encoded + 65535L.toVarInt() + ByteArray(9, { 0 })),
Arguments.of(Protocol.DNS4.encoded + Int.MAX_VALUE.toLong().toVarInt() + ByteArray(9, { 0 })),
Expand Down
37 changes: 34 additions & 3 deletions src/test/kotlin/io/libp2p/etc/types/UvarintTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,42 @@ class UvarintTest {
}

@Test
fun testDecodeInvalid() {
val buf = Unpooled.buffer()
buf.writeBytes(ByteArray(11) { 0x81.toByte() })
fun testDecodeTooManyBytes() {
// Helper function for creating uvarint buffer.
fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) {
pos ->
ints[pos].toByte()
}

// Try to decode a uvarint with 10 bytes.
val arr: ByteArray = byteArrayOfInts(
Integer.parseInt("10000000", 2), // 1
Integer.parseInt("10000000", 2), // 2
Integer.parseInt("10000000", 2), // 3
Integer.parseInt("10000000", 2), // 4
Integer.parseInt("10000000", 2), // 5
Integer.parseInt("10000000", 2), // 6
Integer.parseInt("10000000", 2), // 7
Integer.parseInt("10000000", 2), // 8
Integer.parseInt("10000000", 2), // 9
Integer.parseInt("00000001", 2) // 10
)
val buf = Unpooled.copiedBuffer(arr)
val exception = assertThrows<IllegalStateException> { buf.readUvarint() }
assertEquals("uvarint too long", exception.message)
}

@Test
fun testRoundTripMaximumValue() {
val buf = Unpooled.buffer()
buf.writeUvarint(Long.MAX_VALUE)
assertEquals(Long.MAX_VALUE, buf.readUvarint())
}

@Test
fun testEncodeNegativeValue() {
val buf = Unpooled.buffer()
val exception = assertThrows<IllegalArgumentException> { buf.writeUvarint(-1) }
assertEquals("uvarint value must be positive", exception.message)
}
}