Skip to main content

Out-of-bounds read

Need

Prevention of out-of-bounds read vulnerabilities

Context

  • Usage of Scala for building scalable and high-performance applications
  • Usage of Scala Library for building scalable and high-performance applications

Description

Non compliant code

def readData(buffer: Array[Byte], index: Int): Byte = {
return buffer(index)
}

// Usage
val buffer = Array[Byte](1, 2, 3, 4, 5)
val data = readData(buffer, 10)

In the above code snippet, the function readData takes in a byte array buffer and an integer index as parameters. It then tries to return the byte at the position index in the buffer.

In the usage example, we have a buffer of size 5 and we are trying to read the data at position 10. This is an example of an out-of-bounds read vulnerability because the buffer array only has indices from 0 to 4 (inclusive).

Attempting to read data at position 10, which is beyond the end of the buffer, can cause unpredictable behavior. The system could potentially read and return data that is stored in memory after the buffer, which might be sensitive information or it could cause the application to crash.

This is a critical issue that can lead to information leakage or denial of service and needs to be fixed.

Steps

  • Validate the index parameter to ensure it is within the bounds of the buffer.
  • Handle out-of-bounds scenarios gracefully, such as returning a default value or throwing an exception.
  • Consider using safer alternatives like Option types or Try/Catch blocks to handle potential errors.
  • Perform thorough testing to ensure the fix is effective and does not introduce new vulnerabilities.

Compliant code

def readData(buffer: Array[Byte], index: Int): Option[Byte] = {
if (index >= 0 && index < buffer.length) {
Some(buffer(index))
} else {
None
}
}

// Usage
val buffer = Array[Byte](1, 2, 3, 4, 5)
val data = readData(buffer, 10)

data match {
case Some(value) => println(s"Data: $value")
case None => println("Index out of bounds")
}

The function readData has been modified to return an Option[Byte] instead of a Byte. This allows us to handle the case where the index is out of bounds in a safe way.

The if condition inside the function checks if the index is within the bounds of the buffer. If it is, the function returns Some(buffer(index)), which means that the function was able to successfully read a byte from the buffer at the given index.

If the index is out of bounds, the function returns None, which represents the absence of a value. This is a safe way to indicate that an error occurred, as it prevents the function from trying to read data from an invalid location in the buffer.

In the usage example, we use a match expression to handle the Option[Byte] returned by readData. If readData was able to read a byte from the buffer, it prints the byte. If readData returned None, it prints a message indicating that the index was out of bounds.

This approach ensures that the index is always within the bounds of the buffer, preventing out-of-bounds reads. It also provides a clear way to handle errors, making the code safer and easier to understand.

References