Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure buff size constraints in chip logging module name fetch #14844

Merged
merged 4 commits into from
Feb 7, 2022
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
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