From 335fc3dde2d7851817e2d42d79133a1e5e009c0a Mon Sep 17 00:00:00 2001 From: gaojiawei Date: Tue, 17 May 2022 15:46:49 +0800 Subject: [PATCH] syslog: Fixed a potential buffer overflow issue The `CONFIG_SYSLOG_MAX_CHANNELS` is user-specified, however, if the user defines more channels than what `CONFIG_SYSLOG_MAX_CHANNELS` was defined as, a potential buffer overflow occurred. Although the compiler does warn us about that, we should explicitly tell the user this is an error. Signed-off-by: gaojiawei --- drivers/syslog/syslog_channel.c | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/drivers/syslog/syslog_channel.c b/drivers/syslog/syslog_channel.c index 1b26eef7f68..d5f938a926c 100644 --- a/drivers/syslog/syslog_channel.c +++ b/drivers/syslog/syslog_channel.c @@ -129,6 +129,43 @@ static struct syslog_channel_s g_default_channel = }; #endif +/* This is a simply sanity check to avoid we have more elements than the + * `g_syslog_channel` array can hold + */ + +#ifdef CONFIG_SYSLOG_DEFAULT +# define SYSLOG_DEFAULT_AVAILABLE 1 +#else +# define SYSLOG_DEFAULT_AVAILABLE 0 +#endif + +#ifdef CONFIG_RAMLOG_SYSLOG +# define RAMLOG_SYSLOG_AVAILABLE 1 +#else +# define RAMLOG_SYSLOG_AVAILABLE 0 +#endif + +#ifdef CONFIG_SYSLOG_RPMSG +# define SYSLOG_RPMSG_AVAILABLE 1 +#else +# define SYSLOG_RPMSG_AVAILABLE 0 +#endif + +#ifdef CONFIG_SYSLOG_RTT +# define SYSLOG_RTT_AVAILABLE 1 +#else +# define SYSLOG_RTT_AVAILABLE 0 +#endif + +#define SYSLOG_NCHANNELS (SYSLOG_DEFAULT_AVAILABLE + \ + RAMLOG_SYSLOG_AVAILABLE + \ + SYSLOG_RPMSG_AVAILABLE + \ + SYSLOG_RTT_AVAILABLE) + +#if SYSLOG_NCHANNELS > CONFIG_SYSLOG_MAX_CHANNELS +# error "Maximum channel number exceeds." +#endif + /* This is the current syslog channel in use */ FAR struct syslog_channel_s