Skip to content

Conversation

@MegaManSec
Copy link
Contributor

Previously, doV2Query() trusted header.length and passed a potentially
unterminated URL to consumers, which could lead to out-of-bounds reads
on truncated ICPv2 QUERY packets. This is fixed by requiring length >
sizeof(icp_common_t)+4, guaranteeing at least one URL byte, and writing
a NUL within the received packet bounds; short packets are logged and
dropped.

Previously, the incoming ICP socket installed the read handler before
multicast/outgoing setup, leaving a window where icpHandleUdp() could
run with icpOutgoingConn == nullptr (when udp_outgoing.isNoAddr()) and
attempt to dereference icpOutgoingConn->local. This is fixed by
registering the read handler after initialization completes.

Previously, icpGetRequest() reassigned url = rfc1738_escape(url) on the
error path, causing double encoding of the URL used in the error
response.

Require len > sizeof(hdr)+4 and write NUL within packet bounds.
Among other things, this patch ensures that derefs of
`icpOutgoingConn->local` do not result in null-pointer deref. For
example, when Config.Addrs.udp_outgoing.isNoAddr() is true,
icpOutgoingConn is not set in icpOpenPorts(). It is later assigned
in icpIncomingConnectionOpened() after the read handler is installed.
That short window allows icpHandleUdp() to run with
icpOutgoingConn == nullptr.
Copy link
Contributor

@yadij yadij left a comment

Choose a reason for hiding this comment

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

Yay one less rfc1738_escape.
Some code polish needed.

const auto qhdr = hdr + sizeof(uint32_t);
const auto pkt_len = static_cast<size_t>(header.length);
if (pkt_len <= qhdr) {
debugs(12, 3, "icpHandleIcpV2: short ICP_QUERY from " << from);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
debugs(12, 3, "icpHandleIcpV2: short ICP_QUERY from " << from);
debugs(12, 3, "short ICP_QUERY from " << from);

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
debugs(12, 3, "icpHandleIcpV2: short ICP_QUERY from " << from);
debugs(12, 3, "too small query from " << from);

or (if we have already checked header size) perhaps

Suggested change
debugs(12, 3, "icpHandleIcpV2: short ICP_QUERY from " << from);
debugs(12, 3, "query without URL from " << from);

"ICP" context will be added to output by debugs() because this file name is "icp_v2.cc".

Other related messages already use "too small" terminology. I do not insist on it, but the message should indicate that there is a problem here.

Comment on lines +484 to +485
const auto hdr = sizeof(icp_common_t);
const auto qhdr = hdr + sizeof(uint32_t);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a size. Please indicate that in the variable name.

Suggested change
const auto hdr = sizeof(icp_common_t);
const auto qhdr = hdr + sizeof(uint32_t);
const auto queryHeaderSize = sizeof(icp_common_t) + sizeof(uint32_t);

related lines will need adjusting to match

@yadij yadij added the S-waiting-for-author author action is expected (and usually required) label Sep 8, 2025
@yadij yadij changed the title icp_v2: bound-check ICP_QUERY; register read after init ICP: bound-check ICP_QUERY; register read after init Sep 8, 2025
const auto qhdr = hdr + sizeof(uint32_t);
const auto pkt_len = static_cast<size_t>(header.length);
if (pkt_len <= qhdr) {
debugs(12, 3, "icpHandleIcpV2: short ICP_QUERY from " << from);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
debugs(12, 3, "icpHandleIcpV2: short ICP_QUERY from " << from);
debugs(12, 3, "too small query from " << from);

or (if we have already checked header size) perhaps

Suggested change
debugs(12, 3, "icpHandleIcpV2: short ICP_QUERY from " << from);
debugs(12, 3, "query without URL from " << from);

"ICP" context will be added to output by debugs() because this file name is "icp_v2.cc".

Other related messages already use "too small" terminology. I do not insist on it, but the message should indicate that there is a problem here.

debugs(12, DBG_IMPORTANT, "Sending ICP messages from " << icpOutgoingConn->local);
}

Comm::SetSelect(conn->fd, COMM_SELECT_READ, icpHandleUdp, nullptr, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Previously, the incoming ICP socket installed the read handler before multicast/outgoing setup, leaving a window where icpHandleUdp() could run with icpOutgoingConn == nullptr (when udp_outgoing.isNoAddr()) and attempt to dereference icpOutgoingConn->local. This is fixed by registering the read handler after initialization completes.

AFAICT, the above allegation is misleading or incorrect: Calling Comm::SetSelect(handler) must never call the handler callback synchronously/immediately as it may result in "reentrant" code; the "window" described above does not exist. I/O handlers are called much later, by Comm::DoSelect() and such.

Official code is structured/ordered slightly better IMO. Please undo this change unless my analysis above is flawed.

It is probably a good idea to add a "icpOutgoingConn is set" assertion to icpHandleUdp(). AFAICT, we have similar assertions elsewhere, and existing module initialization code is trying to uphold that invariant (albeit clumsily; if you want to improve that, I would consider replacing the above if...isNoAddr statement condition to !icpOutgoingConn).

Please do not forget to update PR title/description after addressing this change request.

}
/* Guarantee an in-bounds NUL terminator for any downstream C-string use. */
buf[pkt_len - 1] = '\0';
auto url = buf + qhdr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto url = buf + qhdr;
const auto url = buf + queryHeaderSize;

int rtt = 0;
int src_rtt = 0;
uint32_t flags = 0;
/* We have a valid packet */
Copy link
Contributor

Choose a reason for hiding this comment

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

PR description: doV2Query() trusted header.length and passed a potentially
unterminated URL to consumers, which could lead to out-of-bounds reads
on truncated ICPv2 QUERY packets

doV2Query() caller validates that the query was not truncated:

    /*
     * Length field should match the number of bytes read
     */

    if (len != header.length) {
        debugs(12, 3, "icpHandleIcpV2: ICP message is too small");
        return;
    }

Moreover, doV2Query grand-caller -- icpHandleUdp() -- unconditionally terminates the buffer:

        buf[len] = '\0';

If the above analysis is correct, then neither alleged truncation nor lack of URL termination are real. If you agree, please adjust this PR metadata and code accordingly.

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

Labels

S-waiting-for-author author action is expected (and usually required)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants