Am I affected?

type your flop version to check

Description

Deserialization of Untrusted Data vulnerability in woylie flop allows a remote attacker to execute code on the server via a crafted pagination cursor.

Flop.Cursor.decode/2 in lib/flop/cursor.ex decodes the after and before parameters with :erlang.binary_to_term/2 in :safe mode. Its check rejects functions, PIDs, ports and references, but it accepts maps, and therefore structs of any module. Flop passes the decoded values to Ecto as query parameters. A cursor value that nests an Ecto.Query.DynamicExpr struct with a DBConnection.Stream struct as its binding fails the Ecto cast. Ecto then inspects the value for the error message, the Inspect implementation enumerates the stream, and DBConnection calls the module, function and arguments in the :log option of the stream.

This issue affects flop: from 0.8.0 before 0.26.7, from 0.27.0 before 0.27.3, from 0.28.0 before 0.28.1, and from 0.29.0 before 0.29.2.

Technical analysis

1. Cursor decoding

Flop.Cursor.decode/2 decodes the Base64 value of the after or before parameter and calls :erlang.binary_to_term/2 with the :safe option. The private sanitize/1 function raises for functions, PIDs, ports and references. It accepts atoms, numbers, binaries, lists, tuples and maps. A struct is a map, so the decoder returns structs of any module whose atoms exist in the VM.

2. Type validation

Flop casts each cursor value against the Ecto type of its order_by field. For a :naive_datetime field, the cast accepts a DateTime struct and does not check the type of the struct fields. A DateTime struct whose year field holds another struct passes.

3. Query execution

Flop passes the value to Ecto as a pinned query parameter. Ecto.Query.Planner.cast_param/3 fails to cast the value and builds an Ecto.Query.CastError, which inspects the value. Inspect.NaiveDateTime.inspect/2 raises on the non-integer year, and the resulting Inspect.Error inspects the function arguments, which include the nested struct.

4. Gadget

The nested struct is an Ecto.Query.DynamicExpr. Its Inspect implementation calls Enum.map/2 on the binding field. The binding field holds a DBConnection.Stream struct, and the Enumerable implementation of that struct runs DBConnection.reduce/3 with the options from the struct. DBConnection.log/5 applies the {module, function, arguments} tuple in the :log option with the log entry as the first argument. With {:proc_lib, :init_p, [[], module, function, arguments]}, :proc_lib.init_p/5 applies the chosen module, function and arguments.

Proof of concept

  1. Define an Ecto schema with timestamps/1 and derive Flop.Schema with sortable: [:inserted_at] and a default order on :inserted_at.
  2. Build the map %{inserted_at: datetime}, where datetime is a DateTime struct whose year field is %Ecto.Query.DynamicExpr{binding: %DBConnection.Stream{conn: %DBConnection{}, query: %Postgrex.Result{}, params: [], opts: [log: {:proc_lib, :init_p, [[], IO, :inspect, [:hi]]}]}}.
  3. Encode the map with :erlang.term_to_binary/1 and Base.url_encode64/1.
  4. Call Flop.validate_and_run!(Post, %{"after" => payload}, for: Post, repo: Repo).
  5. The call raises an Ecto.Query.CastError. Before it raises, the process prints :hi, which shows that IO.inspect(:hi) ran.

Weaknesses & attack patterns

Weakness

CWE-502 · Deserialization of Untrusted Data in catalog → MITRE ↗

Attack patterns

CAPEC-586 · Object Injection MITRE ↗

An attacker who can send a pagination cursor to an application can call an exported function of a loaded module with chosen arguments in the application's VM. The module, function and argument atoms must exist in the VM.

Affected — Hex / flop Hex.pm ↗ Repository ↗

≥ 0.8.0 < 0.26.7 affected
0.27 series ≥ 0.27.0 < 0.27.3 affected
0.28 series ≥ 0.28.0 < 0.28.1 affected
0.29 series ≥ 0.29.0 < 0.29.2 affected
every other version: unaffected
cpe cpe:2.3:a:woylie:flop:*:*:*:*:*:*:*:*
version type semver
modules · source files · routines
modules 'Elixir.Flop.Cursor' · 'Elixir.Flop'
source files lib/flop/cursor.ex · lib/flop.ex
routines 'Elixir.Flop.Cursor':decode/2 · 'Elixir.Flop.Cursor':'decode!'/2 · 'Elixir.Flop':validate_and_run/3 · 'Elixir.Flop':'validate_and_run!'/3

Affected — GitHub / woylie/flop Repository ↗

≥ 4535794 and up affected
→ 6382b93 not affected
→ 82e9399 not affected
→ 93fabac not affected
→ bfd9ee2 not affected
every other version: unaffected
cpe cpe:2.3:a:woylie:flop:*:*:*:*:*:*:*:*
version type git
modules · source files · routines
modules 'Elixir.Flop.Cursor' · 'Elixir.Flop'
source files lib/flop/cursor.ex · lib/flop.ex
routines 'Elixir.Flop.Cursor':decode/2 · 'Elixir.Flop.Cursor':'decode!'/2 · 'Elixir.Flop':validate_and_run/3 · 'Elixir.Flop':'validate_and_run!'/3

Workarounds

Set the pagination_types option so that it excludes :first and :last, for example pagination_types: [:page, :offset]. A request with a crafted after cursor then does not trigger the call. This disables cursor-based pagination.

Configurations

The application passes request parameters to Flop, for example with Flop.validate_and_run/3, and permits cursor-based pagination. Cursor-based pagination is permitted by default. The demonstrated call chain also needs ecto and db_connection in the application, and uses an order_by field of the Ecto type :naive_datetime.

References

Credits

Jonatan Männchen / EEF Finder Coordinator
Wojtek Mach Finder Reporter

CVSS breakdown

CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N

Exploitability

Attack Vector
Network
Attack Complexity
Low
Attack Requirements
Present
Privileges Required
None
User Interaction
None

Vulnerable System

Confidentiality
High
Integrity
High
Availability
High

Subsequent Systems

Confidentiality
None
Integrity
None
Availability
None
« All CVEs