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

Fix attr read overflows #36003

Merged
merged 4 commits into from
Mar 5, 2025
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
43 changes: 39 additions & 4 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app/InteractionModelEngine.h>
#include <app/reporting/reporting.h>
#include <app/util/config.h>
#include <app/util/ember-io-storage.h>
#include <app/util/ember-strings.h>
#include <app/util/endpoint-config-api.h>
#include <app/util/generic-callbacks.h>
Expand Down Expand Up @@ -292,6 +293,30 @@ CHIP_ERROR emberAfSetDynamicEndpoint(uint16_t index, EndpointId id, const EmberA
}
}

const size_t bufferSize = Compatibility::Internal::gEmberAttributeIOBufferSpan.size();
for (uint8_t i = 0; i < ep->clusterCount; i++)
{
const EmberAfCluster * cluster = &(ep->cluster[i]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also do:

Suggested change
const EmberAfCluster * cluster = &(ep->cluster[i]);
const EmberAfCluster & cluster = ep->cluster[i];

but either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave the pointer-style since the structure is ultimately storing pointers -- in hindsight I probably should have done ep->cluster + i to just access the pointer directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, the structure is not in fact storing pointers. It's storing EmberAfCluster objects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!cluster->attributes)
{
continue;
}

for (uint16_t j = 0; j < cluster->attributeCount; j++)
{
const EmberAfAttributeMetadata * attr = &(cluster->attributes[j]);
uint16_t attrSize = emberAfAttributeSize(attr);
if (attrSize > bufferSize)
{
ChipLogError(DataManagement,
"Attribute size %u exceeds max size %lu, (attrId=" ChipLogFormatMEI ", clusterId=" ChipLogFormatMEI
")",
attrSize, static_cast<unsigned long>(bufferSize), ChipLogValueMEI(attr->attributeId),
ChipLogValueMEI(cluster->clusterId));
return CHIP_ERROR_NO_MEMORY;
}
}
}
emAfEndpoints[index].endpoint = id;
emAfEndpoints[index].deviceTypeList = deviceTypeList;
emAfEndpoints[index].endpointType = ep;
Expand Down Expand Up @@ -642,10 +667,20 @@ Status emAfReadOrWriteAttribute(const EmberAfAttributeSearchRecord * attRecord,
// Is the attribute externally stored?
if (am->mask & MATTER_ATTRIBUTE_FLAG_EXTERNAL_STORAGE)
{
return (write ? emberAfExternalAttributeWriteCallback(attRecord->endpoint, attRecord->clusterId,
am, buffer)
: emberAfExternalAttributeReadCallback(attRecord->endpoint, attRecord->clusterId,
am, buffer, emberAfAttributeSize(am)));
if (write)
{
return emberAfExternalAttributeWriteCallback(attRecord->endpoint, attRecord->clusterId, am,
buffer);
}

if (readLength < emberAfAttributeSize(am))
{
// Prevent a potential buffer overflow
return Status::ResourceExhausted;
}

return emberAfExternalAttributeReadCallback(attRecord->endpoint, attRecord->clusterId, am,
buffer, emberAfAttributeSize(am));
}

// Internal storage is only supported for fixed endpoints
Expand Down
3 changes: 2 additions & 1 deletion src/controller/tests/TestServerCommandDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ TEST_F(TestServerCommandDispatch, TestNoHandler)
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
}

static const int kDescriptorAttributeArraySize = 254;
// Use 8 so that we don't exceed the size of ATTRIBUTE_LARGEST defined by ZAP
static const int kDescriptorAttributeArraySize = 8;

// Declare Descriptor cluster attributes
DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(descriptorAttrs)
Expand Down
Loading