From f48706be071924b851a652b00d88ae410e252b9f Mon Sep 17 00:00:00 2001 From: wuying39 <921169248@qq.com> Date: Wed, 10 Sep 2025 15:10:17 +0800 Subject: [PATCH] add validation for externally specified struct pointers --- include/pcerrc.h | 1 + pmu/dump_perf_data.cpp | 5 +++++ pmu/pmu.cpp | 13 ++++++++++++- pmu/pmu_trace_analysis.cpp | 4 ++++ python/modules/kperf/perror.py | 1 + util/pcerr.cpp | 3 ++- 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/pcerrc.h b/include/pcerrc.h index b2a1fdf..ff666ea 100644 --- a/include/pcerrc.h +++ b/include/pcerrc.h @@ -125,6 +125,7 @@ extern "C" { #define LIBPERF_ERR_COUNTER_INDEX_IS_ZERO 1078 #define LIBPERF_ERR_BPF_ACT_FAILED 1079 #define LIBPERF_ERR_INVALID_BPF_PARAM 1080 +#define LIBPERF_ERR_NULL_POINTER 1081 #define UNKNOWN_ERROR 9999 diff --git a/pmu/dump_perf_data.cpp b/pmu/dump_perf_data.cpp index c17937b..fe307b0 100644 --- a/pmu/dump_perf_data.cpp +++ b/pmu/dump_perf_data.cpp @@ -636,6 +636,11 @@ map> dumpers; PmuFile PmuBeginWrite(const char *path, const PmuAttr *pattr, const int addIdHdr) { + if (pattr == nullptr) { + New(LIBPERF_ERR_NULL_POINTER, "PmuAttr cannot be null"); + return NULL; + } + try { unique_ptr dumper(new PerfDataDumper(path, addIdHdr)); int err = dumper->Start(pattr); diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index 6e2f1c7..b83c4af 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -545,6 +545,10 @@ int PmuOpen(enum PmuTaskType collectType, struct PmuAttr *attr) { SetWarn(SUCCESS); New(SUCCESS); + if (attr == nullptr) { + New(LIBPERF_ERR_NULL_POINTER, "PmuAttr cannot be null"); + return -1; + } PmuAttr copiedAttr = *attr; pair previousEventList = {0, nullptr}; try { @@ -1112,12 +1116,19 @@ static void DumpStack(ofstream &out, Stack *stack, int dumpDwf) int PmuDumpData(struct PmuData *pmuData, unsigned len, char *filepath, int dumpDwf) { SetWarn(SUCCESS); + if (filepath == nullptr) { + New(LIBPERF_ERR_NULL_POINTER, "filepath cannot be null"); + return -1; + } ofstream out(filepath, ios_base::app); if (!out.is_open()) { New(LIBPERF_ERR_PATH_INACCESSIBLE, "cannot access: " + string(filepath)); return -1; } - + if (pmuData == nullptr) { + New(LIBPERF_ERR_NULL_POINTER, "PmuData cannot be null"); + return -1; + } for (unsigned i = 0; i < len; ++i) { auto &data = pmuData[i]; if (data.comm) { diff --git a/pmu/pmu_trace_analysis.cpp b/pmu/pmu_trace_analysis.cpp index 0411f68..2f1df1f 100644 --- a/pmu/pmu_trace_analysis.cpp +++ b/pmu/pmu_trace_analysis.cpp @@ -184,6 +184,10 @@ int PmuTraceOpen(enum PmuTraceType traceType, struct PmuTraceAttr *traceAttr) return -1; #else SetWarn(SUCCESS); + if (traceAttr == nullptr) { + New(LIBPERF_ERR_NULL_POINTER, "PmuTraceAttr cannot be null"); + return -1; + } auto err = CheckTraceAttr(traceType, traceAttr); if (err != SUCCESS) { return -1; diff --git a/python/modules/kperf/perror.py b/python/modules/kperf/perror.py index 304f548..62cb601 100644 --- a/python/modules/kperf/perror.py +++ b/python/modules/kperf/perror.py @@ -124,6 +124,7 @@ class Error: LIBPERF_ERR_COUNTER_INDEX_IS_ZERO = 1078 LIBPERF_ERR_BPF_ACT_FAILED = 1079 LIBPERF_ERR_INVALID_BPF_PARAM = 1080 + LIBPERF_ERR_NULL_POINTER = 1081 UNKNOWN_ERROR = 9999 diff --git a/util/pcerr.cpp b/util/pcerr.cpp index 6d9c53c..9cf3f64 100644 --- a/util/pcerr.cpp +++ b/util/pcerr.cpp @@ -66,7 +66,8 @@ namespace pcerr { {LIBPERF_ERR_ALLOCATE_REGISTER_FAILED, "Allocate register failed!"}, {LIBPERF_ERR_CHECK_USER_ACCESS, "Check user access failed!"}, {LIBPERF_ERR_INVALID_BPF_PARAM, "check bpf mode failed"}, - {LIBPERF_ERR_BPF_ACT_FAILED, "failed to execute bpf obj action"} + {LIBPERF_ERR_BPF_ACT_FAILED, "failed to execute bpf obj action"}, + {LIBPERF_ERR_NULL_POINTER, "the input pointer cannot be null"} }; static std::unordered_map warnMsgs = { {LIBPERF_WARN_CTXID_LOST, "Some SPE context packets are not found in the traces."}, -- Gitee