The CompTIA Security+ exam includes many concepts related to secure coding techniques. If you’re planning to take the SY0-401 or the SY0-501 Security+ exam, you should have a basic understanding of techniques such as proper error handling, input validation, and more.
As an example, can you answer this sample SY0-501 practice question?
Q. You are reviewing some Java code for an application and come across the following snippet:
public class donuts {
public static void main (String[] args) {
object stuffed = null;
stuffed.heat ();
…
}
}
You suspect that this will cause a problem. Which of the following BEST describes the problem?
A. NullPointerException
B. Invalid null assignment
C. Pointer dereference
D. Buffer overflow
More, do you know why the correct answer is correct and the incorrect answers are incorrect? The answer and explanation is available at the end of this post.
Deeper Than I’d Expect
Admittedly, this is much deeper than I would expect for a Security+ exam. A reader queried me asking how Java handled the use of null objects, and if accessing a null object could cause a dereference error.
My first thought was that this wasn’t relevant to the Security+ exam. However, I remembered that the objectives specifically mentioned pointer dereference as one of the issues that could cause a memory/buffer vulnerability.
I decided to dig in.
Simplest Explanation
Here is the simplest explanation:
- This is a pointer dereference issue, or more specifically, a null-pointer dereference issue.
- The object stuffed = null line creates an object (called stuffed) and assigns it a value of null (nothing).
- The stuffed.heat (); line attempts to execute a method within a non-existent (null) object and causes a null-pointer dereference error (or exception).
- If code attempts to use an object with a value of null, it assumes the object reference points to a valid memory error, but it doesn’t.
Programming languages allow you to assign null to objects when creating them so the code doesn’t describe an invalid null assignment.
While this will throw a NullPointerException error, the error isn’t the problem. The code that causes the pointer dereference error is the problem.
- If the code is configured to handle exceptions (errors) gracefully, it doesn’t cause a significant vulnerability.
- However, if the code doesn’t handle the exception gracefully, an attacker may be able to exploit the exception.
Note: The difference between the code causing a null-pointer dereference error, and the NullPointerException error that it causes is subtle. Think of it this way. Bart is driving down the road at 100 MPH on his new Harley Davidson motorcycle. Chief Wiggum witnesses this and gives him a ticket. While Bart may think that the ticket is the problem, the actual cause is him speeding at 100 MPH.
This exception doesn’t necessarily cause a buffer overflow vulnerability. However, depending on how the application handles the exception (or doesn’t handle the exception), an attacker may be able to use exception to cause a buffer overflow.
Deep Dive
If you are a Java developer, the syntax of the second line is probably familiar to you. It is a standard line to identify the Main method, or the entry point for the application.
public static void main (String[] args)
If you aren’t a Java developer and this is the first time you saw it, you’d just have to guess about the programming language.
- The code is creating a class called donuts.
public class donuts {
A class provides a definition for an object, but it doesn’t create an object.
For comparison, a home builder has building plans to create a home. These plans aren’t a home. However, the builder can create multiple homes using these plans.
Similarly, a class provides the building plans (or the definition) of an object. The class isn’t an object. However, an application can create multiple instances of the object (donuts in this example) using the definition provided by the class. - The following line creates an instance of a stuffed donut and assigns a value of null (nothing) to the object.
object stuffed = null;
A value of null indicates that the object doesn’t have any identification.
An object is an instance of a class. It typically has properties that describe it and methods that identify actions for the object.
In this example, “stuffed” indicates it is a stuffed donut.- Properties may be jelly-filled, chocolate-filled, no-stuffing, and so on.
- A simple method may be “heat” (to make it hot).
- The next line is the error.
stuffed.heat ();
It tries to call the heat method of a null object. In Java, it throws the NullPointerException error.
You might like to read the OWASP Null Dereference article.
See Chapter 8 of the CompTIA Security+: Get Certified Get Ahead: SY0-501 Study Guide for more on Secure Coding Techniques.