Skip to content

Commit

Permalink
syscall: Fix static analysis compalins
Browse files Browse the repository at this point in the history
Since K_SYSCALL_MEMORY can be called with signed/unsigned size types, if
we check if size >= 0, static anlysis will complain about it when
size in unsigned.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
  • Loading branch information
Flavio Ceolin authored and henrikbrixandersen committed Dec 27, 2023
1 parent ff89703 commit 87d056b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion include/zephyr/internal/syscall_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@ int k_usermode_string_copy(char *dst, const char *src, size_t maxlen);
*/
#define K_SYSCALL_VERIFY(expr) K_SYSCALL_VERIFY_MSG(expr, #expr)

/**
* @brief Macro to check if size is negative
*
* K_SYSCALL_MEMORY can be called with signed/unsigned types
* and because of that if we check if size is greater or equal to
* zero, many static analyzers complain about no effect expression.
*
* @param ptr Memory area to examine
* @param size Size of the memory area
* @return true if size is valid, false otherwise
* @note This is an internal API. Do not use unless you are extending
* functionality in the Zephyr tree.
*/
#define K_SYSCALL_MEMORY_SIZE_CHECK(ptr, size) \
(((uintptr_t)ptr + size) >= (uintptr_t)ptr)

/**
* @brief Runtime check that a user thread has read and/or write permission to
* a memory area
Expand All @@ -413,7 +429,8 @@ int k_usermode_string_copy(char *dst, const char *src, size_t maxlen);
* functionality in the Zephyr tree.
*/
#define K_SYSCALL_MEMORY(ptr, size, write) \
K_SYSCALL_VERIFY_MSG((size >= 0) && !Z_DETECT_POINTER_OVERFLOW(ptr, size) \
K_SYSCALL_VERIFY_MSG(K_SYSCALL_MEMORY_SIZE_CHECK(ptr, size) \
&& !Z_DETECT_POINTER_OVERFLOW(ptr, size) \
&& (arch_buffer_validate((void *)ptr, size, write) \
== 0), \
"Memory region %p (size %zu) %s access denied", \
Expand Down

0 comments on commit 87d056b

Please sign in to comment.