[Edge] validate command
authorJaeyun <jy1210.jung@samsung.com>
Mon, 4 Jul 2022 08:02:08 +0000 (17:02 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Mon, 4 Jul 2022 08:34:42 +0000 (17:34 +0900)
Prevent receive failure, update source to validate edge command.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
src/libnnstreamer-edge/nnstreamer-edge-internal.c

index 119e4d701919180078311b7cf96153d2e6e58fbc..bba4f6de730a864c7940340c25806d5a45ff341b 100644 (file)
@@ -34,6 +34,7 @@ typedef enum
  */
 typedef struct
 {
+  unsigned int magic;
   nns_edge_cmd_e cmd;
   int64_t client_id;
 
@@ -209,6 +210,7 @@ _nns_edge_cmd_init (nns_edge_cmd_s * cmd, nns_edge_cmd_e c, int64_t cid)
     return;
 
   memset (cmd, 0, sizeof (nns_edge_cmd_s));
+  cmd->info.magic = NNS_EDGE_MAGIC;
   cmd->info.cmd = c;
   cmd->info.client_id = cid;
 }
@@ -224,6 +226,8 @@ _nns_edge_cmd_clear (nns_edge_cmd_s * cmd)
   if (!cmd)
     return;
 
+  cmd->info.magic = NNS_EDGE_MAGIC_DEAD;
+
   for (i = 0; i < cmd->info.num; i++) {
     if (cmd->mem[i])
       free (cmd->mem[i]);
@@ -231,6 +235,27 @@ _nns_edge_cmd_clear (nns_edge_cmd_s * cmd)
   }
 }
 
+/**
+ * @brief Validate edge command.
+ */
+static bool
+_nns_edge_cmd_is_valid (nns_edge_cmd_s * cmd)
+{
+  int command;
+
+  if (!cmd)
+    return false;
+
+  command = (int) cmd->info.cmd;
+
+  if (!NNS_EDGE_MAGIC_IS_VALID (&cmd->info) ||
+      (command < 0 || command >= _NNS_EDGE_CMD_END)) {
+    return false;
+  }
+
+  return true;
+}
+
 /**
  * @brief Send edge command to connected device.
  */
@@ -242,6 +267,11 @@ _nns_edge_cmd_send (nns_edge_conn_s * conn, nns_edge_cmd_s * cmd)
   if (!conn || !cmd)
     return NNS_EDGE_ERROR_INVALID_PARAMETER;
 
+  if (!_nns_edge_cmd_is_valid (cmd)) {
+    nns_edge_loge ("Failed to send command, invalid command.");
+    return NNS_EDGE_ERROR_INVALID_PARAMETER;
+  }
+
   if (!_send_raw_data (conn->socket, &cmd->info,
           sizeof (nns_edge_cmd_info_s), conn->cancellable)) {
     nns_edge_loge ("Failed to send command to socket.");
@@ -277,6 +307,11 @@ _nns_edge_cmd_receive (nns_edge_conn_s * conn, nns_edge_cmd_s * cmd)
     return NNS_EDGE_ERROR_IO;
   }
 
+  if (!_nns_edge_cmd_is_valid (cmd)) {
+    nns_edge_loge ("Failed to receive command, invalid command.");
+    return NNS_EDGE_ERROR_IO;
+  }
+
   nns_edge_logd ("Received command:%d (num:%u)", cmd->info.cmd, cmd->info.num);
 
   for (n = 0; n < cmd->info.num; n++) {