From 172b209f2d2cbe275c62fe4e5ee29170242e9942 Mon Sep 17 00:00:00 2001 From: Ville Juven Date: Thu, 22 Dec 2022 12:21:56 +0200 Subject: [PATCH] sched/assert: Re-implement assert() into user space _assert is a kernel procedure, entered via system call to make the core dump in privileged mode. Running exit() from this context is not OK as it runs the registered exit functions and flushes streams, which must not be done from privileged mode as it is a security hole. Thus, implement assert() into user space (again) and remove the exit() call from the kernel procedure. --- include/assert.h | 25 ++++++++++++++++++++--- libs/libc/assert/Make.defs | 2 ++ libs/libc/assert/lib_assert.c | 38 +++++++++++++++++++++++++++++++++++ sched/misc/assert.c | 1 - syscall/syscall.csv | 2 +- 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 libs/libc/assert/lib_assert.c diff --git a/include/assert.h b/include/assert.h index 51dae09542e..eabf6163bc2 100644 --- a/include/assert.h +++ b/include/assert.h @@ -43,9 +43,9 @@ #undef DEBUGVERIFY /* Like VERIFY, but only if CONFIG_DEBUG_ASSERTIONS is defined */ #ifdef CONFIG_HAVE_FILENAME -# define PANIC() _assert(__FILE__, __LINE__) +# define PANIC() __assert(__FILE__, __LINE__) #else -# define PANIC() _assert("unknown", 0) +# define PANIC() __assert("unknown", 0) #endif #define ASSERT(f) do { if (!(f)) PANIC(); } while (0) @@ -106,7 +106,26 @@ extern "C" * Public Function Prototypes ****************************************************************************/ -void _assert(FAR const char *filename, int linenum) noreturn_function; +/**************************************************************************** + * Name: _assert + * + * Description: + * This is the assert system call that performs the core dump etc. Function + * might not return if it is not safe to do so (in IRQ or in IDLE task). + * + ****************************************************************************/ + +void _assert(FAR const char *filename, int linenum); + +/**************************************************************************** + * Name: __assert + * + * Description: + * This is the user space assert procedure. + * + ****************************************************************************/ + +void __assert(FAR const char *filename, int linenum) noreturn_function; #undef EXTERN #ifdef __cplusplus diff --git a/libs/libc/assert/Make.defs b/libs/libc/assert/Make.defs index 0b1f03f408d..dfe45759d41 100644 --- a/libs/libc/assert/Make.defs +++ b/libs/libc/assert/Make.defs @@ -18,6 +18,8 @@ # ############################################################################ +CSRCS += lib_assert.c + ifeq ($(CONFIG_STACK_CANARIES),y) CSRCS += lib_stackchk.c endif diff --git a/libs/libc/assert/lib_assert.c b/libs/libc/assert/lib_assert.c new file mode 100644 index 00000000000..7a0d96f39e1 --- /dev/null +++ b/libs/libc/assert/lib_assert.c @@ -0,0 +1,38 @@ +/**************************************************************************** + * libs/libc/assert/lib_assert.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void __assert(FAR const char *filename, int linenum) +{ + _assert(filename, linenum); + exit(EXIT_FAILURE); +} diff --git a/sched/misc/assert.c b/sched/misc/assert.c index 1c51e04fd40..3a74e183809 100644 --- a/sched/misc/assert.c +++ b/sched/misc/assert.c @@ -544,5 +544,4 @@ void _assert(FAR const char *filename, int linenum) showtasks(); assert_end(); - exit(EXIT_FAILURE); } diff --git a/syscall/syscall.csv b/syscall/syscall.csv index f5909bf2959..f41e0650fdf 100644 --- a/syscall/syscall.csv +++ b/syscall/syscall.csv @@ -1,5 +1,5 @@ "_exit","unistd.h","","noreturn","int" -"_assert","assert.h","","noreturn","FAR const char *","int" +"_assert","assert.h","","void","FAR const char *","int" "accept","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct sockaddr *","FAR socklen_t *" "adjtime","sys/time.h","defined(CONFIG_CLOCK_TIMEKEEPING)","int","FAR const struct timeval *","FAR struct timeval *" "aio_cancel","aio.h","defined(CONFIG_FS_AIO)","int","int","FAR struct aiocb *"