Subject: [PATCH 4.19 447/639] perf/ioctl: Add check for the sample_period value

From: Greg Kroah-Hartman <[email-masked]>
Date: Fri, 24 Jan 2020 10:30:17 +0100

From: Ravi Bangoria <[email-masked]>

[ Upstream commit 913a90bc5a3a06b1f04c337320e9aeee2328dd77 ]

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).

Signed-off-by: Ravi Bangoria <[email-masked]>
Signed-off-by: Peter Zijlstra (Intel) <[email-masked]>
Cc: Alexander Shishkin <[email-masked]>
Cc: Arnaldo Carvalho de Melo <[email-masked]>
Cc: Jiri Olsa <[email-masked]>
Cc: Linus Torvalds <[email-masked]>
Cc: Peter Zijlstra <[email-masked]>
Cc: Stephane Eranian <[email-masked]>
Cc: Thomas Gleixner <[email-masked]>
Cc: Vince Weaver <[email-masked]>
Cc: [email-masked]
Cc: [email-masked]
Cc: [email-masked]
Cc: [email-masked]
Fixes: 0819b2e30ccb ("perf: Limit perf_event_attr::sample_period to 63 bits")
Link: https://lkml.kernel.org/r/[email-masked]
Signed-off-by: Ingo Molnar <[email-masked]>
Signed-off-by: Sasha Levin <[email-masked]>
---
 kernel/events/core.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 751888cbed5c0..16af86ab24c42 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5012,6 +5012,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;
-- 
2.20.1