From: otto Date: Wed, 20 Dec 2023 15:36:36 +0000 (+0000) Subject: introduce log_ntp_addr() and use it where applicable, avoids a null X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=8745f5cf9c71e9801d335b988c13bd14a57ce14d;p=openbsd introduce log_ntp_addr() and use it where applicable, avoids a null pointer deref in constraint.c reported by bluhm@; ok millert@ --- diff --git a/usr.sbin/ntpd/client.c b/usr.sbin/ntpd/client.c index 10924fc1ce3..a2915fd030a 100644 --- a/usr.sbin/ntpd/client.c +++ b/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.117 2022/03/24 07:37:19 otto Exp $ */ +/* $OpenBSD: client.c,v 1.118 2023/12/20 15:36:36 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -351,8 +351,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime, u_int8_t automatic) interval = error_interval(); set_next(p, interval); log_info("reply from %s: not synced (%s), next query %llds", - log_sockaddr((struct sockaddr *)&p->addr->ss), s, - (long long)interval); + log_ntp_addr(p->addr), s, (long long)interval); return (0); } @@ -379,7 +378,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime, u_int8_t automatic) if (!p->trusted && conf->constraint_median != 0 && (constraint_check(T2) != 0 || constraint_check(T3) != 0)) { log_info("reply from %s: constraint check failed", - log_sockaddr((struct sockaddr *)&p->addr->ss)); + log_ntp_addr(p->addr)); set_next(p, error_interval()); return (0); } @@ -392,7 +391,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime, u_int8_t automatic) set_next(p, interval); log_info("reply from %s: negative delay %fs, " "next query %llds", - log_sockaddr((struct sockaddr *)&p->addr->ss), + log_ntp_addr(p->addr), p->reply[p->shift].delay, (long long)interval); return (0); } @@ -431,7 +430,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime, u_int8_t automatic) if (p->trustlevel < TRUSTLEVEL_BADPEER && p->trustlevel + 1 >= TRUSTLEVEL_BADPEER) log_info("peer %s now valid", - log_sockaddr((struct sockaddr *)&p->addr->ss)); + log_ntp_addr(p->addr)); p->trustlevel++; } @@ -456,10 +455,8 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime, u_int8_t automatic) interval = scale_interval(INTERVAL_QUERY_NORMAL); log_debug("reply from %s: offset %f delay %f, " - "next query %llds", - log_sockaddr((struct sockaddr *)&p->addr->ss), - offset, delay, - (long long)interval); + "next query %llds", log_ntp_addr(p->addr), + offset, delay, (long long)interval); set_next(p, interval); @@ -506,7 +503,7 @@ client_log_error(struct ntp_peer *peer, const char *operation, int error) { const char *address; - address = log_sockaddr((struct sockaddr *)&peer->addr->ss); + address = log_ntp_addr(peer->addr); if (peer->lasterror == error) { log_debug("%s %s: %s", operation, address, strerror(error)); return; diff --git a/usr.sbin/ntpd/constraint.c b/usr.sbin/ntpd/constraint.c index 3bb3c7550a7..8f916067e43 100644 --- a/usr.sbin/ntpd/constraint.c +++ b/usr.sbin/ntpd/constraint.c @@ -1,4 +1,4 @@ -/* $OpenBSD: constraint.c,v 1.55 2023/12/06 15:51:53 otto Exp $ */ +/* $OpenBSD: constraint.c,v 1.56 2023/12/20 15:36:36 otto Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -469,8 +469,7 @@ priv_constraint_check_child(pid_t pid, int status) strsignal(sig) : "unknown"; log_warnx("constraint %s; " "terminated with signal %d (%s)", - log_sockaddr((struct sockaddr *) - &cstr->addr->ss), sig, signame); + log_ntp_addr(cstr->addr), sig, signame); } fail = 1; } @@ -679,7 +678,7 @@ constraint_msg_result(u_int32_t id, u_int8_t *data, size_t len) gettime_from_timeval(&tv[1]); log_info("constraint reply from %s: offset %f", - log_sockaddr((struct sockaddr *)&cstr->addr->ss), + log_ntp_addr(cstr->addr), offset); cstr->state = STATE_REPLY_RECEIVED; @@ -711,8 +710,8 @@ constraint_msg_close(u_int32_t id, u_int8_t *data, size_t len) if (fail) { log_debug("no constraint reply from %s" " received in time, next query %ds", - log_sockaddr((struct sockaddr *) - &cstr->addr->ss), CONSTRAINT_SCAN_INTERVAL); + log_ntp_addr(cstr->addr), + CONSTRAINT_SCAN_INTERVAL); cnt = 0; TAILQ_FOREACH(tmp, &conf->constraints, entry) diff --git a/usr.sbin/ntpd/control.c b/usr.sbin/ntpd/control.c index b86e615e86c..6ca4cefea44 100644 --- a/usr.sbin/ntpd/control.c +++ b/usr.sbin/ntpd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.19 2021/04/21 09:38:11 bluhm Exp $ */ +/* $OpenBSD: control.c,v 1.20 2023/12/20 15:36:36 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -347,7 +347,7 @@ build_show_peer(struct ctl_show_peer *cp, struct ntp_peer *p) now = getmonotime(); if (p->addr) { - a = log_sockaddr((struct sockaddr *)&p->addr->ss); + a = log_ntp_addr(p->addr); if (p->addr->notauth) auth = " (non-dnssec lookup)"; } diff --git a/usr.sbin/ntpd/ntp.c b/usr.sbin/ntpd/ntp.c index eb535de3394..6037c82ec57 100644 --- a/usr.sbin/ntpd/ntp.c +++ b/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.171 2023/12/06 15:51:53 otto Exp $ */ +/* $OpenBSD: ntp.c,v 1.172 2023/12/20 15:36:36 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -261,13 +261,12 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv) if (p->deadline > 0 && p->deadline <= getmonotime()) { timeout = 300; log_debug("no reply from %s received in time, " - "next query %ds", log_sockaddr( - (struct sockaddr *)&p->addr->ss), timeout); + "next query %ds", log_ntp_addr( p->addr), + timeout); if (p->trustlevel >= TRUSTLEVEL_BADPEER && (p->trustlevel /= 2) < TRUSTLEVEL_BADPEER) log_info("peer %s now invalid", - log_sockaddr( - (struct sockaddr *)&p->addr->ss)); + log_ntp_addr(p->addr)); if (client_nextaddr(p) == 1) { peer_addr_head_clear(p); client_nextaddr(p); @@ -276,8 +275,7 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv) } if (p->senderrors > MAX_SEND_ERRORS) { log_debug("failed to send query to %s, " - "next query %ds", log_sockaddr( - (struct sockaddr *)&p->addr->ss), + "next query %ds", log_ntp_addr(p->addr), INTERVAL_QUERY_PATHETIC); p->senderrors = 0; if (client_nextaddr(p) == 1) { @@ -419,16 +417,13 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv) conf->automatic)) { case -1: log_debug("no reply from %s " - "received", log_sockaddr( - (struct sockaddr *) &pp->addr->ss)); + "received", log_ntp_addr(pp->addr)); if (pp->trustlevel >= TRUSTLEVEL_BADPEER && (pp->trustlevel /= 2) < TRUSTLEVEL_BADPEER) log_info("peer %s now invalid", - log_sockaddr( - (struct sockaddr *) - &pp->addr->ss)); + log_ntp_addr(pp->addr)); break; case 0: /* invalid replies are ignored */ break; @@ -634,8 +629,7 @@ ntp_dispatch_imsg_dns(void) continue; } log_debug("Adding address %s to %s", - log_sockaddr((struct sockaddr *) - &h->ss), peer->addr_head.name); + log_ntp_addr(h), peer->addr_head.name); npeer = new_peer(); npeer->weight = peer->weight; npeer->query_addr4 = peer->query_addr4; diff --git a/usr.sbin/ntpd/ntpd.h b/usr.sbin/ntpd/ntpd.h index f294e4c2ccd..81d0c6c83f6 100644 --- a/usr.sbin/ntpd/ntpd.h +++ b/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.152 2022/11/27 13:19:00 otto Exp $ */ +/* $OpenBSD: ntpd.h,v 1.153 2023/12/20 15:36:36 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -407,6 +407,7 @@ double sfp_to_d(struct s_fixedpt); struct s_fixedpt d_to_sfp(double); char *print_rtable(int); const char *log_sockaddr(struct sockaddr *); +const char *log_ntp_addr(struct ntp_addr *); pid_t start_child(char *, int, int, char **); int sanitize_argv(int *, char ***); diff --git a/usr.sbin/ntpd/util.c b/usr.sbin/ntpd/util.c index c570c307a23..d837a9e6796 100644 --- a/usr.sbin/ntpd/util.c +++ b/usr.sbin/ntpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.27 2023/11/19 10:41:25 claudio Exp $ */ +/* $OpenBSD: util.c,v 1.28 2023/12/20 15:36:36 otto Exp $ */ /* * Copyright (c) 2004 Alexander Guy @@ -163,6 +163,14 @@ log_sockaddr(struct sockaddr *sa) return (buf); } +const char * +log_ntp_addr(struct ntp_addr *addr) +{ + if (addr == NULL) + return ("(unknown)"); + return log_sockaddr((struct sockaddr *)&addr->ss); +} + pid_t start_child(char *pname, int cfd, int argc, char **argv) {