Skip to content
Disclosed report

Memory-safety bug in mozilla::layers::Edit (via nsTArray)

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 8 days
  • Paid in 9 days
  • Disclosed after 8 days
Text
mozilla::layers::Edit (obj*\ipc\ipdl\_ipdlheaders\mozilla\layers\LayersMessages.h) is stored in TArray objects (as in PLayerTransactionChild::Read), but it indirectly contains self-pointers in some cases, making this storage unsafe. Because *TArray objects by default memcpy their elements during reallocation (as when the array is expanded), it is generally unsafe to store within them objects that contain self-pointers, because memcpy leaves these pointers pointing at a given object's previous storage, instead of updating them to point to the object's new storage. This restriction can be overcome by specializing nsTArray, as has been done for, e.g., js::Heap, but that has not been done for layers::Edit.

It is unclear whether this bug is active or latent; it would be latent if the questionable TArrays' capacities are never expanded after the first element is added.


Details:
--------

mozilla::layers::Edit contains an OpSetLayerAttributes object (as a char array, which it manipulates as an OpSetLayerAttributes)
OpSetLayerAttributes contains a LayerAttributes object 
LayerAttributes contains a CommonLayerAttributes object
CommonLayerAttributes contains the std::string object contentDescription_

On at least STLPort (which is used in Gonk) std::string's |_M_start_of_storage._M_data| pointer can point to "short string storage" within the object. This is so when it is built with _STLP_USE_SHORT_STRING_OPTIM (which is the default) and when the string is sufficiently short:

(build\stlport\stlport\stl\_string_base.h):

53: #if defined (_STLP_USE_SHORT_STRING_OPTIM)
54:   union _Buffers {
55:     _Tp*  _M_end_of_storage;
56:     _Tp   _M_static_buf[_DEFAULT_SIZE];
57:   } _M_buffers;
58: #else
59:   _Tp*    _M_end_of_storage;
60: #endif /* _STLP_USE_SHORT_STRING_OPTIM */
61: protected:
62: #if defined (_STLP_USE_SHORT_STRING_OPTIM)
63:   bool _M_using_static_buf() const
64:   { return (_M_start_of_storage._M_data == _M_buffers._M_static_buf); }
65:   _Tp const* _M_Start() const { return _M_start_of_storage._M_data; }
66:   _Tp* _M_Start() { return _M_start_of_storage._M_data; }
...
69:   _Tp* _M_End()
70:   { return _M_using_static_buf() ? _M_buffers._M_static_buf + _DEFAULT_SIZE : _M_buffers._M_end_of_storage; }
71:   size_type _M_capacity() const
72:   { return _M_using_static_buf() ? _DEFAULT_SIZE : _M_buffers._M_end_of_storage - _M_start_of_storage._M_data; }
...
75: #else
...
86:   _Tp*    _M_finish;
87:   _AllocProxy _M_start_of_storage;

The code uses _M_Start() (line 65) to access its buffer, which simply returns |_M_start_of_storage._M_data|. Thus, if an std::string object that is using short string storage is moved with memcpy, its _M_data member still points to the _M_static_buf in its former location, which rightfully belongs to some other object. Also, the object incorrectly will conclude that it's using heap storage, because _M_start_of_storage._M_data will != _M_buffers._M_static_buf (see _M_using_static_buf() at lines 63-4) . This will cause other malfunctions; for example, _M_capacity() (lines 71-2) will return an incorrect -- and possibly gigantic -- value.

It seems that the std::string that comes with VS 2013 is unaffected, though I have not puzzled out all of its implementation.



[1] On a related topic, CommonLayerAttributes also contains the member

   nsIntRegion visibleRegion_;

for which nsTArray *is* specialized to avoid memcpy. There does not appear to be a reason for this, other than avoiding the undefined behavior inherent in memcpy-ing anything that isn't a trivially-copyable object. Does anyone know why this was done?

The original report as published on Mozilla's Bugzilla, by q1. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

What is known

A security bug in Graphics: Layers. Mozilla made it public on 3 September 2015.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

More by q1

View all

71 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.