From dda00ceb8e98171a05cb5903cf26378a3d1c18d2 Mon Sep 17 00:00:00 2001
From: wo_cow <niuqianqian@huawei.com>
Date: Fri, 26 Apr 2024 11:19:00 +0800
Subject: [PATCH] bugfix: probe_define access out of bounds

---
 gala-gopher.spec          | 10 +++--
 src/common/ipc.h          | 58 ++++++++++++++++++++++++++++
 src/lib/probe/probe_mng.c | 80 ++++++++++++---------------------------
 src/lib/probe/probe_mng.h |  1 +
 4 files changed, 90 insertions(+), 59 deletions(-)

diff --git a/gala-gopher.spec b/gala-gopher.spec
index 24f6bae..85fa3fa 100644
--- a/gala-gopher.spec
+++ b/gala-gopher.spec
@@ -109,7 +109,7 @@ BUILD_OPTS=(
   -DENABLE_FLAMEGRAPH=%[0%{?without_flamegraph}?0:1]
   -DENABLE_L7=%[0%{?without_l7}?0:1]
   -DENABLE_TCP=%[0%{?without_tcp}?0:1]
-  -DENABLE_SOCKET=%[0%{?without_tcp}?0:1]
+  -DENABLE_SOCKET=%[0%{?without_socket}?0:1]
   -DENABLE_IO=%[0%{?without_io}?0:1]
   -DENABLE_PROC=%[0%{?without_proc}?0:1]
   -DENABLE_JVM=%[0%{?without_jvm}?0:1]
@@ -155,8 +155,12 @@ if [ -d /var/log/gala-gopher ]; then
   othermode=$(expr $(stat -L -c "%a" /var/log/gala-gopher) % 10)
   if [ $othermode -ne 0 ]; then
     chmod 750 /var/log/gala-gopher
-    chmod 750 /var/log/gala-gopher/debug
-    chmod 640 /var/log/gala-gopher/debug/gopher.log
+    if [ -d /var/log/gala-gopher ]; then
+      chmod 750 /var/log/gala-gopher/debug
+    fi
+    if [ -e /var/log/gala-gopher/debug/gopher.log ]; then
+      chmod 640 /var/log/gala-gopher/debug/gopher.log
+    fi
   fi
 fi
 
diff --git a/src/common/ipc.h b/src/common/ipc.h
index 0bc043a..c14bbf1 100644
--- a/src/common/ipc.h
+++ b/src/common/ipc.h
@@ -21,6 +21,64 @@
 #include "args.h"
 #include "object.h"
 
+#ifndef ENABLE_BASEINFO
+#define ENABLE_BASEINFO 0
+#endif
+#ifndef ENABLE_VIRT
+#define ENABLE_VIRT 0
+#endif
+#ifndef ENABLE_FLAMEGRAPH
+#define ENABLE_FLAMEGRAPH 0
+#endif
+#ifndef ENABLE_L7
+#define ENABLE_L7 0
+#endif
+#ifndef ENABLE_TCP
+#define ENABLE_TCP 0
+#endif
+#ifndef ENABLE_SOCKET
+#define ENABLE_SOCKET 0
+#endif
+#ifndef ENABLE_IO
+#define ENABLE_IO 0
+#endif
+#ifndef ENABLE_PROC
+#define ENABLE_PROC 0
+#endif
+#ifndef ENABLE_JVM
+#define ENABLE_JVM 0
+#endif
+#ifndef ENABLE_POSTGRE_SLI
+#define ENABLE_POSTGRE_SLI 0
+#endif
+#ifndef ENABLE_OPENGAUSS_SLI
+#define ENABLE_OPENGAUSS_SLI 0
+#endif
+#ifndef ENABLE_NGINX
+#define ENABLE_NGINX 0
+#endif
+#ifndef ENABLE_KAFKA
+#define ENABLE_KAFKA 0
+#endif
+#ifndef ENABLE_TPROFILING
+#define ENABLE_TPROFILING 0
+#endif
+#ifndef ENABLE_HW
+#define ENABLE_HW 0
+#endif
+#ifndef ENABLE_NGINX
+#define ENABLE_NGINX 0
+#endif
+#ifndef ENABLE_KSLI
+#define ENABLE_KSLI 0
+#endif
+#ifndef ENABLE_CONTAINER
+#define ENABLE_CONTAINER 0
+#endif
+#ifndef ENABLE_SERMANT
+#define ENABLE_SERMANT 0
+#endif
+
 #define SNOOPER_MAX    100
 
 /* FlameGraph subprobe define */
diff --git a/src/lib/probe/probe_mng.c b/src/lib/probe/probe_mng.c
index 83629cc..769a08b 100644
--- a/src/lib/probe/probe_mng.c
+++ b/src/lib/probe/probe_mng.c
@@ -39,60 +39,24 @@ static int set_probe_bin(struct probe_s *probe, const char *bin);
 static void init_probe_bin(struct probe_s *probe, enum probe_type_e probe_type);
 
 struct probe_define_s probe_define[] = {
-#ifdef ENABLE_BASEINFO
-    {"baseinfo",            "system_infos",                                       PROBE_BASEINFO},
-#endif
-#ifdef ENABLE_VIRT
-    {"virt",                "virtualized_infos",                                  PROBE_VIRT},
-#endif
-#ifdef ENABLE_FLAMEGRAPH
-    {"flamegraph",          "/opt/gala-gopher/extend_probes/stackprobe",          PROBE_FG},
-#endif
-#ifdef ENABLE_L7
-    {"l7",                  "/opt/gala-gopher/extend_probes/l7probe",             PROBE_L7},
-#endif
-#ifdef ENABLE_TCP
-    {"tcp",                 "/opt/gala-gopher/extend_probes/tcpprobe",            PROBE_TCP},
-#endif
-#ifdef ENABLE_SOCKET
-    {"socket",              "/opt/gala-gopher/extend_probes/endpoint",            PROBE_SOCKET},
-#endif
-#ifdef ENABLE_IO
-    {"io",                  "/opt/gala-gopher/extend_probes/ioprobe",             PROBE_IO},
-#endif
-#ifdef ENABLE_PROC
-    {"proc",                "/opt/gala-gopher/extend_probes/taskprobe",           PROBE_PROC},
-#endif
-#ifdef ENABLE_JVM
-    {"jvm",                 "/opt/gala-gopher/extend_probes/jvmprobe",            PROBE_JVM},
-#endif
-#ifdef ENABLE_POSTGRE_SLI
-    {"postgre_sli",         "/opt/gala-gopher/extend_probes/pgsliprobe",          PROBE_POSTGRE_SLI},
-#endif
-#ifdef ENABLE_OPENGAUSS_SLI
-    {"opengauss_sli",       "/opt/gala-gopher/extend_probes/pg_stat_probe.py",    PROBE_GAUSS_SLI},
-#endif
-#ifdef ENABLE_NGINX
-    {"nginx",               "/opt/gala-gopher/extend_probes/nginx_probe",         PROBE_NGINX},
-#endif
-#ifdef ENABLE_KAFKA
-    {"kafka",               "/opt/gala-gopher/extend_probes/kafkaprobe",          PROBE_KAFKA},
-#endif
-#ifdef ENABLE_TPROFILING
-    {"tprofiling",          "/opt/gala-gopher/extend_probes/tprofiling",          PROBE_TP},
-#endif
-#ifdef ENABLE_HW
-    {"hw",                  "/opt/gala-gopher/extend_probes/hwprobe",             PROBE_HW},
-#endif
-#ifdef ENABLE_KSLI
-    {"ksli",                "/opt/gala-gopher/extend_probes/ksliprobe",           PROBE_KSLI},
-#endif
-#ifdef ENABLE_CONTAINER
-    {"container",           "/opt/gala-gopher/extend_probes/cadvisor_probe.py",   PROBE_CONTAINER},
-#endif
-#ifdef ENABLE_SERMANT
-    {"sermant",             "/opt/gala-gopher/extend_probes/sermant_probe.py",    PROBE_SERMANT}
-#endif
+    {"baseinfo",            "system_infos",                                       PROBE_BASEINFO,       ENABLE_BASEINFO},
+    {"virt",                "virtualized_infos",                                  PROBE_VIRT,           ENABLE_VIRT},
+    {"flamegraph",          "/opt/gala-gopher/extend_probes/stackprobe",          PROBE_FG,             ENABLE_FLAMEGRAPH},
+    {"l7",                  "/opt/gala-gopher/extend_probes/l7probe",             PROBE_L7,             ENABLE_L7},
+    {"tcp",                 "/opt/gala-gopher/extend_probes/tcpprobe",            PROBE_TCP,            ENABLE_TCP},
+    {"socket",              "/opt/gala-gopher/extend_probes/endpoint",            PROBE_SOCKET,         ENABLE_SOCKET},
+    {"io",                  "/opt/gala-gopher/extend_probes/ioprobe",             PROBE_IO,             ENABLE_IO},
+    {"proc",                "/opt/gala-gopher/extend_probes/taskprobe",           PROBE_PROC,           ENABLE_PROC},
+    {"jvm",                 "/opt/gala-gopher/extend_probes/jvmprobe",            PROBE_JVM,            ENABLE_JVM},
+    {"postgre_sli",         "/opt/gala-gopher/extend_probes/pgsliprobe",          PROBE_POSTGRE_SLI,    ENABLE_POSTGRE_SLI},
+    {"opengauss_sli",       "/opt/gala-gopher/extend_probes/pg_stat_probe.py",    PROBE_GAUSS_SLI,      ENABLE_OPENGAUSS_SLI},
+    {"nginx",               "/opt/gala-gopher/extend_probes/nginx_probe",         PROBE_NGINX,          ENABLE_NGINX},
+    {"kafka",               "/opt/gala-gopher/extend_probes/kafkaprobe",          PROBE_KAFKA,          ENABLE_KAFKA},
+    {"tprofiling",          "/opt/gala-gopher/extend_probes/tprofiling",          PROBE_TP,             ENABLE_TPROFILING},
+    {"hw",                  "/opt/gala-gopher/extend_probes/hwprobe",             PROBE_HW,             ENABLE_HW},
+    {"ksli",                "/opt/gala-gopher/extend_probes/ksliprobe",           PROBE_KSLI,           ENABLE_KSLI},
+    {"container",           "/opt/gala-gopher/extend_probes/cadvisor_probe.py",   PROBE_CONTAINER,      ENABLE_CONTAINER},
+    {"sermant",             "/opt/gala-gopher/extend_probes/sermant_probe.py",    PROBE_SERMANT,        ENABLE_SERMANT},
 
     // If you want to add a probe, add the probe define.
 };
@@ -595,15 +559,20 @@ static enum probe_type_e get_probe_type_by_name(const char *probe_name)
     size_t size = sizeof(probe_define) / sizeof(struct probe_define_s);
 
     if (probe_name == NULL) {
+        PARSE_ERR("invalid probe name");
         return PROBE_TYPE_MAX;
     }
 
     for (int i = 0; i < size; i++) {
         if (!strcasecmp(probe_define[i].desc, probe_name)) {
+            if (probe_define[i].enable == 0) {
+                PARSE_ERR("not supported in the current version");
+                return PROBE_TYPE_MAX;
+            }
             return probe_define[i].type;
         }
     }
-
+    PARSE_ERR("invalid probe name");
     return PROBE_TYPE_MAX;
 }
 
@@ -611,7 +580,6 @@ static struct probe_s *get_probe_by_name(const char *probe_name)
 {
     enum probe_type_e probe_type = get_probe_type_by_name(probe_name);
     if (probe_type >= PROBE_TYPE_MAX) {
-        PARSE_ERR("invalid probe name");
         return NULL;
     }
 
diff --git a/src/lib/probe/probe_mng.h b/src/lib/probe/probe_mng.h
index f09190f..7853f17 100644
--- a/src/lib/probe/probe_mng.h
+++ b/src/lib/probe/probe_mng.h
@@ -41,6 +41,7 @@ struct probe_define_s {
     char *desc;
     char *bin;
     enum probe_type_e type;
+    char enable;
 };
 
 typedef int (*ParseParam)(const char*, struct probe_params *);
-- 
2.28.0.windows.1

