wireguard: fix dereferences null pointer 12/35412/2
authorGabriel Oginski <gabrielx.oginski@intel.com>
Tue, 22 Feb 2022 14:15:11 +0000 (14:15 +0000)
committerFan Zhang <roy.fan.zhang@intel.com>
Wed, 23 Feb 2022 09:15:12 +0000 (09:15 +0000)
Type: fix

Fixed coverity-issue CID 248517.
Originally possible passing null pointer to one function and directly
dereferences it.
This patch fixes the issue by add a new condition to check this
pointer.

Change-Id: If506abaf08c9f003860b641971af291f68613c18
Signed-off-by: Gabriel Oginski <gabrielx.oginski@intel.com>
src/plugins/wireguard/wireguard_input.c
src/plugins/wireguard/wireguard_noise.h

index ba5a1d6..7db1a0c 100644 (file)
@@ -902,10 +902,17 @@ wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
          last_rec_idx = data->receiver_index;
        }
 
-      ASSERT (peer != NULL); /* this pointer never should be NULL */
-      if (PREDICT_FALSE (wg_input_post_process (vm, b[0], next, peer, data,
-                                               &is_keepalive) < 0))
-       goto trace;
+      if (PREDICT_TRUE (peer != NULL))
+       {
+         if (PREDICT_FALSE (wg_input_post_process (vm, b[0], next, peer, data,
+                                                   &is_keepalive) < 0))
+           goto trace;
+       }
+      else
+       {
+         next[0] = WG_INPUT_NEXT_PUNT;
+         goto trace;
+       }
 
       if (PREDICT_FALSE (peer_idx && (last_peer_time_idx != peer_idx)))
        {
index b436120..e95211b 100644 (file)
@@ -199,22 +199,22 @@ noise_remote_encrypt (vlib_main_t * vm, noise_remote_t *,
 static_always_inline noise_keypair_t *
 wg_get_active_keypair (noise_remote_t *r, uint32_t r_idx)
 {
-  if (r->r_current != NULL)
+  if (r->r_current != NULL && r->r_current->kp_local_index == r_idx)
     {
-      if (r->r_current->kp_local_index == r_idx)
-       return r->r_current;
+      return r->r_current;
     }
-  if (r->r_previous != NULL)
+  else if (r->r_previous != NULL && r->r_previous->kp_local_index == r_idx)
     {
-      if (r->r_previous->kp_local_index == r_idx)
-       return r->r_previous;
+      return r->r_previous;
     }
-  if (r->r_next != NULL)
+  else if (r->r_next != NULL && r->r_next->kp_local_index == r_idx)
     {
-      if (r->r_next->kp_local_index == r_idx)
-       return r->r_next;
+      return r->r_next;
+    }
+  else
+    {
+      return NULL;
     }
-  return NULL;
 }
 
 inline bool