diff options
| author | Ian Rogers <irogers@google.com> | 2026-08-09 00:14:48 -0700 |
|---|---|---|
| committer | Namhyung Kim <namhyung@kernel.org> | 2026-08-09 21:58:11 -0700 |
| commit | 0b274050c4d427828595c12c2e4e53d98612b4f9 (patch) | |
| tree | 0c88af19fe668d13a127e78f2d4d4b3730a155a3 /tools/perf/util/python.c | |
| parent | 9a142beb1eba42b986dc7016f4fc1a3bc28b8c09 (diff) | |
perf python: Validate attribute setters in pyrf_evsel
If val is NULL when setting an attribute, PyErr_SetString should be
called as deleting the attribute isn't supported. In addition, ensure
PyErr_Occurred is checked before setting the attribute to avoid setting
a garbage value.
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Diffstat (limited to 'tools/perf/util/python.c')
| -rw-r--r-- | tools/perf/util/python.c | 89 |
1 files changed, 77 insertions, 12 deletions
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index a1334400874c..848912401c4f 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -2301,6 +2301,11 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure CHECK_INITIALIZED_INT(pevsel->evsel, "evsel"); + if (val == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + is_true = PyObject_IsTrue(val); if (is_true < 0) return -1; @@ -2312,11 +2317,21 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure static int pyrf_evsel__set_attr_config(PyObject *self, PyObject *val, void *closure __maybe_unused) { struct pyrf_evsel *pevsel = (void *)self; + unsigned long long new_val; CHECK_INITIALIZED_INT(pevsel->evsel, "evsel"); - pevsel->evsel->core.attr.config = PyLong_AsUnsignedLongLong(val); - return PyErr_Occurred() ? -1 : 0; + if (val == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + + new_val = PyLong_AsUnsignedLongLong(val); + if (PyErr_Occurred()) + return -1; + + pevsel->evsel->core.attr.config = new_val; + return 0; } static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __maybe_unused) @@ -2331,11 +2346,21 @@ static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __may static int pyrf_evsel__set_attr_read_format(PyObject *self, PyObject *val, void *closure __maybe_unused) { struct pyrf_evsel *pevsel = (void *)self; + unsigned long long new_val; CHECK_INITIALIZED_INT(pevsel->evsel, "evsel"); - pevsel->evsel->core.attr.read_format = PyLong_AsUnsignedLongLong(val); - return PyErr_Occurred() ? -1 : 0; + if (val == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + + new_val = PyLong_AsUnsignedLongLong(val); + if (PyErr_Occurred()) + return -1; + + pevsel->evsel->core.attr.read_format = new_val; + return 0; } static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure __maybe_unused) @@ -2350,11 +2375,21 @@ static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure static int pyrf_evsel__set_attr_sample_period(PyObject *self, PyObject *val, void *closure __maybe_unused) { struct pyrf_evsel *pevsel = (void *)self; + unsigned long long new_val; CHECK_INITIALIZED_INT(pevsel->evsel, "evsel"); - pevsel->evsel->core.attr.sample_period = PyLong_AsUnsignedLongLong(val); - return PyErr_Occurred() ? -1 : 0; + if (val == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + + new_val = PyLong_AsUnsignedLongLong(val); + if (PyErr_Occurred()) + return -1; + + pevsel->evsel->core.attr.sample_period = new_val; + return 0; } static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closure __maybe_unused) @@ -2369,11 +2404,21 @@ static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closur static int pyrf_evsel__set_attr_sample_type(PyObject *self, PyObject *val, void *closure __maybe_unused) { struct pyrf_evsel *pevsel = (void *)self; + unsigned long long new_val; CHECK_INITIALIZED_INT(pevsel->evsel, "evsel"); - pevsel->evsel->core.attr.sample_type = PyLong_AsUnsignedLongLong(val); - return PyErr_Occurred() ? -1 : 0; + if (val == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + + new_val = PyLong_AsUnsignedLongLong(val); + if (PyErr_Occurred()) + return -1; + + pevsel->evsel->core.attr.sample_type = new_val; + return 0; } static PyObject *pyrf_evsel__get_attr_sample_type(PyObject *self, void *closure __maybe_unused) @@ -2397,11 +2442,21 @@ static PyObject *pyrf_evsel__get_attr_size(PyObject *self, void *closure __maybe static int pyrf_evsel__set_attr_type(PyObject *self, PyObject *val, void *closure __maybe_unused) { struct pyrf_evsel *pevsel = (void *)self; + unsigned long new_val; CHECK_INITIALIZED_INT(pevsel->evsel, "evsel"); - pevsel->evsel->core.attr.type = PyLong_AsUnsignedLong(val); - return PyErr_Occurred() ? -1 : 0; + if (val == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + + new_val = PyLong_AsUnsignedLong(val); + if (PyErr_Occurred()) + return -1; + + pevsel->evsel->core.attr.type = new_val; + return 0; } static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe_unused) @@ -2416,11 +2471,21 @@ static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe static int pyrf_evsel__set_attr_wakeup_events(PyObject *self, PyObject *val, void *closure __maybe_unused) { struct pyrf_evsel *pevsel = (void *)self; + unsigned long new_val; CHECK_INITIALIZED_INT(pevsel->evsel, "evsel"); - pevsel->evsel->core.attr.wakeup_events = PyLong_AsUnsignedLong(val); - return PyErr_Occurred() ? -1 : 0; + if (val == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + + new_val = PyLong_AsUnsignedLong(val); + if (PyErr_Occurred()) + return -1; + + pevsel->evsel->core.attr.wakeup_events = new_val; + return 0; } static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void *closure __maybe_unused) |
