Type narrowing security leak

Raffaello Giulietti raffaello.giulietti at gmail.com
Sat Jan 1 19:45:34 UTC 2022


Hi,

if I understand correctly, you are supposing that method changeAmount() 
is part of a security sensitive application under attack.

Its implementation first attempts a data validation by invoking 
isUserIdAllowedOrThrowException(), then transforms validated data in a 
lossy way (narrowing, then widening) for further usage (invoking 
doChangeAmount()).
A seriously conducted review of security related code, whether by 
experienced engineers or by means of tools, would reject this code as 
fatally flawed in the first place, code that should never become productive.

(BTW, (1L << Integer.SIZE) + 63 is a more efficient way to produce e 
rebound of 63 for your example.)

Yours is just one example of the more general class of integer overflow 
attacks that potentially affects modular integer arithmetic in Java and 
many other languages. To prevent such attacks, operations on integer 
values should throw on overflow. Modifying the Java spec to mandate 
throwing on overflow would be a substantial change with major 
compatibility issues and preventing many legitimate usages of modular 
arithmetic as it is currently specified.

Security sensitive code can (and does) make good use of 
j.l.Math.*Exact() methods (addExact(), toIntExact(), etc.)


Greetings
Raffaello


On 2021-12-29 07:43, Fabrice Tiercelin wrote:
> Greetings,
> Any Java application may be concerned by a hacker attack using a type narrowing leak. If a program does the following things in this order:
>   - Assert that a numerical id is allowed - Do a type narrowing among other things, even followed by a type widening - Do an action with the numerical id
> ...the hacker can do forbidden actions. Let's say that a given user doesn't have rights to change an amount for the id 63:
> public void changeAmount(long userId, double newAmount) throws IllegalArgumentException {  isUserIdAllowedOrThrowException(userId); // userId = 4294967359
> 
>     int theUserId = (int) userId; // theUserId = 63    userId = theUserId; // userId = 63
>    doChangeAmount(userId, newAmount); // userId = 63
> }
> It will fail passing 63 but it will success passing 4294967359 because 4_294_967_359 is narrowed into 63. Let's call 4_294_967_359  a rebound of 63. 4294967359 can be retrieved in few seconds by a basic program like this:
> public class MyClass {
>      public static void main(String args[]) {
>        long targettedNumber = 63;
>        
>         for (long rebound = Integer.MAX_VALUE + 1; true; rebound++) {          int typeNarrowing = (int) rebound;
>            long typeWidening = typeNarrowing;
>            
>            if (typeWidening == targettedNumber) {
>                System.out.println("Rebound for " + targettedNumber + " found: " + rebound);
>                return;
>            }
>        }
>      }
> }
> And it can be optimized. It works for any type narrowing. It not only works for numerical id but also for flags. If a numerical value should contain or not several flags, you can search a rebound among billions of rebounds until you find one with the perfect features. All the Java versions are concerned. The security layer can even be coded in another programming language.
> 
> To fix it, I suggest to add a test just before the type narrowing in the bytecode. The test verifies if the type narrowing will alter the numerical value. If true, it throws an unchecked exception or an error. Otherwise, it continues as currently. Note that it changes the behavior but the current behavior is dangerous, useless and is a failing case. You can add a compiler option to restore the original behavior but the new behavior should be the default.
> 
> Best regards,Fabrice TIERCELIN
> 


More information about the core-libs-dev mailing list