60080 Commits

Author SHA1 Message Date
Andreas Karlsson
b11d51ca85 Use separate variables instead of an array when rotating files
This makes the code easier to read plus gives us things like letting
the compiler check for unused variables.
2025-04-07 11:14:28 +02:00
Andreas Karlsson
07756f2734 PG-1530 Fix off-by-one bug when determining which WAL key to use
The end LSN of the current buffer to decript was exclusive while the end
LSN of the key was inclusive which had led to confusion and an
off-by-one bug.

Also add a simple test case for the WAL encryption using the logical
decoding's test plugin.
2025-04-07 10:43:13 +02:00
Andreas Karlsson
d83fc90ac5 Use %m for error message in our modification of pg_regress
This follows the pattern in the rest of the PostgreSQL source code.
2025-04-07 10:30:57 +02:00
Andreas Karlsson
5514727353 PG-1416 Sign principal key info to protect against the wrong principal key
We already had protection against decrypting relation keys with the wrong
principal key but to properly protect us against new relation keys being
encrypted with the wrong principal key we need to also verify that the
principal key was correct when we fetch the principal key from the key
provider. We do so by signing the principal key info header of the key map
file using AES-128-GCM.

This way we cannot get a jumbled mess of relation keys encrypted with
multiple different principal keys.
2025-04-07 10:30:02 +02:00
Andreas Karlsson
e9d4927b2c Use correct type for fd 2025-04-04 21:21:20 +02:00
Andreas Karlsson
d788185440 PG-1437 Simplify pg_tde_read_one_map_entry2()
While this does not merge the two similar functions it removes a lot
of cruft from pg_tde_read_one_map_entry2() making the functions more
similar and the code easier to read.
2025-04-04 21:21:20 +02:00
Andreas Karlsson
36bc82d6c0 PG-1455 Change xlog base IVs to use addition
This way they can eventually be made more similar to using OpenSSL
for encrypting and decrypting the CTR stream.
2025-04-04 19:50:07 +02:00
Andreas Karlsson
72050ff258 PG-1455 Add random base numbers to IVs for WAL encryption
Same as last commit but for the WAL encryption.

Rewrote the calculation in a way gcc's vectorizer likes, as verified
with Godbolt. The code generated by clang is ok and branch free but it
fails to properly vectorize both before and after.
2025-04-04 19:50:07 +02:00
Andreas Karlsson
127c568623 PG-1455 Add random base numbers to IVs for relation data
We might as well increase the entropy by adding a random base value to
the IVs used used when encrypting relations. But since for now the pair
of key + IV is generated and used together it adds little extra security
over what we already have.

We add the IV with XOR since that is a cheap and easy operation which
uases no extra collissions.
2025-04-04 19:50:07 +02:00
Andreas Karlsson
7ea53b4176 PG-1455 Add field with random base IV to relation and WAL keys
For now we do not use this field but we plan to use it when encrypting
Wrelation data and WAL.
2025-04-04 19:50:07 +02:00
Andreas Karlsson
b223d2d254 PG-1437 Fix race condition in the event trigger
If we look up something about a relation in the command start event
trigger we need to hold onto that lock until the end of the command
and not release it so no other commd is able to change the data we
looked at between now and when the DDL command itself actually locks
the relation.
2025-04-04 19:48:52 +02:00
Andreas Karlsson
21a3794bbd PG-1437 Clean up definition of special TDE oids
The oids worked as-is, in general any reserved catalog oid which is
frefers to an object of another type will work as a special magical
oid. Here we decide to use tablespce oids for special database
values and a database oid, 1, for the relation value. But in the
latter case anything would have worked since we are guarnteed to
have no collissions due to the database oid not being and actual
database.

The old solution which use a mix worked too but this is more
consistent.
2025-04-03 12:09:05 +02:00
Andreas Karlsson
9ea86cc19f PG-1437 Document and pick a smaller initial DSA size for principal key cache
The previous 8192 * 100 initial allocation was arbitrary and bigger than
necessary. We pick something bassically as arbitrary but now a multiple of
the actual page size of DSAs, 4 KB.
2025-04-03 11:22:40 +02:00
Andreas Karlsson
ba763da204 PG-1437 Error out if another SMGR has been laoded 2025-04-03 11:22:40 +02:00
Andreas Karlsson
4de414881d PG-1437 Remove comment about checking for objects before deleting files
This comment is pretty strange since we actually do verify, badly, that
there are no encrypted tables.

Additionally we merge two small functions to make the code easier to
understand.
2025-04-03 11:22:40 +02:00
Andreas Karlsson
40bcad6c35 PG-1437 Just remove TODO from error from RAND_new()
Since this is a very unlikely error we do not need to do an extra work
to get a good error message.
2025-04-03 11:22:40 +02:00
Andreas Karlsson
bf4cc68833 PG-1437 Remove no longer relevant or usless TODO comments
- Some have Jira tickets for them
- Others are no longer relevant
- And yet others are just not helpful
2025-04-03 11:22:40 +02:00
Andreas Karlsson
02d0fb2256 PG-1437 Only allocate WAL encryption buffer when encryption enabled
There is no reason to waste memory when WAL encryption is not enabled
sd long as we do not support changing the WAL encryption status while
running. And if we add support for this we can revist this code.
2025-04-03 11:21:35 +02:00
Andreas Karlsson
d8cc666986 Remove key_type argument which is the same for all callers
All callers call pg_tde_free_key_map_entry() with MAP_ENTRY_VALID so
we can move it down to the place where it is used instead of passing
it as an argument.
2025-04-01 23:06:26 +02:00
Andreas Karlsson
36739e09e5 PG-1446 Add missing lock to pg_tde_create_wal_key()
Make sure that we lock the key file before adding a WAL key. It should
be harmless since this happens very early in the start but if we ever
add the ability to rotate WAL keys while running this will become an
issue.

Additionally we add asserts to make sure we hold the right lock level
when we open the key map file.
2025-03-31 22:10:01 +02:00
Andreas Karlsson
2c74cdc7b8 PG-1446 Do not realase locks too early
We need to hold onto the lock until after the last use of the
principal key we fetched from the cache.
2025-03-31 22:10:01 +02:00
Andreas Karlsson
25e78256df PG-1446 Move code for checking if we have a principal key
This is done to collect all logic in one place to make it easier to
verify the locking.
2025-03-31 22:10:01 +02:00
Andreas Karlsson
14e6bcd9d8 PG-1446 Move locking from the xlog code
This way we collect all locking in fewer places.
2025-03-31 22:10:01 +02:00
Andreas Karlsson
2d42a384fe PG-1446 Always open the key map file using the same helpers
This way we can easily in the future add asserts for that we hold the
right locks.
2025-03-31 22:10:01 +02:00
Andreas Karlsson
45323a5dfd Remove dead code
This code is not used by anything.
2025-03-31 22:10:01 +02:00
Andreas Karlsson
4421a7203c Make sure to free SSL context before raising error 2025-03-31 22:08:30 +02:00
Andreas Karlsson
05eaf2aef4 Clean up KMIP code a bit
Reduce the number of magic numbers and fix the array size.
2025-03-31 22:08:30 +02:00
Andreas Karlsson
6c376727cd Remove fprintf() left over from debugging 2025-03-31 22:08:30 +02:00
Andreas Karlsson
1d728b02ca PG-1437 Remove keyring_min.h
Now that we have fixed the conflict between PostgreSQL's and libkmip's
header files we no longer need this file.
2025-03-31 22:08:30 +02:00
Andreas Karlsson
fc6dc8bc28 PG-1347 Refactor KMIP code to avoid ugly tricks and pragmas
Instead of using ugly hacks like extern and defining kmip_ereoprt() in
a separate file we just move all code which requires the KMIP headers to
a separate file which does not use any PostgreSQL features.
2025-03-31 22:08:30 +02:00
Andreas Karlsson
7120bc5d04 Add a test case for recreating sequence storage 2025-03-31 21:46:16 +02:00
Andreas Karlsson
9dd690a72e Always raise error on failed key rotation
Since we only ever returned false in one odd internal error, i.e.
when something is wrong with the file system, we may as well remove
the returned boolean flag and jsut always raise an error when the
rotation fails.
2025-03-31 21:45:51 +02:00
Kai Wagner
0070c820dc Update contrib/pg_tde/documentation/docs/setup.md
Co-authored-by: Anastasia Alexandrova <anastasia.alexandrova@percona.com>
2025-03-31 20:01:51 +02:00
Kai Wagner
b9da089cec Update setup.md
The warning box wasn't shown correctly
2025-03-31 20:01:51 +02:00
Andreas Karlsson
a3a4cde15e PG-1416 Encrypt internal keys using AES-128-GCM
To protect us against people decrypting relation keys using the wrong
principal key we change to encrypting intenral keys using AES-128-GCM
which verifies that the data was encrypted with the same key as we
decrypt it with. Additionally we make sure to add some of the plain
test data when claculating the AEAD tag.
2025-03-31 15:48:14 +02:00
Andreas Karlsson
a15f69fcc4 PG-1416 Encrypt each internal key using and indvidual IV
To imrpove security plus prepare for implementing AES-GCM we save a
unique initialization vector per entry in the key map. This required
refactoring the API a bit but that is a nice thing for GCM too.
2025-03-31 15:48:14 +02:00
Andreas Karlsson
ac87addc37 PG-1416 Make assertions on length on encrypted data more local
This makes the code easier to follow by keeping things local. There
were already harmless misunderstandings of this API in the SMGR code.
2025-03-31 15:48:14 +02:00
Andreas Karlsson
9155b9e654 PG-1416 Remove unnecessary function pg_tde_write_map_entry()
This function just added another layer of function calls for no gain.
2025-03-31 15:48:14 +02:00
Andreas Karlsson
5de0f971c3 Use a separate define for the length of principal keys
We should probably unify and clean up this further but Whatever we do we
should not use the INTERNAL_KEY_LEN define.
2025-03-28 01:35:04 +01:00
Andreas Karlsson
95bf7723e7 Use names for fields in struct literal
The PostgreSQL project has supported this for quite some time now.
2025-03-27 19:42:16 +01:00
Andreas Karlsson
e96e9b738c PG-1491 Simplify cache lookup logic to make it easier to reason about
This caused our code to act in a flaky way when looking up the SMGR
key for relations without storage, e.g. with InvalidRelFileNumber.
2025-03-27 19:42:00 +01:00
Andreas Karlsson
56f9a8ecda PG-1419 Make sure pg_tde_is_encrypted() returns NULL on paritioned tables
Since partitioned tables, and indexes, lack storage the "is encrypted"
property is not relevant to them because encryption is done at the SMGR
level. Therefore we should either throw an error or return NULL, and
here we choose to return NULL to make the function easier to work with.
2025-03-27 19:42:00 +01:00
Andreas Karlsson
27b88e1334 Remove cached OpenSSL context from internal keys
Since only WAL encryption use the cached context it should not be part
of every internal key. Instead store it in the WAL decryption key cache
and the backend global state for WAL encryption.
2025-03-27 19:41:37 +01:00
Andreas Karlsson
1dc74bdfac Remove unused field from the TDE file header
The userId field of the principal key info has never been used since
it was introduced in commit 210c95cf00cacc3304321950279201406708d09d
so we can safely remove it.
2025-03-27 19:41:11 +01:00
Anastasia Alexandrova
2b94887a34
PG-1500 Extended mlock description (#171)
PG-1500 Extended mlock description


---------

Co-authored-by: Andrew Pogrebnoi <absourd.noise@gmail.com>
2025-03-27 17:42:01 +01:00
Anastasia Alexandrova
86af45ed24
Doc improvements for RC release (#167)
Doc improvements for RC release

Fixed links, wording
2025-03-27 13:32:32 +01:00
Zsolt Parragi
ef17134167
Documentation updates for variables/tools and a few other things (#108)
* Documentation improvements

* Added new pages to nav tree, improved readability

---------

Co-authored-by: Anastasia Alexadrova <anastasia.alexandrova@percona.com>
Co-authored-by: Andrew Pogrebnoi <absourd.noise@gmail.com>
2025-03-27 11:15:40 +01:00
Andreas Karlsson
d2231f9b98 Unlock pages in the key cache after clearing them
While it is not necessarily much memory it is bad manners to lock more
pages in memory than we need to.
2025-03-26 21:51:50 +01:00
Zsolt Parragi
e066375635
Merge pull request #166 from jeltz/tde/merge-17.4.1
Merge tag 'release-17.4.1' into TDE_REL_17_STABLE
2025-03-26 20:50:10 +00:00
Andreas Karlsson
3a6422b9f9 Merge tag 'release-17.4.1' into TDE_REL_17_STABLE
Conflicts

- contrib/pg_tde/Makefile
- contrib/pg_tde/expected/change_access_method.out
- contrib/pg_tde/meson.build
- contrib/pg_tde/src/access/pg_tde_tdemap.c
- contrib/pg_tde/src/access/pg_tde_xlog_encrypt.c
- contrib/pg_tde/src/keyring/keyring_vault.c
- contrib/pg_tde/src/pg_tde.c
- contrib/pg_tde/src/pg_tde_alter_key_provider.c
- contrib/pg_tde/src/pg_tde_event_capture.c
- src/bin/Makefile
- src/bin/meson.build

Deleted in TDE_REL_17_STABLE

- contrib/pg_tde/expected/change_access_method_basic.out
- contrib/pg_tde/expected/vault_v2_test_basic.out
- contrib/pg_tde/sql/change_access_method.inc
- contrib/pg_tde/sql/vault_v2_test.inc
- src/bin/pg_tde_change_key_provider/Makefile
- src/bin/pg_tde_change_key_provider/meson.build
2025-03-26 13:36:01 +01:00