diff options
| author | Dag-Erling Smørgrav <des@FreeBSD.org> | 2026-02-13 15:57:46 +0000 |
|---|---|---|
| committer | Dag-Erling Smørgrav <des@FreeBSD.org> | 2026-02-13 15:57:58 +0000 |
| commit | 585190dff436eeea3be97300e36c82559028d3dd (patch) | |
| tree | 7a26c876dc803df72afec773212403ab3de03bb4 | |
| parent | 3cbdcabf714d5187e22d8ff1cbfbc261dc8622eb (diff) | |
ngctl: Check hook name length
Check the length of the hook name when copying it into the sockaddr.
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D55258
| -rw-r--r-- | usr.sbin/ngctl/write.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/usr.sbin/ngctl/write.c b/usr.sbin/ngctl/write.c index 98bf213a2dad..b86533eca49c 100644 --- a/usr.sbin/ngctl/write.c +++ b/usr.sbin/ngctl/write.c @@ -34,10 +34,12 @@ #include <sys/socket.h> #include <err.h> +#include <stddef.h> #include <stdio.h> #include <string.h> #include <unistd.h> +#include <netgraph/ng_message.h> #include <netgraph/ng_socket.h> #include "ngctl.h" @@ -62,6 +64,7 @@ WriteCmd(int ac, char **av) struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf; u_char buf[BUF_SIZE]; const char *hook; + size_t hooklen; FILE *fp; u_int len; int byte; @@ -71,6 +74,14 @@ WriteCmd(int ac, char **av) if (ac < 3) return (CMDRTN_USAGE); hook = av[1]; + _Static_assert(sizeof(sagbuf) >= + offsetof(struct sockaddr_ng, sg_data) + NG_HOOKSIZ, + "sagbuf is too small for NG_HOOKSIZ"); + hooklen = strlcpy(sag->sg_data, hook, NG_HOOKSIZ); + if (hooklen >= NG_HOOKSIZ) { + warnx("hook name \"%s\" too long", hook); + return (CMDRTN_ERROR); + } /* Get data */ if (strcmp(av[2], "-f") == 0) { @@ -103,11 +114,10 @@ WriteCmd(int ac, char **av) } /* Send data */ - sag->sg_len = 3 + strlen(hook); + sag->sg_len = 3 + hooklen; sag->sg_family = AF_NETGRAPH; - strlcpy(sag->sg_data, hook, sizeof(sagbuf) - 2); - if (sendto(dsock, buf, len, - 0, (struct sockaddr *)sag, sag->sg_len) == -1) { + if (sendto(dsock, buf, len, 0, (struct sockaddr *)sag, + sag->sg_len) < 0) { warn("writing to hook \"%s\"", hook); return (CMDRTN_ERROR); } |
