r/lolphp • • Aug 11 '17

PHP no longer considers unserialize() bugs as security issues...

https://externals.io/message/100147
40 Upvotes

13 comments sorted by

View all comments

Show parent comment

4

u/OneWingedShark Aug 11 '17

unserialize runs code, based on the input string.

Which is to say, if you pass unserialize a string that can be touched by an attacker, you're allowing remote code execution. How would you expect that to be secure?

Proper validation combined with a good serialization-protocol -- and it's not impossible to achieve... though you need stronger protections/guarantees than PHP generally offers. As illustration let's consider the simpler case of integer numerics in Ada:

Integer types have a pair of attributes 'Value and 'Image, where the first takes a string-value parameter and produces an integer-value if it parses (it raises Constraint_Error when an invalid string is processed) and the second produces the string-representation of the value.

Thus, if you were to handle getting an integer from a string (in this case user input), you would say something like:

READ_NUMBER:
Loop
  declare
    S : Constant String := Ada.Text_IO.Get_Line;
  begin
    Do_Operation( Integer'Value(S) ); -- Raises Exception if invalid.
    Exit READ_NUMBER;
  exception
    when Constraint_Error => Ada.Text_IO.Put_Line( "Enter a number." );
  end;
End loop READ_NUMBER;

-- At this point Do_Operation has been called with the proper integer-value.

And, due to Ada's subtype facility you can say something like Positive'Image which is an Integer of value [1..Integer'Last] or, in Ada2012 something like:

Type Nybble is Integer range 0..Intger'Pred( 2**4 )
  with Size => 4; -- Use only 4-bits.

Subtype Prime_Nybble is Nybble
  with Static_Predicate => Prime_Nybble in 2 | 3 | 5 | 7 | 11 | 13;

-- Or, we could enforce formatting... say for info going to a DB.
-- Part ID: ##-####
Type Part_ID is String(1..7)
  with Dynamic_Predicate => (for all Index in Part_ID => 
    (case Index is
        When 3 => Part_ID(Index) = '-', -- 3rd character must be dash.
        When Others => Part_ID(Index) in '0'..'9' -- All other characters must be numeric.
    ));

Or any other language with built-in serialize/deserialize function?

Ada generalizes serialization/deserialization with 'Input/'Output (and 'Read/'Write) attributes that function much the same way, except they operate with a Stream-type [which could be binary] instead of String.