perf ioctl: Add check for the sample_period value
Subsystems (2)
PATCH 1/2 perf ioctl: Add check for the sample_period value
Add a check for sample_period value sent from userspace. Negative value does not make sense. And in powerpc arch code this could cause a recursive PMI leading to a hang (reported when running perf-fuzzer).
--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/events/core.c +3 -0
@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ; +if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ; +event_function_call (event, __perf_event_period, &value);return 0 ;
Signature · 1 line
2.20.1
PATCH 2/2 powerpc/perf: Fix mmcra corruption by bhrb_filter
Consider a scenario where user creates two events:
1st event:
attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY;
fd = perf_event_open(attr, 0, 1, -1, 0); This sets cpuhw->bhrb_filter to 0 and returns valid fd.
2nd event:
attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
attr.branch_sample_type = PERF_SAMPLE_BRANCH_CALL;
fd = perf_event_open(attr, 0, 1, -1, 0); It overrides cpuhw->bhrb_filter to -1 and returns with error.
Now if power_pmu_enable() gets called by any path other than power_pmu_add(), ppmu->config_bhrb(-1) will set mmcra to -1.
--- arch/powerpc/perf/core-book3s.c | 6 ++++-- arch/powerpc/perf/power8-pmu.c | 3 +++ arch/powerpc/perf/power9-pmu.c | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-)
arch/powerpc/perf/core-book3s.c +4 -2
@@ -1846,6 +1846,7 @@ static int power_pmu_event_init(struct perf_event *event)int n;int err;struct cpu_hw_events *cpuhw; +u64 bhrb_filter;if (!ppmu)return -ENOENT ;
@@ -1951,13 +1952,14 @@ static int power_pmu_event_init(struct perf_event *event) err =power_check_constraints (cpuhw, events, cflags, n +1 );if (has_branch_stack (event)) { - cpuhw->bhrb_filter = ppmu->bhrb_filter_map ( + bhrb_filter = ppmu->bhrb_filter_map ( event->attr.branch_sample_type); -if (cpuhw->bhrb_filter ==-1 ) { +if (bhrb_filter ==-1 ) {put_cpu_var (cpu_hw_events);return -EOPNOTSUPP ; } + cpuhw->bhrb_filter = bhrb_filter; }put_cpu_var (cpu_hw_events);
arch/powerpc/perf/power8-pmu.c +3 -0
@@ -29,6 +29,7 @@ enum {#define POWER8_MMCRA_IFM1 0x0000000040000000UL#define POWER8_MMCRA_IFM2 0x0000000080000000UL#define POWER8_MMCRA_IFM3 0x00000000C0000000UL +#define POWER8_MMCRA_BHRB_MASK 0x00000000C0000000UL/* * Raw event encoding for PowerISA v2.07 (Power8):
@@ -243,6 +244,8 @@ static u64 power8_bhrb_filter_map(u64 branch_sample_type)static void power8_config_bhrb (u64 pmu_bhrb_filter) { + pmu_bhrb_filter &=POWER8_MMCRA_BHRB_MASK ; +/* Enable BHRB filter in PMU */ mtspr (SPRN_MMCRA , (mfspr (SPRN_MMCRA ) | pmu_bhrb_filter)); }
arch/powerpc/perf/power9-pmu.c +3 -0
@@ -92,6 +92,7 @@ enum {#define POWER9_MMCRA_IFM1 0x0000000040000000UL#define POWER9_MMCRA_IFM2 0x0000000080000000UL#define POWER9_MMCRA_IFM3 0x00000000C0000000UL +#define POWER9_MMCRA_BHRB_MASK 0x00000000C0000000UL/* Nasty Power9 specific hack */ #define PVR_POWER9_CUMULUS 0x00002000
@@ -300,6 +301,8 @@ static u64 power9_bhrb_filter_map(u64 branch_sample_type)static void power9_config_bhrb (u64 pmu_bhrb_filter) { + pmu_bhrb_filter &=POWER9_MMCRA_BHRB_MASK ; +/* Enable BHRB filter in PMU */ mtspr (SPRN_MMCRA , (mfspr (SPRN_MMCRA ) | pmu_bhrb_filter)); }
Signature · 1 line
2.20.1
Re: [PATCH 2/2] powerpc/perf: Fix mmcra corruption by bhrb_filter
Quoted 14 lines · Ravi Bangoria
Consider a scenario where user creates two events:
1st event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY; fd = perf_event_open(attr, 0, 1, -1, 0);This sets
cpuhw->bhrb_filterto 0 and returns valid fd.2nd event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_CALL; fd = perf_event_open(attr, 0, 1, -1, 0);
Quoted 6 lines
It overrides
cpuhw->bhrb_filterto -1 and returns with error.Now if
power_pmu_enable()gets called by any path other thanpower_pmu_add(),ppmu->config_bhrb(-1)will set mmcra to -1.Signed-off-by Ravi Bangoria <[email protected]>
Re: [PATCH 2/2] powerpc/perf: Fix mmcra corruption by bhrb_filter
In reply to Ravi BangoriaQuoted 16 lines · Ravi Bangoria
Consider a scenario where user creates two events:
1st event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY; fd = perf_event_open(attr, 0, 1, -1, 0);This sets cpuhw->bhrb_filter to 0 and returns valid fd. 2nd event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_CALL; fd = perf_event_open(attr, 0, 1, -1, 0); It overrides cpuhw->bhrb_filter to -1 and returns with error.
Quoted 2 lines
Now if
power_pmu_enable()gets called by any path other thanpower_pmu_add(),ppmu->config_bhrb(-1)will set mmcra to -1.
Quoted 78 lines
Signed-off-by Ravi Bangoria <[email protected]>--- arch/powerpc/perf/core-book3s.c | 6 ++++-- arch/powerpc/perf/power8-pmu.c | 3 +++ arch/powerpc/perf/power9-pmu.c | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-)arch/powerpc/perf/core-book3s.c +4
-2@@ -1846,6 +1846,7 @@ static int power_pmu_event_init(struct perf_event *event)int n;int err;struct cpu_hw_events *cpuhw; +u64 bhrb_filter;if (!ppmu)return -ENOENT ;@@ -1951,13 +1952,14 @@ static int power_pmu_event_init(struct perf_event *event) err =power_check_constraints (cpuhw, events, cflags, n +1 );if (has_branch_stack (event)) { - cpuhw->bhrb_filter = ppmu->bhrb_filter_map ( + bhrb_filter = ppmu->bhrb_filter_map ( event->attr.branch_sample_type); -if (cpuhw->bhrb_filter ==-1 ) { +if (bhrb_filter ==-1 ) {put_cpu_var (cpu_hw_events);return -EOPNOTSUPP ; } + cpuhw->bhrb_filter = bhrb_filter; }put_cpu_var (cpu_hw_events);arch/powerpc/perf/power8-pmu.c +3
-0@@ -29,6 +29,7 @@ enum {#define POWER8_MMCRA_IFM1 0x0000000040000000UL#define POWER8_MMCRA_IFM2 0x0000000080000000UL#define POWER8_MMCRA_IFM3 0x00000000C0000000UL +#define POWER8_MMCRA_BHRB_MASK 0x00000000C0000000UL/* * Raw event encoding for PowerISA v2.07 (Power8): @@ -243,6 +244,8 @@ static u64 power8_bhrb_filter_map(u64 branch_sample_type)static void power8_config_bhrb (u64 pmu_bhrb_filter) { + pmu_bhrb_filter &=POWER8_MMCRA_BHRB_MASK ; +/* Enable BHRB filter in PMU */ mtspr (SPRN_MMCRA , (mfspr (SPRN_MMCRA ) | pmu_bhrb_filter)); }arch/powerpc/perf/power9-pmu.c +3
-0@@ -92,6 +92,7 @@ enum {#define POWER9_MMCRA_IFM1 0x0000000040000000UL#define POWER9_MMCRA_IFM2 0x0000000080000000UL#define POWER9_MMCRA_IFM3 0x00000000C0000000UL +#define POWER9_MMCRA_BHRB_MASK 0x00000000C0000000UL/* Nasty Power9 specific hack */ #define PVR_POWER9_CUMULUS 0x00002000@@ -300,6 +301,8 @@ static u64 power9_bhrb_filter_map(u64 branch_sample_type)static void power9_config_bhrb (u64 pmu_bhrb_filter) { + pmu_bhrb_filter &=POWER9_MMCRA_BHRB_MASK ; +/* Enable BHRB filter in PMU */ mtspr (SPRN_MMCRA , (mfspr (SPRN_MMCRA ) | pmu_bhrb_filter)); }
Michael Ellerman
AppliedRe: [PATCH 2/2] powerpc/perf: Fix mmcra corruption by bhrb_filter
In reply to Ravi BangoriaQuoted 14 lines · Ravi Bangoria
Consider a scenario where user creates two events:
1st event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY; fd = perf_event_open(attr, 0, 1, -1, 0);This sets
cpuhw->bhrb_filterto 0 and returns valid fd.2nd event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_CALL; fd = perf_event_open(attr, 0, 1, -1, 0);
Quoted 7 lines
It overrides
cpuhw->bhrb_filterto -1 and returns with error.Now if
power_pmu_enable()gets called by any path other thanpower_pmu_add(),ppmu->config_bhrb(-1)will set mmcra to -1.Signed-off-by Ravi Bangoria <[email protected]>Reviewed-by Madhavan Srinivasan <[email protected]>
Applied to powerpc fixes, thanks.
https://git.kernel.org/powerpc/c/3202e35ec1c8fc19cea24253ff83edf7
cheers
Peter Zijlstra
maintainerQuoted 14 lines · Ravi Bangoria
Add a check for
sample_periodvalue sent from userspace. Negative value does not make sense. And in powerpc arch code this could cause a recursive PMI leading to a hang (reported when running perf-fuzzer).Signed-off-by Ravi Bangoria <[email protected]>--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/events/core.c +0
-0
kernel/events/core.c static int perf_event_period(struct perf_event *event, u64 __user *arg) · line 5009
Quoted 5 lines
kernel/events/core.c +2
-0@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ; +if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ;
Well, perf_event_attr::sample_period is __u64. Would not be the site using it as signed be the one in error?
Peter Zijlstra
maintainerQuoted 18 lines · Peter Zijlstra
Quoted 17 lines · Ravi Bangoria
Add a check for
sample_periodvalue sent from userspace. Negative value does not make sense. And in powerpc arch code this could cause a recursive PMI leading to a hang (reported when running perf-fuzzer).Signed-off-by Ravi Bangoria <[email protected]>--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/events/core.c +0
-0@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ;
Quoted 5 lines
kernel/events/core.c static int perf_event_period(struct perf_event *event, u64 __user *arg) · line 5009
Quoted 2 lines
kernel/events/core.c +2
-0+if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ;Well,
perf_event_attr::sample_periodis__u64. Would not be the site using it as signed be the one in error?
You forgot to mention commit: 0819b2e30ccb9, so I guess this just makes it consistent and is fine.
Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Peter Zijlstra
Quoted 17 lines · Ravi Bangoria
Add a check for
sample_periodvalue sent from userspace. Negative value does not make sense. And in powerpc arch code this could cause a recursive PMI leading to a hang (reported when running perf-fuzzer).Signed-off-by Ravi Bangoria <[email protected]>--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/events/core.c +0
-0@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ;
Quoted 9 lines
Quoted 5 lines
kernel/events/core.c static int perf_event_period(struct perf_event *event, u64 __user *arg) · line 5009
Quoted 2 lines
kernel/events/core.c +2
-0+if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ;Well,
perf_event_attr::sample_periodis__u64. Would not be the site using it as signed be the one in error?You forgot to mention commit:
0819b2e30ccb9, so I guess this just makes it consistent and is fine.
Yeah, I was about to reply :)
Michael Ellerman
reviewerQuoted 26 lines · Ravi Bangoria
Quoted 25 lines · Peter Zijlstra
Quoted 23 lines · Peter Zijlstra
Quoted 14 lines · Ravi Bangoria
Add a check for
sample_periodvalue sent from userspace. Negative value does not make sense. And in powerpc arch code this could cause a recursive PMI leading to a hang (reported when running perf-fuzzer).Signed-off-by Ravi Bangoria <[email protected]>--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/events/core.c +0
-0kernel/events/core.c static int perf_event_period(struct perf_event *event, u64 __user *arg) · line 5009
Quoted 5 lines
kernel/events/core.c +2
-0@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ; +if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ;Well,
perf_event_attr::sample_periodis__u64. Would not be the site using it as signed be the one in error?
Quoted 5 lines
Quoted 3 lines
You forgot to mention commit:
0819b2e30ccb9, so I guess this just makes it consistent and is fine.Yeah, I was about to reply :)
I've taken patch 2. You should probably do a v2 of patch 1 with an updated change log that explains things fully?
cheers
PATCH v2 1/1 perf ioctl: Add check for the sample_period value
perf_event_open() limits the sample_period to 63 bits. See commit 0819b2e30ccb ("perf: Limit perf_event_attr::sample_period to 63 bits"). Make ioctl() consistent with it.
Also on powerpc, negative sample_period could cause a recursive PMIs leading to a hang (reported when running perf-fuzzer).
--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/events/core.c +3 -0
@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ; +if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ; +event_function_call (event, __perf_event_period, &value);return 0 ;
Signature · 1 line
2.20.1
Peter / mpe,
Is the v2 looks good? If so, can anyone of you please pick this up.
Quoted 27 lines · Ravi Bangoria
perf_event_open()limits thesample_periodto 63 bits. See commit0819b2e30ccb("perf: Limitperf_event_attr::sample_periodto 63 bits"). Makeioctl()consistent with it.Also on powerpc, negative
sample_periodcould cause a recursive PMIs leading to a hang (reported when running perf-fuzzer).Signed-off-by Ravi Bangoria <[email protected]>--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/events/core.c +3
-0@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ; +if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ; +event_function_call (event, __perf_event_period, &value);return 0 ;
Michael Ellerman
reviewerQuoted 3 lines · Ravi Bangoria
Peter / mpe,
Is the v2 looks good? If so, can anyone of you please pick this up.
I usually wouldn't take it, it's generic perf code. Unless peter/ingo/acme tell me otherwise.
It's sort of a bug fix for 0819b2e30ccb, should it have a fixes and/or stable tag?
cheers
Quoted 28 lines · Ravi Bangoria
Quoted 27 lines
perf_event_open()limits thesample_periodto 63 bits. See commit0819b2e30ccb("perf: Limitperf_event_attr::sample_periodto 63 bits"). Makeioctl()consistent with it.Also on powerpc, negative
sample_periodcould cause a recursive PMIs leading to a hang (reported when running perf-fuzzer).Signed-off-by Ravi Bangoria <[email protected]>--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/events/core.c +3
-0@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ; +if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ; +event_function_call (event, __perf_event_period, &value);return 0 ;
tip-bot for Ravi Bangoria
merge bot[tip:perf/urgent] perf/ioctl: Add check for the sample_period value
In reply to Ravi Bangoriaperf/ioctl: Add check for the sample_period value
Commit-ID: 913a90bc5a3a06b1f04c337320e9aeee2328dd77 Gitweb: https://git.kernel.org/tip/913a90bc5a3a06b1f04c337320e9aeee2328dd77 Author: Ravi Bangoria <[email protected]> AuthorDate: Tue, 4 Jun 2019 09:59:53 +0530 Committer: Ingo Molnar <[email protected]> CommitDate: Mon, 24 Jun 2019 19:19:22 +0200
perf/ioctl: Add check for the sample_period value
perf_event_open() limits the sample_period to 63 bits. See:
0819b2e30ccb ("perf: Limit perf_event_attr::sample_period to 63 bits")
Make ioctl() consistent with it.
Also on PowerPC, negative sample_period could cause a recursive PMIs leading to a hang (reported when running perf-fuzzer).
--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/events/core.c +3 -0
@@ -5005,6 +5005,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)if (perf_event_check_period (event, value))return -EINVAL ; +if (!event->attr.freq && (value & (1ULL <<63 ))) +return -EINVAL ; +event_function_call (event, __perf_event_period, &value);return 0 ;