staging: greybus: audio: bound topology parsing to the received buffer
Subsystems (4)
PATCH 1/1 staging: greybus: audio: bound topology parsing to the received buffer
The Greybus audio topology parser trusts length and count fields taken straight from the module's topology blob and never checks them against the size of the buffer it actually allocated, leading to out-of-bounds reads of the kernel heap.
gb_audio_gb_get_topology() reads a u16 size from the module, allocates a buffer of that size, fetches the topology into it, and then discards the size. gbaudio_tplg_parse_data() then walks that buffer using fields stored inside it:
- gbaudio_tplg_process_header() computes the control, widget and route block offsets by adding the wire-supplied __le32 size_dais, size_controls and size_widgets onto the buffer base with no bound, so a module that reports a small allocation size but large block sizes moves those offsets far past the end of the buffer before they are dereferenced.
- gbaudio_tplg_process_kcontrols(), _process_widgets() and _process_routes() iterate num_controls / num_widgets / num_routes (also from the blob) and advance a pointer by a per-element size that includes the __le16 names_length of an enumerated control, again with no check that the element stays inside the buffer.
- gb_generate_enum_strings() loops over an attacker-controlled __le32 items count and, for each, scans for a NUL terminator with no end pointer, walking off the end of the buffer.
A malicious or malfunctioning module can therefore make the parser read past the allocation. The wild block offsets are most likely to hit an unmapped page and oops (denial of service); the byte-at-a-time enum scan walks from a still-valid pointer and can copy adjacent heap bytes into ALSA control name strings, which are readable by unprivileged local users, so an information leak cannot be ruled out.
Thread the allocated topology size from gb_audio_gb_get_topology() through to gbaudio_tplg_parse_data() and bound every walk against the end of the buffer: verify the block offsets are ordered and within the buffer (the "< previous" tests also catch a 32-bit unsigned wrap of the running offset), check each control, widget and route lies fully inside its block before use, and give gb_generate_enum_strings() an explicit end pointer plus an items-versus-names_length sanity check.
--- drivers/staging/greybus/audio_codec.h | 4 +- drivers/staging/greybus/audio_gb.c | 13 ++- drivers/staging/greybus/audio_module.c | 6 +- drivers/staging/greybus/audio_topology.c | 135 ++++++++++++++++++----- 4 files changed, 120 insertions(+), 38 deletions(-)
drivers/staging/greybus/audio_codec.h +2 -2
@@ -167,7 +167,7 @@ struct gbaudio_module_info { };int gbaudio_tplg_parse_data (struct gbaudio_module_info *module, -struct gb_audio_topology *tplg_data); +struct gb_audio_topology *tplg_data,size_t size);void gbaudio_tplg_release (struct gbaudio_module_info *module); int gbaudio_module_update(struct gbaudio_codec_info *codec,
@@ -179,7 +179,7 @@ void gbaudio_unregister_module(struct gbaudio_module_info *module);/* protocol related */ int gb_audio_gb_get_topology (struct gb_connection *connection, -struct gb_audio_topology **topology); +struct gb_audio_topology **topology,size_t *size);int gb_audio_gb_get_control (struct gb_connection *connection,u8 control_id,u8 index,struct gb_audio_ctl_elem_value *value);
drivers/staging/greybus/audio_gb.c +7 -6
@@ -10,11 +10,11 @@/* TODO: Split into separate calls */ int gb_audio_gb_get_topology (struct gb_connection *connection, -struct gb_audio_topology **topology) +struct gb_audio_topology **topology,size_t *size) {struct gb_audio_get_topology_size_response size_resp;struct gb_audio_topology *topo; -u16 size; +u16 tplg_size;int ret; ret = gb_operation_sync(connection, GB_AUDIO_TYPE_GET_TOPOLOGY_SIZE,
@@ -22,22 +22,23 @@ int gb_audio_gb_get_topology(struct gb_connection *connection,if (ret)return ret; - size =le16_to_cpu (size_resp.size); -if (size <sizeof (*topo)) + tplg_size =le16_to_cpu (size_resp.size); +if (tplg_size <sizeof (*topo))return -ENODATA ; - topo =kzalloc (size,GFP_KERNEL ); + topo =kzalloc (tplg_size,GFP_KERNEL );if (!topo)return -ENOMEM ; ret =gb_operation_sync (connection,GB_AUDIO_TYPE_GET_TOPOLOGY ,NULL ,0 , - topo, size); + topo, tplg_size);if (ret) {kfree (topo);return ret; } *topology = topo; + *size = tplg_size;return 0 ; }
drivers/staging/greybus/audio_module.c +4 -2
@@ -240,6 +240,7 @@ static int gb_audio_probe(struct gb_bundle *bundle,struct gbaudio_data_connection *dai, *_dai;int ret, i;struct gb_audio_topology *topology; +size_t tplg_size;/* There should be at least one Management and one Data cport */ if (bundle->num_cports <2 )
@@ -308,14 +309,15 @@ static int gb_audio_probe(struct gb_bundle *bundle,* FIXME: malloc for topology happens via audio_gb driver * should be done within codec driver itself */ - ret =gb_audio_gb_get_topology (gbmodule->mgmt_connection, &topology); + ret =gb_audio_gb_get_topology (gbmodule->mgmt_connection, &topology, + &tplg_size);if (ret) {dev_err (dev,"%d:Error while fetching topology\n" , ret); goto disable_connection; }/* process topology data */ - ret =gbaudio_tplg_parse_data (gbmodule, topology); + ret =gbaudio_tplg_parse_data (gbmodule, topology, tplg_size);if (ret) {dev_err (dev,"%d:Error while parsing topology data\n" , ret);
drivers/staging/greybus/audio_topology.c +107 -28
269 lines not on this page · raw message
Signature · 1 line
2.55.0
Greg Kroah-Hartman
maintainerQuoted 42 lines · Muhammad Bilal
The Greybus audio topology parser trusts length and count fields taken straight from the module's topology blob and never checks them against the size of the buffer it actually allocated, leading to out-of-bounds reads of the kernel heap.
gb_audio_gb_get_topology()reads a u16 size from the module, allocates a buffer of that size, fetches the topology into it, and then discards the size.gbaudio_tplg_parse_data()then walks that buffer using fields stored inside it:-
gbaudio_tplg_process_header()computes the control, widget and route block offsets by adding the wire-supplied__le32size_dais,size_controlsandsize_widgetsonto the buffer base with no bound, so a module that reports a small allocation size but large block sizes moves those offsets far past the end of the buffer before they are dereferenced.-
gbaudio_tplg_process_kcontrols(),_process_widgets()and_process_routes()iteratenum_controls/num_widgets/num_routes(also from the blob) and advance a pointer by a per-element size that includes the__le16names_lengthof an enumerated control, again with no check that the element stays inside the buffer.-
gb_generate_enum_strings()loops over an attacker-controlled__le32items count and, for each, scans for a NUL terminator with no end pointer, walking off the end of the buffer.A malicious or malfunctioning module can therefore make the parser read past the allocation. The wild block offsets are most likely to hit an unmapped page and oops (denial of service); the byte-at-a-time enum scan walks from a still-valid pointer and can copy adjacent heap bytes into ALSA control name strings, which are readable by unprivileged local users, so an information leak cannot be ruled out.
Thread the allocated topology size from
gb_audio_gb_get_topology()through togbaudio_tplg_parse_data()and bound every walk against the end of the buffer: verify the block offsets are ordered and within the buffer (the "< previous" tests also catch a 32-bit unsigned wrap of the running offset), check each control, widget and route lies fully inside its block before use, and givegb_generate_enum_strings()an explicit end pointer plus an items-versus-names_length sanity check.
Quoted 3 lines
Fixes 6339d2322c47 ("greybus: audio: Add topology parser for GB codec")Signed-off-by Muhammad Bilal <[email protected]>
How was this found, and tested?
thanks,
greg k-h
Quoted 1 line · Greg Kroah-Hartman
How was this found, and tested?
Found by manual code audit of the topology parsing path in drivers/staging/greybus/audio_topology.c.
The pattern that caught my attention was in gb_audio_gb_get_topology():
it reads a u16 size from the module, allocates and fills a buffer of that size, but then discards the size and never passes it to the parsing functions. The parser then walks the buffer using lengths and counts taken from inside the buffer itself (size_dais, size_controls, size_widgets, num_controls, num_widgets, num_routes, names_length, items) with no bounds checks against the actual allocation size, so every one of those walks is an unchecked trust of wire data.
gb_generate_enum_strings() in particular has an unbounded while (*data != '\0') scan with no end pointer, which is the most straightforward out-of-bounds read.
Testing: I verified the patch builds cleanly with
make M=drivers/staging/greybus on x86_64 (allyesconfig with CONFIG_STAGING=y and CONFIG_GREYBUS=m). I do not have Greybus hardware, so runtime testing was limited to build verification and manual review of the bounds logic.
I confirmed that:
- All four block offsets (dai, control, widget, route) are validated as ordered and within the buffer in gbaudio_tplg_process_header().
- Each per-element walk in _process_kcontrols(), _process_widgets(), and _process_routes() checks that the element fits before dereferencing.
- gb_generate_enum_strings() now has an explicit end pointer and rejects items > names_length.
- The gbaudio_control_size() helper centralises the variable-length control size calculation and validates it before use.
Thanks,
Muhammad Bilal