520
is returned. In essence, this makes it easier for an attacker to obtain half of the necessary
authentication credentials.
While this type of information may be helpful to a user, it is also useful to a potential attacker. In the
above example, the message for both failed cases should be the same, such as:
Example Language: (Result)
"Login Failed - incorrect username or password"
Example 2:
In this example, the attacker observes how long an authentication takes when the user types in the
correct password.
When the attacker tries their own values, they can first try strings of various length. When they
find a string of the right length, the computation will take a bit longer, because the for loop will
run at least once. Additionally, with this code, the attacker can possibly learn one character of the
password at a time, because when they guess the first character right, the computation will take
longer than a wrong guesses. Such an attack can break even the most sophisticated password with
a few hundred guesses.
Example Language: Python (Bad)
def validate_password(actual_pw, typed_pw):
if len(actual_pw) <> len(typed_pw):
return 0
for i in len(actual_pw):
if actual_pw[i] <> typed_pw[i]:
return 0
return 1
Note that in this example, the actual password must be handled in constant time as far as the
attacker is concerned, even if the actual password is of an unusual length. This is one reason why
it is good to use an algorithm that, among other things, stores a seeded cryptographic one-way
hash of the password, then compare the hashes, which will always be of the same length.
Example 3:
Non-uniform processing time causes timing channel.
Example Language: (Bad)
Suppose an algorithm for implementing an encryption routine works fine per se, but the time taken to output the result of the
encryption routine depends on a relationship between the input plaintext and the key (e.g., suppose, if the plaintext is similar
to the key, it would run very fast).
In the example above, an attacker may vary the inputs, then observe differences between
processing times (since different plaintexts take different time). This could be used to infer
information about the key.
Example Language: (Good)
Artificial delays may be added to ensured all calculations take equal time to execute.
Example 4:
Suppose memory access patterns for an encryption routine are dependent on the secret key.
An attacker can recover the key by knowing if specific memory locations have been accessed or
not. The value stored at those memory locations is irrelevant. The encryption routine's memory
accesses will affect the state of the processor cache. If cache resources are shared across
contexts, after the encryption routine completes, an attacker in different execution context can
CWE Version 4.14
CWE-203: Observable Discrepancy
CWE-203: Observable Discrepancy