perf/core: don't sample kernel regs upon skid
Subsystems (3)
Mark Rutland
reviewerRFC PATCH 1/1 perf/core: don't sample kernel regs upon skid
Users can request that general purpose registers, instruction pointer, etc, are sampled when a perf event counter overflows. To try to avoid this resulting in kernel state being leaked, unprivileged users are usually forbidden from opening events which count while the kernel is running.
Unfortunately, this is not sufficient to avoid leading kernel state.
For various reasons, there can be a delay between the overflow occurring and the resulting overflow exception (e.g. an NMI) being taken. During this window, other instructions may be executed, resulting in skid.
This skid means that a userspace-only event overflowing may result in an exception being taken *after* entry to the kernel, allowing kernel registers to be sampled. Depending on the amount of skid, this may only leak the PC (breaking KASLR), or it may leak secrets which are currently live in GPRs.
Let's avoid this by only sampling from the user registers when an event is supposed to exclude the kernel, providing the illusion that the overflow exception is taken from userspace.
We also have similar cases when sampling a guest, where we get the host regs in some cases. It's not entirely clear to me how we should handle these.
--- kernel/events/core.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
kernel/events/core.c +28 -0
@@ -6361,6 +6361,32 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)return callchain ?: &__empty_callchain; } +static struct pt_regs *perf_get_sample_regs (struct perf_event *event, +struct pt_regs *regs) +{ +/* +* Due to interrupt latency (AKA "skid"), we may enter the kernel +* before taking an overflow, even if the PMU is only counting user +* events. +* +* If we're not counting kernel events, always use the user regs when +* sampling. +* +* TODO: what do we do about sampling a guest's registers? The IP is +* special-cased, but for the rest of the regs they'll get the +* user/kernel regs depending on whether exclude_kernel is set, which +* is nonsensical. +* +* We can't get at the full set of regs in all cases (e.g. Xen's PV PMU +* can't provide the GPRs), so should we just zero the GPRs when in a +* guest? Or skip outputting the regs in perf_output_sample? +*/ +if (event->attr.exclude_kernel && !user_mode (regs)) +return task_pt_regs (current); + +return regs; +} + void perf_prepare_sample(struct perf_event_header *header, struct perf_sample_data *data, struct perf_event *event,
@@ -6368,6 +6394,8 @@ void perf_prepare_sample(struct perf_event_header *header, {u64 sample_type = event->attr.sample_type; + regs =perf_get_sample_regs (event, regs); + header->type =PERF_RECORD_SAMPLE ; header->size =sizeof (*header) + event->header_size;
Signature · 1 line
2.11.0
Peter Zijlstra
maintainerQuoted 7 lines · Mark Rutland
Users can request that general purpose registers, instruction pointer, etc, are sampled when a perf event counter overflows. To try to avoid this resulting in kernel state being leaked, unprivileged users are usually forbidden from opening events which count while the kernel is running.
Unfortunately, this is not sufficient to avoid leading kernel state.
'leaking' surely.
Quoted 11 lines
For various reasons, there can be a delay between the overflow occurring and the resulting overflow exception (e.g. an NMI) being taken. During this window, other instructions may be executed, resulting in skid.
This skid means that a userspace-only event overflowing may result in an exception being taken *after* entry to the kernel, allowing kernel registers to be sampled. Depending on the amount of skid, this may only leak the PC (breaking KASLR), or it may leak secrets which are currently live in GPRs.
Quoted 7 lines
Let's avoid this by only sampling from the user registers when an event is supposed to exclude the kernel, providing the illusion that the overflow exception is taken from userspace.
We also have similar cases when sampling a guest, where we get the host regs in some cases. It's not entirely clear to me how we should handle these.
Would not a similar:
if ((event->attr.exclude_hv || event->attr.exclude_host) /* WTF both !? */ && perf_guest_cbs && !perf_guest_cbs->is_in_guest()) return perf_guest_cbs->guest_pt_regs();
work there? Of course, perf_guest_info_callbacks is currently lacking that guest_pt_regs() thingy..
Quoted 26 lines
Signed-off-by Mark Rutland <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jin Yao <[email protected]>Cc Peter Zijlstra <[email protected]>--- kernel/events/core.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)kernel/events/core.c +9
-0@@ -6361,6 +6361,32 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)return callchain ?: &__empty_callchain; } +static struct pt_regs *perf_get_sample_regs (struct perf_event *event, +struct pt_regs *regs) +{ +/* +* Due to interrupt latency (AKA "skid"), we may enter the kernel +* before taking an overflow, even if the PMU is only counting user +* events. +* +* If we're not counting kernel events, always use the user regs when
kernel/events/core.c perf_callchain(struct perf_event *event, struct pt_regs *regs) · line 6382
Quoted 10 lines
kernel/events/core.c +10
-0+* sampling. +* +* TODO: what do we do about sampling a guest's registers? The IP is +* special-cased, but for the rest of the regs they'll get the +* user/kernel regs depending on whether exclude_kernel is set, which +* is nonsensical. +* +* We can't get at the full set of regs in all cases (e.g. Xen's PV PMU +* can't provide the GPRs), so should we just zero the GPRs when in a +* guest? Or skip outputting the regs in perf_output_sample?
Seems daft Xen cannot provide registers; why is that? Boris?
Quoted 11 lines
kernel/events/core.c +7
-0+*/ +if (event->attr.exclude_kernel && !user_mode (regs)) +return task_pt_regs (current); + +return regs; +} + void perf_prepare_sample(struct perf_event_header *header, struct perf_sample_data *data, struct perf_event *event,
kernel/events/core.c void perf_prepare_sample(struct perf_event_header *header, · line 6400
Quoted 7 lines
kernel/events/core.c +2
-0@@ -6368,6 +6394,8 @@ void perf_prepare_sample(struct perf_event_header *header, {u64 sample_type = event->attr.sample_type; + regs =perf_get_sample_regs (event, regs); + header->type =PERF_RECORD_SAMPLE ; header->size =sizeof (*header) + event->header_size;
In any case ACK for this thing.
Mark Rutland
reviewerQuoted 30 lines · Peter Zijlstra
Quoted 7 lines · Mark Rutland
Users can request that general purpose registers, instruction pointer, etc, are sampled when a perf event counter overflows. To try to avoid this resulting in kernel state being leaked, unprivileged users are usually forbidden from opening events which count while the kernel is running.
Unfortunately, this is not sufficient to avoid leading kernel state.
'leaking' surely.
Quoted 11 lines
For various reasons, there can be a delay between the overflow occurring and the resulting overflow exception (e.g. an NMI) being taken. During this window, other instructions may be executed, resulting in skid.
This skid means that a userspace-only event overflowing may result in an exception being taken *after* entry to the kernel, allowing kernel registers to be sampled. Depending on the amount of skid, this may only leak the PC (breaking KASLR), or it may leak secrets which are currently live in GPRs.
Quoted 7 lines
Let's avoid this by only sampling from the user registers when an event is supposed to exclude the kernel, providing the illusion that the overflow exception is taken from userspace.
We also have similar cases when sampling a guest, where we get the host regs in some cases. It's not entirely clear to me how we should handle these.
Quoted 7 lines
Would not a similar:
if ((event->attr.exclude_hv || event->attr.exclude_host) /* WTF both !? */ && perf_guest_cbs && !perf_guest_cbs->is_in_guest()) return perf_guest_cbs->guest_pt_regs();work there?
Mostly. It's fun if the user also passed exclude_guest -- I have no idea what should be sampled there, if anything.
It's easy to get stuck in a rabbit hole looking at this.
Quoted 2 lines
Of course,
perf_guest_info_callbacksis currently lacking thatguest_pt_regs()thingy..
Yeah; I started looking at implementing it, but ran away since it wasn't clear to me how to build that on most architectures.
Quoted 29 lines
Quoted 29 lines
Signed-off-by Mark Rutland <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jin Yao <[email protected]>Cc Peter Zijlstra <[email protected]>--- kernel/events/core.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)kernel/events/core.c +13
-0@@ -6361,6 +6361,32 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)return callchain ?: &__empty_callchain; } +static struct pt_regs *perf_get_sample_regs (struct perf_event *event, +struct pt_regs *regs) +{ +/* +* Due to interrupt latency (AKA "skid"), we may enter the kernel +* before taking an overflow, even if the PMU is only counting user +* events. +* +* If we're not counting kernel events, always use the user regs when +* sampling. +* +* TODO: what do we do about sampling a guest's registers? The IP is +* special-cased, but for the rest of the regs they'll get the
Quoted 8 lines
kernel/events/core.c perf_callchain(struct perf_event *event, struct pt_regs *regs) · line 6382
Quoted 6 lines
kernel/events/core.c +6
-0+* user/kernel regs depending on whether exclude_kernel is set, which +* is nonsensical. +* +* We can't get at the full set of regs in all cases (e.g. Xen's PV PMU +* can't provide the GPRs), so should we just zero the GPRs when in a +* guest? Or skip outputting the regs in perf_output_sample? Seems daft Xen cannot provide registers; why is that? Boris?
The xen_pmu_regs structure simply doesn't have them, so I assume there's no API to get them.
Given we don't currently sample the guest regs, I'd be tempted to just zero them for now, or skip the sample at output time (if that doesn't break some other case).
Thanks,
Mark.
Boris Ostrovsky
reviewerQuoted 81 lines · Mark Rutland
Quoted 27 lines · Peter Zijlstra
Quoted 7 lines · Mark Rutland
Users can request that general purpose registers, instruction pointer, etc, are sampled when a perf event counter overflows. To try to avoid this resulting in kernel state being leaked, unprivileged users are usually forbidden from opening events which count while the kernel is running.
Unfortunately, this is not sufficient to avoid leading kernel state.
'leaking' surely.
Quoted 10 lines
For various reasons, there can be a delay between the overflow occurring and the resulting overflow exception (e.g. an NMI) being taken. During this window, other instructions may be executed, resulting in skid.
This skid means that a userspace-only event overflowing may result in an exception being taken *after* entry to the kernel, allowing kernel registers to be sampled. Depending on the amount of skid, this may only leak the PC (breaking KASLR), or it may leak secrets which are currently live in GPRs.
Quoted 7 lines
Let's avoid this by only sampling from the user registers when an event is supposed to exclude the kernel, providing the illusion that the overflow exception is taken from userspace.
We also have similar cases when sampling a guest, where we get the host regs in some cases. It's not entirely clear to me how we should handle these.
Quoted 7 lines
Would not a similar:
if ((event->attr.exclude_hv || event->attr.exclude_host) /* WTF both !? */ && perf_guest_cbs && !perf_guest_cbs->is_in_guest()) return perf_guest_cbs->guest_pt_regs();work there?
Mostly. It's fun if the user also passed
exclude_guest-- I have no idea what should be sampled there, if anything.It's easy to get stuck in a rabbit hole looking at this.
Quoted 2 lines
Of course,
perf_guest_info_callbacksis currently lacking thatguest_pt_regs()thingy..Yeah; I started looking at implementing it, but ran away since it wasn't clear to me how to build that on most architectures.
Quoted 28 lines
Quoted 28 lines
Signed-off-by Mark Rutland <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jin Yao <[email protected]>Cc Peter Zijlstra <[email protected]>--- kernel/events/core.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)kernel/events/core.c +12
-0@@ -6361,6 +6361,32 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)return callchain ?: &__empty_callchain; } +static struct pt_regs *perf_get_sample_regs (struct perf_event *event, +struct pt_regs *regs) +{ +/* +* Due to interrupt latency (AKA "skid"), we may enter the kernel +* before taking an overflow, even if the PMU is only counting user +* events. +* +* If we're not counting kernel events, always use the user regs when +* sampling. +* +* TODO: what do we do about sampling a guest's registers? The IP is Quoted 8 lines
kernel/events/core.c perf_callchain(struct perf_event *event, struct pt_regs *regs) · line 6382
Quoted 7 lines
kernel/events/core.c +7
-0+* special-cased, but for the rest of the regs they'll get the +* user/kernel regs depending on whether exclude_kernel is set, which +* is nonsensical. +* +* We can't get at the full set of regs in all cases (e.g. Xen's PV PMU +* can't provide the GPRs), so should we just zero the GPRs when in a +* guest? Or skip outputting the regs in perf_output_sample? Seems daft Xen cannot provide registers; why is that? Boris?
Quoted 6 lines
The
xen_pmu_regsstructure simply doesn't have them, so I assume there's no API to get them.Given we don't currently sample the guest regs, I'd be tempted to just zero them for now, or skip the sample at output time (if that doesn't break some other case).
(Was out on vacation, couldn't respond earlier)
Yes, PV guests only get a limited set of registers passed to the handler by the hypervisor. GPRs are not part of this set.
I think we need do
arch/x86/xen/pmu.c +0 -0
@@ -478,7 +478,7 @@ static void xen_convert_regs(const struct
xen_pmu_regs *xen_regs,
irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id) {
int err, ret = IRQ_NONE;
- struct pt_regs regs;
+ struct pt_regs regs = {0};
const struct xen_pmu_data *xenpmu_data = get_xenpmu_data(); uint8_t xenpmu_flags = get_xenpmu_flags();
Do you want me to submit a separate patch or can you make this part of yours?
Thanks.
-boris
Mark Rutland
reviewerQuoted 26 lines · Boris Ostrovsky
Quoted 25 lines · Mark Rutland
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Mark Rutland
(quoted diff) +12
-0+static struct pt_regs *perf_get_sample_regs(struct perf_event *event, + struct pt_regs *regs) +{ + /* + * Due to interrupt latency (AKA "skid"), we may enter the kernel + * before taking an overflow, even if the PMU is only counting user + * events. + * + * If we're not counting kernel events, always use the user regs when + * sampling. + * + * TODO: what do we do about sampling a guest's registers? The IP isQuoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * special-cased, but for the rest of the regs they'll get the + * user/kernel regs depending on whether exclude_kernel is set, which + * is nonsensical. + * + * We can't get at the full set of regs in all cases (e.g. Xen's PV PMU + * can't provide the GPRs), so should we just zero the GPRs when in a + * guest? Or skip outputting the regs in perf_output_sample?Seems daft Xen cannot provide registers; why is that? Boris?
The
xen_pmu_regsstructure simply doesn't have them, so I assume there's no API to get them.
Quoted 8 lines
Quoted 3 lines
Given we don't currently sample the guest regs, I'd be tempted to just zero them for now, or skip the sample at output time (if that doesn't break some other case).
(Was out on vacation, couldn't respond earlier)
Yes, PV guests only get a limited set of registers passed to the handler by the hypervisor. GPRs are not part of this set.
Is that also true for Dom0?
Quoted 17 lines
I think we need do
arch/x86/xen/pmu.c +1
-1@@ -478,7 +478,7 @@ static void xen_convert_regs(const structxen_pmu_regs *xen_regs, irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id) { int err, ret = IRQ_NONE; - struct pt_regs regs; + struct pt_regs regs = {0}; const struct xen_pmu_data *xenpmu_data = get_xenpmu_data(); uint8_t xenpmu_flags = get_xenpmu_flags();
Quoted 2 lines
Do you want me to submit a separate patch or can you make this part of yours?
I think this is going to become a series rather than a single patch, but I can have a go. I need to get my head around how the various cases interact with each other.
Thanks,
Mark.
Boris Ostrovsky
reviewerQuoted 27 lines · Mark Rutland
Quoted 26 lines · Boris Ostrovsky
Quoted 25 lines · Mark Rutland
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Mark Rutland
(quoted diff) +12
-0+static struct pt_regs *perf_get_sample_regs(struct perf_event *event, + struct pt_regs *regs) +{ + /* + * Due to interrupt latency (AKA "skid"), we may enter the kernel + * before taking an overflow, even if the PMU is only counting user + * events. + * + * If we're not counting kernel events, always use the user regs when + * sampling. + * + * TODO: what do we do about sampling a guest's registers? The IP isQuoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * special-cased, but for the rest of the regs they'll get the + * user/kernel regs depending on whether exclude_kernel is set, which + * is nonsensical. + * + * We can't get at the full set of regs in all cases (e.g. Xen's PV PMU + * can't provide the GPRs), so should we just zero the GPRs when in a + * guest? Or skip outputting the regs in perf_output_sample?Seems daft Xen cannot provide registers; why is that? Boris?
The
xen_pmu_regsstructure simply doesn't have them, so I assume there's no API to get them.
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Given we don't currently sample the guest regs, I'd be tempted to just zero them for now, or skip the sample at output time (if that doesn't break some other case).
(Was out on vacation, couldn't respond earlier)
Yes, PV guests only get a limited set of registers passed to the handler by the hypervisor. GPRs are not part of this set.
Is that also true for Dom0?
Yes. As far as VPMU is concerned there is (almost) no differences between privileged (i.e. dom0) and unprivileged PV guests.
-boris
Mark Rutland
reviewerQuoted 27 lines · Mark Rutland
Quoted 26 lines · Boris Ostrovsky
Quoted 25 lines · Mark Rutland
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Mark Rutland
(quoted diff) +12
-0+static struct pt_regs *perf_get_sample_regs(struct perf_event *event, + struct pt_regs *regs) +{ + /* + * Due to interrupt latency (AKA "skid"), we may enter the kernel + * before taking an overflow, even if the PMU is only counting user + * events. + * + * If we're not counting kernel events, always use the user regs when + * sampling. + * + * TODO: what do we do about sampling a guest's registers? The IP isQuoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * special-cased, but for the rest of the regs they'll get the + * user/kernel regs depending on whether exclude_kernel is set, which + * is nonsensical. + * + * We can't get at the full set of regs in all cases (e.g. Xen's PV PMU + * can't provide the GPRs), so should we just zero the GPRs when in a + * guest? Or skip outputting the regs in perf_output_sample?Seems daft Xen cannot provide registers; why is that? Boris?
The
xen_pmu_regsstructure simply doesn't have them, so I assume there's no API to get them.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Given we don't currently sample the guest regs, I'd be tempted to just zero them for now, or skip the sample at output time (if that doesn't break some other case).
I think this is going to become a series rather than a single patch, but I can have a go. I need to get my head around how the various cases interact with each other.
As a heads-up, due to sabbatical leave I'm probably not going to have the chance to dig into this until September.
If someone else is able to take a look at this, that would be great. Otherwise, I'm happy to take a look once I get back.
Thanks,
Mark.
Mark Rutland
reviewerQuoted 52 lines · Boris Ostrovsky
Quoted 22 lines · Mark Rutland
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Mark Rutland
(quoted diff) +12
-0+static struct pt_regs *perf_get_sample_regs(struct perf_event *event, + struct pt_regs *regs) +{ + /* + * Due to interrupt latency (AKA "skid"), we may enter the kernel + * before taking an overflow, even if the PMU is only counting user + * events. + * + * If we're not counting kernel events, always use the user regs when + * sampling. + * + * TODO: what do we do about sampling a guest's registers? The IP isQuoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * special-cased, but for the rest of the regs they'll get the + * user/kernel regs depending on whether exclude_kernel is set, which + * is nonsensical. + * + * We can't get at the full set of regs in all cases (e.g. Xen's PV PMU + * can't provide the GPRs), so should we just zero the GPRs when in a + * guest? Or skip outputting the regs in perf_output_sample?Seems daft Xen cannot provide registers; why is that? Boris?
Quoted 6 lines
The
xen_pmu_regsstructure simply doesn't have them, so I assume there's no API to get them.Given we don't currently sample the guest regs, I'd be tempted to just zero them for now, or skip the sample at output time (if that doesn't break some other case).
(Was out on vacation, couldn't respond earlier)
Yes, PV guests only get a limited set of registers passed to the handler by the hypervisor. GPRs are not part of this set.
I think we need do
arch/x86/xen/pmu.c +1
-1@@ -478,7 +478,7 @@ static void xen_convert_regs(const structxen_pmu_regs *xen_regs, irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id) { int err, ret = IRQ_NONE; - struct pt_regs regs; + struct pt_regs regs = {0}; const struct xen_pmu_data *xenpmu_data = get_xenpmu_data(); uint8_t xenpmu_flags = get_xenpmu_flags();
Quoted 2 lines
Do you want me to submit a separate patch or can you make this part of yours?
I've only just realised that this is an issue today, without my synthezied pt_regs changes. For any PERF_SAMPLE_REGS_* event under Xen, we'll leak uninitialised kernel stack to userspace in the GPR fields.
Boris, I think it's worth spinning a patch to address that now, with Cc stable, if you're still happy to do so?
Thanks,
Mark.
Boris Ostrovsky
reviewerQuoted 52 lines · Mark Rutland
Quoted 51 lines · Boris Ostrovsky
Quoted 22 lines · Mark Rutland
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Mark Rutland
(quoted diff) +12
-0+static struct pt_regs *perf_get_sample_regs(struct perf_event *event, + struct pt_regs *regs) +{ + /* + * Due to interrupt latency (AKA "skid"), we may enter the kernel + * before taking an overflow, even if the PMU is only counting user + * events. + * + * If we're not counting kernel events, always use the user regs when + * sampling. + * + * TODO: what do we do about sampling a guest's registers? The IP isQuoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * special-cased, but for the rest of the regs they'll get the + * user/kernel regs depending on whether exclude_kernel is set, which + * is nonsensical. + * + * We can't get at the full set of regs in all cases (e.g. Xen's PV PMU + * can't provide the GPRs), so should we just zero the GPRs when in a + * guest? Or skip outputting the regs in perf_output_sample?Seems daft Xen cannot provide registers; why is that? Boris?
Quoted 6 lines
The
xen_pmu_regsstructure simply doesn't have them, so I assume there's no API to get them.Given we don't currently sample the guest regs, I'd be tempted to just zero them for now, or skip the sample at output time (if that doesn't break some other case).
(Was out on vacation, couldn't respond earlier)
Yes, PV guests only get a limited set of registers passed to the handler by the hypervisor. GPRs are not part of this set.
I think we need do
arch/x86/xen/pmu.c +1
-1@@ -478,7 +478,7 @@ static void xen_convert_regs(const structxen_pmu_regs *xen_regs, irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id) { int err, ret = IRQ_NONE; - struct pt_regs regs; + struct pt_regs regs = {0}; const struct xen_pmu_data *xenpmu_data = get_xenpmu_data(); uint8_t xenpmu_flags = get_xenpmu_flags();
Quoted 8 lines
Quoted 2 lines
Do you want me to submit a separate patch or can you make this part of yours?
I've only just realised that this is an issue today, without my synthezied
pt_regschanges. For any PERF_SAMPLE_REGS_* event under Xen, we'll leak uninitialised kernel stack to userspace in the GPR fields.Boris, I think it's worth spinning a patch to address that now, with Cc stable, if you're still happy to do so?
Sure, I can do this.
(As a side note, I also noticed this issue but wasn't especially concerned about it since VPMU is not supported in Xen from security
perspective: http://xenbits.xenproject.org/xsa/advisory-163.html).
-boris