-
Notifications
You must be signed in to change notification settings - Fork 2
/
anon.c
executable file
·1040 lines (900 loc) · 28.2 KB
/
anon.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
/*
* PostgreSQL Anonymizer
*
*/
#include "postgres.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"
#include "commands/seclabel.h"
#include "parser/analyze.h"
#include "parser/parser.h"
#include "fmgr.h"
#include "catalog/pg_attrdef.h"
#if PG_VERSION_NUM >= 110000
#include "catalog/pg_attrdef_d.h"
#endif
#include "catalog/pg_authid.h"
#include "catalog/pg_class.h"
#include "catalog/pg_database.h"
#include "catalog/pg_namespace.h"
#include "catalog/namespace.h"
#include "miscadmin.h"
#include "optimizer/planner.h"
#include "tcop/utility.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/ruleutils.h"
#include "utils/varlena.h"
PG_MODULE_MAGIC;
/* Definition */
#define PA_MAX_SIZE_MASKING_RULE 1024
/* Saved hook values in case of unload */
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
static ProcessUtility_hook_type prev_ProcessUtility_hook = NULL;
/*
* External Functions
*/
PGDLLEXPORT Datum anon_get_function_schema(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum anon_init(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum anon_masking_expressions_for_table(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum anon_masking_value_for_column(PG_FUNCTION_ARGS);
PGDLLEXPORT void _PG_init(void);
PGDLLEXPORT void _PG_fini(void);
PG_FUNCTION_INFO_V1(anon_get_function_schema);
PG_FUNCTION_INFO_V1(anon_init);
PG_FUNCTION_INFO_V1(anon_masking_expressions_for_table);
PG_FUNCTION_INFO_V1(anon_masking_value_for_column);
/*
* GUC Parameters
*/
static char * guc_anon_k_anonymity_provider;
static char * guc_anon_masking_policies;
static bool guc_anon_privacy_by_default;
static bool guc_anon_restrict_to_trusted_schemas;
static bool guc_anon_strict_mode;
// Some GUC vars below are not used in the C code
// but they are used in the plpgsql code
// compile with `-Wno-unused-variable` to avoid warnings
static char *guc_anon_algorithm;
static char *guc_anon_mask_schema;
static char *guc_anon_salt;
static char *guc_anon_source_schema;
static bool guc_anon_transparent_dynamic_masking;
/*
* Internal Functions
*/
static bool pa_check_masking_policies(char **newval, void **extra, GucSource source);
static char * pa_get_masking_policy_for_role(Oid roleid);
static void pa_masking_policy_object_relabel(const ObjectAddress *object, const char *seclabel);
static bool pa_has_mask_in_policy(Oid roleid, char *policy);
static void pa_rewrite(Query * query, char * policy);
static void pa_rewrite_utility(PlannedStmt *pstmt, char * policy);
static char * pa_cast_as_regtype(char * value, int atttypid);
static char * pa_masking_value_for_att(Relation rel, FormData_pg_attribute * att, char * policy);
static char * pa_masking_expressions_for_table(Oid relid, char * policy);
Node * pa_masking_stmt_for_table(Oid relid, char * policy);
/*
* Hook functions
*/
// https://github.com/taminomara/psql-hooks/blob/master/Detailed.md#post_parse_analyze_hook
#if PG_VERSION_NUM >= 140000
static void pa_post_parse_analyze_hook(ParseState *pstate, Query *query, JumbleState *jstate);
#else
static void pa_post_parse_analyze_hook(ParseState *pstate, Query *query);
#endif
// https://github.com/taminomara/psql-hooks/blob/master/Detailed.md#ProcessUtility_hook
#if PG_VERSION_NUM >= 140000
static void
pa_ProcessUtility_hook( PlannedStmt *pstmt,
const char *queryString,
bool readOnlyTree,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
QueryCompletion *qc);
#elif PG_VERSION_NUM >= 130000
static void
pa_ProcessUtility_hook( PlannedStmt *pstmt,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
QueryCompletion *qc);
#else
static void
pa_ProcessUtility_hook( PlannedStmt *pstmt,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
char *completionTag);
#endif
/*
* Checking the syntax of the masking rules
*/
static void
pa_masking_policy_object_relabel(const ObjectAddress *object, const char *seclabel)
{
char *checksemicolon;
/* SECURITY LABEL FOR anon ON COLUMN foo.bar IS NULL */
if (seclabel == NULL) return;
/* Prevent SQL injection attacks inside the security label */
checksemicolon = strchr(seclabel, ';');
switch (object->classId)
{
/* SECURITY LABEL FOR anon ON DATABASE d IS 'TABLESAMPLE SYSTEM(10)' */
case DatabaseRelationId:
if ( pg_strncasecmp(seclabel, "TABLESAMPLE", 11) == 0
&& checksemicolon == NULL
)
return;
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label for a database", seclabel)));
break;
case RelationRelationId:
/* SECURITY LABEL FOR anon ON TABLE t IS 'TABLESAMPLE SYSTEM(10)' */
if (object->objectSubId == 0)
{
if ( pg_strncasecmp(seclabel, "TABLESAMPLE", 11) == 0
&& checksemicolon == NULL
)
return;
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label for a table", seclabel)));
}
/* SECURITY LABEL FOR anon ON COLUMN t.i IS 'MASKED WITH VALUE $x$' */
if ( pg_strncasecmp(seclabel, "MASKED WITH FUNCTION", 20) == 0
|| pg_strncasecmp(seclabel, "MASKED WITH VALUE", 17) == 0
|| pg_strncasecmp(seclabel, "NOT MASKED", 10) == 0
)
return;
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label for a column", seclabel)));
break;
/* SECURITY LABEL FOR anon ON ROLE batman IS 'MASKED' */
case AuthIdRelationId:
if (pg_strcasecmp(seclabel,"MASKED") == 0)
return;
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label for a role", seclabel)));
break;
/* SECURITY LABEL FOR anon ON SCHEMA public IS 'TRUSTED' */
case NamespaceRelationId:
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("only superuser can set an anon label for a schema")));
if (pg_strcasecmp(seclabel,"TRUSTED") == 0)
return;
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label for a schema", seclabel)));
break;
/* everything else is unsupported */
default:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("The anon extension does not support labels on this object")));
break;
}
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label", seclabel)));
}
/*
* Checking the syntax of the k-anonymity declarations
*/
static void
pa_k_anonymity_object_relabel(const ObjectAddress *object, const char *seclabel)
{
switch (object->classId)
{
case RelationRelationId:
/* SECURITY LABEL FOR k_anonymity ON COLUMN t.i IS 'INDIRECT IDENTIFIER' */
if ( pg_strncasecmp(seclabel, "QUASI IDENTIFIER",17) == 0
|| pg_strncasecmp(seclabel, "INDIRECT IDENTIFIER",19) == 0
)
return;
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label for a column", seclabel)));
break;
/* everything else is unsupported */
default:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("The k_anonymity provider does not support labels on this object")));
break;
}
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid label", seclabel)));
}
/*
* Register the extension and declare its GUC variables
*/
void
_PG_init(void)
{
List * masking_policies;
ListCell * c;
char * dup;
/* GUC parameters */
DefineCustomStringVariable
(
"anon.algorithm",
"The hash method used for pseudonymizing functions",
"",
&guc_anon_algorithm,
"sha256",
PGC_SUSET,
GUC_SUPERUSER_ONLY,
NULL,
NULL,
NULL
);
DefineCustomStringVariable
(
"anon.k_anonymity_provider",
"The security label provider used for k-anonymity",
"",
&guc_anon_k_anonymity_provider,
"k_anonymity",
PGC_SUSET,
GUC_SUPERUSER_ONLY,
NULL,
NULL,
NULL
);
DefineCustomStringVariable
(
"anon.masking_policies",
"Define multiple masking policies (NOT IMPLEMENTED YET)",
"",
&guc_anon_masking_policies,
"anon",
PGC_SUSET,
GUC_LIST_INPUT | GUC_SUPERUSER_ONLY,
pa_check_masking_policies,
NULL,
NULL
);
DefineCustomStringVariable
(
"anon.maskschema",
"The schema where the dynamic masking views are stored",
"",
&guc_anon_mask_schema,
"mask",
PGC_SUSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable
(
"anon.privacy_by_default",
"Mask all columns with NULL (or the default value for NOT NULL columns).",
"",
&guc_anon_privacy_by_default,
false,
PGC_SUSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable
(
"anon.restrict_to_trusted_schemas",
"Masking filters must be in a trusted schema",
"Activate this option to prevent non-superuser from using their own masking filters",
&guc_anon_restrict_to_trusted_schemas,
false,
PGC_SUSET,
GUC_SUPERUSER_ONLY,
NULL,
NULL,
NULL
);
DefineCustomStringVariable
(
"anon.salt",
"The salt value used for the pseudonymizing functions",
"",
&guc_anon_salt,
"",
PGC_SUSET,
GUC_SUPERUSER_ONLY,
NULL,
NULL,
NULL
);
DefineCustomStringVariable
(
"anon.sourceschema",
"The schema where the table are masked by the dynamic masking engine",
"",
&guc_anon_source_schema,
"public",
PGC_SUSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable
(
"anon.strict_mode",
"A masking rule cannot change a column data type, unless you disable this",
"Disabling the mode is not recommended",
&guc_anon_strict_mode,
true,
PGC_SUSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable
(
"anon.transparent_dynamic_masking",
"New masking engine (EXPERIMENTAL)",
"",
&guc_anon_transparent_dynamic_masking,
false,
PGC_SUSET,
0,
NULL,
NULL,
NULL
);
/* Register the security label provider for k-anonymity */
register_label_provider(guc_anon_k_anonymity_provider,
pa_k_anonymity_object_relabel
);
/* Register a security label provider for each masking policy */
dup = pstrdup(guc_anon_masking_policies);
SplitGUCList(dup, ',', &masking_policies);
foreach(c,masking_policies)
{
const char *pg_catalog = "pg_catalog";
ObjectAddress schema;
Oid schema_id;
char *policy = (char *) lfirst(c);
register_label_provider(policy,pa_masking_policy_object_relabel);
}
/* Install the hooks */
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = pa_post_parse_analyze_hook;
prev_ProcessUtility_hook = ProcessUtility_hook;
ProcessUtility_hook = pa_ProcessUtility_hook;
}
/*
* Unregister and restore the hooks
*/
void
_PG_fini(void) {
post_parse_analyze_hook = prev_post_parse_analyze_hook;
ProcessUtility_hook = prev_ProcessUtility_hook;
}
/*
* anon_init
* Initialize the extension
*
*/
Datum
anon_init(PG_FUNCTION_ARGS)
{
List * masking_policies;
ListCell * m;
char * dup;
/*
* In each masking policy, mark `anon` and `pg_catalog` as TRUSTED
* For some reasons, this can't be done in _PG_init()
*/
dup = pstrdup(guc_anon_masking_policies);
SplitGUCList(dup, ',', &masking_policies);
foreach(m,masking_policies)
{
ObjectAddress anon_schema;
ObjectAddress pg_catalog_schema;
Oid schema_id;
char *policy = (char *) lfirst(m);
char *seclabel = NULL;
register_label_provider(policy,pa_masking_policy_object_relabel);
schema_id=get_namespace_oid("anon",false);
ObjectAddressSet(anon_schema, NamespaceRelationId, schema_id);
seclabel = GetSecurityLabel(&anon_schema, policy);
if ( ! seclabel || pg_strcasecmp(seclabel,"TRUSTED") != 0)
SetSecurityLabel(&anon_schema,policy,"TRUSTED");
schema_id=get_namespace_oid("pg_catalog",false);
ObjectAddressSet(pg_catalog_schema, NamespaceRelationId, schema_id);
seclabel = GetSecurityLabel(&pg_catalog_schema, policy);
if ( ! seclabel || pg_strcasecmp(seclabel,"TRUSTED") != 0)
SetSecurityLabel(&pg_catalog_schema,policy,"TRUSTED");
}
PG_RETURN_BOOL(true);
}
/*
* pa_cast_as_regtype
* decorates a value with a CAST function
*/
static char *
pa_cast_as_regtype(char * value, int atttypid)
{
StringInfoData casted_value;
initStringInfo(&casted_value);
appendStringInfo( &casted_value,
"CAST(%s AS %s)",
value,
format_type_be(atttypid)
);
return casted_value.data;
}
/*
* anon_get_function_schema
* Given a function call, e.g. 'anon.fake_city()', returns the namespace of
* the function (if possible)
*
* returns the schema name if the function is properly schema-qualified
* returns an empty string if we can't find the schema name
*
* We're calling the parser to split the function call into a "raw parse tree".
* At this stage, there's no way to know if the schema does really exists. We
* simply deduce the schema name as it is provided.
*
*/
Datum
anon_get_function_schema(PG_FUNCTION_ARGS)
{
bool input_is_null = PG_ARGISNULL(0);
char* function_call= text_to_cstring(PG_GETARG_TEXT_PP(0));
char query_string[1024];
List *raw_parsetree_list;
SelectStmt *stmt;
ResTarget *restarget;
FuncCall *fc;
if (input_is_null) PG_RETURN_NULL();
/* build a simple SELECT statement and parse it */
query_string[0] = '\0';
strlcat(query_string, "SELECT ", sizeof(query_string));
strlcat(query_string, function_call, sizeof(query_string));
#if PG_VERSION_NUM >= 140000
raw_parsetree_list = raw_parser(query_string,RAW_PARSE_DEFAULT);
#else
raw_parsetree_list = raw_parser(query_string);
#endif
/* walk throught the parse tree, down to the FuncCall node (if present) */
#if PG_VERSION_NUM >= 100000
stmt = (SelectStmt *) linitial_node(RawStmt, raw_parsetree_list)->stmt;
#else
stmt = (SelectStmt *) linitial(raw_parsetree_list);
#endif
restarget = (ResTarget *) linitial(stmt->targetList);
if (! IsA(restarget->val, FuncCall))
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("'%s' is not a valid function call", function_call)));
}
/* if the function name is qualified, extract and return the schema name */
fc = (FuncCall *) restarget->val;
if ( list_length(fc->funcname) == 2 )
{
PG_RETURN_TEXT_P(cstring_to_text(strVal(linitial(fc->funcname))));
}
PG_RETURN_TEXT_P(cstring_to_text(""));
}
/*
* pa_check_masking_policies
* A validation function (hook) called when `anon.masking_policies` is set.
*
* see: https://github.com/postgres/postgres/blob/REL_15_STABLE/src/backend/commands/variable.c#L44
*/
static bool
pa_check_masking_policies(char **newval, void **extra, GucSource source)
{
char *rawstring;
List *elemlist;
if (!*newval || strnlen(*newval,PA_MAX_SIZE_MASKING_RULE) == 0 )
{
GUC_check_errdetail("anon.masking_policies cannot be NULL or empty");
return false;
}
/* Need a modifiable copy of string */
rawstring = pstrdup(*newval);
/* Parse string into list of identifiers */
if (!SplitIdentifierString(rawstring, ',', &elemlist))
{
/* syntax error in list */
GUC_check_errdetail("List syntax is invalid.");
pfree(rawstring);
list_free(elemlist);
return false;
}
return true;
}
/*
* pa_has_mask_in_policy
* checks that a role is masked in the given policy
*
*/
static bool
pa_has_mask_in_policy(Oid roleid, char *policy)
{
ObjectAddress role;
char * seclabel = NULL;
ObjectAddressSet(role, AuthIdRelationId, roleid);
seclabel = GetSecurityLabel(&role, policy);
return (seclabel && pg_strncasecmp(seclabel, "MASKED",6) == 0);
}
/*
* pa_get_masking_policy
* For a given role, returns the policy in which he/she is masked or the NULL
* if the role is not masked.
*
* https://github.com/fmbiete/pgdisablelogerror/blob/main/disablelogerror.c
*/
static char *
pa_get_masking_policy(Oid roleid)
{
ListCell * r;
char * policy = NULL;
policy=pa_get_masking_policy_for_role(roleid);
if (policy) return policy;
// Look at the parent roles
//
// is_member_of_role(0,0);
// foreach(r,roles_is_member_of(roleid,1,InvalidOid, NULL))
// {
// policy=pa_get_masking_policy_for_role(lfirst_oid(r));
// if (policy) return policy;
// }
/* No masking policy found */
return NULL;
}
static char *
pa_get_masking_policy_for_role(Oid roleid)
{
List * masking_policies;
ListCell * c;
char * dup = pstrdup(guc_anon_masking_policies);
SplitGUCList(dup, ',', &masking_policies);
foreach(c,masking_policies)
{
char * policy = (char *) lfirst(c);
if (pa_has_mask_in_policy(roleid,policy))
return policy;
}
return NULL;
}
/*
* pa_masking_stmt_for_table
* prepare a Raw Statement object that will replace the authentic relation
*
*/
Node *
pa_masking_stmt_for_table(Oid relid, char * policy)
{
const char * query_format="SELECT %s FROM %s.%s";
StringInfoData query_string;
List * raw_parsetree_list;
Query * masking_query;
initStringInfo(&query_string);
appendStringInfo( &query_string,
query_format,
pa_masking_expressions_for_table(relid,policy),
quote_identifier(get_namespace_name(get_rel_namespace(relid))),
quote_identifier(get_rel_name(relid))
);
ereport(DEBUG1, (errmsg_internal("%s",query_string.data)));
raw_parsetree_list = pg_parse_query(query_string.data);
return (Node *) linitial_node(RawStmt, raw_parsetree_list)->stmt;
}
/*
* Post-parse-analysis hook: mask query
* https://github.com/taminomara/psql-hooks/blob/master/Detailed.md#post_parse_analyze_hook
*/
static void
#if PG_VERSION_NUM >= 140000
pa_post_parse_analyze_hook(ParseState *pstate, Query *query, JumbleState *jstate)
#else
pa_post_parse_analyze_hook(ParseState *pstate, Query *query)
#endif
{
char * policy = NULL;
if (prev_post_parse_analyze_hook)
#if PG_VERSION_NUM >= 140000
prev_post_parse_analyze_hook(pstate, query, jstate);
#else
prev_post_parse_analyze_hook(pstate, query);
#endif
if (!IsTransactionState()) return;
if (!guc_anon_transparent_dynamic_masking) return;
policy = pa_get_masking_policy(GetUserId());
if (policy)
pa_rewrite(query,policy);
return;
}
/*
* pa_ProcessUtility_hook
* rewrites COPY statement for masked roles
*/
#if PG_VERSION_NUM >= 140000
static void
pa_ProcessUtility_hook( PlannedStmt *pstmt,
const char *queryString,
bool readOnlyTree,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
QueryCompletion *qc)
#elif PG_VERSION_NUM >= 130000
static void
pa_ProcessUtility_hook( PlannedStmt *pstmt,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
QueryCompletion *qc)
#else
static void
pa_ProcessUtility_hook( PlannedStmt *pstmt,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
char *completionTag)
#endif
{
char * policy = NULL;
if (IsTransactionState()) {
policy = pa_get_masking_policy(GetUserId());
if ( guc_anon_transparent_dynamic_masking && policy)
pa_rewrite_utility(pstmt,policy);
}
#if PG_VERSION_NUM >= 140000
if (prev_ProcessUtility_hook)
prev_ProcessUtility_hook(pstmt, queryString, readOnlyTree, context, params, queryEnv, dest, qc);
else
standard_ProcessUtility(pstmt, queryString, readOnlyTree, context, params, queryEnv, dest, qc);
#elif PG_VERSION_NUM >= 130000
if (prev_ProcessUtility_hook)
prev_ProcessUtility_hook(pstmt, queryString, context, params, queryEnv, dest, qc);
else
standard_ProcessUtility(pstmt, queryString, context, params, queryEnv, dest, qc);
#else
if (prev_ProcessUtility_hook)
prev_ProcessUtility_hook(pstmt, queryString, context, params, queryEnv, dest, completionTag);
else
standard_ProcessUtility(pstmt, queryString, context, params, queryEnv, dest, completionTag);
#endif
}
/*
* pa_rewrite
*/
static void
pa_rewrite(Query * query, char * policy)
{
/*
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("NOT IMPLEMENTED YET")));
*/
}
/*
* pa_rewrite_utility
* In a COPY statement, substitute a masked relation by its
* masking view
*/
static void
pa_rewrite_utility(PlannedStmt *pstmt, char * policy)
{
Node * parsetree = pstmt->utilityStmt;
int readonly_flags;
Assert(IsA(pstmt, PlannedStmt));
Assert(pstmt->commandType == CMD_UTILITY);
if ( IsA(parsetree, ExplainStmt)
|| IsA(parsetree, TruncateStmt)
)
{
ereport(ERROR,
( errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("role \"%s\" is masked",
GetUserNameFromId(GetUserId(), false)),
errdetail("Masked roles are read-only.")
));
}
if (IsA(parsetree, CopyStmt))
{
/* https://doxygen.postgresql.org/structCopyStmt.html */
CopyStmt * copystmt = (CopyStmt *) parsetree;
ereport(DEBUG1, (errmsg_internal("COPY found")));
if (! copystmt->is_from)
{
Oid relid;
/* replace the relation by the masking subquery */
relid=RangeVarGetRelid(copystmt->relation,AccessShareLock,false);
copystmt->relation = NULL;
copystmt->attlist = NULL;
copystmt->query=pa_masking_stmt_for_table(relid,policy);
ereport(DEBUG1, (errmsg_internal("COPY rewritten")));
}
}
}
/*
* pa_masking_expression_for_att
* returns the value for an attribute based on its masking rule (if any),
* which can be either:
* - the attribute name (i.e. the authentic value)
* - the function or value from the masking rule
* - the defaut value of the column
* - "NULL"
*/
static char *
pa_masking_value_for_att(Relation rel, FormData_pg_attribute * att, char * policy)
{
Oid relid;
ObjectAddress columnobject;
char * seclabel = NULL;
char * attname = (char *)quote_identifier(NameStr(att->attname));
// Get the masking rule, if any
relid=RelationGetRelid(rel);
ObjectAddressSubSet(columnobject, RelationRelationId, relid, att->attnum);
seclabel = GetSecurityLabel(&columnobject, policy);
// No masking rule found && Privacy By Default is off,
// the authentic value is shown
if (!seclabel && !guc_anon_privacy_by_default) return attname;
// A masking rule was found
if (seclabel && pg_strncasecmp(seclabel, "MASKED WITH FUNCTION", 20) == 0)
{
char * substr=malloc(strnlen(seclabel,PA_MAX_SIZE_MASKING_RULE));
strncpy(substr, seclabel+21, strnlen(seclabel,PA_MAX_SIZE_MASKING_RULE));
if (guc_anon_strict_mode) return pa_cast_as_regtype(substr, att->atttypid);
return substr;
}
if (seclabel && pg_strncasecmp(seclabel, "MASKED WITH VALUE", 17) == 0)
{
char * substr=malloc(strnlen(seclabel,PA_MAX_SIZE_MASKING_RULE));
strncpy(substr, seclabel+18, strnlen(seclabel,PA_MAX_SIZE_MASKING_RULE));
if (guc_anon_strict_mode) return pa_cast_as_regtype(substr, att->atttypid);
return substr;
}
// The column is declared as not masked, the authentic value is show
if (seclabel && pg_strncasecmp(seclabel,"NOT MASKED", 10) == 0) return attname;
// At this stage, we know privacy_by_default is on
// Let's try to find the default value of the column
if (att->atthasdef)
{
int i;
TupleDesc reldesc;
reldesc = RelationGetDescr(rel);
for(i=0; i< reldesc->constr->num_defval; i++)
{
if (reldesc->constr->defval[i].adnum == att->attnum )
return deparse_expression(stringToNode(reldesc->constr->defval[i].adbin),
NIL, false, false);
}
}
// No default value, NULL is the last possibility
return "NULL";
}
/*
* pa_masking_expressions_for_table
* returns the "select clause filters" that will mask the authentic data
* of a table for a given masking policy
*/
static char *
pa_masking_expressions_for_table(Oid relid, char * policy)
{
char comma[] = " ";
Relation rel;
TupleDesc reldesc;
StringInfoData filters;
int i;
rel = relation_open(relid, AccessShareLock);
initStringInfo(&filters);
reldesc = RelationGetDescr(rel);
for (i = 0; i < reldesc->natts; i++)
{
FormData_pg_attribute * a;
a = TupleDescAttr(reldesc, i);
if (a->attisdropped) continue;
appendStringInfoString(&filters,comma);
appendStringInfo( &filters,
"%s AS %s",
pa_masking_value_for_att(rel,a,policy),
(char *)quote_identifier(NameStr(a->attname))
);
comma[0]=',';
}
relation_close(rel, NoLock);
return filters.data;
}
/*
* anon_masking_expressions_for_table
* returns the "select clause filters" that will mask the authentic data
* of a table for a given masking policy
*/
Datum
anon_masking_expressions_for_table(PG_FUNCTION_ARGS)
{
Oid relid = PG_GETARG_OID(0);
char * masking_policy = text_to_cstring(PG_GETARG_TEXT_PP(1));
char comma[] = " ";
Relation rel;
TupleDesc reldesc;
StringInfoData filters;
int i;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL();
rel = relation_open(relid, AccessShareLock);
if (!rel) PG_RETURN_NULL();
initStringInfo(&filters);
reldesc = RelationGetDescr(rel);
for (i = 0; i < reldesc->natts; i++)
{
FormData_pg_attribute * a;
a = TupleDescAttr(reldesc, i);
if (a->attisdropped) continue;
appendStringInfoString(&filters,comma);
appendStringInfo( &filters,
"%s AS %s",
pa_masking_value_for_att(rel,a,masking_policy),
(char *)quote_identifier(NameStr(a->attname))
);
comma[0]=',';
}
relation_close(rel, NoLock);