Skip to content

SQL example: reads can modify one replica and QuerySize is not checked against the message length #40

Open

Description

@resetius

Problem

Two defects in the SQL example (examples/sql.cpp):

  1. A "read" can modify the database. TSql::Read passes the client's text straight to sqlite3_exec (sql.cpp#L125-L128, sql.cpp#L110-L123). The read path does not go through the Raft log, so anything a client sends as a read (INSERT, UPDATE, DROP TABLE, a multi-statement string, UPDATE raft_metadata_ ...) runs on whichever replica served the read, outside the log.
  2. QuerySize is trusted. Both Read and Write build the query as std::string(Query, QuerySize) (sql.cpp#L127, sql.cpp#L162) without comparing QuerySize to the message length (Len - sizeof(...)), so a crafted message makes the node read past the end of the message buffer.

Scenario

A client sends a read request whose text is DELETE FROM t (or UPDATE raft_metadata_ SET value = '0' WHERE key = 'LastAppliedIndex'). The replica that happens to serve it applies the change locally; the other replicas never see it. The replicas diverge permanently, and the log no longer describes the state machine. For the second defect, a request with QuerySize = 1000000 and an empty body makes std::string copy up to 1 MB of heap memory following the buffer into the query, or crash.

Expected

Reads are executed read-only (for example a prepared statement that must report sqlite3_stmt_readonly, and a single statement), so a read cannot change replica state (Raft paper section 8: the state machine must change only by applying committed log entries). QuerySize is validated against the message length before use, both for reads and for log entries.

Fix in PR #31

PR #31 (closed, not merged) has the fixes in b514088 (read-only reads, length checks) and the SQL part of ca86106. Tests that cover it: test/test_sql_payloads.cpp. Since the PR is closed, the fix would come as a separate small PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions