Skip to content

Commit 16b4640

Browse files
committed
Fix policy name comparison in remove_policies
The policy name from the user was compared without first turning it into a proper C string, so the comparison could read past the input. The retention branch also matched on a prefix only, which let names like "policy_retention_extra" remove the retention policy. Convert the input to a C string and require an exact match for all three policy names.
1 parent e73b43e commit 16b4640

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

.unreleased/pr_9707

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #9707 Fix policy name comparison in remove_policies

tsl/src/bgw_policy/policies_v2.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ policies_remove(PG_FUNCTION_ARGS)
393393

394394
for (i = 0; i < npolicies; i++)
395395
{
396-
char *curr_policy = VARDATA(policy[i]);
396+
char *curr_policy = TextDatumGetCString(policy[i]);
397397

398398
if (pg_strcasecmp(curr_policy, POLICY_REFRESH_CAGG_PROC_NAME) == 0)
399399
{
@@ -403,16 +403,15 @@ policies_remove(PG_FUNCTION_ARGS)
403403
{
404404
success = policy_compression_remove_internal(cagg_oid, if_exists);
405405
}
406-
else if (pg_strncasecmp(curr_policy,
407-
POLICY_RETENTION_PROC_NAME,
408-
strlen(POLICY_RETENTION_PROC_NAME)) == 0)
406+
else if (pg_strcasecmp(curr_policy, POLICY_RETENTION_PROC_NAME) == 0)
409407
{
410408
success = policy_retention_remove_internal(cagg_oid, if_exists);
411409
}
412410
else
413411
{
414412
ereport(NOTICE, (errmsg("No relevant policy found")));
415413
}
414+
pfree(curr_policy);
416415
if (!success)
417416
{
418417
++failures;

tsl/test/expected/cagg_policy.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,25 @@ NOTICE: No relevant policy found
523523
-----------------
524524
f
525525

526+
-- Short and prefix-matching policy names must not match a real policy
527+
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'a');
528+
NOTICE: No relevant policy found
529+
remove_policies
530+
-----------------
531+
f
532+
533+
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'policy_retention_extra');
534+
NOTICE: No relevant policy found
535+
remove_policies
536+
-----------------
537+
f
538+
539+
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'p');
540+
NOTICE: No relevant policy found
541+
remove_policies
542+
-----------------
543+
f
544+
526545
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'policy_refresh_continuous_aggregate', 'policy_compression');
527546
remove_policies
528547
-----------------

tsl/test/sql/cagg_policy.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false);
254254
-- Code coverage: incorrect name of policy
255255
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'refresh_policy');
256256

257+
-- Short and prefix-matching policy names must not match a real policy
258+
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'a');
259+
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'policy_retention_extra');
260+
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'p');
261+
257262
SELECT timescaledb_experimental.remove_policies('max_mat_view_date', false, 'policy_refresh_continuous_aggregate', 'policy_compression');
258263
SELECT timescaledb_experimental.show_policies('max_mat_view_date');
259264

0 commit comments

Comments
 (0)