28584 Commits

Author SHA1 Message Date
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
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
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
David Horstmann
3e72db4f51 Improve FREE_LOCAL_INPUT() and FREE_LOCAL_OUTPUT()
* Set swapped pointers to NULL when the buffers are freed.
* Change example name <buffer> to <input> and <output> to reduce
  confusion.
* Document assumptions of FREE_LOCAL_ macros.
* Add comment on error case in FREE_LOCAL_OUTPUT(), explaining why it's
  okay to mask the existing status code.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
b7a5b6ed35 Add comment explaining the purpose of header
Explain why we have the wrappers in psa_memory_poisoning_wrappers.h

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
00d7a04b82 Add more information to comment on test hooks
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
bbd44a767f Add missing license header
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
d596862418 Remove unnecessary include directory from CMake
Since psa_crypto.c does not include tests/include/test/memory.h, we do
not need the tests/include include path.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
513101b00f Add MBEDTLS_PSA_COPY_CALLER_BUFFERS config option
This allows us to entirely remove copying code, where the convenience
macros are used for copying.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
e9a88ab0d5 Use macros to manage buffer copies
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
410823730b Remove write check in driver wrappers tests
This check is intended to ensure that we do not write intermediate
results to the shared output buffer. This check will be made obselete
by generic memory-poisoning-based testing for all functions.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
e138dce329 Change to use test-hook-based approach
Since we are applying hooks transparently to all tests, we cannot setup
and teardown test hooks in the tests. Instead we must do this in the
test wrappers which are used to pre-poison and unpoison memory.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
c6977b4899 Copy input and output in psa_cipher_encrypt()
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
8f35a4f003 Create memory poisoning wrapper for cipher encrypt
Use the preprocessor to wrap psa_cipher_encrypt in a wrapper that
poisons the input and output buffers.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
38e4e9c499 Add explicit UNPOISON calls to memory tests
These are needed to allow them to operate on buffer copies without
triggering ASan use-after-poison detection.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
372b8bb6c5 Add memory poisoning hooks
Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-12-11 17:58:56 +00:00
David Horstmann
dbbfdabfd8
Merge pull request #1121 from gilles-peskine-arm/psa-buffers-test-poison
Memory poisoning function for Asan
2023-12-11 17:56:13 +00:00
Janos Follath
f7f88d6443 Fix style
Signed-off-by: Janos Follath <janos.follath@arm.com>
2023-12-08 08:41:08 +00:00
Janos Follath
8209ff335e Make local function static
Signed-off-by: Janos Follath <janos.follath@arm.com>
2023-12-08 08:41:08 +00:00
Janos Follath
aa5cc7b930 Add Changelog for the Marvin attack fix
Signed-off-by: Janos Follath <janos.follath@arm.com>
2023-12-08 08:41:08 +00:00
Gilles Peskine
a7d5662f15 Allow PSA test cases to depend on test conditions
In particular, this allows MBEDTLS_TEST_HOOKS.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 20:49:24 +01:00
Gilles Peskine
6f8ca29ce4 Use the existing definition of __func__
Now that library is in the include path when compiling metatest.c, don't
duplicate the definition of __func__ from library/common.h.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 19:42:43 +01:00