From nobody Mon May 30 10:57:25 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 65B701B666CF; Mon, 30 May 2022 10:57:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LBXRn2JN4z4r0g; Mon, 30 May 2022 10:57:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653908245; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mQuubVmr5l1hBvy9y61e2tgxfJ7T9Y1nXOa0Lbx7wis=; b=PHQM4/ZP8b21XFZQDBLsBphpm5KwKNKggvGW/0wGooy1zqV8F3yOb+gnttGxSqM0u61C4s dgP+yBKErhgZQuqcIZoOnkcclVQMUsy76YbW+U6h+PzcRGrJHN85vG5Cln0FIMXPNEIYC3 O9jvIR0GA2uJlH+7IHMdIIi/k4Bz1BM+Zb/R5mUwDr0N6zsN0ur+lzo3xpgppnK3y9cmOW iYsoQpHc1Bg4GtXo1u6amA/GW1ucV/wRd8LI3wTmlgmgUUbiRrkuN3FNCZEPXm2/Ct2Rog b/OCqiC1K0N5Nt9Ni9mi+L0JyOijQvf7t3h9tpEIsSsQekUWzCepjGTxLVZGXA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 301D615AF2; Mon, 30 May 2022 10:57:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 24UAvPnv067281; Mon, 30 May 2022 10:57:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 24UAvP8b067280; Mon, 30 May 2022 10:57:25 GMT (envelope-from git) Date: Mon, 30 May 2022 10:57:25 GMT Message-Id: <202205301057.24UAvP8b067280@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 08e77283b7ba - main - sockstat: be more verbose when reporting kernel/userland size mismatch. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 08e77283b7baa63c04373dc099cfed4fed308f92 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653908245; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mQuubVmr5l1hBvy9y61e2tgxfJ7T9Y1nXOa0Lbx7wis=; b=BJdAa3HtqlRfqU5mYiWV961MqWI8bb/E4yLaLWb+jRmc9wVot8YF9RuHRMvPlE4iPnO3g2 1h+wftIWsLqK1j6DsyXvOjlAC8AkEXaUdlzROv2D13WLJxOwowI6Sz7hPvt7afVsagoZsk JO8TOXj5S7asU3gYX3R3a+UpZoxei2WHTgg7CiRfWYKYENNm4/Ro3gU38clRxiSdAtE0p3 1GKurOvoWJ+57yFJwSG1ZYoFcJ3/uOoFSQXrjnDPHsz5xYKm/mTifuja/klBra7zi22RJb UTE17Ckx3C22gRj7keN/9rS8rZbFtpzRCg7GVoSrqa4sac4qkGUrX9I9WWG+CQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1653908245; a=rsa-sha256; cv=none; b=SNSuhskMGl+Kil/sm1/y0czE/NdJgx7+RUD4ZjEmimcs6hy/mrgAXpBhAlrWnvT1Bqm3rw VCUDPCggNfb+CJUEjzPo6pkL6PzN+hclIy8kfKu6crRbgGFsAqqst8EzhuWRKlQ4Th5i+G 7jCaeNri9ZcYWZW6DI6k3D8FDEy21Nfn5d48NbCcT0awXhDC8v4TgUePCZDL6BJFUQExQr hLLbdOWmVOx8/xZCHLhThQQLw462hEiVNV26nrZk1gd7vol/SPcMPA82YmfmRSWnPgtIGz CfpLTd6Komi0L8BWgoLe5z18p5yTk3t/gGjOdW83+uNDDnywH89w2TpFQpRDog== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=08e77283b7baa63c04373dc099cfed4fed308f92 commit 08e77283b7baa63c04373dc099cfed4fed308f92 Author: Alexander V. Chernikov AuthorDate: 2022-05-30 10:54:53 +0000 Commit: Alexander V. Chernikov CommitDate: 2022-05-30 10:54:53 +0000 sockstat: be more verbose when reporting kernel/userland size mismatch. MFC after: 2 weeks --- usr.bin/sockstat/sockstat.c | 49 ++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c index 7a84b5ad47a3..fc53d910012d 100644 --- a/usr.bin/sockstat/sockstat.c +++ b/usr.bin/sockstat/sockstat.c @@ -161,6 +161,28 @@ xprintf(const char *fmt, ...) return (len); } +static bool +_check_ksize(size_t received_size, size_t expected_size, const char *struct_name) +{ + if (received_size != expected_size) { + warnx("%s size mismatch: expected %zd, received %zd", + struct_name, expected_size, received_size); + return false; + } + return true; +} +#define check_ksize(_sz, _struct) (_check_ksize(_sz, sizeof(_struct), #_struct)) + +static void +_enforce_ksize(size_t received_size, size_t expected_size, const char *struct_name) +{ + if (received_size != expected_size) { + errx(1, "fatal: struct %s size mismatch: expected %zd, received %zd", + struct_name, expected_size, received_size); + } +} +#define enforce_ksize(_sz, _struct) (_enforce_ksize(_sz, sizeof(_struct), #_struct)) + static int get_proto_type(const char *proto) { @@ -648,9 +670,8 @@ gather_inet(int proto) xig = (struct xinpgen *)buf; exig = (struct xinpgen *)(void *) ((char *)buf + len - sizeof *exig); - if (xig->xig_len != sizeof *xig || - exig->xig_len != sizeof *exig) - errx(1, "struct xinpgen size mismatch"); + enforce_ksize(xig->xig_len, struct xinpgen); + enforce_ksize(exig->xig_len, struct xinpgen); } while (xig->xig_gen != exig->xig_gen && retry--); if (xig->xig_gen != exig->xig_gen && opt_v) @@ -664,19 +685,15 @@ gather_inet(int proto) case IPPROTO_TCP: xtp = (struct xtcpcb *)xig; xip = &xtp->xt_inp; - if (xtp->xt_len != sizeof(*xtp)) { - warnx("struct xtcpcb size mismatch"); + if (!check_ksize(xtp->xt_len, struct xtcpcb)) goto out; - } protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp"; break; case IPPROTO_UDP: case IPPROTO_DIVERT: xip = (struct xinpcb *)xig; - if (xip->xi_len != sizeof(*xip)) { - warnx("struct xinpcb size mismatch"); + if (!check_ksize(xip->xi_len, struct xinpcb)) goto out; - } break; default: errx(1, "protocol %d not supported", proto); @@ -798,11 +815,9 @@ gather_unix(int proto) xug = (struct xunpgen *)buf; exug = (struct xunpgen *)(void *) ((char *)buf + len - sizeof(*exug)); - if (xug->xug_len != sizeof(*xug) || - exug->xug_len != sizeof(*exug)) { - warnx("struct xinpgen size mismatch"); + if (!check_ksize(xug->xug_len, struct xunpgen) || + !check_ksize(exug->xug_len, struct xunpgen)) goto out; - } } while (xug->xug_gen != exug->xug_gen && retry--); if (xug->xug_gen != exug->xug_gen && opt_v) @@ -813,10 +828,8 @@ gather_unix(int proto) if (xug >= exug) break; xup = (struct xunpcb *)xug; - if (xup->xu_len != sizeof(*xup)) { - warnx("struct xunpcb size mismatch"); + if (!check_ksize(xup->xu_len, struct xunpcb)) goto out; - } if ((xup->unp_conn == 0 && !opt_l) || (xup->unp_conn != 0 && !opt_c)) continue; @@ -864,8 +877,8 @@ getfiles(void) if ((xfiles = realloc(xfiles, len)) == NULL) err(1, "realloc()"); } - if (len > 0 && xfiles->xf_size != sizeof(*xfiles)) - errx(1, "struct xfile size mismatch"); + if (len > 0) + enforce_ksize(xfiles->xf_size, struct xfile); nxfiles = len / sizeof(*xfiles); }