Add boundary check in nfa_hci_handle_admin_gate_rsp

Bug: 124524315
Test: Nfc Enable/Disable with NFC SIM
Merged-In: Ic5b9398f7fc4f3aa8c83bd902e47d7785c5a6161
Change-Id: Ic5b9398f7fc4f3aa8c83bd902e47d7785c5a6161
diff --git a/src/nfa/hci/nfa_hci_act.c b/src/nfa/hci/nfa_hci_act.c
index d68cddf..2a530ff 100644
--- a/src/nfa/hci/nfa_hci_act.c
+++ b/src/nfa/hci/nfa_hci_act.c
@@ -21,6 +21,7 @@
  *  This file contains the action functions for the NFA HCI.
  *
  ******************************************************************************/
+#include <log/log.h>
 #include <string.h>
 #include "nfa_dm_int.h"
 #include "nfa_hci_api.h"
@@ -1389,7 +1390,8 @@
         } else if (nfa_hci_cb.param_in_use == NFA_HCI_SESSION_IDENTITY_INDEX) {
           /* The only parameter we get when initializing is the session ID.
            * Check for match. */
-          if (!memcmp((uint8_t*)nfa_hci_cb.cfg.admin_gate.session_id, p_data,
+          if (data_len >= NFA_HCI_SESSION_ID_LEN &&
+              !memcmp((uint8_t*)nfa_hci_cb.cfg.admin_gate.session_id, p_data,
                       NFA_HCI_SESSION_ID_LEN)) {
             /* Session has not changed, Set WHITELIST */
             nfa_hciu_send_set_param_cmd(
@@ -1400,6 +1402,9 @@
              * default session id */
             nfa_hciu_send_clear_all_pipe_cmd();
             nfa_hci_cb.b_hci_netwk_reset = true;
+            if (data_len < NFA_HCI_SESSION_ID_LEN) {
+              android_errorWriteLog(0x534e4554, "124524315");
+            }
           }
         }
         break;
@@ -1452,7 +1457,8 @@
 
       case NFA_HCI_ANY_GET_PARAMETER:
         if (nfa_hci_cb.param_in_use == NFA_HCI_SESSION_IDENTITY_INDEX) {
-          if (!memcmp((uint8_t*)default_session, p_data,
+          if (data_len >= NFA_HCI_SESSION_ID_LEN &&
+              !memcmp((uint8_t*)default_session, p_data,
                       NFA_HCI_SESSION_ID_LEN)) {
             memcpy(&nfa_hci_cb.cfg.admin_gate
                         .session_id[(NFA_HCI_SESSION_ID_LEN / 2)],
@@ -1467,6 +1473,9 @@
                 NFA_HCI_SESSION_ID_LEN,
                 (uint8_t*)nfa_hci_cb.cfg.admin_gate.session_id);
           } else {
+            if (data_len < NFA_HCI_SESSION_ID_LEN) {
+              android_errorWriteLog(0x534e4554, "124524315");
+            }
             if (nfa_hci_cb.hci_state == NFA_HCI_STATE_APP_DEREGISTER)
               nfa_hci_api_deregister(NULL);
             else if (nfa_hci_cb.hci_state == NFA_HCI_STATE_REMOVE_GATE)
@@ -1474,6 +1483,10 @@
           }
         } else if (nfa_hci_cb.param_in_use == NFA_HCI_HOST_LIST_INDEX) {
           evt_data.hosts.status = status;
+          if (data_len > NFA_HCI_MAX_HOST_IN_NETWORK) {
+            data_len = NFA_HCI_MAX_HOST_IN_NETWORK;
+            android_errorWriteLog(0x534e4554, "124524315");
+          }
           evt_data.hosts.num_hosts = data_len;
           memcpy(evt_data.hosts.host, p_data, data_len);
 
@@ -1505,7 +1518,8 @@
         break;
 
       case NFA_HCI_ADM_CREATE_PIPE:
-        if (status == NFA_STATUS_OK) {
+        // p_data should have at least 5 bytes length for pipe info
+        if (data_len >= 5 && status == NFA_STATUS_OK) {
           STREAM_TO_UINT8(source_host, p_data);
           STREAM_TO_UINT8(source_gate, p_data);
           STREAM_TO_UINT8(dest_host, p_data);
@@ -1522,6 +1536,9 @@
           }
 
           nfa_hciu_add_pipe_to_gate(pipe, source_gate, dest_host, dest_gate);
+        } else if (data_len < 5 && status == NFA_STATUS_OK) {
+          android_errorWriteLog(0x534e4554, "124524315");
+          status = NFA_STATUS_FAILED;
         }
 
         /* Tell the application his pipe was created or not */