Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions include/sanitizer/asan_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/****************************************************************************
* include/sanitizer/asan_interface.h
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/* AddressSanitizer public interface.
*
* This is the same header the host compiler-rt runtime provides so that code
* written against user-space ASan (for example the mbedtls test framework)
* can include <sanitizer/asan_interface.h> and call
* __asan_(un)poison_memory_region() unchanged on a NuttX target. NuttX does
* not link the compiler-rt user-space ASan runtime; on a KASAN target these
* entry points are provided by mm/kasan and bridge to the in-kernel KASAN
* shadow. The signatures match the upstream compiler-rt header.
*/

#ifndef __INCLUDE_SANITIZER_ASAN_INTERFACE_H
#define __INCLUDE_SANITIZER_ASAN_INTERFACE_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/compiler.h>

#include <stddef.h>

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

#ifdef __cplusplus
extern "C"
{
#endif

/****************************************************************************
* Name: __asan_poison_memory_region
*
* Description:
* Mark the memory region [addr, addr+size) as unaddressable, so that any
* instrumented access to it triggers a KASAN report until unpoisoned.
*
****************************************************************************/

void __asan_poison_memory_region(FAR void const volatile *addr, size_t size);

/****************************************************************************
* Name: __asan_unpoison_memory_region
*
* Description:
* Mark the memory region [addr, addr+size) as addressable again.
*
****************************************************************************/

void __asan_unpoison_memory_region(FAR void const volatile *addr,
size_t size);

#ifdef __cplusplus
}
#endif

#endif /* __INCLUDE_SANITIZER_ASAN_INTERFACE_H */
2 changes: 1 addition & 1 deletion mm/kasan/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ config MM_KASAN_DISABLE_NULL_POINTER_CHECK

config MM_KASAN_DISABLE_READS_CHECK
bool "Disable reads check"
default n
default y
---help---
This option disables kasan reads check. It speeds up performance
compared with default read/write check. Only disable it when you are
Expand Down
61 changes: 60 additions & 1 deletion mm/kasan/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <nuttx/mm/kasan.h>
#include <nuttx/compiler.h>

#include <sanitizer/asan_interface.h>

#include <assert.h>
#include <nuttx/debug.h>
#include <execinfo.h>
Expand Down Expand Up @@ -146,7 +148,7 @@ static void kasan_show_memory(FAR const uint8_t *addr, size_t size,
_alert("Shadow bytes around the buggy address:\n");
for (p = start; p < end; p += 16)
{
int ret = sprintf(buffer, " %p: ", p);
int ret = sprintf(buffer, "%p: ", p);
int i;

for (i = 0; i < 16; i++)
Expand Down Expand Up @@ -378,6 +380,55 @@ void __asan_unregister_globals(void *ptr, size_t size)
/* Shut up compiler complaints */
}

/****************************************************************************
* Name: __asan_poison_memory_region
*
* Description:
* Compiler-rt AddressSanitizer public interface. User-space code built for
* ASan (for example the mbedtls test framework) includes
* <sanitizer/asan_interface.h> and calls this to mark a region as
* unaddressable. NuttX has no compiler-rt user-space ASan runtime, so on a
* KASAN target this bridges the standard entry point to the in-kernel
* KASAN shadow. Regions not registered with KASAN are ignored by
* kasan_poison().
*
* Input Parameters:
* addr - range start address
* size - range size
*
* Returned Value:
* None.
*
****************************************************************************/

void __asan_poison_memory_region(FAR void const volatile *addr, size_t size)
{
kasan_poison((FAR const void *)addr, size);
}

/****************************************************************************
* Name: __asan_unpoison_memory_region
*
* Description:
* Compiler-rt AddressSanitizer public interface, the counterpart of
* __asan_poison_memory_region(). Marks a region as addressable again by
* bridging to the in-kernel KASAN shadow.
*
* Input Parameters:
* addr - range start address
* size - range size
*
* Returned Value:
* None.
*
****************************************************************************/

void __asan_unpoison_memory_region(FAR void const volatile *addr,
size_t size)
{
kasan_unpoison((FAR const void *)addr, size);
}

void __sanitizer_annotate_contiguous_container(FAR const void *beg,
FAR const void *end,
FAR const void *old_mid,
Expand All @@ -386,6 +437,14 @@ void __sanitizer_annotate_contiguous_container(FAR const void *beg,
/* Shut up compiler complaints */
}

void __sanitizer_annotate_double_ended_contiguous_container(
FAR const void *beg, FAR const void *end,
FAR const void *old_beg, FAR const void *old_end,
FAR const void *new_beg, FAR const void *new_end)
{
/* Shut up compiler complaints */
}

void __asan_report_load_n_noabort(FAR void *addr, size_t size)
{
kasan_report(addr, size, false, return_address(0));
Expand Down
Loading