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

Re-alignment logic in LwIPReceiveUDPMessage() makes incorrect assumptions #17213

Closed
tcarmelveilleux opened this issue Apr 8, 2022 · 2 comments
Assignees
Labels
bug Something isn't working inet lwip stale Stale issue or PR V1.0

Comments

@tcarmelveilleux
Copy link
Contributor

Problem

In UDPEndPointImplLwIP::GetPacketInfo, there is a check of if (!aBuffer->EnsureReservedSize(sizeof(IPPacketInfo) + 3)) which implies desire to re-align. However, the code makes assumptions about the remaining use of the information consumed until now.

GetPacketInfo() returns a pointer to "loose data" before the actual payload, which is first gotten to set the info.

It is first used in the chain in UDPEndPointImplLwIP::LwIPReceiveUDPMessage():

   pktInfo = GetPacketInfo(buf);   // <------ Get pointer to front of data no longer used
    if (pktInfo != nullptr)   // <---- Reuse the space to shoe-horn ipInfo
    {
        pktInfo->SrcAddress  = IPAddress(*addr);
        pktInfo->DestAddress = IPAddress(*ip_current_dest_addr());
        pktInfo->Interface   = InterfaceId(ip_current_netif());
        pktInfo->SrcPort     = port;
        pktInfo->DestPort    = pcb->local_port;
    }

Then it's used to get the data elsewhere:

void UDPEndPointImplLwIP::HandleDataReceived(System::PacketBufferHandle && msg)
{
    if ((mState == State::kListening) && (OnMessageReceived != nullptr))
    {
        const IPPacketInfo * pktInfo = GetPacketInfo(msg);

        if (pktInfo != nullptr)
        {
            const IPPacketInfo pktInfoCopy = *pktInfo; // copy the address info so that the app can free the
                                                       // PacketBuffer without affecting access to address info.
            OnMessageReceived(this, std::move(msg), &pktInfoCopy);

The re-alignment logic makes 2 bad assumptions that no longer hold at this point in Matter

1- That the platform is 32 bits
2- That the size of the struct IPPacketInfo will always fit within the "consumed IP header space" from before, which implies we do not grow IPPacketInfo too much

Proposed Solution

  • Need to align to native word boundary, not 32 bits
  • Need to make sure that sizeof(IPPacketInfo) is < the minimum set of headers one could receive
  • Possibly replace this shoehorning with passing more state between calls.
@tcarmelveilleux tcarmelveilleux added bug Something isn't working inet V1.0 lwip labels Apr 8, 2022
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this issue Jun 3, 2022
#### Problem

Some UDPEndPoint implementation code assumes that IPPacketInfo
is aligned to 4 bytes, when storing one inside PacketBuffer
reserved space. It would also fail to identify the available
reserve if there was enough space, but less that 3 bytes more
than enough.

See project-chip#17213 Re-alignment logic makes incorrect assumptions

#### Change overview

Adds a PacketBuffer::GetReserve<T>() that returns a pointer in
the buffer reserve space suitable in size and alignment for a T,
and uses it for `GetPacketInfo()` in `UDPEndPointImplLwIP` and
`UDPEndPointImplOT`.

#### Testing

Added a unit test, `PacketBufferTest::CheckGetReserve()`.
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this issue Jun 6, 2022
#### Problem

Some UDPEndPoint implementation code assumes that IPPacketInfo
is aligned to 4 bytes, when storing one inside PacketBuffer
reserved space. It would also fail to identify the available
reserve if there was enough space, but less that 3 bytes more
than enough.

See project-chip#17213 Re-alignment logic makes incorrect assumptions

#### Change overview

Adds a PacketBuffer::GetReserve<T>() that returns a pointer in
the buffer reserve space suitable in size and alignment for a T,
and uses it for `GetPacketInfo()` in `UDPEndPointImplLwIP` and
`UDPEndPointImplOT`.

#### Testing

Added a unit test, `PacketBufferTest::CheckGetReserve()`.
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this issue Jun 6, 2022
#### Problem

Some UDPEndPoint implementation code assumes that IPPacketInfo
is aligned to 4 bytes, when storing one inside PacketBuffer
reserved space. It would also fail to identify the available
reserve if there was enough space, but less that 3 bytes more
than enough.

See project-chip#17213 Re-alignment logic makes incorrect assumptions

#### Change overview

Adds a PacketBuffer::GetReserve<T>() that returns a pointer in
the buffer reserve space suitable in size and alignment for a T,
and uses it for `GetPacketInfo()` in `UDPEndPointImplLwIP` and
`UDPEndPointImplOT`.

#### Testing

Added a unit test, `PacketBufferTest::CheckGetReserve()`.
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this issue Jun 6, 2022
#### Problem

Some UDPEndPoint implementation code assumes that IPPacketInfo
is aligned to 4 bytes, when storing one inside PacketBuffer
reserved space. It would also fail to identify the available
reserve if there was enough space, but less that 3 bytes more
than enough.

See project-chip#17213 Re-alignment logic makes incorrect assumptions

#### Change overview

Adds a PacketBuffer::GetReserve<T>() that returns a pointer in
the buffer reserve space suitable in size and alignment for a T,
and uses it for `GetPacketInfo()` in `UDPEndPointImplLwIP` and
`UDPEndPointImplOT`.

#### Testing

Added a unit test, `PacketBufferTest::CheckGetReserve()`.
andy31415 pushed a commit that referenced this issue Jun 7, 2022
* Fix alignment when using PacketBuffer reserve space

#### Problem

Some UDPEndPoint implementation code assumes that IPPacketInfo
is aligned to 4 bytes, when storing one inside PacketBuffer
reserved space. It would also fail to identify the available
reserve if there was enough space, but less that 3 bytes more
than enough.

See #17213 Re-alignment logic makes incorrect assumptions

#### Change overview

Adds a PacketBuffer::GetReserve<T>() that returns a pointer in
the buffer reserve space suitable in size and alignment for a T,
and uses it for `GetPacketInfo()` in `UDPEndPointImplLwIP` and
`UDPEndPointImplOT`.

#### Testing

Added a unit test, `PacketBufferTest::CheckGetReserve()`.

* Use EnsureReservedSize()
@stale
Copy link

stale bot commented Oct 8, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale Stale issue or PR label Oct 8, 2022
@stale
Copy link

stale bot commented Oct 18, 2022

This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working inet lwip stale Stale issue or PR V1.0
Projects
None yet
Development

No branches or pull requests

2 participants