1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-28 08:02:54 +00:00

sockstat: be more verbose when reporting kernel/userland size mismatch.

MFC after:	2 weeks
This commit is contained in:
Alexander V. Chernikov 2022-05-30 10:54:53 +00:00
parent d6cd20cc5c
commit 08e77283b7

View File

@ -161,6 +161,28 @@ xprintf(const char *fmt, ...)
return (len); 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 static int
get_proto_type(const char *proto) get_proto_type(const char *proto)
{ {
@ -648,9 +670,8 @@ gather_inet(int proto)
xig = (struct xinpgen *)buf; xig = (struct xinpgen *)buf;
exig = (struct xinpgen *)(void *) exig = (struct xinpgen *)(void *)
((char *)buf + len - sizeof *exig); ((char *)buf + len - sizeof *exig);
if (xig->xig_len != sizeof *xig || enforce_ksize(xig->xig_len, struct xinpgen);
exig->xig_len != sizeof *exig) enforce_ksize(exig->xig_len, struct xinpgen);
errx(1, "struct xinpgen size mismatch");
} while (xig->xig_gen != exig->xig_gen && retry--); } while (xig->xig_gen != exig->xig_gen && retry--);
if (xig->xig_gen != exig->xig_gen && opt_v) if (xig->xig_gen != exig->xig_gen && opt_v)
@ -664,19 +685,15 @@ gather_inet(int proto)
case IPPROTO_TCP: case IPPROTO_TCP:
xtp = (struct xtcpcb *)xig; xtp = (struct xtcpcb *)xig;
xip = &xtp->xt_inp; xip = &xtp->xt_inp;
if (xtp->xt_len != sizeof(*xtp)) { if (!check_ksize(xtp->xt_len, struct xtcpcb))
warnx("struct xtcpcb size mismatch");
goto out; goto out;
}
protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp"; protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
break; break;
case IPPROTO_UDP: case IPPROTO_UDP:
case IPPROTO_DIVERT: case IPPROTO_DIVERT:
xip = (struct xinpcb *)xig; xip = (struct xinpcb *)xig;
if (xip->xi_len != sizeof(*xip)) { if (!check_ksize(xip->xi_len, struct xinpcb))
warnx("struct xinpcb size mismatch");
goto out; goto out;
}
break; break;
default: default:
errx(1, "protocol %d not supported", proto); errx(1, "protocol %d not supported", proto);
@ -798,11 +815,9 @@ gather_unix(int proto)
xug = (struct xunpgen *)buf; xug = (struct xunpgen *)buf;
exug = (struct xunpgen *)(void *) exug = (struct xunpgen *)(void *)
((char *)buf + len - sizeof(*exug)); ((char *)buf + len - sizeof(*exug));
if (xug->xug_len != sizeof(*xug) || if (!check_ksize(xug->xug_len, struct xunpgen) ||
exug->xug_len != sizeof(*exug)) { !check_ksize(exug->xug_len, struct xunpgen))
warnx("struct xinpgen size mismatch");
goto out; goto out;
}
} while (xug->xug_gen != exug->xug_gen && retry--); } while (xug->xug_gen != exug->xug_gen && retry--);
if (xug->xug_gen != exug->xug_gen && opt_v) if (xug->xug_gen != exug->xug_gen && opt_v)
@ -813,10 +828,8 @@ gather_unix(int proto)
if (xug >= exug) if (xug >= exug)
break; break;
xup = (struct xunpcb *)xug; xup = (struct xunpcb *)xug;
if (xup->xu_len != sizeof(*xup)) { if (!check_ksize(xup->xu_len, struct xunpcb))
warnx("struct xunpcb size mismatch");
goto out; goto out;
}
if ((xup->unp_conn == 0 && !opt_l) || if ((xup->unp_conn == 0 && !opt_l) ||
(xup->unp_conn != 0 && !opt_c)) (xup->unp_conn != 0 && !opt_c))
continue; continue;
@ -864,8 +877,8 @@ getfiles(void)
if ((xfiles = realloc(xfiles, len)) == NULL) if ((xfiles = realloc(xfiles, len)) == NULL)
err(1, "realloc()"); err(1, "realloc()");
} }
if (len > 0 && xfiles->xf_size != sizeof(*xfiles)) if (len > 0)
errx(1, "struct xfile size mismatch"); enforce_ksize(xfiles->xf_size, struct xfile);
nxfiles = len / sizeof(*xfiles); nxfiles = len / sizeof(*xfiles);
} }