{"id":"RUSTSEC-2021-0111","summary":"Memory Safety Issue when using `patch` or `merge` on `state` and assign the result back to `state`","details":"Affected versions of this crate maintains references to memory that might have been freed already.\nIf affects the following two `tremor-script` language constructs:\n\n* A [Merge](https://www.tremor.rs/docs/tremor-script/index#merge) where we assign the result back to the target expression\n  and the expression to be merged needs to reference the `event`:\n\n```\nlet state = merge state of event end;\n```\n\n* A [Patch](https://www.tremor.rs/docs/tremor-script/index#patch) where we assign the result back to the target expression\n  and the patch operations used need to reference the `event`:\n\n```\nlet state = patch state of insert event.key =\u003e event.value end;\n```\n\nFor constructs like this (it doesnt matter what is references in the expression to be merged or the patch operations) an optimization\nwas applied to manipulate the target value in-place, instead of cloning it.\n\nOur `Value` struct which underpins all event data in `tremor-script`, is representing as borrowed strings `beef::Cow\u003c'lifetime, str\u003e` \nthat reference the actual `Vec\u003cu8\u003e` the event is based upon. We keep the raw byte-array next to the event structure inside our `Event` struct as a self-referential struct,\nso we make sure that the structured `Value` and its references are valid across its whole lifetime.\n\nThe optimization was considered safe as long as it was only possible to merge or patche `event` data or static data.\nWhen `state` was introduced to `tremor-script` a new possibility existed, to keep `Value` data around for longer than the lifetime of an event.\nIf `event` data is merged or patched into `state` without cloning `state` first, it can still reference keys or values from\nthe previous event, which will now be invalid. This allows access to those already freed regions of memory and to get their content out over the wire.\n\n\n## Workaround\n\nIf an upgrade is not possible, a possible workaround is to avoid the optimization\nby introducing a temporary variable and not immediately reassigning to `state`:\n\n```\nlet tmp = merge state of event end;\nlet state = tmp\n```\n\n## Fix\n\nThe flaw was corrected in `tremor-script` version 0.11.6 via commit [1a2efcd](https://github.com/tremor-rs/tremor-runtime/commit/1a2efcdbe68e5e7fd0a05836ac32d2cde78a0b2e) by removing the optimization\nand always clone the target expression of a [Merge](https://www.tremor.rs/docs/tremor-script/index#merge) or [Patch](https://www.tremor.rs/docs/tremor-script/index#patch.","aliases":["CVE-2021-45701","CVE-2021-45702","GHSA-3pp4-64mp-9cg9","GHSA-9qvw-46gf-4fv8","GHSA-q2x5-6q7q-r872"],"modified":"2023-11-08T04:07:22.748212Z","published":"2021-09-16T12:00:00Z","database_specific":{"license":"CC0-1.0"},"references":[{"type":"PACKAGE","url":"https://crates.io/crates/tremor-script"},{"type":"ADVISORY","url":"https://rustsec.org/advisories/RUSTSEC-2021-0111.html"},{"type":"WEB","url":"https://github.com/tremor-rs/tremor-runtime/pull/1217"}],"affected":[{"package":{"name":"tremor-script","ecosystem":"crates.io","purl":"pkg:cargo/tremor-script"},"ranges":[{"type":"SEMVER","events":[{"introduced":"0.7.3-0"},{"fixed":"0.11.6"}]}],"ecosystem_specific":{"affects":{"arch":[],"os":[],"functions":[]},"affected_functions":null},"database_specific":{"informational":null,"categories":["memory-corruption","memory-exposure"],"source":"https://github.com/rustsec/advisory-db/blob/osv/crates/RUSTSEC-2021-0111.json","cvss":null}}],"schema_version":"1.7.3"}