introduce log_ntp_addr() and use it where applicable, avoids a null
authorotto <otto@openbsd.org>
Wed, 20 Dec 2023 15:36:36 +0000 (15:36 +0000)
committerotto <otto@openbsd.org>
Wed, 20 Dec 2023 15:36:36 +0000 (15:36 +0000)
pointer deref in constraint.c reported by bluhm@; ok millert@

usr.sbin/ntpd/client.c
usr.sbin/ntpd/constraint.c
usr.sbin/ntpd/control.c
usr.sbin/ntpd/ntp.c
usr.sbin/ntpd/ntpd.h
usr.sbin/ntpd/util.c

index 10924fc..a2915fd 100644 (file)
@@ -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 <henning@openbsd.org>
@@ -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;
index 3bb3c75..8f91606 100644 (file)
@@ -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 <reyk@openbsd.org>
@@ -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)
index b86e615..6ca4cef 100644 (file)
@@ -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 <henning@openbsd.org>
@@ -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)";
        }
index eb535de..6037c82 100644 (file)
@@ -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 <henning@openbsd.org>
@@ -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;
index f294e4c..81d0c6c 100644 (file)
@@ -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 <henning@openbsd.org>
@@ -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 ***);
 
index c570c30..d837a9e 100644 (file)
@@ -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 <alexander.guy@andern.org>
@@ -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)
 {