Skip to content

incorrectly reclaimed tcp handler can cause data corruption and segfault #1309

Closed

Description

@dwpfitzner

Describe the bug
A clear and concise description of what the bug is.

For quite a long time (about a year) we (Netflix) have been seeing occasional crashes from unbound, usually in minievent_del(), and related to a corrupt rbtree. It has been difficult to reproduce, and thus diagnose, but I think I now have the root cause, as follows:

First, you need multiple clients making requests over TCP (eg, because the response is too large for UDP). Secondly, you need an upstream where requests can time out (eg, because unbound is running in a remote part of the internet). And you probably also need to satisfy some race conditions.

Part of the issue is that the unbound "mesh" code can have the same comm_reply struct multiple times in a m->reply_list. (I'm not sure how this situation happens, maybe if a client retries a request while unbound is waiting for upstream?). Then on a timeout condition, the mesh code can call comm_point_drop_reply() multiple times for the same comm_point struct. Normally such extra comm_point_drop_reply() calls are not a problem, because, eg, they see that the comm_point is already closed, so do nothing. But they end up calling reclaim_tcp_handler(), which has a somewhat fragile check that it does not reclaim the handler if it is already at the "front" on the tcp_free list. That normally works for such duplicate calls, but there can be a problem if there is another comm_point in the same reply_list in the "right" order. Eg, say the reply_list has:
(comm_point A), (comm_point B), (comm_point A)
with neither A nor B in the free list. Then first we reclaim A, and put it on the free list, then we reclaim B and put it on the free list, then we check reclaiming A, and it is not at the front of the free list, so we reclaim it again, producing a free list with a loop:
parent -> A -> B -> A

Once we have a loop in the tcp_free list as above, then later if we need to use three tcp handlers at the same time, we will first use A, then B, then A again, even though A is already is use. Which can produce data corruption, and a segfault. (Eg, in detail: On re-using A, we free A's event, even though the event is in-use, and present in an rbtree. Then we allocate a new event, which may be the same memory, and initializing that event NULLs the rbtree pointers. Then we go to add the event, but notice that the event_added flag is already set, so first try to remove the event. In doing so we come across the re-used tree node, now with zeroed pointers, and segfault.)

There are various ways this could be fixed. Eg, add a flag to the comm_point struct which says whether the struct is in its parent's free list. (And then use that flag in reclaim_tcp_handler() to check whether to add the struct to the free list.) See attached patch for an example implementation. Or, maybe set the tcp_free pointer to some (non-NULL) sentinel value when the struct is not in its parent's free list.

fix.patch

To reproduce
Steps to reproduce the behavior: It is difficult to reproduce on-demand, but see above.
1.
2.
3.

Expected behavior
A clear and concise description of what you expected to happen.
No crashes :-)

System:

  • Unbound version: 1.23.1, and some earlier versions
  • OS: FreeBSD
  • unbound -V output:

Additional information
Add any other information that you may have gathered about the issue here.

Activity

  1. dwpfitzner commented on Aug 5, 2025

    @dwpfitzner
    Author

    Possibly related: #1292

  2. dwpfitzner commented on Aug 5, 2025

    @dwpfitzner
    Author

    Possibly related #1113

  3. wcawijngaards commented on Aug 5, 2025

    @wcawijngaards
    Member

    Thank you for the patch! I have committed it to fix the issue. That is nice to have as a fix for crash issues, and could likely be the referenced issues. The patch passes the test set.

  4. wcawijngaards commented on Sep 15, 2025

    @wcawijngaards
    Member

    The data corruption, specifically the double reclaim of the event, is maybe what causes the high CPU usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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