Merge "agm: session: Add check for invalid session handle"
diff --git a/service/inc/private/agm/session_obj.h b/service/inc/private/agm/session_obj.h
index acd1f49..4679117 100644
--- a/service/inc/private/agm/session_obj.h
+++ b/service/inc/private/agm/session_obj.h
@@ -109,6 +109,7 @@
 
 int session_obj_init();
 int session_obj_deinit();
+int session_obj_valid_check(uint64_t hndl);
 int session_obj_get(int session_id, struct session_obj **sess_obj);
 int session_obj_open(uint32_t session_id,
                      enum agm_session_mode sess_mode,
diff --git a/service/src/agm.c b/service/src/agm.c
index 13e3a55..bc8bb0c 100644
--- a/service/src/agm.c
+++ b/service/src/agm.c
@@ -689,6 +689,12 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
+
     return session_obj_set_config(handle, stream_config, media_config,
                                                        buffer_config);
 }
@@ -701,6 +707,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_prepare(handle);
 }
 
@@ -712,6 +723,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_start(handle);
 }
 
@@ -723,6 +739,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_stop(handle);
 }
 
@@ -733,6 +754,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_close(handle);
 }
 
@@ -743,6 +769,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_pause(handle);
 }
 
@@ -753,6 +784,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_flush(handle);
 }
 
@@ -763,6 +799,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_resume(handle);
 }
 
@@ -773,6 +814,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_suspend(handle);
 }
 
@@ -783,6 +829,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_write(handle, buff, count);
 }
 
@@ -793,6 +844,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_read(handle, buff, count);
 }
 
@@ -803,6 +859,11 @@
         AGM_LOGE("Invalid handle\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(hndl)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_hw_processed_buff_cnt(handle, dir);
 }
 
@@ -862,6 +923,10 @@
         return -EINVAL;
     }
 
+    if (!session_obj_valid_check(handle)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_eos((struct session_obj *) handle);
 }
 
@@ -871,6 +936,11 @@
         AGM_LOGE("Invalid handle or timestamp pointer\n");
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(handle)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_get_timestamp((struct session_obj *) handle, timestamp);
 }
 
@@ -932,6 +1002,10 @@
         return -EINVAL;
     }
 
+    if (!session_obj_valid_check(handle)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_set_gapless_metadata((struct session_obj *) handle, type,
                                              silence);
 }
@@ -943,17 +1017,27 @@
         AGM_LOGE("%s Invalid handle\n", __func__);
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(handle)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_write_with_metadata((struct session_obj *) handle, buff,
                                             consumed_size);
 }
 
-int agm_session_read_with_metadata(uint64_t handle __unused, struct agm_buff *buff __unused,
-                                    uint32_t *captured_size __unused)
+int agm_session_read_with_metadata(uint64_t handle, struct agm_buff *buff,
+                                    uint32_t *captured_size )
 {
     if (!handle) {
         AGM_LOGE("%s Invalid handle\n", __func__);
         return -EINVAL;
     }
+
+    if (!session_obj_valid_check(handle)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_read_with_metadata((struct session_obj *) handle, buff,
                                            captured_size);
 }
@@ -970,6 +1054,10 @@
         return -EINVAL;
     }
 
+    if (!session_obj_valid_check(handle)) {
+        AGM_LOGE("Invalid handle\n");
+        return -EINVAL;
+    }
     return session_obj_set_non_tunnel_mode_config((struct session_obj *) handle,
                                             session_config,
                                             in_media_config,
diff --git a/service/src/session_obj.c b/service/src/session_obj.c
index cff7c6c..9aba612 100644
--- a/service/src/session_obj.c
+++ b/service/src/session_obj.c
@@ -343,6 +343,23 @@
     pthread_mutex_unlock(&sess_pool->lock);
     return obj;
 }
+int session_obj_valid_check(uint64_t hndl)
+{
+
+    struct session_obj *obj = NULL;
+    struct listnode *node;
+
+    pthread_mutex_lock(&sess_pool->lock);
+    list_for_each(node, &sess_pool->session_list) {
+        obj = node_to_item(node, struct session_obj, node);
+        if (obj == hndl) {
+            pthread_mutex_unlock(&sess_pool->lock);
+            return  1;
+        }
+    }
+    pthread_mutex_unlock(&sess_pool->lock);
+    return 0;
+}
 
 /* returns session_obj associated with session id */
 int session_obj_get(int session_id, struct session_obj **obj)