18456 Commits

Author SHA1 Message Date
Gabor Mezei
3b0c371c04
Add allocate and copy style output buffer handling
Add a new macro `LOCAL_OUTPUT_ALLOC_WITH_COPY` to support the output buffer
handling of the multipart operations like `psa_cipher_update`. This will
allocate a local buffer and copy the content of the original buffer.

Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2024-01-24 17:28:33 +01:00
David Horstmann
001ae3349d
Merge pull request #1143 from davidhorstmann-arm/memory-poisoning-runtime-enable-2.28
[Backport 2.28] Enable and disable memory poisoning at runtime
2024-01-24 14:46:52 +00:00
David Horstmann
7dfb6121fc Remove _Thread_local variable for 2.28
We do not intend to support multithreaded testing in 2.28, so
introducing a C11 feature here is an unnecessary burden.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-23 15:35:20 +00:00
David Horstmann
e7a84a67f4
Merge pull request #1135 from gilles-peskine-arm/psa-buffers-generate-wrappers-introduce-2.28
Backport 2.28: PSA wrappers with buffer poisoning for psa_cipher_encrypt
2024-01-19 15:13:02 +00:00
David Horstmann
e7bfbc27bf Add underflow check to UNPOISON counter decrement
Make sure that extra UNPOISON calls do not cause the poisoning counter
to underflow and wrap around.

Memory that is unpoisoned multiple times should remain unpoisoned.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-17 15:37:54 +00:00
David Horstmann
1b421b1005 Separate memory poisoning tests from generic ASan
Some platforms may support ASan but be C99-only (no C11 support).
These platforms will support ASan metatests but not memory poisoning,
which requires C11 features.

To allow for this, create a separate platform requirement, "poison",
in metatest.c to distinguish generic ASan metatests from ones that
require suppport for memory poisoning.

In practice our platforms support both, so run "poison" tests in
the same all.sh components where we run "asan" ones.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-17 15:37:54 +00:00
David Horstmann
6de5828288 Change memory poisoning flag to a count
This allows unusually-nested memory poisoning to work correctly, since
it keeps track of whether any buffers are still poisoned, rather than
just disabling poisoning at the first call to the UNPOISON() macro.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-17 15:37:54 +00:00
Gilles Peskine
265415afa1
Merge pull request #1146 from yanesca/update-Marvin-changelog-backport
Update Marvin fix Changelog entry - BACKPORT
2024-01-16 11:19:27 +01:00
Janos Follath
86bbb13779 Update Marvin fix Changelog entry
Upon further consideration we think that a remote attacker close to the
victim might be able to have precise enough timing information to
exploit the side channel as well. Update the Changelog to reflect this.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-16 08:56:42 +00:00
David Horstmann
d074a5a147 Only run memory poisoning metatests when poisoning
When we cannot memory poison due to platform constraints, do not attempt
to run memory poisoning metatests (but still run other ASan metatests).

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-12 15:47:49 +00:00
David Horstmann
756b4dcfa4 Use thread-local flag to enable memory poisoning
Allow memory poisoning to be enabled and disabled at runtime using a
thread-local flag. This allows poisoning to be disabled whenever a PSA
function is called but not through the test wrappers, removing false
positive use-after-poisons.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-12 15:47:49 +00:00
Janos Follath
8e72c8f154
Merge pull request #1136 from yanesca/fix-marvin-attack-backport
Fix for the Marvin attack - BACKPORT
2024-01-10 12:16:26 +00:00
Janos Follath
ce4a3c25fe Add new internal header to visualc project
Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-10 08:54:17 +00:00
Janos Follath
8b736290ad Fix 'missing prototype' warnings
Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-09 09:37:06 +00:00
Janos Follath
6f499b7ed4 Align Montgomery init with development
The signature and naming of the Montgomrey initialisation function in
development and in the LTS was different. Align them for easier
readability and maintenance.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-09 09:28:48 +00:00
Gilles Peskine
f81f191770 Fix parsing of C line comments
Fix // comments stopping on 'n' instead of newlines. Also allow
backslash-newline in // comments.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-08 21:06:47 +01:00
Janos Follath
d50f7a7a17 Add Changelog for the Marvin attack fix
Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-08 15:28:38 +00:00
Janos Follath
54b2edaa8b Add warning for PKCS 1.5 decryption
Any timing variance dependant on the output of this function enables a
Bleichenbacher attack. It is extremely difficult to use safely.

In the Marvin attack paper
(https://people.redhat.com/~hkario/marvin/marvin-attack-paper.pdf) the
author suggests that implementations of PKCS 1.5 decryption that don't
include a countermeasure should be considered inherently dangerous.

They suggest that all libraries implement the same countermeasure, as
implementing different countermeasures across libraries enables the
Bleichenbacher attack as well.

This is extremely fragile and therefore we don't implement it. The use
of PKCS 1.5 in Mbed TLS implements the countermeasures recommended in
the TLS standard (7.4.7.1 of RFC 5246) and is not vulnerable.

Add a warning to PKCS 1.5 decryption to warn users about this.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-08 15:19:39 +00:00
Janos Follath
24bb226232 Extend blinding to RSA result check
Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-08 15:19:11 +00:00
Janos Follath
8b246b3d16 Make RSA unblinding constant flow
Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-08 15:12:38 +00:00
Janos Follath
f9cc4763f1 Move some bignum functions to internal header
We will need a couple of low level functions to implement safe
unblinding in RSA.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-08 14:15:08 +00:00
Janos Follath
404160a533 Move calculating RR into a separate function
So far we needed it only locally here, but we will need calculating RR
for safe unblinding in RSA as well.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2024-01-08 13:53:33 +00:00
Gilles Peskine
a980aa0894 Disable PSA wrappers psa_collect_statuses builds
`psa_collect_statuses.py` runs `make RECORD_PSA_STATUS_COVERAGE_LOG=1`,
which builds with `RECORD_PSA_STATUS_COVERAGE_LOG`. In this mode, the build
includes wrappers for PSA functions, which conflict with the newly
introduced wrappers that are enabled whenever `MBEDTLS_TEST_HOOKS` is
enabled. In the future, the collect-statuses mechanism should use the new
generic wrapper mechanism. For the time being, keep the old wrappers and
avoid the new wrappers when doing the collect-statuses build.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 21:07:35 +01:00
Gilles Peskine
b3d457ce2f PSA wrappers: don't poison buffers when buffer copying is disabled
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 20:33:29 +01:00
Gilles Peskine
7c7b7d5db3 Add review exception warning
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 17:28:59 +01:00
Gilles Peskine
e5ebe5e1f5 Remove the manually written poisoning wrapper
This fixes the build with ASan + MBEDTLS_TEST_HOOKS.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 17:11:54 +01:00
Gilles Peskine
42fa8ea38c Update generated PSA wrappers
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:59:28 +01:00
Gilles Peskine
c8b22d0713 Generated PSA wrappers: poison/unpoison buffer parameters
For now, only instrument the one function for which buffer copying has been
implemented, namely `psa_cipher_encrypt`.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:58:23 +01:00
Gilles Peskine
50580a51ff Enable generated PSA wrappers
Code in unit tests (`tests/suites/*.function`) and in test support
code (`tests/src/**.c`) will now go through the wrapper functions when they
call a PSA API function and `MBEDTLS_TEST_HOOKS` is enabled.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:47:15 +01:00
Gilles Peskine
b7119c527c Declare the outputs from generate_psa_wrappers.py as generated files
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:47:15 +01:00
Gilles Peskine
1b2aec260e Update generated Visual Studio project
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:47:15 +01:00
Gilles Peskine
d5e5e6dbae Commit generated PSA wrappers
Commit files generated by `tests/scripts/generate_psa_wrappers.py`. As of
this commit, the new code is neither useful (the wrappers just call the
underlying functions) nor used (the wrapper functions are not called from
anywhere). This will change in subsequent commits.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:42:40 +01:00
Gilles Peskine
17a14f1711 Adapt wrapper generators from 3.5 to 2.28
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:41:30 +01:00
Gilles Peskine
8519dc9c7e PSA wrapper generator
The new script `tests/scripts/generate_psa_wrappers.py` generates the
implementation of wrapper functions for PSA API functions, as well as a
header that defines macros that redirect calls to the wrapper functions. By
default, the wrapper functions just call the underlying library function.
With `--log`, the wrapper functions log the arguments and return values.

This commit only introduces the new script. Subsequent commits will
integrate the wrappers in the build.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 16:38:17 +01:00
Gilles Peskine
61a852216e Guard the macro definition
It doesn't make sense to define a macro expanding to a non-existent function.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 14:41:10 +01:00
Gilles Peskine
15d32bb60f C function wrapper generator
The Base class generates trivial wrappers that just call the underlying
function. It is meant as a base class to construct useful wrapper generators.

The Logging class generates wrappers that can log the inputs and outputs to
a function.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 14:39:40 +01:00
Gilles Peskine
cedb112359 Python module to parse function declarations from a header file
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-04 14:39:34 +01:00
David Horstmann
4b987b434d
Merge pull request #1131 from davidhorstmann-arm/prototype-single-fn-copytesting-2.28
[Backport 2.28] Prototype poisoning testing with a single function
2023-12-20 17:07:33 +00:00
David Horstmann
3ce3e7a193 Add new config option to generated files
Add MBEDTLS_PSA_COPY_CALLER_BUFFERS to query_config.c,
version_features.c and mbedTLS.vcxproj via their respective scripts.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 15:23:04 +00:00
David Horstmann
5d05b47844 Only poison memory when buffer copying is enabled
Make sure that we don't enable memory poisoning when
MBEDTLS_PSA_COPY_CALLER_BUFFERS is disabled.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
dda52ae1c6 Add all.sh coponent to test with copying disabled
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
823f9a9e18 Invert note about buffer overlap support
When MBEDTLS_PSA_COPY_CALLER_BUFFERS is disabled, it causes overlap to
not be supported.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
4dc176a430 Add missing newline at end of file
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
cb229db971 Add extra MBEDTLS_PSA_CRYPTO_C guard for header
Do not include psa_memory_poisoning_wrappers.h unless
MBEDTLS_PSA_CRYPTO_C is set.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
fcad4d5939 Remove accidental addition of MBEDTLS_TEST_HOOKS
Remove MBEDTLS_TEST_HOOKS from the default config, to which it was
erroneously added.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
ffcc769184 Improve guards around memory poisoning setup
We should not setup or teardown test hooks when we do not have
MBEDTLS_PSA_CRYPTO_C.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
b80e35a54a Tweak the behaviour of copy handling macros
Specifically:
* Move the creation of the pointer to the copied buffer into the
  DECLARE() macro, to solve warnings about potentially skipping
  initialization.
* Reorder the arguments of the FREE() macro - having a different order
  made it confusing, so keep the order the same throughout.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
7cb734370a Add missing newline at end of file
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
93b2dc7853 Add note about support for buffer overlap
Note that enabling MBEDTLS_PSA_COPY_CALLER_BUFFERS allows full buffer
overlap support, whereas without it, overlap support is reduced to that
documented in the function descriptions.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00
David Horstmann
9c97fda0ab Move wrapper include to psa_crypto_helpers.h
This makes memory poisoning wrappers available to (almost) all tests.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 14:49:41 +00:00