601 Commits

Author SHA1 Message Date
Gilles Peskine
7d68a1954c Protect against compiler optimizations
GCC 5.4 optimized the write after poisoning (the surprising thing is that
11.4 doesn't).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 20:51:56 +01:00
Gilles Peskine
0c7d3eda0a Memory poisoning: meta-test writes as well as reads
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 20:51:56 +01:00
Gilles Peskine
d751406f8d Port to platforms where printf doesn't have %zu
Reuse the existing abstraction from include/mbedtls/debug.h.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 20:51:56 +01:00
Gilles Peskine
ac8cd66bda Fix MSVC build failure
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 20:51:56 +01:00
Gilles Peskine
962c5da52a Fix memory poisoning with Asan on arbitrary byte boundaries
Asan poisons memory with an 8-byte granularity. We want to make sure that
the whole specified region is poisoned (our typical use case is a
heap-allocated object, and we want to poison the whole object, and we don't
care about the bytes after the end of the object and up to the beginning of
the next object). So align the start and end of the region to (un)poison to
an 8-byte boundary.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 20:51:56 +01:00
Gilles Peskine
071d144a2e Add memory poisoning framework
While an area of memory is poisoned, reading or writing from it triggers a
sanitizer violation.

Implemented for ASan.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-29 20:51:56 +01:00
Gilles Peskine
e9616fdbc9 Fix the build with gcc-12 -Wuse-after-free
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-22 17:37:57 +01:00
Gilles Peskine
e00255c41c Improve explanations of what bad thing a metatest does
Especially clarify the situation with respect to mutex usage.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:52 +01:00
Gilles Peskine
96c87c4e7b Uniformly use MBEDTLS_THREADING_C guards
Since the code compiles with MBEDTLS_THREADING_C, not just with
MBEDTLS_THREADING_PTHREAD, use MBEDTLS_THREADING_C as the guard. The runtime
behavior is only as desired under certain conditions that imply
MBEDTLS_THREADING_PTHREAD, but that's fine: no metatest is expected to pass
in all scenarios, only under specific build- and run-time conditions.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:52 +01:00
Gilles Peskine
c41133b90d Add documentation
Explain the goals of metatests, how to write them, and how to read their
output.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:52 +01:00
Gilles Peskine
efc57cabd0 Uninitialized read: make the pointer non-volatile rather than the buffer
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:52 +01:00
Gilles Peskine
226f1bc080 More consistent usage of volatile
Fix MSVC warning C4090.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:52 +01:00
Gilles Peskine
53833516bf Strengthen against possible compiler optimizations
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:52 +01:00
Gilles Peskine
2c04f57ffc programs/test/metatest indirectly includes library/common.h
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:50 +01:00
Gilles Peskine
e38eb79e89 Add metatests for mutex usage
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
db2b5c94a6 Don't use %llx in printf
We still do MinGW builds on our CI whose printf doesn't support it!

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
6b3b7f832b Fix cast from pointer to integer of different size
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
64c6c80e28 Don't cast a function pointer to a data pointer
That's nonstandard. Instead, convert to an integer.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
6289ccc006 Use casts when doing nonstandard pointer conversions
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
967714d8e7 Strengthen against Clang optimizations
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
970584f32f Metatests for basic Asan and Msan features
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
21d8d59ce2 Metatests for null pointer dereference
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
30380dacdb Validate that test_fail causes a test failure
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:23 +01:00
Gilles Peskine
c33940da51 Add a metatest program
This program can be used to validate that things that should be detected as
test failures are indeed caught, either by setting the test result to
MBEDTLS_TEST_RESULT_FAILED or by aborting the program.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-11-16 15:56:22 +01:00
Dave Rodgman
7ff7965561 Update headers
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
2023-11-03 12:04:52 +00:00
Gilles Peskine
7e36ad1f5c
Merge pull request #7983 from gilles-peskine-arm/sample-program-demos-2.28
Backport 2.28: Run sample program demo scripts in all.sh
2023-10-19 15:11:00 +00:00
Yanray Wang
c55060d42a benchmark: improve code readability
Signed-off-by: Yanray Wang <yanray.wang@arm.com>
2023-10-10 10:49:32 +08:00
Yanray Wang
c96db3bf25 benchmark: add AES_CFB8
Signed-off-by: Yanray Wang <yanray.wang@arm.com>
2023-10-10 10:49:32 +08:00
Yanray Wang
b382c2b0e7 benchmark: add AES_CFB128
Signed-off-by: Yanray Wang <yanray.wang@arm.com>
2023-10-10 10:49:32 +08:00
Gilles Peskine
e008890afa Update generated files
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-09-07 19:20:34 +02:00
Gilles Peskine
7fbbb61238 Merge remote-tracking branch 'mbedtls-2.28' into calloc-also-zeroizes-2-28 2023-08-21 17:25:47 +02:00
Gowtham Suresh Kumar
34d8bd37d9 Fix warnings from clang-16
Running clang-16 on mbedtls reports warnings of type "-Wstrict-prototypes".
This patch fixes these warnings by adding void to functions with no
arguments. The generate_test_code.py is modified to insert void into test
functions with no arguments in *.function files.

Signed-off-by: Gowtham Suresh Kumar <gowtham.sureshkumar@arm.com>
2023-07-27 00:03:21 +01:00
Gilles Peskine
f5a2ce056c Skip dlopen demo in static builds
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-07-25 21:54:36 +02:00
Gilles Peskine
7f2b98c162 Use demo_common.sh in dlopen test script
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-07-25 21:54:36 +02:00
Andrzej Kurek
f35490e7af Move the overallocation test to test suites
This way the compiler does not complain about
an overly large allocation made.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
2023-07-14 10:13:31 -04:00
Andrzej Kurek
8ca66a0795 Add an mbedtls_calloc(SIZE_MAX/2, SIZE_MAX/2) test
It should return NULL and not a valid pointer.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
2023-07-14 10:13:26 -04:00
Andrzej Kurek
c8bf05954b Add a calloc selftest for more than a page
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
2023-07-14 09:56:12 -04:00
Andrzej Kurek
ba16859cc6 Documentation and cosmetic fixes
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
2023-07-14 09:56:02 -04:00
Andrzej Kurek
5ffea9dc77 Rework the calloc buffer initialization test in selftest.c
This way it's more in line with development.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
2023-07-14 09:53:08 -04:00
David Horstmann
39aee1214a Fix demo scripts for out-of-tree builds
Allow demo scripts to be run from the build directory for out-of-tree
builds.

If the executable is not found in the source tree then search in the
current directory in case the script is being run from a build
directory.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
2023-05-22 12:23:59 +01:00
Andrzej Kurek
97cbff7c0d Add a test for calloc zeroization
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
2023-04-25 05:51:34 -04:00
Tom Cosgrove
9149e12767 Stop selftest hanging when run on CI
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2023-03-18 14:49:07 +00:00
Aditya Deshpande
56d90038fd Fix more argc checks
Signed-off-by: Aditya Deshpande <aditya.deshpande@arm.com>
2023-02-05 15:47:45 +00:00
Gilles Peskine
ec03b0431d Update generated files
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-01-11 14:52:37 +01:00
Gilles Peskine
1b6c09a62e Switch to the new code style
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2023-01-11 14:52:35 +01:00
Tom Cosgrove
49f99bc3db Fix typos prior to release
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-12-04 16:44:21 +00:00
Andrzej Kurek
b3b0ec9bed Add missing dependencies
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
2022-10-20 06:22:13 -04:00
Gilles Peskine
093a4aa51b Remove useless platform macro redefinitions: automatic part
Some source files had code to set mbedtls_xxx aliases when
MBEDTLS_PLATFORM_C is not defined. These aliases are defined unconditionally
by mbedtls/platform.h, so these macro definitions were redundant. Remove
them.

This commit used the following code:
```
perl -i -0777 -pe 's~#if !defined\(MBEDTLS_PLATFORM_C\)\n(#define (mbedtls|MBEDTLS)_.*\n|#include <(stdarg|stddef|stdio|stdlib|string|time)\.h>\n)*#endif.*\n~~mg' $(git grep -l -F '#if !defined(MBEDTLS_PLATFORM_C)')
```

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2022-10-05 11:54:51 +02:00
Gilles Peskine
36f19b97e2 Include platform.h unconditionally: automatic part
We used to include platform.h only when MBEDTLS_PLATFORM_C was enabled, and
to define ad hoc replacements for mbedtls_xxx functions on a case-by-case
basis when MBEDTLS_PLATFORM_C was disabled. The only reason for this
complication was to allow building individual source modules without copying
platform.h. This is not something we support or recommend anymore, so get
rid of the complication: include platform.h unconditionally.

There should be no change in behavior since just including the header should
not change the behavior of a program.

This commit replaces most occurrences of conditional inclusion of
platform.h, using the following code:

```
perl -i -0777 -pe 's!#if.*\n#include "mbedtls/platform.h"\n(#else.*\n(#define (mbedtls|MBEDTLS)_.*\n|#include <(stdarg|stddef|stdio|stdlib|string|time)\.h>\n)*)?#endif.*!#include "mbedtls/platform.h"!mg' $(git grep -l '#include "mbedtls/platform.h"')
```

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2022-10-05 11:26:07 +02:00
Dave Rodgman
eee5c8ac23
Merge pull request #5982 from gilles-peskine-arm/selftest-calloc-pointer-comparison-fix-2.28
Backport 2.28: Remove largely useless bit of test log to silence GCC 12
2022-06-29 15:25:00 +01:00