|
| 1 | +/* |
| 2 | + * fix-CVE-2024-4317.sql |
| 3 | + * |
| 4 | + * Copyright (c) 2024, PostgreSQL Global Development Group |
| 5 | + * |
| 6 | + * src/backend/catalog/fix-CVE-2024-4317.sql |
| 7 | + * |
| 8 | + * This file should be run in every database in the cluster to address |
| 9 | + * CVE-2024-4317. |
| 10 | +*/ |
| 11 | + |
| 12 | +SET search_path= pg_catalog; |
| 13 | + |
| 14 | +CREATE OR REPLACEVIEWpg_stats_ext WITH (security_barrier)AS |
| 15 | +SELECTcn.nspnameAS schemaname, |
| 16 | +c.relnameAS tablename, |
| 17 | +sn.nspnameAS statistics_schemaname, |
| 18 | +s.stxnameAS statistics_name, |
| 19 | + pg_get_userbyid(s.stxowner)AS statistics_owner, |
| 20 | + (SELECT array_agg(a.attnameORDER BYa.attnum) |
| 21 | +FROM unnest(s.stxkeys) k |
| 22 | +JOIN pg_attribute a |
| 23 | +ON (a.attrelid=s.stxrelidANDa.attnum= k) |
| 24 | + )AS attnames, |
| 25 | + pg_get_statisticsobjdef_expressions(s.oid)as exprs, |
| 26 | +s.stxkindAS kinds, |
| 27 | +sd.stxdinheritAS inherited, |
| 28 | +sd.stxdndistinctAS n_distinct, |
| 29 | +sd.stxddependenciesAS dependencies, |
| 30 | +m.most_common_vals, |
| 31 | +m.most_common_val_nulls, |
| 32 | +m.most_common_freqs, |
| 33 | +m.most_common_base_freqs |
| 34 | +FROM pg_statistic_ext sJOIN pg_class cON (c.oid=s.stxrelid) |
| 35 | +JOIN pg_statistic_ext_data sdON (s.oid=sd.stxoid) |
| 36 | +LEFT JOIN pg_namespace cnON (cn.oid=c.relnamespace) |
| 37 | +LEFT JOIN pg_namespace snON (sn.oid=s.stxnamespace) |
| 38 | +LEFT JOIN LATERAL |
| 39 | + (SELECT array_agg(values)AS most_common_vals, |
| 40 | + array_agg(nulls)AS most_common_val_nulls, |
| 41 | + array_agg(frequency)AS most_common_freqs, |
| 42 | + array_agg(base_frequency)AS most_common_base_freqs |
| 43 | +FROM pg_mcv_list_items(sd.stxdmcv) |
| 44 | + ) mONsd.stxdmcvIS NOT NULL |
| 45 | +WHERE pg_has_role(c.relowner,'USAGE') |
| 46 | +AND (c.relrowsecurity= falseOR NOT row_security_active(c.oid)); |
| 47 | + |
| 48 | +CREATE OR REPLACEVIEWpg_stats_ext_exprs WITH (security_barrier)AS |
| 49 | +SELECTcn.nspnameAS schemaname, |
| 50 | +c.relnameAS tablename, |
| 51 | +sn.nspnameAS statistics_schemaname, |
| 52 | +s.stxnameAS statistics_name, |
| 53 | + pg_get_userbyid(s.stxowner)AS statistics_owner, |
| 54 | +stat.expr, |
| 55 | +sd.stxdinheritAS inherited, |
| 56 | + (stat.a).stanullfracAS null_frac, |
| 57 | + (stat.a).stawidthAS avg_width, |
| 58 | + (stat.a).stadistinctAS n_distinct, |
| 59 | + (CASE |
| 60 | + WHEN (stat.a).stakind1=1 THEN (stat.a).stavalues1 |
| 61 | + WHEN (stat.a).stakind2=1 THEN (stat.a).stavalues2 |
| 62 | + WHEN (stat.a).stakind3=1 THEN (stat.a).stavalues3 |
| 63 | + WHEN (stat.a).stakind4=1 THEN (stat.a).stavalues4 |
| 64 | + WHEN (stat.a).stakind5=1 THEN (stat.a).stavalues5 |
| 65 | + END)AS most_common_vals, |
| 66 | + (CASE |
| 67 | + WHEN (stat.a).stakind1=1 THEN (stat.a).stanumbers1 |
| 68 | + WHEN (stat.a).stakind2=1 THEN (stat.a).stanumbers2 |
| 69 | + WHEN (stat.a).stakind3=1 THEN (stat.a).stanumbers3 |
| 70 | + WHEN (stat.a).stakind4=1 THEN (stat.a).stanumbers4 |
| 71 | + WHEN (stat.a).stakind5=1 THEN (stat.a).stanumbers5 |
| 72 | + END)AS most_common_freqs, |
| 73 | + (CASE |
| 74 | + WHEN (stat.a).stakind1=2 THEN (stat.a).stavalues1 |
| 75 | + WHEN (stat.a).stakind2=2 THEN (stat.a).stavalues2 |
| 76 | + WHEN (stat.a).stakind3=2 THEN (stat.a).stavalues3 |
| 77 | + WHEN (stat.a).stakind4=2 THEN (stat.a).stavalues4 |
| 78 | + WHEN (stat.a).stakind5=2 THEN (stat.a).stavalues5 |
| 79 | + END)AS histogram_bounds, |
| 80 | + (CASE |
| 81 | + WHEN (stat.a).stakind1=3 THEN (stat.a).stanumbers1[1] |
| 82 | + WHEN (stat.a).stakind2=3 THEN (stat.a).stanumbers2[1] |
| 83 | + WHEN (stat.a).stakind3=3 THEN (stat.a).stanumbers3[1] |
| 84 | + WHEN (stat.a).stakind4=3 THEN (stat.a).stanumbers4[1] |
| 85 | + WHEN (stat.a).stakind5=3 THEN (stat.a).stanumbers5[1] |
| 86 | + END) correlation, |
| 87 | + (CASE |
| 88 | + WHEN (stat.a).stakind1=4 THEN (stat.a).stavalues1 |
| 89 | + WHEN (stat.a).stakind2=4 THEN (stat.a).stavalues2 |
| 90 | + WHEN (stat.a).stakind3=4 THEN (stat.a).stavalues3 |
| 91 | + WHEN (stat.a).stakind4=4 THEN (stat.a).stavalues4 |
| 92 | + WHEN (stat.a).stakind5=4 THEN (stat.a).stavalues5 |
| 93 | + END)AS most_common_elems, |
| 94 | + (CASE |
| 95 | + WHEN (stat.a).stakind1=4 THEN (stat.a).stanumbers1 |
| 96 | + WHEN (stat.a).stakind2=4 THEN (stat.a).stanumbers2 |
| 97 | + WHEN (stat.a).stakind3=4 THEN (stat.a).stanumbers3 |
| 98 | + WHEN (stat.a).stakind4=4 THEN (stat.a).stanumbers4 |
| 99 | + WHEN (stat.a).stakind5=4 THEN (stat.a).stanumbers5 |
| 100 | + END)AS most_common_elem_freqs, |
| 101 | + (CASE |
| 102 | + WHEN (stat.a).stakind1=5 THEN (stat.a).stanumbers1 |
| 103 | + WHEN (stat.a).stakind2=5 THEN (stat.a).stanumbers2 |
| 104 | + WHEN (stat.a).stakind3=5 THEN (stat.a).stanumbers3 |
| 105 | + WHEN (stat.a).stakind4=5 THEN (stat.a).stanumbers4 |
| 106 | + WHEN (stat.a).stakind5=5 THEN (stat.a).stanumbers5 |
| 107 | + END)AS elem_count_histogram |
| 108 | +FROM pg_statistic_ext sJOIN pg_class cON (c.oid=s.stxrelid) |
| 109 | +LEFT JOIN pg_statistic_ext_data sdON (s.oid=sd.stxoid) |
| 110 | +LEFT JOIN pg_namespace cnON (cn.oid=c.relnamespace) |
| 111 | +LEFT JOIN pg_namespace snON (sn.oid=s.stxnamespace) |
| 112 | +JOIN LATERAL ( |
| 113 | +SELECT unnest(pg_get_statisticsobjdef_expressions(s.oid))AS expr, |
| 114 | + unnest(sd.stxdexpr)::pg_statisticAS a |
| 115 | + ) statON (stat.exprIS NOT NULL) |
| 116 | +WHERE pg_has_role(c.relowner,'USAGE') |
| 117 | +AND (c.relrowsecurity= falseOR NOT row_security_active(c.oid)); |