Skip to content

Commit

Permalink
Ensure buff size constraints in chip logging module name fetch (#14844)
Browse files Browse the repository at this point in the history
* Update logging logic a bit

* Enforce buffer size at compile time

* Place more logging bits into anon namespace to make them static. Help the linker out a bit
  • Loading branch information
andy31415 authored and pull[bot] committed Feb 18, 2022
1 parent 8424cba commit 2231472
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
97 changes: 51 additions & 46 deletions src/lib/support/logging/CHIPLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ namespace {

std::atomic<LogRedirectCallback_t> sLogRedirectCallback{ nullptr };

}

/*
* Array of strings containing the names for each of the chip log
* modules.
Expand All @@ -57,57 +55,64 @@ std::atomic<LogRedirectCallback_t> sLogRedirectCallback{ nullptr };
* necessary.
*
*/
static const char ModuleNames[] = "-\0\0" // None
"IN\0" // Inet
"BLE" // BLE
"ML\0" // MessageLayer
"SM\0" // SecurityManager
"EM\0" // ExchangeManager
"TLV" // TLV
"ASN" // ASN1
"CR\0" // Crypto
"CTL" // Controller
"AL\0" // Alarm
"SC\0" // SecureChannel
"BDX" // BulkDataTransfer
"DMG" // DataManagement
"DC\0" // DeviceControl
"DD\0" // DeviceDescription
"ECH" // Echo
"FP\0" // FabricProvisioning
"NP\0" // NetworkProvisioning
"SD\0" // ServiceDirectory
"SP\0" // ServiceProvisioning
"SWU" // SoftwareUpdate
"TP\0" // TokenPairing
"TS\0" // TimeServices
"HB\0" // Heartbeat
"CSL" // chipSystemLayer
"EVL" // Event Logging
"SPT" // Support
"TOO" // chipTool
"ZCL" // Zcl
"SH\0" // Shell
"DL\0" // DeviceLayer
"SPL" // SetupPayload
"SVR" // AppServer
"DIS" // Discovery
"IM\0" // InteractionModel
"TST" // Test
"ODP" // OperationalDeviceProxy
"ATM" // Automation
const char ModuleNames[] = "-\0\0" // None
"IN\0" // Inet
"BLE" // BLE
"ML\0" // MessageLayer
"SM\0" // SecurityManager
"EM\0" // ExchangeManager
"TLV" // TLV
"ASN" // ASN1
"CR\0" // Crypto
"CTL" // Controller
"AL\0" // Alarm
"SC\0" // SecureChannel
"BDX" // BulkDataTransfer
"DMG" // DataManagement
"DC\0" // DeviceControl
"DD\0" // DeviceDescription
"ECH" // Echo
"FP\0" // FabricProvisioning
"NP\0" // NetworkProvisioning
"SD\0" // ServiceDirectory
"SP\0" // ServiceProvisioning
"SWU" // SoftwareUpdate
"TP\0" // TokenPairing
"TS\0" // TimeServices
"HB\0" // Heartbeat
"CSL" // chipSystemLayer
"EVL" // Event Logging
"SPT" // Support
"TOO" // chipTool
"ZCL" // Zcl
"SH\0" // Shell
"DL\0" // DeviceLayer
"SPL" // SetupPayload
"SVR" // AppServer
"DIS" // Discovery
"IM\0" // InteractionModel
"TST" // Test
"ODP" // OperationalDeviceProxy
"ATM" // Automation
;

#define ModuleNamesCount ((sizeof(ModuleNames) - 1) / chip::Logging::kMaxModuleNameLen)

void GetModuleName(char * buf, uint8_t bufSize, uint8_t module)
void GetModuleName(char (&buf)[chip::Logging::kMaxModuleNameLen + 1], uint8_t module)
{
const char * moduleNamePtr = ModuleNames + ((module < ModuleNamesCount) ? module * chip::Logging::kMaxModuleNameLen : 0);

snprintf(buf, bufSize, "%s", moduleNamePtr);
buf[chip::Logging::kMaxModuleNameLen] = 0;
const char * module_name = ModuleNames;
if (module < ModuleNamesCount)
{
module_name += module * chip::Logging::kMaxModuleNameLen;
}

memcpy(buf, module_name, chip::Logging::kMaxModuleNameLen);
buf[chip::Logging::kMaxModuleNameLen] = 0; // ensure null termination
}

} // namespace

void SetLogRedirectCallback(LogRedirectCallback_t callback)
{
sLogRedirectCallback.store(callback);
Expand Down Expand Up @@ -183,7 +188,7 @@ void LogV(uint8_t module, uint8_t category, const char * msg, va_list args)
}

char moduleName[chip::Logging::kMaxModuleNameLen + 1];
GetModuleName(moduleName, sizeof(moduleName), module);
GetModuleName(moduleName, module);

LogRedirectCallback_t redirect = sLogRedirectCallback.load();

Expand Down
6 changes: 0 additions & 6 deletions src/lib/support/logging/CHIPLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ static constexpr uint16_t kMaxMessagePadding = (chip::Logging::kMaxPrefixLen + c
chip::Logging::kMaxSeparatorLen + chip::Logging::kMaxTrailerLen);

void GetMessageWithPrefix(char * buf, uint8_t bufSize, uint8_t module, const char * msg);
void GetModuleName(char * buf, uint8_t bufSize, uint8_t module);

#else

Expand All @@ -213,11 +212,6 @@ static inline void GetMessageWithPrefix(char * buf, uint8_t bufSize, uint8_t mod
return;
}

static inline void GetModuleName(char * buf, uint8_t bufSize, uint8_t module)
{
return;
}

#endif // _CHIP_USE_LOGGING

bool IsCategoryEnabled(uint8_t category);
Expand Down

0 comments on commit 2231472

Please sign in to comment.