Skip to content

SmallTable copies too few bytes when removing an item #114

Description

@nding0405
  1. Problem
    The pointer version of remove() contains:
memmove(element,
        element + 1,
        size() - ((reinterpret_cast<uint8_t *>(element + 1) -
                   reinterpret_cast<uint8_t *>(base())) /
                  sizeof(T)));

[SmallTable::remove(T *)](https://github.com/CHERIoT-Platform/network-stack/blob/58425e6a1c65c4357b037aa817ba492e7a8fa70b/lib/firewall/firewall.cc#L275-L287)

The last value is the number of items left. but memmove() expects the number of bytes.

  1. Why is it bad?
    If T is larger than one byte, the code copies too little data. This can damage the next firewall rules in the table.

This function is used here:

table.remove(&tuple);

[Firewall use](

for (auto &tuple : table)
{
if (tuple.localPort == localPort)
{
table.remove(&tuple);
break;
)

  1. Suggested fix
    Multiply the number of items by `sizeof(T).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions