From 9966d85c0ac308b8b3988388f5811cb4b21e4e31 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Wed, 10 Mar 2021 15:55:27 +0100 Subject: [PATCH] lib: os: cbprintf: Add alignment check to cbprintf_package Added validation of alignment to cbprintf_package. Error is returned if input buffer is not aligned to the largest argument. Signed-off-by: Krzysztof Chruscinski --- include/sys/cbprintf.h | 1 + lib/os/cbprintf_packaged.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/include/sys/cbprintf.h b/include/sys/cbprintf.h index f4b3f56378f..51700bca37a 100644 --- a/include/sys/cbprintf.h +++ b/include/sys/cbprintf.h @@ -139,6 +139,7 @@ typedef int (*cbprintf_cb)(/* int c, void *ctx */); * @retval nonegative the number of bytes successfully stored at @p packaged. * This will not exceed @p len. * @retval -EINVAL if @p format is not acceptable + * @retval -EFAULT if @p packaged alignment is not acceptable * @retval -ENOSPC if @p packaged was not null and the space required to store * exceed @p len. */ diff --git a/lib/os/cbprintf_packaged.c b/lib/os/cbprintf_packaged.c index 68a9aec0ca6..24e50d2f512 100644 --- a/lib/os/cbprintf_packaged.c +++ b/lib/os/cbprintf_packaged.c @@ -374,6 +374,11 @@ int cbvprintf_package(void *packaged, size_t len, /* align destination buffer location */ buf = (void *) ROUND_UP(buf, align); + /* Check if buffer is properly aligned. */ + if ((uintptr_t)buf0 & (align - 1)) { + return -EFAULT; + } + /* make sure the data fits */ if (buf0 && buf - buf0 + size > len) { return -ENOSPC;