Skip to content

Instantly share code, notes, and snippets.

@will
Last active February 24, 2020 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will/cde195be78a78cb9623b3159d8c42efa to your computer and use it in GitHub Desktop.
Save will/cde195be78a78cb9623b3159d8c42efa to your computer and use it in GitHub Desktop.
fuzzing postgres
  • Fuzzing finds bugs feeding malformed or semi-maifomred data into a program.

  • This unexpected input can hopefully find bugs before they are found by attackers.

  • coverage guided or not

  • greg stark 2016

  • problems coverage guided and postgres

  • sqlsmith https://github.com/anse1/sqlsmith

  1. compile sqlsmith
  2. create a target citus formation
  3. create some sort of schema and load it with some data that will test all the features of citus you want. I've been using this https://gist.github.com/will/e8a1e6efd46ac82f1b61d0c0ccab1b52
  4. create a database to hold the logs/results
  5. use the "log.sql" file in the sqlsmith repo to build the schema for the log db
  6. populate the known and known_re tables with the files in the repo 7a) or use the ones in the file I've attached to the end of this gist which include the ones from the repo plus errors that I've decided to ignore
  7. run it with ./sqlsmith --target=$TARGET --log-to=$DATABASE_URL 8a) I've been running it on across 16 instances all using the same log and target database.
  8. wait a while
  9. look at the report and other views in the log database for interesting errors, and look at any crash dumps on the target citus databases that have been generated.

report

 10074 | 2019-12-25 20:20:23.653909-08 | ERROR:  connection error: ec2-44-229-243-130.us-west-2.compute.amazonaws.com:5432
  6730 | 2019-12-26 22:50:40.648963-08 | could not connect to server: Connection timed out
  1739 | 2019-12-25 22:08:30.322891-08 | server closed the connection unexpectedly
  1011 | 2019-12-25 22:39:35.163359-08 | ERROR:  invalid attnum -6 for rangetable entry rel
   356 | 2019-12-25 17:53:24.550249-08 | ERROR:  WindowFunc found in non-WindowAgg plan node
   276 | 2019-12-25 22:42:16.808511-08 | ERROR:  register width does not match: source uses 5 and dest uses 0

an error

-[ RECORD 1 ]----------------------------------------------------------------------------------------------------------------
id       | 260
msg      | ERROR:  WindowFunc found in non-WindowAgg plan node                                                               +
         |
query    | delete from public.users                                                                                          +
         | where                                                                                                             +
         | (cast(null as money) = cast(coalesce(case when EXISTS (                                                           +
         |           select                                                                                                  +
         |               public.users.id as c0,                                                                              +
         |               sample_0.shardmaxvalue as c1,                                                                       +
         |               public.users.country_id as c2,                                                                      +
         |               sample_0.shardid as c3,                                                                             +
         |               public.users.created_at as c4,                                                                      +
         |               public.users.id as c5,                                                                              +
         |               sample_0.shardstorage as c6,                                                                        +
         |               public.users.country_id as c7,                                                                      +
         |               (select pg_catalog.var_samp(total_time) from pg_catalog.pg_stat_xact_user_functions)                +
         |                  as c8,                                                                                           +
         |               sample_0.shardid as c9,                                                                             +
         |               sample_0.shardminvalue as c10,                                                                      +
         |               public.users.name as c11,                                                                           +
         |               public.users.country_id as c12,                                                                     +
         |               public.users.id as c13,                                                                             +
         |               sample_0.shardid as c14,                                                                            +
         |               (select table_name from information_schema.constraint_table_usage limit 1 offset 4)                 +
         |                  as c15,                                                                                          +
         |               sample_0.shardmaxvalue as c16,                                                                      +
         |               public.users.org_id as c17,                                                                         +
         |               public.users.id as c18,                                                                             +
         |               sample_0.logicalrelid as c19,                                                                       +
         |               (select action_orientation from information_schema.triggers limit 1 offset 4)                       +
         |                  as c20,                                                                                          +
         |               sample_0.shardmaxvalue as c21,                                                                      +
         |               sample_0.shardid as c22                                                                             +
         |             from                                                                                                  +
         |               pg_catalog.pg_dist_shard as sample_0 tablesample bernoulli (4.5)                                    +
         |             where true                                                                                            +
         |             limit 136) then cast(coalesce(cast(null as money),                                                    +
         |           case when cast(null as point) @ cast(null as line) then cast(null as money) else cast(null as money) end+
         |             ) as money) else cast(coalesce(cast(null as money),                                                   +
         |           case when cast(null as point) @ cast(null as line) then cast(null as money) else cast(null as money) end+
         |             ) as money) end                                                                                       +
         |         ,                                                                                                         +
         |       cast(null as money)) as money))                                                                             +
         |   or (cast(null as _lquery) ^? cast(null as ltree))                                                               +
         | returning                                                                                                         +
         |   public.users.name as c0,                                                                                        +
         |   public.users.created_at as c1,                                                                                  +
         |   public.users.org_id as c2
target   | (null)
sqlstate | XX000
t        | 2019-12-14 00:38:28.116281-08
errid    | 47025159

postgres bug:

planner bugs:

segfaults:

  • sqlsmith generate string sql for citus UDFs
  • coverage guided
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment