-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathallowed_objects.c
More file actions
366 lines (317 loc) · 12.2 KB
/
Copy pathallowed_objects.c
File metadata and controls
366 lines (317 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "postgres.h"
#include "access/sysattr.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "pg_diffix/oid_cache.h"
#include "pg_diffix/query/allowed_objects.h"
#include "pg_diffix/utils.h"
static const char *const g_allowed_casts[] = {
"i2tod", "i2tof", "i2toi4", "i4toi2", "i4tod", "i4tof", "i8tod", "i8tof", "int48", "int84",
"ftod", "dtof",
"int2_numeric", "int4_numeric", "int8_numeric", "float4_numeric", "float8_numeric",
"numeric_float4", "numeric_float8",
"date_timestamptz",
/**/
};
typedef struct FunctionByName
{
const char *name;
int primary_arg;
} FunctionByName;
typedef struct FunctionByOid
{
Oid funcid;
int primary_arg;
} FunctionByOid;
static const FunctionByName g_allowed_builtins[] = {
/* rounding casts */
{.name = "ftoi2", .primary_arg = 0},
{.name = "ftoi4", .primary_arg = 0},
{.name = "ftoi8", .primary_arg = 0},
{.name = "dtoi2", .primary_arg = 0},
{.name = "dtoi4", .primary_arg = 0},
{.name = "dtoi8", .primary_arg = 0},
{.name = "numeric_int2", .primary_arg = 0},
{.name = "numeric_int4", .primary_arg = 0},
{.name = "numeric_int8", .primary_arg = 0},
/* substring */
{.name = "text_substr", .primary_arg = 0},
{.name = "text_substr_no_len", .primary_arg = 0},
{.name = "bytea_substr", .primary_arg = 0},
{.name = "bytea_substr_no_len", .primary_arg = 0},
/* numeric generalization */
{.name = "dround", .primary_arg = 0},
{.name = "numeric_round", .primary_arg = 0},
{.name = "dceil", .primary_arg = 0},
{.name = "numeric_ceil", .primary_arg = 0},
{.name = "dfloor", .primary_arg = 0},
{.name = "numeric_floor", .primary_arg = 0},
/* width_bucket */
{.name = "width_bucket_float8", .primary_arg = 0},
{.name = "width_bucket_numeric", .primary_arg = 0},
/* date_trunc */
{.name = "timestamptz_trunc", .primary_arg = 1},
{.name = "timestamp_trunc", .primary_arg = 1},
/* extract & date_part*/
{.name = "extract_date", .primary_arg = 1},
{.name = "extract_timestamp", .primary_arg = 1},
{.name = "extract_timestamptz", .primary_arg = 1},
{.name = "timestamp_part", .primary_arg = 1},
{.name = "timestamptz_part", .primary_arg = 1},
/**/
};
static const char *const g_substring_builtins[] = {
"text_substr", "text_substr_no_len", "bytea_substr", "bytea_substr_no_len"
/**/
};
/* Only those allowed in `anonymized_untrusted` access level. */
static const char *const g_implicit_range_builtins_untrusted[] = {
"dround", "numeric_round", "dfloor", "numeric_floor",
/**/
};
/* Some allowed functions don't appear in the builtins catalog, so we must allow them manually by OID. */
#define F_NUMERIC_ROUND_INT 1708
/*
* `date_part` for `date` is a SQL builtin and doesn't show up in `fmgr_isbuiltin`.
* PG 14 has the define, but PG 13 doesn't.
*/
#if PG_MAJORVERSION_NUM < 14
#define F_DATE_PART_TEXT_DATE 1384
#endif
static const FunctionByOid g_allowed_builtins_extra[] = {
{.funcid = F_NUMERIC_ROUND_INT, .primary_arg = 0},
{.funcid = F_DATE_PART_TEXT_DATE, .primary_arg = 1},
/**/
};
typedef struct AllowedCols
{
const char *rel_name; /* Name of the relation */
Bitmapset *cols; /* Indices of the allowed columns of the relation */
const char *const col_names[100]; /* Names of columns in the relation */
} AllowedCols;
static const char *const g_pg_catalog_allowed_rels[] = {
"pg_aggregate", "pg_am", "pg_attrdef", "pg_attribute", "pg_auth_members", "pg_authid", "pg_available_extension_versions",
"pg_available_extensions", "pg_cast", "pg_collation", "pg_constraint", "pg_database", "pg_db_role_setting", "pg_default_acl",
"pg_depend", "pg_depend", "pg_description", "pg_event_trigger", "pg_extension", "pg_foreign_data_wrapper", "pg_foreign_server",
"pg_foreign_table", "pg_index", "pg_inherits", "pg_language", "pg_largeobject_metadata", "pg_locks", "pg_namespace",
"pg_opclass", "pg_operator", "pg_opfamily", "pg_policy", "pg_prepared_statements", "pg_prepared_xacts", "pg_publication",
"pg_publication_rel", "pg_rewrite", "pg_roles", "pg_seclabel", "pg_seclabels", "pg_sequence", "pg_settings", "pg_shadow",
"pg_shdepend", "pg_shdescription", "pg_shseclabel", "pg_stat_gssapi", "pg_subscription", "pg_subscription_rel", "pg_tablespace",
"pg_trigger", "pg_ts_config", "pg_ts_dict", "pg_ts_parser", "pg_ts_template", "pg_type", "pg_user", "pg_tables", "pg_matviews",
"pg_indexes", "pg_class", "pg_enum", "pg_proc" /* `pg_proc` contains `procost` and `prorows` but both seem to be fully static data. */
};
static AllowedCols g_pg_catalog_allowed_cols[] = {
{.rel_name = "pg_statistic_ext", .col_names = {"tableoid", "oid", "stxrelid", "stxname", "stxnamespace", "stxstattarget", "stxkeys", "stxkind"}},
{.rel_name = "pg_stat_activity", .col_names = {"datname", "pid", "usename", "application_name", "client_addr", "backend_start", "xact_start", "query_start", "state_change", "wait_event_type", "wait_event", "state", "query", "backend_type", "client_hostname", "client_port", "backend_start", "backend_xid", "backend_xmin"}},
/*
* In `pg_stat_database` there are also `tup_*` and `blks_*` columns, but blocking them doesn't break clients
* dramatically, so opting to leave them out to err on the safe side.
*/
{.rel_name = "pg_stat_database", .col_names = {"datname", "xact_commit", "xact_rollback"}},
/**/
};
static const char *const g_decimal_integer_casts[] = {
"numeric_int2", "numeric_int4", "numeric_int8", "dtoi2", "dtoi4", "dtoi8", "ftoi2", "ftoi4", "ftoi8",
/**/
};
static const char *const g_extract_functions[] = {
"extract_date", "extract_timestamp", "extract_timestamptz", "timestamp_part", "timestamptz_part",
/**/
};
static void prepare_pg_catalog_allowed(Oid relation_oid, AllowedCols *allowed_cols)
{
MemoryContext old_context = MemoryContextSwitchTo(TopMemoryContext);
for (int i = 0; allowed_cols->col_names[i] != NULL; i++)
{
int attnum = get_attnum(relation_oid, allowed_cols->col_names[i]) - FirstLowInvalidHeapAttributeNumber;
allowed_cols->cols = bms_add_member(allowed_cols->cols, attnum);
}
MemoryContextSwitchTo(old_context);
}
/* Pointers to OID cache which is populated at runtime. */
static const Oid *const g_implicit_range_udfs[] = {
&g_oid_cache.round_by_nn,
&g_oid_cache.round_by_dd,
&g_oid_cache.ceil_by_nn,
&g_oid_cache.ceil_by_dd,
&g_oid_cache.floor_by_nn,
&g_oid_cache.floor_by_dd,
};
/* Only those allowed in `anonymized_untrusted` access level. */
static const Oid *const g_implicit_range_udfs_untrusted[] = {
&g_oid_cache.round_by_nn,
&g_oid_cache.round_by_dd,
&g_oid_cache.floor_by_nn,
&g_oid_cache.floor_by_dd,
};
/* Taken from fmgrtab.h. */
typedef struct
{
Oid foid; /* OID of the function */
short nargs; /* 0..FUNC_MAX_ARGS, or -1 if variable count */
bool strict; /* T if function is "strict" */
bool retset; /* T if function returns a set */
const char *funcName; /* C name of the function */
PGFunction func; /* pointer to compiled function */
} FmgrBuiltin;
#define InvalidOidBuiltinMapping PG_UINT16_MAX
#ifdef WIN32
/* On Windows, FMGR exports have to be linked dynamically. */
#define FMGRIMPORTTYPE PGDLLIMPORT
#else
#define FMGRIMPORTTYPE
#endif
/*
* This table stores info about all the built-in functions (ie, functions
* that are compiled into the Postgres executable).
*/
extern FMGRIMPORTTYPE const FmgrBuiltin fmgr_builtins[];
extern FMGRIMPORTTYPE const int fmgr_nbuiltins;
extern FMGRIMPORTTYPE const Oid fmgr_last_builtin_oid;
extern FMGRIMPORTTYPE const uint16 fmgr_builtin_oid_index[];
/* Taken from fmgr.c. */
static const FmgrBuiltin *fmgr_isbuiltin(Oid id)
{
if (id > fmgr_last_builtin_oid)
return NULL;
uint16 index = fmgr_builtin_oid_index[id];
if (index == InvalidOidBuiltinMapping)
return NULL;
return &fmgr_builtins[index];
}
static bool is_member_of(const char *s, const char *const array[], int length)
{
for (int i = 0; i < length; i++)
{
if (strcmp(array[i], s) == 0)
return true;
}
return false;
}
static bool is_func_member_of(Oid funcoid, const FunctionByName func_array[], int length)
{
const FmgrBuiltin *fmgr_builtin = fmgr_isbuiltin(funcoid);
if (fmgr_builtin != NULL)
{
for (int i = 0; i < length; i++)
{
if (strcmp(func_array[i].name, fmgr_builtin->funcName) == 0)
return true;
}
}
return false;
}
static bool is_funcname_member_of(Oid funcoid, const char *const name_array[], int length)
{
const FmgrBuiltin *fmgr_builtin = fmgr_isbuiltin(funcoid);
if (fmgr_builtin != NULL)
return is_member_of(fmgr_builtin->funcName, name_array, length);
return false;
}
int primary_arg_index(Oid funcoid)
{
for (int i = 0; i < ARRAY_LENGTH(g_implicit_range_udfs); i++)
{
/* We ensured that our UDFs have the primary arg first. */
if (*g_implicit_range_udfs[i] == funcoid)
return 0;
}
const FmgrBuiltin *fmgr_builtin = fmgr_isbuiltin(funcoid);
if (fmgr_builtin != NULL)
{
for (int i = 0; i < ARRAY_LENGTH(g_allowed_builtins); i++)
{
if (strcmp(g_allowed_builtins[i].name, fmgr_builtin->funcName) == 0)
return g_allowed_builtins[i].primary_arg;
}
}
for (int i = 0; i < ARRAY_LENGTH(g_allowed_builtins_extra); i++)
{
if (g_allowed_builtins_extra[i].funcid == funcoid)
return g_allowed_builtins_extra[i].primary_arg;
}
FAILWITH("Cannot identify the primary argument position for funcid %u.", funcoid);
}
bool is_allowed_cast(const FuncExpr *func_expr)
{
if (is_funcname_member_of(func_expr->funcid, g_allowed_casts, ARRAY_LENGTH(g_allowed_casts)))
{
return true;
}
else if (is_funcname_member_of(func_expr->funcid, g_decimal_integer_casts, ARRAY_LENGTH(g_decimal_integer_casts)))
{
/* Handle cases like `cast(extract(minute from ...) as integer)`. */
Node *cast_arg = linitial(func_expr->args);
if (IsA(cast_arg, FuncExpr))
{
FuncExpr *cast_arg_expr = (FuncExpr *)cast_arg;
if (is_funcname_member_of(cast_arg_expr->funcid, g_extract_functions, ARRAY_LENGTH(g_extract_functions)) ||
cast_arg_expr->funcid == F_DATE_PART_TEXT_DATE)
return true;
}
}
return false;
}
bool is_implicit_range_udf_untrusted(Oid funcoid)
{
for (int i = 0; i < ARRAY_LENGTH(g_implicit_range_udfs_untrusted); i++)
{
if (*g_implicit_range_udfs_untrusted[i] == funcoid)
return true;
}
return false;
}
bool is_allowed_function(Oid funcoid)
{
for (int i = 0; i < ARRAY_LENGTH(g_implicit_range_udfs); i++)
{
if (*g_implicit_range_udfs[i] == funcoid)
return true;
}
if (is_func_member_of(funcoid, g_allowed_builtins, ARRAY_LENGTH(g_allowed_builtins)))
return true;
for (int i = 0; i < ARRAY_LENGTH(g_allowed_builtins_extra); i++)
{
if (g_allowed_builtins_extra[i].funcid == funcoid)
return true;
}
DEBUG_LOG("Rejecting usage of function %u.", funcoid);
return false;
}
bool is_substring_builtin(Oid funcoid)
{
return is_funcname_member_of(funcoid, g_substring_builtins, ARRAY_LENGTH(g_substring_builtins));
}
bool is_implicit_range_builtin_untrusted(Oid funcoid)
{
return is_funcname_member_of(funcoid, g_implicit_range_builtins_untrusted, ARRAY_LENGTH(g_implicit_range_builtins_untrusted));
}
bool is_allowed_pg_catalog_rte(Oid relation_oid, const Bitmapset *selected_cols)
{
/* First handle `SELECT count(*) FROM pg_catalog.x`. */
if (selected_cols == NULL)
return true;
char *rel_name = get_rel_name(relation_oid);
/* Then check if the entire relation is allowed. */
if (is_member_of(rel_name, g_pg_catalog_allowed_rels, ARRAY_LENGTH(g_pg_catalog_allowed_rels)))
{
pfree(rel_name);
return true;
}
/* Otherwise specific selected columns must be checked against the allow-list. */
bool allowed = false;
for (int i = 0; i < ARRAY_LENGTH(g_pg_catalog_allowed_cols); i++)
{
if (strcmp(g_pg_catalog_allowed_cols[i].rel_name, rel_name) != 0)
continue;
if (g_pg_catalog_allowed_cols[i].cols == NULL)
prepare_pg_catalog_allowed(relation_oid, &g_pg_catalog_allowed_cols[i]);
allowed = bms_is_subset(selected_cols, g_pg_catalog_allowed_cols[i].cols);
break;
}
pfree(rel_name);
return allowed;
}