Bug 298085 - hastd/hastctl abort on a legitimate zero-size protocol message (hast_proto_recv_hdr asserts non-NULL on an empty ebuf)
Summary: hastd/hastctl abort on a legitimate zero-size protocol message (hast_proto_re...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 16.0-CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Dag-Erling Smørgrav
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-08-31 22:38 UTC by Glen Barber
Modified: 2026-10-14 16:05 UTC (History)
4 users (show)

See Also:
des: mfc-stable15+
des: mfc-stable14+


Attachments
patch to only dereference/receive when there's actually a nonzero-size payload to read (541 bytes, patch)
2026-08-31 22:40 UTC, Glen Barber
no flags Details | Diff
fixed patch (531 bytes, patch)
2026-08-31 22:44 UTC, Glen Barber
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Glen Barber freebsd_committer freebsd_triage 2026-08-31 22:38:30 UTC
hastd and hastctl both link hast_proto.c's hast_proto_recv_hdr(), which asserts that ebuf_data() returns a non-NULL pointer before receiving a message's payload — but ebuf_data() legitimately returns NULL for a zero-size buffer, and a zero-size payload (a bare protocol header with no attached nv data) is a valid message on the wire. Any peer that sends one crashes the receiving process with SIGABRT.

In practice this makes service hastd onerestart 100% reproducible crash, even against a completely empty /etc/hast.conf: hastd's own stop_precmd (see /etc/rc.d/hastd) runs hastctl role init all before stopping the daemon, and that exchange includes a zero-size message — so hastctl (not hastd itself) aborts every single time.

Environment:
- FreeBSD 16.0-CURRENT, main-n288684-420428718da7, amd64
- Reproduced independently on two separate physical/virtual machines running the same CURRENT snapshot
- Not related to the connection-migration bug tracked in bug 292322 / reviewed as D57511 (a different function, proto_common_recv's MSG_WAITALL sizing) — this is a separate defect

Reproduction:
On a system with hastd_enable="YES" (an otherwise empty /etc/hast.conf is sufficient — no resources need to be configured):

  # service hastd onerestart

Proposed fix: Only dereference/receive when there's actually a nonzero-size payload to read — there is nothing to assert or receive when hdr.size == 0:

--- a/sbin/hastd/hast_proto.c
+++ b/sbin/hastd/hast_proto.c
@@ -149,10 +149,12 @@ hast_proto_recv_hdr(const struct proto_conn *conn, struct nv **nvp)
 	if (ebuf_add_tail(eb, NULL, hdr.size) == -1)
 		goto fail;
-	hptr = ebuf_data(eb, NULL);
-	PJDLOG_ASSERT(hptr != NULL);
-	if (proto_recv(conn, hptr, hdr.size) == -1)
-		goto fail;
+	if (hdr.size > 0) {
+		hptr = ebuf_data(eb, NULL);
+		PJDLOG_ASSERT(hptr != NULL);
+		if (proto_recv(conn, hptr, hdr.size) == -1)
+			goto fail;
+	}
 	nv = nv_ntoh(eb);
 	if (nv == NULL)
 		goto fail;


This has been built and tested locally: hastd/hastctl rebuilt from a patched tree, installed on two machines, and service hastd onerestart now succeeds cleanly and repeatedly on both, including under a real, active hastd configuration (not just an empty one).
Comment 1 Glen Barber freebsd_committer freebsd_triage 2026-08-31 22:40:20 UTC
Created attachment 274320 [details]
patch to only dereference/receive when there's actually a nonzero-size payload to read
Comment 2 Glen Barber freebsd_committer freebsd_triage 2026-08-31 22:44:08 UTC
Created attachment 274321 [details]
fixed patch

Hopefully the attachment is not mangled this time.
Comment 3 Mark Johnston freebsd_committer freebsd_triage 2026-09-01 14:36:32 UTC
Do all callers of hast_proto_recv_hdr() actually handle the case where the payload size is zero?  Are they potentially relying on this assertion to guard against malicious payloads?
Comment 4 Dag-Erling Smørgrav freebsd_committer freebsd_triage 2026-09-01 15:55:29 UTC
https://reviews.freebsd.org/D59306
Comment 5 Dag-Erling Smørgrav freebsd_committer freebsd_triage 2026-09-01 16:24:38 UTC
(In reply to Mark Johnston from comment #3)
Callers of hast_proto_recv_hdr() get an nvlist.  If hdr.size == 0, that nvlist is empty and nv_get_*() fails, which the code already checks for.
Comment 6 commit-hook freebsd_committer freebsd_triage 2026-09-03 08:00:11 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=8646d65b45339642d4aab1de35a2bc79fc45f09e

commit 8646d65b45339642d4aab1de35a2bc79fc45f09e
Author:     Glen Barber <gjb@FreeBSD.org>
AuthorDate: 2026-09-03 07:51:28 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-09-03 07:58:54 +0000

    hastd: Fix crash on empty message

    A HAST message can be empty, in which case ebuf_add_tail() does nothing
    and ebuf_data() returns NULL because the size of the ebuf is zero, but
    hast_proto_recv_hdr() asserts that the return value is not NULL,
    resulting in an immediate crash if hastctl or hastd receive an empty
    message.  This is trivially reproducable by running `hastctl status` or
    `hastctl role init` (as the rc script does prior to stopping hastd).

    To avoid this, don't try to grow the ebuf or receive additional data
    if the header size is zero.

    PR:             298085
    MFC after:      3 days
    Reviewed by:    kevans, gjb
    Differential Revision:  https://reviews.freebsd.org/D59306

 sbin/hastd/hast_proto.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2026-09-03 11:57:56 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=2581bc3af3815e92b0eef44bee224fff92931de8

commit 2581bc3af3815e92b0eef44bee224fff92931de8
Author:     Glen Barber <gjb@FreeBSD.org>
AuthorDate: 2026-09-03 07:51:28 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-09-03 11:56:38 +0000

    hastd: Fix crash on empty message

    A HAST message can be empty, in which case ebuf_add_tail() does nothing
    and ebuf_data() returns NULL because the size of the ebuf is zero, but
    hast_proto_recv_hdr() asserts that the return value is not NULL,
    resulting in an immediate crash if hastctl or hastd receive an empty
    message.  This is trivially reproducable by running `hastctl status` or
    `hastctl role init` (as the rc script does prior to stopping hastd).

    To avoid this, don't try to grow the ebuf or receive additional data
    if the header size is zero.

    PR:             298085
    MFC after:      3 days
    Reviewed by:    kevans, gjb
    Differential Revision:  https://reviews.freebsd.org/D59306

    (cherry picked from commit 8646d65b45339642d4aab1de35a2bc79fc45f09e)

 sbin/hastd/hast_proto.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
Comment 8 commit-hook freebsd_committer freebsd_triage 2026-09-03 11:57:57 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=4d83686bc38780405459b45819e9f6482dcd335c

commit 4d83686bc38780405459b45819e9f6482dcd335c
Author:     Glen Barber <gjb@FreeBSD.org>
AuthorDate: 2026-09-03 07:51:28 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-09-03 11:56:44 +0000

    hastd: Fix crash on empty message

    A HAST message can be empty, in which case ebuf_add_tail() does nothing
    and ebuf_data() returns NULL because the size of the ebuf is zero, but
    hast_proto_recv_hdr() asserts that the return value is not NULL,
    resulting in an immediate crash if hastctl or hastd receive an empty
    message.  This is trivially reproducable by running `hastctl status` or
    `hastctl role init` (as the rc script does prior to stopping hastd).

    To avoid this, don't try to grow the ebuf or receive additional data
    if the header size is zero.

    PR:             298085
    MFC after:      3 days
    Reviewed by:    kevans, gjb
    Differential Revision:  https://reviews.freebsd.org/D59306

    (cherry picked from commit 8646d65b45339642d4aab1de35a2bc79fc45f09e)

 sbin/hastd/hast_proto.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2026-10-14 16:04:04 UTC
A commit in branch releng/14.4 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=a6ccb9d6905beabbfccb18b01d454695b496deea

commit a6ccb9d6905beabbfccb18b01d454695b496deea
Author:     Glen Barber <gjb@FreeBSD.org>
AuthorDate: 2026-09-03 07:51:28 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-10-13 13:47:11 +0000

    hastd: Fix crash on empty message

    A HAST message can be empty, in which case ebuf_add_tail() does nothing
    and ebuf_data() returns NULL because the size of the ebuf is zero, but
    hast_proto_recv_hdr() asserts that the return value is not NULL,
    resulting in an immediate crash if hastctl or hastd receive an empty
    message.  This is trivially reproducable by running `hastctl status` or
    `hastctl role init` (as the rc script does prior to stopping hastd).

    To avoid this, don't try to grow the ebuf or receive additional data
    if the header size is zero.

    Approved by:    so
    Security:       FreeBSD-EN-26:26.hastd
    PR:             298085
    MFC after:      3 days
    Reviewed by:    kevans, gjb
    Differential Revision:  https://reviews.freebsd.org/D59306

    (cherry picked from commit 8646d65b45339642d4aab1de35a2bc79fc45f09e)
    (cherry picked from commit 4d83686bc38780405459b45819e9f6482dcd335c)

 sbin/hastd/hast_proto.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
Comment 10 commit-hook freebsd_committer freebsd_triage 2026-10-14 16:05:06 UTC
A commit in branch releng/14.5 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=3d10f8cdb001655ba0638c933820abbd0130c1eb

commit 3d10f8cdb001655ba0638c933820abbd0130c1eb
Author:     Glen Barber <gjb@FreeBSD.org>
AuthorDate: 2026-09-03 07:51:28 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-10-13 14:03:00 +0000

    hastd: Fix crash on empty message

    A HAST message can be empty, in which case ebuf_add_tail() does nothing
    and ebuf_data() returns NULL because the size of the ebuf is zero, but
    hast_proto_recv_hdr() asserts that the return value is not NULL,
    resulting in an immediate crash if hastctl or hastd receive an empty
    message.  This is trivially reproducable by running `hastctl status` or
    `hastctl role init` (as the rc script does prior to stopping hastd).

    To avoid this, don't try to grow the ebuf or receive additional data
    if the header size is zero.

    Approved by:    so
    Security:       FreeBSD-EN-26:26.hastd
    PR:             298085
    MFC after:      3 days
    Reviewed by:    kevans, gjb
    Differential Revision:  https://reviews.freebsd.org/D59306

    (cherry picked from commit 8646d65b45339642d4aab1de35a2bc79fc45f09e)
    (cherry picked from commit 4d83686bc38780405459b45819e9f6482dcd335c)

 sbin/hastd/hast_proto.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
Comment 11 commit-hook freebsd_committer freebsd_triage 2026-10-14 16:05:07 UTC
A commit in branch releng/15.1 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ddbf289ac125447c1bcccf784a05f263d8676186

commit ddbf289ac125447c1bcccf784a05f263d8676186
Author:     Glen Barber <gjb@FreeBSD.org>
AuthorDate: 2026-09-03 07:51:28 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-10-13 14:10:21 +0000

    hastd: Fix crash on empty message

    A HAST message can be empty, in which case ebuf_add_tail() does nothing
    and ebuf_data() returns NULL because the size of the ebuf is zero, but
    hast_proto_recv_hdr() asserts that the return value is not NULL,
    resulting in an immediate crash if hastctl or hastd receive an empty
    message.  This is trivially reproducable by running `hastctl status` or
    `hastctl role init` (as the rc script does prior to stopping hastd).

    To avoid this, don't try to grow the ebuf or receive additional data
    if the header size is zero.

    Approved by:    so
    Security:       FreeBSD-EN-26:26.hastd
    PR:             298085
    MFC after:      3 days
    Reviewed by:    kevans, gjb
    Differential Revision:  https://reviews.freebsd.org/D59306

    (cherry picked from commit 8646d65b45339642d4aab1de35a2bc79fc45f09e)
    (cherry picked from commit 2581bc3af3815e92b0eef44bee224fff92931de8)

 sbin/hastd/hast_proto.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)