|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CWE-456: Missing Initialization of a Variable
Description Summary The software does not initialize critical variables, which causes the execution environment to use unexpected values.
Example 1 Here, an uninitialized field in a Java class is used in a seldom-called method, which would cause a NullPointerException to be thrown. (Bad Code) Example
Language: Java private User user; public void someMethod() { // Do something interesting.
...
// Throws NPE if user hasn't been properly
initialized.
String username = user.getName();
} Example 2 This code first authenticates a user, then allows a delete command if the user is an administrator. (Bad Code) Example
Language: PHP if (authenticate($username,$password) &&
setAdmin($username)){ $isAdmin = true;
} /.../ if ($isAdmin){ deleteUser($userToDelete);
} The $isAdmin variable is set to true if the user is an admin, but is uninitialized otherwise. If PHP's register_globals feature is enabled, an attacker can set uninitialized variables like $isAdmin to arbitrary values, in this case gaining administrator privileges by setting $isAdmin to true. Example 3 In the following Java code the BankManager class uses the user variable of the class User to allow authorized users to perform bank manager tasks. The user variable is initialized within the method setUser that retrieves the User from the User database. The user is then authenticated as unauthorized user through the method authenticateUser. (Bad Code) Example
Language: Java public class BankManager { // user allowed to perform bank manager tasks
private User user = null;
private boolean isUserAuthentic = false;
// constructor for BankManager class
public BankManager() {
...
}
// retrieve user from database of users
public User getUserFromUserDatabase(String username){
...
}
// set user variable using username
public void setUser(String username) {
this.user = getUserFromUserDatabase(username);
}
// authenticate user
public boolean authenticateUser(String username, String
password) {
if (username.equals(user.getUsername()) &&
password.equals(user.getPassword())) {
isUserAuthentic = true;
}
return isUserAuthentic;
}
// methods for performing bank manager tasks
...
} However, if the method setUser is not called before authenticateUser then the user variable will not have been initialized and will result in a NullPointerException. The code should verify that the user variable has been initialized before it is used, as in the following code. (Good Code) Example
Language: Java public class BankManager { // user allowed to perform bank manager tasks
private User user = null;
private boolean isUserAuthentic = false;
// constructor for BankManager class
public BankManager(String username) {
user = getUserFromUserDatabase(username);
}
// retrieve user from database of users
public User getUserFromUserDatabase(String username)
{...}
// authenticate user
public boolean authenticateUser(String username, String
password) {
if (user == null) {
System.out.println("Cannot find user " +
username);
}
else {
if (password.equals(user.getPassword())) {
isUserAuthentic = true;
}
}
return isUserAuthentic;
}
// methods for performing bank manager tasks
...
}
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Page Last Updated:
February 20, 2013
|
|
CWE is co-sponsored by the office of Cybersecurity and Communications at the U.S. Department of Homeland Security. This Web site is sponsored and managed by The MITRE Corporation to enable stakeholder collaboration. Copyright © 2006-2013, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation. Contact cwe@mitre.org for more information. |
|||



