• Alexander Barkov's avatar
    MDEV-15005 ASAN: stack-buffer-overflow in my_strnncollsp_simple · f538a648
    Alexander Barkov authored
    cmp_item_sort_string::store_value() did not cache the string returned
    from item->val_str(), whose result can point to various private members
    such as Item_char_typecast::tmp_value.
    
    - cmp_item_sort_string::store_value() remembered the pointer returned
      from item->val_str() poiting to tmp_value into cmp_item_string::value_res.
    - Later, cmp_item_real::store_value() was called, which called
      Item_str_func::val_real(), which called Item_char_typecast::val_str(&tmp)
      using a local stack variable "String tmp". Item_char_typecast::tmp_value
      was overwritten and become a link to "tmp":
      tmp_value.Ptr freed its own buffer and set to point to the buffer
      owned by "tmp".
    - On return from Item_str_func::val_real(), "String tmp" was destructed,
      but "tmp_value" still pointed to the buffer owned by "tmp",
      So tmp_value.Ptr became invalid.
    - Then cmp_item_sort_string() passed cmp_item_string::value_res to sortcmp().
      At this point, value_res still pointed to an invalid value of
      Item_char_typecast::tmp_value.
    
    Fix:
    changing cmp_item_sort_string::store_value() to force copying
    to cmp_item_string::value if item->val_str(&value) returned
    a different pointer (instead of &value).
    f538a648
item_cmpfunc.h 85.3 KB