28603 Commits

Author SHA1 Message Date
Dave Rodgman
64172bc2ec Use constant_time module from cmac
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
2024-01-29 12:41:02 +00:00
David Horstmann
9c5c9c351d
Merge pull request #1141 from davidhorstmann-arm/memory-poisoning-runtime-enable
Enable and disable memory poisoning at runtime
2024-01-24 14:46:43 +00:00
David Horstmann
a0fdc262a1
Merge pull request #1145 from gilles-peskine-arm/psa-buffers-generate-wrappers-introduce
PSA wrappers with buffer poisoning for psa_cipher_encrypt
2024-01-19 15:12:59 +00:00
David Horstmann
d3efb92922 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:27:50 +00:00
David Horstmann
9de6edd462 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 14:53:08 +00:00
David Horstmann
fad038c501 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 14:23:20 +00:00
David Horstmann
c2ab398d01 Request C11 in CMake (but only for tests)
Set the C_STANDARD property on the mbedtls_test target to 11.
This requests C11 for the tests only.

If C11 is not supported the build will not fail, since
C_STANDARD_REQUIRED is not set, and memory poisoning will be disabled
by a preprocessor check on __STDC_VERSION__.

Additionally, reintroduce previous C99 enforcement on the rest of the
library.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-17 14:02:40 +00:00
Gilles Peskine
afd8154b1e
Merge pull request #1139 from yanesca/update-Marvin-changelog
Update Marvin fix Changelog entry
2024-01-16 11:19:20 +01:00
Gilles Peskine
89b50a7cb4 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-15 19:30:54 +01:00
Gilles Peskine
4411c9c1f8 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-15 19:30:48 +01:00
Gilles Peskine
88385c2f74 PSA wrappers: don't poison buffers when buffer copying is disabled
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-15 19:30:29 +01:00
Gilles Peskine
a1871f318b Add review exception warning
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-15 19:30:28 +01:00
Gilles Peskine
8e7960b685 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-15 19:22:15 +01:00
Gilles Peskine
90d14d7fc2 Update generated PSA wrappers
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-15 19:21:36 +01:00
Gilles Peskine
4adacac142 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-15 19:20:59 +01:00
Gilles Peskine
2b106dec8b 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-15 19:16:16 +01:00
Gilles Peskine
e00150df4a Declare the outputs from generate_psa_wrappers.py as generated files
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-15 19:16:13 +01:00
Gilles Peskine
6e4332cc24 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.

This is a deviation from our normal practice of generating
configuration-independent platform-independent files as part of the build in
the development branch. The PSA test wrappers will be committed to the
repository for some time for two reasons:

* In the short term, we will review the generated code but not fully review
  the generator script.
* The build scripts cannot yet accommodate a generated header file.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-15 19:12:05 +01:00
Gilles Peskine
5294bb347c 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-15 19:03:08 +01:00
Gilles Peskine
f838eb2259 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-15 19:03:08 +01:00
Gilles Peskine
d022093ea6 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-15 19:03:08 +01:00
Gilles Peskine
00ed38c25b Python module to parse function declarations from a header file
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-01-15 19:03:08 +01:00
David Horstmann
9432e64933 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:13:54 +00:00
David Horstmann
8174478f82 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:13:54 +00:00
David Horstmann
8ba3f68561 Stop enforcing C99 in CMakeLists.txt
This allows us to sometimes use C11 features.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2024-01-12 15:13:54 +00:00
Janos Follath
0b39d1ed7d 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-11 14:24:02 +00:00
Janos Follath
890c74447d
Merge pull request #1123 from yanesca/fix-marvin-attack
Fix for the Marvin attack
2024-01-10 12:16:57 +00:00
Janos Follath
2125443aef 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>
2023-12-29 11:14:58 +00:00
Janos Follath
8c4cabf6aa Fix typo
Signed-off-by: Janos Follath <janos.follath@arm.com>
2023-12-27 10:47:21 +00:00
Janos Follath
d83dc85a10 RSA: improve readability
Signed-off-by: Janos Follath <janos.follath@arm.com>
2023-12-27 10:44:36 +00:00
Janos Follath
c762521e73 RSA: remove unneeded temporaries
Signed-off-by: Janos Follath <janos.follath@arm.com>
2023-12-27 10:33:00 +00:00
Janos Follath
dad6d66661 RSA: document Montgomery trick in unblind
Signed-off-by: Janos Follath <janos.follath@arm.com>
2023-12-27 10:22:59 +00:00
David Horstmann
132eddd57d
Merge pull request #1127 from davidhorstmann-arm/prototype-single-fn-copytesting
Prototype poisoning testing with a single function
2023-12-20 17:07:29 +00:00
David Horstmann
2de5abf284 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 11:26:40 +00:00
David Horstmann
0f06bde936 Add all.sh coponent to test with copying disabled
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-20 11:10:16 +00:00
David Horstmann
c09f36dd1b 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 10:57:43 +00:00
David Horstmann
6d43e6d76a Add missing newline at end of file
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-18 15:58:17 +00:00
David Horstmann
83ece2fe49 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-18 15:34:06 +00:00
David Horstmann
d20ffaf06f 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-15 19:05:40 +00:00
David Horstmann
666845322c 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-15 18:35:09 +00:00
David Horstmann
62a56d966d 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-14 18:16:02 +00:00
David Horstmann
853f9f97eb Add missing newline at end of file
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-14 17:17:20 +00:00
David Horstmann
0d405d8bb9 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-14 16:20:18 +00:00
David Horstmann
7de0928fd1 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-14 16:09:07 +00:00
David Horstmann
b489257a0b Move test hook setup and teardown to helpers.c
Setup and teardown test hooks during full test platform setup.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-14 16:09:07 +00:00
David Horstmann
a7cde5d296 Move test hook setup functions into a C file
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-14 16:09:07 +00:00
David Horstmann
36df4b24d4 Redesign local copy handling macros
* Separate initialization from allocation.
* Rewrite description of macros to fit the new interface.
* Use a longer name to store the local copy objects, to reduce the risk
  of shadowing.
* Use different names for the original and the copy. Append the suffix
  '_external' to the original argument and use the previous name
  for the copy.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-14 16:09:00 +00:00
David Horstmann
5a945f584e Put local output status in scope
This means that a unique name is no longer needed.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-13 14:09:08 +00:00
David Horstmann
d57c0731c9 Remove spaces around token-pasting macro operator
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-13 14:03:40 +00:00
David Horstmann
bf4ec79085 Make return statuses unique in FREE_LOCAL_OUTPUT()
Previously the return from psa_crypto_local_output_free() had a fixed
name, which meant that multiple outputs would cause redefinitions of the
same variable.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00