-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpg_anonymize.c
1141 lines (994 loc) · 29 KB
/
pg_anonymize.c
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* pg_anonymize.c
* Anonymize data on-the-fly
*
*
* pg_anonymize
* Copyright (C) 2022-2024 - Julien Rouhaud.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/genam.h"
#if PG_VERSION_NUM >= 120000
#include "access/relation.h"
#include "access/table.h"
#else
#include "access/heapam.h"
#include "access/htup_details.h"
#endif
#include "access/xact.h"
#if PG_VERSION_NUM < 140000
#include "catalog/indexing.h"
#endif
#include "catalog/namespace.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_inherits.h"
#if PG_VERSION_NUM >= 110000
#include "catalog/pg_namespace_d.h"
#else
#include "catalog/pg_namespace.h"
#endif
#include "catalog/pg_seclabel.h"
#include "catalog/pg_type.h"
#include "commands/copy.h"
#include "commands/seclabel.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/plancat.h"
#include "parser/analyze.h"
#include "rewrite/rewriteHandler.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
PG_MODULE_MAGIC;
#define PGAN_PROVIDER "pg_anonymize"
#define PGAN_ROLE_ANONYMIZED "anonymize"
/* Backward compatibility macros */
#if PG_VERSION_NUM < 120000
#define table_open(r, l) heap_open(r, l)
#define table_close(r, l) heap_close(r, l)
#endif
#if PG_VERSION_NUM < 150000
#define parse_analyze_fixedparams(r, s, p, n, e) parse_analyze(r, s, p, n, e)
#define MarkGUCPrefixReserved(c) EmitWarningsOnPlaceholders(c)
#endif
/* Reimplement some AttrMap features to keep later code simpler. */
#if PG_VERSION_NUM < 130000
#include "access/tupconvert.h"
typedef struct AttrMap
{
AttrNumber *attnums;
int maplen;
} AttrMap;
static AttrMap *
build_attrmap_by_name_if_req(TupleDesc indesc, TupleDesc outdesc)
{
AttrMap *attrMap;
AttrNumber *attnums;
#if PG_VERSION_NUM >= 120000
attnums = convert_tuples_by_name_map_if_req(indesc, outdesc,
"could not convert row type");
#else
/* pg11- doesn't expose the "_if_req" part */
attnums = convert_tuples_by_name_map(indesc, outdesc,
"could not convert row type");
#endif
if (attnums != NULL)
{
attrMap = (AttrMap *) palloc(sizeof(AttrMap));
attrMap->attnums = attnums;
attrMap->maplen = outdesc->natts;
}
else
attrMap = NULL;
return attrMap;
}
static void
free_attrmap(AttrMap *attrMap)
{
pfree(attrMap->attnums);
pfree(attrMap);
}
#endif
/* Used for pgan_get_rel_seclabels_worker() */
typedef struct pganWalkerContext
{
Relation secRel; /* Cached pg_seclabel relation */
Relation inhRel; /* Cached pg_inherits relation */
char **seclabels; /* The array of found security labels */
int nb_labels; /* # of columns for which we found a seclabel */
TupleDesc tupdesc; /* The original relation tupledesc */
} pganWalkerContext;
/*---- Local variables ----*/
static bool pgan_toplevel = true;
/*---- GUC variables ----*/
static bool pgan_check_labels = true;
static bool pgan_inherit_labels = true;
static bool pgan_enabled = true;
/*---- Function declarations ----*/
void _PG_init(void);
static ProcessUtility_hook_type prev_ProcessUtility = NULL;
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
static void pgan_post_parse_analyze(ParseState *pstate, Query *query
#if PG_VERSION_NUM >= 140000
, JumbleState *jstate
#endif
);
static void pgan_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
#if PG_VERSION_NUM >= 140000
bool readOnlyTree,
#endif
ProcessUtilityContext context, ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
#if PG_VERSION_NUM >= 130000
QueryCompletion *qc
#else
char *completionTag
#endif
);
static void pgan_check_injection(Relation rel,
const ObjectAddress *object,
const char *seclabel);
static void pgan_check_expression_valid(Relation rel,
const ObjectAddress *object,
const char *seclabel);
static void pgan_check_preload_lib(char *libnames, char *kind, bool missing_ok);
static List *pgan_get_attnums(TupleDesc tupDesc, Relation rel,
List *attnamelist, bool is_copy);
static char *pgan_get_query_for_relid(Relation rel, List *attlist,
bool is_copy);
static char **pgan_get_rel_seclabels(Relation rel);
static void pgan_get_rel_seclabels_worker(Relation rel,
pganWalkerContext *context);
static bool pgan_hack_query(Node *node, void *context);
static void pgan_hack_rte(RangeTblEntry *rte);
static bool pgan_is_role_anonymized(void);
static void pgan_object_relabel(const ObjectAddress *object,
const char *seclabel);
void
_PG_init(void)
{
/*
* This extension can modify the Query in post_parse_analyze_hook, but
* doesn't have a way to adapt the raw query string accordingly. This can
* cause problem with some extensions like pg_stat_statements that rely on
* both referring to the same thing. To make sure that we don't cause
* interference, we process other post_parse_analyze_hook first before our
* own processing, but we have to make sure that we're the last module
* loaded. Unfortunately we can only check that when our code is loaded,
* so we can only hope that no incompatible extension will be loaded
* afterwards.
*/
if (process_shared_preload_libraries_in_progress)
{
pgan_check_preload_lib(shared_preload_libraries_string,
"shared_preload_libraries", false);
}
else
{
/*
* Check on session_preload_libraries and local_preload_libraries in
* case that's how we're loaded.
*/
pgan_check_preload_lib(session_preload_libraries_string,
"session_preload_libraries", true);
pgan_check_preload_lib(local_preload_libraries_string,
"local_preload_libraries", true);
}
register_label_provider(PGAN_PROVIDER, pgan_object_relabel);
DefineCustomBoolVariable("pg_anonymize.check_labels",
"Check SECURITY LABELS when they are defined.",
NULL,
&pgan_check_labels,
true,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
DefineCustomBoolVariable("pg_anonymize.enabled",
"Globally enable pg_anonymize.",
NULL,
&pgan_enabled,
true,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
DefineCustomBoolVariable("pg_anonymize.inherit_labels",
"Also use security label from parents if any.",
NULL,
&pgan_inherit_labels,
true,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
MarkGUCPrefixReserved("pg_anonymize");
/* Install hooks. */
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = pgan_post_parse_analyze;
prev_ProcessUtility = ProcessUtility_hook;
ProcessUtility_hook = pgan_ProcessUtility;
}
/*
* Make sure that the given expression doesn't contain any SQL injection
* attempt.
*/
static void
pgan_check_injection(Relation rel,
const ObjectAddress *object,
const char *seclabel)
{
FormData_pg_attribute *att;
StringInfoData sql;
List *parsetree_list;
att = TupleDescAttr(RelationGetDescr(rel), object->objectSubId - 1);
initStringInfo(&sql);
appendStringInfo(&sql, "SELECT %s AS %s FROM %s.%s",
seclabel,
quote_identifier(NameStr(att->attname)),
quote_identifier(get_namespace_name(RelationGetNamespace(rel))),
quote_identifier(RelationGetRelationName(rel)));
PG_TRY();
{
parsetree_list = pg_parse_query(sql.data);
}
PG_CATCH();
{
errcontext("during validation of expression \"%s\"", seclabel);
PG_RE_THROW();
}
PG_END_TRY();
if (list_length(parsetree_list) != 1)
elog(ERROR, "SQL injection detected!");
}
/*
* Perform sanity checks on the user provided security label
*/
static void
pgan_check_expression_valid(Relation rel, const ObjectAddress *object,
const char *seclabel)
{
StringInfoData sql;
int ret;
bool prev_xact_read_only;
char *prev_search_path;
initStringInfo(&sql);
appendStringInfo(&sql, "SELECT pg_typeof(%s)::regtype::oid FROM %s.%s LIMIT 1",
seclabel,
quote_identifier(get_namespace_name(RelationGetNamespace(rel))),
quote_identifier(RelationGetRelationName(rel)));
if ((ret = SPI_connect()) < 0)
{
/* internal error */
elog(ERROR, "SPI_connect returned %d", ret);
}
/*
* We ask for read-only SPI execution, but it doesn't reliably detect write
* queries, so force additional executor check.
*/
prev_xact_read_only = XactReadOnly;
prev_search_path = pstrdup(namespace_search_path);
PG_TRY();
{
XactReadOnly = true;
set_config_option("search_path", "pg_catalog", PGC_SUSET,
PGC_S_SESSION, GUC_ACTION_SET, true, 0, false);
SPI_execute(sql.data, true, 1);
XactReadOnly = prev_xact_read_only;
set_config_option("search_path", prev_search_path, PGC_SUSET,
PGC_S_SESSION, GUC_ACTION_SET, true, 0, false);
}
PG_CATCH();
{
XactReadOnly = prev_xact_read_only;
set_config_option("search_path", prev_search_path, PGC_SUSET,
PGC_S_SESSION, GUC_ACTION_SET, true, 0, false);
errcontext("during validation of expression \"%s\"", seclabel);
PG_RE_THROW();
}
PG_END_TRY();
/*
* No row in the source table, can't say about the expression apart that
* it's valid.
*/
if (SPI_processed == 0)
elog(NOTICE, "the expression \"%s\" is valid but no data in table"
" %s.%s, cannot check returned type",
seclabel,
quote_identifier(get_namespace_name(RelationGetNamespace(rel))),
quote_identifier(RelationGetRelationName(rel)));
else
{
FormData_pg_attribute *att;
Oid typid;
bool isnull;
Assert(SPI_processed == 1);
typid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0],
SPI_tuptable->tupdesc,
1, &isnull));
/* Should not happen */
if (isnull)
elog(ERROR, "unexpected NULL value");
att = TupleDescAttr(RelationGetDescr(rel), object->objectSubId - 1);
if (typid != att->atttypid)
{
if (typid == UNKNOWNOID && att->atttypid == TEXTOID)
{
/* Should be valid, but notify the user. */
elog(NOTICE, "The expression has an unknown type, you may "
"want to explicitly cast it to text");
}
else
elog(ERROR, "The expression returns \"%s\" type, but the "
" column is defined as \"%s\"",
format_type_be(typid),
format_type_be(att->atttypid));
}
}
SPI_finish();
}
/*
* Check that pg_anonymize is loaded last according to the given
* xxx_preload_libraries_string.
*
* If missing_ok is true, don't raise an error if pg_anonymize is not present.
*/
static void
pgan_check_preload_lib(char *libnames, char *kind, bool missing_ok)
{
List *xpl;
ListCell *lc;
char *libname;
int nb;
if (!SplitIdentifierString(libnames, ',', &xpl))
elog(ERROR, "Could not parse %s", kind);
if (!missing_ok)
{
/* First a quick check to make sure we're the last element. */
libname = (char *) llast(xpl);
if (strcmp(libname, "pg_anonymize") != 0)
elog(ERROR, "pg_anonymize needs to be last in %s", kind);
}
/*
* Some paranoid check: make sure we're not also somewhere else in the
* xxx_preload_libraries, as in that case we would not be loaded
* last.
*/
nb = 1;
foreach(lc, xpl)
{
libname = (char *) lfirst(lc);
if (nb != list_length(xpl) && strcmp(libname, "pg_anonymize") == 0)
elog(ERROR, "pg_anonymize needs to be last in %s", kind);
nb++;
}
}
/*
* Adaptation of CopyGetAttnums that optionally allows generated attributes
*/
static List *
pgan_get_attnums(TupleDesc tupDesc, Relation rel, List *attnamelist,
bool is_copy)
{
List *attnums = NIL;
if (attnamelist == NIL)
{
/* Generate default column list */
int attr_count = tupDesc->natts;
int i;
for (i = 0; i < attr_count; i++)
{
if (TupleDescAttr(tupDesc, i)->attisdropped)
continue;
#if PG_VERSION_NUM >= 120000
/* Only COPY should ignore generated attributes */
if (TupleDescAttr(tupDesc, i)->attgenerated && is_copy)
continue;
#endif
attnums = lappend_int(attnums, i + 1);
}
}
else
{
/* Validate the user-supplied list and extract attnums */
ListCell *l;
foreach(l, attnamelist)
{
char *name = strVal(lfirst(l));
int attnum;
int i;
/* Lookup column name */
attnum = InvalidAttrNumber;
for (i = 0; i < tupDesc->natts; i++)
{
Form_pg_attribute att = TupleDescAttr(tupDesc, i);
if (att->attisdropped)
continue;
if (namestrcmp(&(att->attname), name) == 0)
{
#if PG_VERSION_NUM >= 120000
if (att->attgenerated && is_copy)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("column \"%s\" is a generated column",
name),
errdetail("Generated columns cannot be used in COPY.")));
#endif
attnum = att->attnum;
break;
}
}
if (attnum == InvalidAttrNumber)
{
if (rel != NULL)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" of relation \"%s\" does not exist",
name, RelationGetRelationName(rel))));
else
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" does not exist",
name)));
}
/* Check for duplicates */
if (list_member_int(attnums, attnum))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_COLUMN),
errmsg("column \"%s\" specified more than once",
name)));
attnums = lappend_int(attnums, attnum);
}
}
return attnums;
}
/*
* Generate an SQL query returning the anonymized data.
*/
static char *
pgan_get_query_for_relid(Relation rel, List *attlist, bool is_copy)
{
char **seclabels;
List *attnums;
ListCell *lc;
TupleDesc tupdesc;
StringInfoData select;
bool first;
/*
* We only anonymize plain (possibly partitioned) relations and
* materialized views.
*/
if (rel->rd_rel->relkind != RELKIND_RELATION &&
rel->rd_rel->relkind != RELKIND_MATVIEW &&
rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
return NULL;
/* COPY isn't allowed for partitioned table. */
if (is_copy && rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
return NULL;
/* Fetch all the declared SECURITY LABEL on the relation. */
seclabels = pgan_get_rel_seclabels(rel);
/* Nothing to do if no SECURITY LABEL declared. */
if (seclabels == NULL)
return NULL;
tupdesc = RelationGetDescr(rel);
attnums = pgan_get_attnums(tupdesc, rel, attlist, is_copy);
initStringInfo(&select);
appendStringInfoString(&select, "SELECT ");
first = true;
foreach(lc, attnums)
{
FormData_pg_attribute *att;
int attnum = lfirst_int(lc);
if (!first)
appendStringInfoString(&select, ", ");
else
first = false;
att = TupleDescAttr(tupdesc, attnum - 1);
/*
* If the column is anonymized, emit the proper expression, otherwise
* just emit the (quoted) column name.
*/
if (seclabels[attnum] != NULL)
{
appendStringInfo(&select, "%s AS %s", seclabels[attnum],
quote_identifier(NameStr(att->attname)));
}
else
{
Assert(!att->attisdropped);
appendStringInfoString(&select,
quote_identifier(NameStr(att->attname)));
}
}
/* Finish building the query if we found any security label on the table. */
appendStringInfo(&select, " FROM%s %s.%s",
(is_copy ? " ONLY" : ""),
quote_identifier(get_namespace_name(RelationGetNamespace(rel))),
quote_identifier(RelationGetRelationName(rel)));
return select.data;
}
/*
* Get all SECURITY LABELs for the given relation.
*
* This function returns an array, indexed by the underlying column attribute
* number, of the security labels.
*
* If the relation doesn't have any security label defined, NULL is returned.
*/
static char **
pgan_get_rel_seclabels(Relation rel)
{
pganWalkerContext *context;
context = (pganWalkerContext *) palloc0(sizeof(pganWalkerContext));
context->secRel = table_open(SecLabelRelationId, AccessShareLock);
/* The worker function does all the work. */
pgan_get_rel_seclabels_worker(rel, context);
table_close(context->secRel, AccessShareLock);
if (context->inhRel)
table_close(context->inhRel, AccessShareLock);
if (context->nb_labels == 0)
return NULL;
return context->seclabels;
}
/*
* Function that looks for security labels for the given relation, and any of
* its ancestor(s).
*
* This function will recurse, using a depth-first search, for all the given
* relation ancestor(s) (if any) until either a security label has been found
* for all columns of the original relation, or no more ancestor exist.
*
* Caller is responsible for opening and caching pg_catalog in secRel, with the
* rest of the members zero-initialized. This function will allocate the
* seclabel array only if nb_labels is not zero and may open and cache
* pg_inherits in inhRel. Caller is also responsible for checking if inhRel is
* cached and closing it in that case.
*/
static void
pgan_get_rel_seclabels_worker(Relation rel, pganWalkerContext *context)
{
TupleDesc tupdesc;
AttrMap *attrMap;
ScanKeyData keys[3];
SysScanDesc scan;
HeapTuple tuple;
ScanKeyInit(&keys[0],
Anum_pg_seclabel_objoid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(RelationGetRelid(rel)));
ScanKeyInit(&keys[1],
Anum_pg_seclabel_classoid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(RelationRelationId));
ScanKeyInit(&keys[2],
Anum_pg_seclabel_provider,
BTEqualStrategyNumber, F_TEXTEQ,
CStringGetTextDatum(PGAN_PROVIDER));
scan = systable_beginscan(context->secRel, SecLabelObjectIndexId, true,
NULL, 3, keys);
tuple = systable_getnext(scan);
/*
* Bail out if we didn't find any SECURITY LABEL for that relation and user
* don't want to inherit security labels, but after the necessary cleanup.
*/
if (!HeapTupleIsValid(tuple) && !pgan_inherit_labels)
{
systable_endscan(scan);
return;
}
tupdesc = RelationGetDescr(rel);
/*
* If this is the first call, we're passed the original relation. In that
* case we save a copy of its tupledesc and allocate the seclabel array.
* Otherwise, we need to build an AttrMap as there's no guarantee that the
* original relation and the current ancestor have the same tuple
* descriptor.
*/
if (context->tupdesc == NULL)
{
context->tupdesc = CreateTupleDescCopy(tupdesc);
/* No map needed. */
attrMap = NULL;
context->seclabels = palloc0(sizeof(char *) *
/* AttrNumber is 1-based */
(RelationGetNumberOfAttributes(rel) + 1));
}
else
{
attrMap = build_attrmap_by_name_if_req(context->tupdesc,
tupdesc
#if PG_VERSION_NUM >= 160000
, false
#endif
);
}
while (HeapTupleIsValid(tuple))
{
Datum datum;
bool isnull;
datum = heap_getattr(tuple, Anum_pg_seclabel_label,
RelationGetDescr(context->secRel), &isnull);
if (!isnull)
{
int attnum = ((FormData_pg_seclabel *) GETSTRUCT(tuple))->objsubid;
/* If an AttrMap was build, get the mapped AttrNumber. */
if (attrMap)
{
Assert(attnum <= attrMap->maplen);
attnum = attrMap->attnums[attnum - 1];
Assert(attnum <= context->tupdesc->natts);
}
/* Don't overload an existing security label. */
if (context->seclabels[attnum] == NULL)
{
context->seclabels[attnum] = TextDatumGetCString(datum);
context->nb_labels++;
}
}
tuple = systable_getnext(scan);
}
systable_endscan(scan);
if (attrMap)
free_attrmap(attrMap);
/*
* If we found a security label for all columns of the ancestor relation,
* or user don't want to inherit security labels, we're done!
*/
if (context->nb_labels == context->tupdesc->natts ||
!pgan_inherit_labels)
{
return;
}
/* Check if we need to inherit security labels from ancestor */
if (context->inhRel == NULL)
context->inhRel = table_open(InheritsRelationId, AccessShareLock);
ScanKeyInit(&keys[0],
Anum_pg_inherits_inhrelid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(RelationGetRelid(rel)));
scan = systable_beginscan(context->inhRel, InheritsRelidSeqnoIndexId, true,
NULL, 1, keys);
/* Iterate over all ancestors if any, using a depth-first search. */
while ((tuple = systable_getnext(scan)) != NULL)
{
Form_pg_inherits inh = (Form_pg_inherits) GETSTRUCT(tuple);
Oid inhparent = inh->inhparent;
Relation parentRel;
parentRel = table_open(inhparent, AccessShareLock);
pgan_get_rel_seclabels_worker(parentRel, context);
table_close(parentRel, AccessShareLock);
}
systable_endscan(scan);
}
/*
* Walker function for query_tree_walker.
* Inspect all range table entries in all found queries.
*/
static bool
pgan_hack_query(Node *node, void *context)
{
if (node == NULL)
return false;
/* Ignore any Query that we generated. */
if (IsA(node, Query) && ((Query *) node)->querySource != QSRC_PARSER)
{
Query *query = (Query *) node;
ListCell *rtable;
foreach(rtable, query->rtable)
{
RangeTblEntry *rte = lfirst_node(RangeTblEntry, rtable);
if (rte->rtekind != RTE_RELATION)
continue;
pgan_hack_rte(rte);
}
return query_tree_walker(query,
pgan_hack_query,
context,
0);
}
return expression_tree_walker(node,
pgan_hack_query,
context);
}
/*
* Transform the given plain relation RangeTblEntry to a subquery based on the
* anonymized table if any of the relation's field should be anonymized.
*/
static void
pgan_hack_rte(RangeTblEntry *rte)
{
Relation rel;
char *sql;
rel = relation_open(rte->relid, AccessShareLock);
sql = pgan_get_query_for_relid(rel, NIL, false);
relation_close(rel, NoLock);
/*
* If we got a query, transform the given rte in a subquery pointing to it.
*/
if (sql)
{
List *parselist;
RawStmt *raw;
Query *subquery;
bool prev_toplevel = pgan_toplevel;
PG_TRY();
{
parselist = pg_parse_query(sql);
}
PG_CATCH();
{
errcontext("during anonymization of table %s", get_rel_name(rte->relid));
PG_RE_THROW();
}
PG_END_TRY();
Assert(list_length(parselist) == 1);
Assert(IsA(linitial(parselist), RawStmt));
raw = linitial_node(RawStmt, parselist);
/*
* Be careful to not call our post_parse_analyze_hook when generating
* the new query.
*/
pgan_toplevel = false;
PG_TRY();
{
subquery = parse_analyze_fixedparams(raw, sql, NULL, 0, NULL);
pgan_toplevel = prev_toplevel;
}
PG_CATCH();
{
pgan_toplevel = prev_toplevel;
PG_RE_THROW();
}
PG_END_TRY();
/* Remember to not process it again */
subquery->querySource = QSRC_PARSER;
AcquireRewriteLocks(subquery, true, false);
rte->rtekind = RTE_SUBQUERY;
rte->subquery = subquery;
rte->security_barrier = false;
/* Clear fields that should not be set in a subquery RTE */
rte->relid = InvalidOid;
rte->relkind = 0;
#if PG_VERSION_NUM >= 120000
rte->rellockmode = 0;
#endif
rte->tablesample = NULL;
#if PG_VERSION_NUM >= 160000
rte->perminfoindex = 0; /* no permission checking for this RTE */
#endif
rte->inh = false; /* must not be set for a subquery */
#if PG_VERSION_NUM < 160000
rte->requiredPerms = 0; /* no permission check on subquery itself */
rte->checkAsUser = InvalidOid;
rte->selectedCols = NULL;
rte->insertedCols = NULL;
rte->updatedCols = NULL;
#if PG_VERSION_NUM >= 120000
rte->extraUpdatedCols = NULL;
#endif /* pg12+ */
#endif /* pg16- */
}
}
static bool
pgan_is_role_anonymized(void)
{
ObjectAddress addr;
char *seclabel;
ObjectAddressSet(addr, AuthIdRelationId, GetUserId());
seclabel = GetSecurityLabel(&addr, PGAN_PROVIDER);
return (seclabel && strcmp(seclabel, PGAN_ROLE_ANONYMIZED) == 0);
}
/*
* Walks the given query and replace any reference to an anonymized table with
* a subquery generating the anonymized data and configured.
*/
static void
pgan_post_parse_analyze(ParseState *pstate, Query *query
#if PG_VERSION_NUM >= 140000
, JumbleState *jstate
#endif
)
{
/* XXX - should we try to prevent write queries ? */
if (prev_post_parse_analyze_hook)
prev_post_parse_analyze_hook(pstate, query
#if PG_VERSION_NUM >= 140000
, jstate
#endif
);
/* Module disabled, recursive call or aborted transaction, bail out. */
if (!pgan_enabled || !pgan_toplevel || !IsTransactionState())
return;
/* Role isn't declared as anonymized, bail out. */
if (!pgan_is_role_anonymized())
return;
/*
* Walk the query and generate rewritten subqueries when needed. We need
* to do this last as we don't have a way to generate a proper query string
* for that new Query, other any module relying on the Query and the
* query string to be consistent (like pg_stat_statements) would fail.
*/
pgan_hack_query((Node *) query, NULL);
}
/*
* Intercept COPY TO commands to make sure anonymized data is emitted.
*/
static void
pgan_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
#if PG_VERSION_NUM >= 140000
bool readOnlyTree,
#endif
ProcessUtilityContext context,
ParamListInfo params, QueryEnvironment *queryEnv,
DestReceiver *dest,
#if PG_VERSION_NUM >= 130000
QueryCompletion *qc
#else
char *completionTag
#endif
)
{
Node *parsetree = pstmt->utilityStmt;
Relation rel;
CopyStmt *stmt;
char *sql;
bool prev_toplevel = pgan_toplevel;
const char *newsql = queryString;
/* Module disabled, recursive call or not a COPY statement, bail out. */
if (!pgan_enabled || !pgan_toplevel || !IsA(parsetree, CopyStmt))
goto hook;
stmt = (CopyStmt *) parsetree;
/* Only intercept plain COPY relation TO */
if (stmt->is_from || !stmt->relation)