Module Name:    src
Committed By:   riastradh
Date:           Thu Aug 20 21:34:42 UTC 2020

Modified Files:
        src/sys/net: if_wg.c

Log Message:
Use consttime_memequal, not memcmp, to compare secrets for equality.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/net/if_wg.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/net/if_wg.c
diff -u src/sys/net/if_wg.c:1.12 src/sys/net/if_wg.c:1.13
--- src/sys/net/if_wg.c:1.12	Thu Aug 20 21:34:32 2020
+++ src/sys/net/if_wg.c	Thu Aug 20 21:34:42 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_wg.c,v 1.12 2020/08/20 21:34:32 riastradh Exp $	*/
+/*	$NetBSD: if_wg.c,v 1.13 2020/08/20 21:34:42 riastradh Exp $	*/
 
 /*
  * Copyright (C) Ryota Ozaki <ozaki.ry...@gmail.com>
@@ -43,7 +43,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_wg.c,v 1.12 2020/08/20 21:34:32 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_wg.c,v 1.13 2020/08/20 21:34:42 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -1357,7 +1357,7 @@ wg_handle_msg_init(struct wg_softc *wg, 
 	 * "the responder, ..., must always reject messages with an invalid
 	 *  msg.mac1"
 	 */
-	if (memcmp(mac1, wgmi->wgmi_mac1, sizeof(mac1)) != 0) {
+	if (!consttime_memequal(mac1, wgmi->wgmi_mac1, sizeof(mac1))) {
 		WG_DLOG("mac1 is invalid\n");
 		goto out;
 	}
@@ -1373,7 +1373,7 @@ wg_handle_msg_init(struct wg_softc *wg, 
 		 *  message"
 		 */
 		uint8_t zero[WG_MAC_LEN] = {0};
-		if (memcmp(wgmi->wgmi_mac2, zero, sizeof(zero)) == 0) {
+		if (consttime_memequal(wgmi->wgmi_mac2, zero, sizeof(zero))) {
 			WG_TRACE("sending a cookie message: no cookie included");
 			(void)wg_send_cookie_msg(wg, wgp, wgmi->wgmi_sender,
 			    wgmi->wgmi_mac1, src);
@@ -1389,7 +1389,7 @@ wg_handle_msg_init(struct wg_softc *wg, 
 		wg_algo_mac(mac2, sizeof(mac2), wgp->wgp_last_sent_cookie,
 		    WG_COOKIE_LEN, (const uint8_t *)wgmi,
 		    offsetof(struct wg_msg_init, wgmi_mac2), NULL, 0);
-		if (memcmp(mac2, wgmi->wgmi_mac2, sizeof(mac2)) != 0) {
+		if (!consttime_memequal(mac2, wgmi->wgmi_mac2, sizeof(mac2))) {
 			WG_DLOG("mac2 is invalid\n");
 			goto out;
 		}
@@ -1715,7 +1715,7 @@ wg_handle_msg_resp(struct wg_softc *wg, 
 	 * "the responder, ..., must always reject messages with an invalid
 	 *  msg.mac1"
 	 */
-	if (memcmp(mac1, wgmr->wgmr_mac1, sizeof(mac1)) != 0) {
+	if (!consttime_memequal(mac1, wgmr->wgmr_mac1, sizeof(mac1))) {
 		WG_DLOG("mac1 is invalid\n");
 		goto out;
 	}
@@ -1731,7 +1731,7 @@ wg_handle_msg_resp(struct wg_softc *wg, 
 		 *  message"
 		 */
 		uint8_t zero[WG_MAC_LEN] = {0};
-		if (memcmp(wgmr->wgmr_mac2, zero, sizeof(zero)) == 0) {
+		if (consttime_memequal(wgmr->wgmr_mac2, zero, sizeof(zero))) {
 			WG_TRACE("sending a cookie message: no cookie included");
 			(void)wg_send_cookie_msg(wg, wgp, wgmr->wgmr_sender,
 			    wgmr->wgmr_mac1, src);
@@ -1747,7 +1747,7 @@ wg_handle_msg_resp(struct wg_softc *wg, 
 		wg_algo_mac(mac2, sizeof(mac2), wgp->wgp_last_sent_cookie,
 		    WG_COOKIE_LEN, (const uint8_t *)wgmr,
 		    offsetof(struct wg_msg_resp, wgmr_mac2), NULL, 0);
-		if (memcmp(mac2, wgmr->wgmr_mac2, sizeof(mac2)) != 0) {
+		if (!consttime_memequal(mac2, wgmr->wgmr_mac2, sizeof(mac2))) {
 			WG_DLOG("mac2 is invalid\n");
 			goto out;
 		}
@@ -1892,7 +1892,8 @@ wg_lookup_peer_by_pubkey(struct wg_softc
 	int s = pserialize_read_enter();
 	/* XXX O(n) */
 	WG_PEER_READER_FOREACH(wgp, wg) {
-		if (memcmp(wgp->wgp_pubkey, pubkey, sizeof(wgp->wgp_pubkey)) == 0)
+		if (consttime_memequal(wgp->wgp_pubkey, pubkey,
+			sizeof(wgp->wgp_pubkey)))
 			break;
 	}
 	if (wgp != NULL)
@@ -4058,7 +4059,8 @@ wg_ioctl_get(struct wg_softc *wg, struct
 			goto next;
 
 		uint8_t psk_zero[WG_PRESHARED_KEY_LEN] = {0};
-		if (memcmp(wgp->wgp_psk, psk_zero, sizeof(wgp->wgp_psk) != 0)) {
+		if (!consttime_memequal(wgp->wgp_psk, psk_zero,
+			sizeof(wgp->wgp_psk))) {
 			if (!prop_dictionary_set_data(prop_peer,
 				"preshared_key",
 				wgp->wgp_psk, sizeof(wgp->wgp_psk)))

Reply via email to