From 321cbc89fbc57a1df02acea65399247b4104d6d7 Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 16:22:04 +0530 Subject: [PATCH 1/8] Update Makefile.am --- src/unittest/Makefile.am | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/unittest/Makefile.am b/src/unittest/Makefile.am index e92e103c4..ea8ca1fbb 100644 --- a/src/unittest/Makefile.am +++ b/src/unittest/Makefile.am @@ -22,7 +22,11 @@ bin_PROGRAMS = remotedebugger_gtest COMMON_CPPFLAGS = -I../ -I../../ -I./mocks -I/usr/include/cjson -I/usr/include/nettle -I/usr/include/msgpack -DGTEST_ENABLE # Define the libraries to link against -COMMON_LDADD = -lgtest -lgtest_main -lgmock_main -lgmock -lcjson -lmsgpackc -lgcov -lsecure_wrapper +COMMON_LDADD = -lgtest -lgtest_main -lgmock_main -lgmock -lcjson -lmsgpackc -lgcov + +if IS_YOCTO_ENABLED +AM_LDFLAGS = -lsecure_wrapper +endif # Define the compiler flags COMMON_CXXFLAGS = -frtti -fprofile-arcs -ftest-coverage From 750ceae3136eea3f3b39ed5f5193b4bea6bb9b68 Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 16:34:53 +0530 Subject: [PATCH 2/8] Create secure_wrapper.h --- src/unittest/mocks/secure_wrapper.h | 112 ++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/unittest/mocks/secure_wrapper.h diff --git a/src/unittest/mocks/secure_wrapper.h b/src/unittest/mocks/secure_wrapper.h new file mode 100644 index 000000000..983303ab9 --- /dev/null +++ b/src/unittest/mocks/secure_wrapper.h @@ -0,0 +1,112 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2019 RDK Management + * + * Licensed 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. +*/ + +#include +#include + +#ifndef __SECURE_H +# define __SECURE_H +# ifdef __cplusplus +extern "C" { +# endif + +__attribute__((nonnull)) +__attribute__((format(printf,1,2))) +int v_secure_system(const char *command, ...); + +__attribute__((nonnull)) +__attribute__((format(printf,2,3))) +FILE *v_secure_popen(const char *direction, const char *command, ...); + +__attribute__((nonnull)) +int v_secure_pclose(FILE *); + +/* OBSOLETE CONSTANTS */ +#define _SPIPE "|" +#define _SOR "||" +#define _SAND "&&" +#define _SBG "&" +#define _STHEN ";" + +/* The following is just some gcc magic to make sure + * 1) the format string isn't a variable + * 2) the number of arguments matches the format + * 3) popen's direction arg is "r" or "w" + */ + +#if (defined (__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) || defined(__clang__) +#define PRAGMA_PUSH \ + _Pragma ("GCC diagnostic push") \ + _Pragma ("GCC diagnostic error \"-Wformat\"") \ + _Pragma ("GCC diagnostic error \"-Wformat-security\"") +#define PRAGMA_POP \ + _Pragma ("GCC diagnostic pop") +#else +#define PRAGMA_PUSH +#define PRAGMA_POP +#endif + +#define v_secure_system(fmt, args...) \ + ({ \ + int ret; \ + PRAGMA_PUSH; \ + if (!__builtin_constant_p(fmt)) { \ + extern void format_error() __attribute__((error("command argument cannot be a variable\nreplace \"sprintf(buffer, command, args); v_secure_system(buffer);\" with \"v_secure_system(command, args);\""))); \ + format_error(); \ + } \ + ret = v_secure_system(fmt, ##args); \ + PRAGMA_POP; \ + ret; \ + }) + +#define v_secure_popen(direction, fmt, args...) \ + ({ \ + FILE *ret; \ + PRAGMA_PUSH; \ + if ( \ + __builtin_constant_p(*direction) && \ + ((direction[0] != 'r' && direction[0] != 'w') || direction[1] != '\0') \ + ) { \ + extern void popen_check() __attribute__((error("v_secure_popen(direction, command, ...) direction must be \"r\" or \"w\""))); \ + popen_check(); \ + } \ + if (!__builtin_constant_p(fmt)) { \ + extern void format_error() __attribute__((error("command argument cannot be a variable"))); \ + format_error(); \ + } \ + ret = v_secure_popen(direction, fmt, ##args); \ + PRAGMA_POP; \ + ret; \ + }) + +extern int system(const char *command) __attribute__((warning("please replace system() with v_secure_system()"))); + +__attribute__((deprecated)) +__attribute__((warning("contains_secure_separator is obsolete"))) +static inline int contains_secure_separator(__attribute__((unused)) char *str) { + return 0; +} + +extern int secure_system_call_p(const char *cmd, char *argv[]); +extern int secure_system_call_vp(const char *cmd, ...); + +# ifdef __cplusplus +} +# endif +#endif From 2d6b04459ea8312bcd061d96c4ebecfc866b14fd Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 16:37:24 +0530 Subject: [PATCH 3/8] Update Makefile.am --- src/unittest/Makefile.am | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/unittest/Makefile.am b/src/unittest/Makefile.am index ea8ca1fbb..9c8e0107c 100644 --- a/src/unittest/Makefile.am +++ b/src/unittest/Makefile.am @@ -24,10 +24,6 @@ COMMON_CPPFLAGS = -I../ -I../../ -I./mocks -I/usr/include/cjson -I/usr/include/n # Define the libraries to link against COMMON_LDADD = -lgtest -lgtest_main -lgmock_main -lgmock -lcjson -lmsgpackc -lgcov -if IS_YOCTO_ENABLED -AM_LDFLAGS = -lsecure_wrapper -endif - # Define the compiler flags COMMON_CXXFLAGS = -frtti -fprofile-arcs -ftest-coverage From a35153014734347cdcf24752950b8224beeb9c18 Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 16:47:41 +0530 Subject: [PATCH 4/8] Update rrdExecuteScript.c --- src/rrdExecuteScript.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rrdExecuteScript.c b/src/rrdExecuteScript.c index d3e0722b7..151941036 100644 --- a/src/rrdExecuteScript.c +++ b/src/rrdExecuteScript.c @@ -23,7 +23,9 @@ #else #define RRD_SCRIPT "./mockSampleUploadScript.sh" #endif +#if !defined(GTEST_ENABLE) #include "secure_wrapper.h" +#endif static void normalizeIssueName(char *str); From ce8a3e1d097a5cc4b859b6186e1e1434497c2235 Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 16:49:30 +0530 Subject: [PATCH 5/8] Delete src/unittest/mocks/secure_wrapper.h --- src/unittest/mocks/secure_wrapper.h | 112 ---------------------------- 1 file changed, 112 deletions(-) delete mode 100644 src/unittest/mocks/secure_wrapper.h diff --git a/src/unittest/mocks/secure_wrapper.h b/src/unittest/mocks/secure_wrapper.h deleted file mode 100644 index 983303ab9..000000000 --- a/src/unittest/mocks/secure_wrapper.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - * If not stated otherwise in this file or this component's LICENSE file the - * following copyright and licenses apply: - * - * Copyright 2019 RDK Management - * - * Licensed 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. -*/ - -#include -#include - -#ifndef __SECURE_H -# define __SECURE_H -# ifdef __cplusplus -extern "C" { -# endif - -__attribute__((nonnull)) -__attribute__((format(printf,1,2))) -int v_secure_system(const char *command, ...); - -__attribute__((nonnull)) -__attribute__((format(printf,2,3))) -FILE *v_secure_popen(const char *direction, const char *command, ...); - -__attribute__((nonnull)) -int v_secure_pclose(FILE *); - -/* OBSOLETE CONSTANTS */ -#define _SPIPE "|" -#define _SOR "||" -#define _SAND "&&" -#define _SBG "&" -#define _STHEN ";" - -/* The following is just some gcc magic to make sure - * 1) the format string isn't a variable - * 2) the number of arguments matches the format - * 3) popen's direction arg is "r" or "w" - */ - -#if (defined (__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) || defined(__clang__) -#define PRAGMA_PUSH \ - _Pragma ("GCC diagnostic push") \ - _Pragma ("GCC diagnostic error \"-Wformat\"") \ - _Pragma ("GCC diagnostic error \"-Wformat-security\"") -#define PRAGMA_POP \ - _Pragma ("GCC diagnostic pop") -#else -#define PRAGMA_PUSH -#define PRAGMA_POP -#endif - -#define v_secure_system(fmt, args...) \ - ({ \ - int ret; \ - PRAGMA_PUSH; \ - if (!__builtin_constant_p(fmt)) { \ - extern void format_error() __attribute__((error("command argument cannot be a variable\nreplace \"sprintf(buffer, command, args); v_secure_system(buffer);\" with \"v_secure_system(command, args);\""))); \ - format_error(); \ - } \ - ret = v_secure_system(fmt, ##args); \ - PRAGMA_POP; \ - ret; \ - }) - -#define v_secure_popen(direction, fmt, args...) \ - ({ \ - FILE *ret; \ - PRAGMA_PUSH; \ - if ( \ - __builtin_constant_p(*direction) && \ - ((direction[0] != 'r' && direction[0] != 'w') || direction[1] != '\0') \ - ) { \ - extern void popen_check() __attribute__((error("v_secure_popen(direction, command, ...) direction must be \"r\" or \"w\""))); \ - popen_check(); \ - } \ - if (!__builtin_constant_p(fmt)) { \ - extern void format_error() __attribute__((error("command argument cannot be a variable"))); \ - format_error(); \ - } \ - ret = v_secure_popen(direction, fmt, ##args); \ - PRAGMA_POP; \ - ret; \ - }) - -extern int system(const char *command) __attribute__((warning("please replace system() with v_secure_system()"))); - -__attribute__((deprecated)) -__attribute__((warning("contains_secure_separator is obsolete"))) -static inline int contains_secure_separator(__attribute__((unused)) char *str) { - return 0; -} - -extern int secure_system_call_p(const char *cmd, char *argv[]); -extern int secure_system_call_vp(const char *cmd, ...); - -# ifdef __cplusplus -} -# endif -#endif From 927dd899ba07da750be02fb8386c9dc0c9c4e444 Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 17:01:05 +0530 Subject: [PATCH 6/8] Update rrdRunCmdThread.c --- src/rrdRunCmdThread.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rrdRunCmdThread.c b/src/rrdRunCmdThread.c index 63cf27ae8..e8de08c29 100644 --- a/src/rrdRunCmdThread.c +++ b/src/rrdRunCmdThread.c @@ -24,7 +24,9 @@ #include #include "rrdRunCmdThread.h" #include "rrdCommandSanity.h" +#if !defined(GTEST_ENABLE) #include "secure_wrapper.h" +#endif pthread_mutex_t rrdCacheMut; static cacheData *cacheDataNode = NULL; From 982ccadf6906d943ebf6ed79e1361ed7b3130f3d Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 17:25:10 +0530 Subject: [PATCH 7/8] Update rrdRbus.h --- src/rrdRbus.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rrdRbus.h b/src/rrdRbus.h index 4df7f156b..341aaf2b3 100644 --- a/src/rrdRbus.h +++ b/src/rrdRbus.h @@ -25,9 +25,8 @@ extern "C" { #endif -#if !defined(GTEST_ENABLE) #include - +#if !defined(GTEST_ENABLE) rbusEventSubscription_t subscriptions[2]; #endif From 1d42ecdf4a9d24cda2f7b1c2ed4259f27ba1eb9d Mon Sep 17 00:00:00 2001 From: Saranya2421 Date: Wed, 22 Jan 2025 17:28:56 +0530 Subject: [PATCH 8/8] Update rrdRbus.h --- src/rrdRbus.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rrdRbus.h b/src/rrdRbus.h index 341aaf2b3..4df7f156b 100644 --- a/src/rrdRbus.h +++ b/src/rrdRbus.h @@ -25,8 +25,9 @@ extern "C" { #endif -#include #if !defined(GTEST_ENABLE) +#include + rbusEventSubscription_t subscriptions[2]; #endif