oscore_validate_sender_seq() moves the replay window on before the
message has been decrypted, and oscore_roll_back_seq() is meant to undo
that when decryption fails. It only restored last_seq and
sliding_window when the saved values were non zero, and never restored
initial_state. For a recipient context still in its initial state
(rfc8613_b_1_2 false), or one whose last_seq is 0, a single request
with a forged Partial IV and an invalid tag left last_seq at the
sender's chosen value, so the genuine peer's following requests were
rejected as outside the replay window.
The client did not roll back at all when a response failed decryption,
and the server called oscore_update_seq_num_cb() with the new window
before decrypting the request, so credential storage could be updated
with a forged sequence number even when the in memory window was
restored.
Record whether there is a window update to undo and restore
initial_state with it, roll back on response decryption failure as
well, and only call oscore_update_seq_num_cb() once the request has
been decrypted.
Add a unit test.
coap_notify_observers() walked the subscribers with LL_FOREACH_SAFE(),
which holds on to the next subscription while the current one is
notified. The request handler runs with the lock released, and a
notification that cannot be sent disconnects the session, which deletes
all of that session's subscriptions. When the next subscription was one
of those, the walk carried on with freed memory.
Mark the subscriptions to visit at the start of the walk and look up the
next marked one from the head of the list after each notification, so
nothing deleted during a notification is used afterwards.
Subscriptions added during the walk are not notified in that pass, as
before.
Add a unit test.
When a block-wise body is passed on as a single body, its length was taken
from total_len, which tracks the peer's Size2 / Size1 rather than the data
actually received.
Client Q-Block2: total_len was raised to a larger Size2 in a later block
before the Size2 mismatch check, so the check never fired for that case,
and nothing tied the last block to Size2. The body handed to the
application could then be longer than the data buffer behind it, or
include its uninitialised tail.
Server Block1 / Q-Block1: total_len kept a Size1 that was larger than
where the last block ended, so the body included the uninitialised tail
of the buffer allocated for the Size1 estimate.
Now the last block (M bit not set) defines the body length: for Q-Block2
and Q-Block1 it must end at Size2 / Size1 (RFC9177 requires these to be the
full size), and for Block1 / Block2 it replaces the Size2 / Size1 estimate
(RFC7959 4). A new total_len_set flag records that the last block has set
total_len, after which a block that ends past it, or another last block
that ends elsewhere, is rejected instead of changing the length.
Add unit tests to tests/test_block.c.
Reduce value of partial timeout if there are a lot of outstanding
large block receipts that are not active.
Add function coap_context_set_max_bodies_ram() to define an upper memory
limit usage across all active body receipts.
Packets from the same peer can overwrite a session's local address with a
multicast destination. Save the address when an observation is created or
renewed and restore it before generating notifications, so deferred responses
snapshot the correct address instead of being dropped as invalid multicast.
Add regression coverage for CON and NON observations, including renewals and
the local address captured by a notification's async response.
Validation: 215 unit tests passed (1712 assertions).
Server sessions are keyed on (remote address, local port) only, and
session->addr_info.local is overwritten by the destination of every
received packet. When a unicast CON request is parked in a coap_async_t
for a separate response and a multicast packet from the same peer
arrives before the async fires, the re-invoked handle_request() sees a
multicast local address, drops the CON per RFC7252 8.1 ("Invalid
multicast packet received"), and coap_check_async() frees the async:
the response is silently never sent.
Snapshot the local address in coap_register_async() and restore it on
the session before the async fires, so the deferred request is handled
(and its response source selected) against the address it actually
arrived on.
Seen in the field with a CoAP remote control whose GET /endpoint was
answered via the async path while it also multicast periodic
announcements from the same source port: the device received the empty
ACK and then never got the response.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Clean up duplicate MID handling, and just send back last response if MID
matches.
Removed COAP_BLOCK_CACHE_RESPONSE as no longer required for duplicate MID
handling, and so no need to send an Empty ACK response for a subsequent
Separate response as a referenced copy of the last PDU respponse is now kept
for a session.
Note that if a request is sent by a proxy to an upstream server, this always
converts the upstream server's response into a Separate response back to the
client as the upstream server may take time to respond, do any error recovery
etc.
If both MEMP_USE_CUSTOM_POOLS and MEM_USE_POOLS are defined in
lwipopts.h and there is a suitable
LWIP_MALLOC_MEMPOOL_START .. LWIP_MALLOC_MEMPOOL_END
definition in lwippools.h, then any coap_malloc_type(COAP_STRING, ..) will
make use of the defined pools.
A new src/coap_mem_lwip.c file has been added.
Add reference counting to recipient contexts so they can be safely removed
while still in use by active sessions. Expose callback hooks for credential
lookup, echo, and sequence storage to allow custom backends.
Expand example server to demonstrate the external storage via a simple
file based database. Expand coap_oscore_rcp_conf_t to enable settings
preconfigured echo value, sequence counter and window value as uint64
Enable temporary credentials returned by the find function and are destroyed
after use.
Check incoming Uri-Path and Location-Path options for just . or .. (or %2e
hex encoded variants) and treat as being invalid.
Stop insertion of Uri-Path and Location-Path options that contain just
. or .. (or %2e hex encoded variants).
Don't report
WARN coap_socket_accept_tcp: accept: Resource temporarily unavailable
when multiple threads are trying to process the same incoming TCP connection.