SV.STRBUF.CLEAN
SV.STRBUF.CLEAN occurs when potentially secure data is not cleared from a StringBuffer.
Vulnerability and risk
Relying on the garbage collector to clear data is not safe. An attacker can access data that has not been released.
Klocwork security vulnerability (SV) checkers identify calls that create potentially dangerous data; these calls are considered unsafe sources. An unsafe source can be any data provided by the user, since the user could be an attacker or has the potential for introducing human error.
Mitigation and prevention
Do not use mutable objects to store sensitive data. If you must use mutable objects for sensitive data, NULL the object immediately after use.
Example 1
private void processingData(String user) throws SQLException {
String credential = getCredential(user);
// ...
}
private String getCredential(String user) throws SQLException {
Statement statement = con.createStatement();
try {
StringBuffer buffer = new StringBuffer();
ResultSet resultSet = statement.executeQuery("SELECT * FROM credentials WHERE name='" + user + '\'');
while (resultSet.next()) {
String key = resultSet.getString("key");
buffer.append(key);
}
final String result = buffer.toString();
return result;
} finally {
statement.close();
}
}
SV.STRBUF.CLEAN is reported for line 24: String buffer 'buffer' not cleaned before garbage collection.
Example 2
private void processingData(String user) throws SQLException {
String credential = getCredential(user);
// ...
}
private String getCredential(String user) throws SQLException {
Statement statement = con.createStatement();
StringBuffer buffer = new StringBuffer();
try {
ResultSet resultSet = statement.executeQuery("SELECT * FROM credentials WHERE name='" + user + '\'');
while (resultSet.next()) {
String key = resultSet.getString("key");
buffer.append(key);
}
final String result = buffer.toString();
return result;
} finally {
buffer.delete(0, buffer.length());
statement.close();
}
}
The snippet from the previous section is fixed; SV.STRBUF.CLEAN is not reported here.
Extension
This checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information.