Skip to content

udp_multicast: drop malformed datagrams instead of raising in _recv_internal - #2174

Merged
zariiii9003 merged 2 commits into
hardbyte:mainfrom
ucsurf:fix-udp-multicast-malformed-datagram
Dec 1, 2026
Merged

zariiii9003 merged 2 commits into
hardbyte:mainfrom
ucsurf:fix-udp-multicast-malformed-datagram

Conversation

@ucsurf

@ucsurf ucsurf commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

  • UdpMulticastBus._recv_internal() now logs and drops a datagram that cannot be decoded into a can.Message, instead of raising CanOperationError. A raised error there ends a thread-based Notifier, so one bad datagram stopped message delivery.
  • Socket, select, ancillary-data and timestamp errors from recv() are still raised as CanOperationError. This matches the existing handling of FD frames on a non-FD bus, which are already dropped.

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Tests: Added test/test_udp_multicast.py; those 3 tests pass locally with a mocked socket; full suite/tox not run. The contribution-guide box is left unticked because tox and the full suite were not run. No documentation change was needed beyond the news fragment.

Credit: Ugur Celik (ucsurf), @ucsurf

— Ugur Celik (ucsurf) · https://uc.surf
We find and report security issues in open source before attackers do. If our work helped you, a small contribution keeps it going and makes the internet a little safer for everyone:
https://buymeacoffee.com/ucsurf

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: udp_multicast: drop malformed datagrams instead of raising

The fix is correct and worthwhile. On main, a single datagram that does not decode into a can.Message raises CanOperationError from recv(). This ends a thread-based Notifier, so valid frames that arrive later are never delivered. With this PR the datagram is logged together with its sender and dropped, and reception continues. Socket and select() errors are still raised.

Lint failure

  • can/interfaces/udp_multicast/bus.py:139: tox -e lint (ruff check can) now reports BLE001, because the blind except Exception no longer re-raises. Please keep the broad catch: a datagram whose data field is a large integer raises OverflowError or MemoryError in Message.__init__, and a narrower clause would let such a datagram stop the Notifier again. Suppress the rule as can/interface.py and can/interfaces/nixnet.py do:
    except Exception as exception:  # noqa: BLE001

Tests

  • test/back2back_test.py already covers UdpMulticastBus (BasicTestUdpMulticastBusIPv4/IPv6). Please test the fix there, through a real socket and the public recv(), instead of patching the internal GeneralPurposeUdpMulticastBus in a new file. For example, in BasicTestUdpMulticastBusIPv4 (plus import socket):
    def test_malformed_datagram_is_dropped(self):
        with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
            sock.sendto(b"\x00", (self.CHANNEL_2, 43113))
            sender_port = sock.getsockname()[1]
        msg = can.Message(arbitration_id=0x42)
        self.bus1.send(msg)
    
        with self.assertLogs("can.interfaces.udp_multicast.bus", "WARNING") as logs:
            self._check_received_message(self.bus2.recv(self.TIMEOUT), msg)
        self.assertIn(f", {sender_port})", logs.output[0])
    This test fails on main and when the sender is removed from the log message.
  • Please then delete test/test_udp_multicast.py:
    • test_valid_message_is_received duplicates test_timestamp, test_timestamp_is_absolute and test_sub_second_timestamp_resolution in the same back2back_test.py classes.
    • test_socket_error_is_still_raised covers code this PR does not change, and only by patching an internal class.

News fragment

  • doc/changelog.d/+udp-multicast-malformed-datagram.fixed.rst: the contribution guide reserves + fragments for changes with no issue or PR. Please rename it to 2173.fixed.rst.

Verdict: request changes

ucsurf and others added 2 commits December 1, 2026 03:11
A datagram that does not decode to a can.Message is bad input from a
sender, not a bus or socket fault. Log it and keep receiving so a
thread-based Notifier is not stopped. Socket, select and timestamp
errors from recv() are still raised as CanOperationError.
Keep the broad except with a BLE001 noqa, test the drop through a real
socket on BasicTestUdpMulticastBusIPv4, remove the mock-based unit file,
and rename the news fragment to 2173.fixed.rst.
@ucsurf
ucsurf force-pushed the fix-udp-multicast-malformed-datagram branch from f2987f5 to 8da1ed7 Compare December 1, 2026 00:13
@ucsurf

ucsurf commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review — I've applied the requested changes.

The broad except Exception is unchanged, with # noqa: BLE001 to silence the lint. The drop is now covered by test_malformed_datagram_is_dropped on BasicTestUdpMulticastBusIPv4 (I left the IPv6 class alone; its sender tuple wouldn't match the same assertion), and test/test_udp_multicast.py is gone. The news fragment is now 2173.fixed.rst, and the branch is rebased onto current main.

— Ugur Celik (ucsurf) · https://uc.surf
We find and report security issues in open source before attackers do. If our work helped you, a small contribution keeps it going and makes the internet a little safer for everyone: https://buymeacoffee.com/ucsurf

@zariiii9003 zariiii9003 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you

@zariiii9003
zariiii9003 merged commit ff79560 into hardbyte:main Dec 1, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UdpMulticastBus._recv_internal() raises CanOperationError for a malformed datagram, which stops a thread-based Notifier

2 participants