Skip to contentSkip to conversation

powerpc/perf: Fix kernel address leak to userspace via BHRB buffer

PATCH Superseded Madhavan Srinivasan 3 participants 6 messages Mar 4, 2018 – Mar 7, 2018 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 powerpc/perf: Fix kernel address leak to userspace via BHRB buffer

The current Branch History Rolling Buffer (BHRB) code does not check for any privilege levels before updating the data from BHRB. This leaks kernel addresses to userspace even when profiling only with userspace privileges. Add proper checks to prevent it.

---
 arch/powerpc/perf/core-book3s.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/perf/core-book3s.c +4 -0
index f89bbd54ecec..337db5831749 100644
@@ -457,6 +457,10 @@ static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
 				/* invalid entry */
 				continue;
 
+			if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
+				is_kernel_addr(addr))
+				continue;
+
 			/* Branches are read most recent first (ie. mfbhrb 0 is
 			 * the most recent branch).
 			 * There are two types of valid entries:
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 powerpc/perf: Fix the kernel address leak to userspace via SDAR

Sampled Data Address Register (SDAR) is a 64-bit register that contains the effective address of the storage operand of an instruction that was being executed, possibly out-of-order, at or around the time that the Performance Monitor alert occurred.

In certain scenario SDAR happen to contain the kernel address even for userspace only sampling. Add checks to prevent it.

---
 arch/powerpc/perf/core-book3s.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
arch/powerpc/perf/core-book3s.c +8 -3
index 337db5831749..c4525323d691 100644
@@ -95,7 +95,7 @@ static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
 {
 	return 0;
 }
-static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
+static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp, struct perf_event *event) { }
 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
 {
 	return 0;
@@ -174,7 +174,7 @@ static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
  * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
  * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
  */
-static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
+static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp, struct perf_event *event)
 {
 	unsigned long mmcra = regs->dsisr;
 	bool sdar_valid;
@@ -198,6 +198,11 @@ static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
 
 	if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
 		*addrp = mfspr(SPRN_SDAR);
+
+	if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
+		(event->attr.exclude_kernel || event->attr.exclude_hv) &&
+		is_kernel_addr(mfspr(SPRN_SDAR)))
+		*addrp = 0;
 }
 
 static bool regs_sihv(struct pt_regs *regs)
@@ -2054,7 +2059,7 @@ static void record_and_restart(struct perf_event *event, unsigned long val,
 
 		if (event->attr.sample_type &
 		    (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR))
-			perf_get_data_addr(regs, &data.addr);
+			perf_get_data_addr(regs, &data.addr, event);
 
 		if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
 			struct cpu_hw_events *cpuhw;
Signature · 1 line
2.7.4
Acked-by on v1 1/2
#4 lore raw
In reply to Madhavan Srinivasan
Quoted 16 lines · Madhavan Srinivasan

The current Branch History Rolling Buffer (BHRB) code does not check for any privilege levels before updating the data from BHRB. This leaks kernel addresses to userspace even when profiling only with userspace privileges. Add proper checks to prevent it.

---
 arch/powerpc/perf/core-book3s.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/perf/core-book3s.c +0 -0
index f89bbd54ecec..337db5831749 100644

arch/powerpc/perf/core-book3s.c static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) · line 463

Quoted 7 lines
arch/powerpc/perf/core-book3s.c +4 -0
@@ -457,6 +457,10 @@ static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
                                /* invalid entry */
                                continue;
 
+                       if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
+                               is_kernel_addr(addr))
+                               continue;
+

Looks good to me. The scope of the leaks concern is KASLR related or something else (figuring out what's in the cache?)

Balbir Singh.

#2 lore raw

Re: [PATCH 2/2] powerpc/perf: Fix the kernel address leak to userspace via SDAR

In reply to Madhavan Srinivasan

arch/powerpc/perf/core-boo=

Quoted 16 lines · Madhavan Srinivasan

Sampled Data Address Register (SDAR) is a 64-bit register that contains the effective address of the storage operand of an instruction that was being executed, possibly out-of-order, at or around the time that the Performance Monitor alert occurred. =20
In certain scenario SDAR happen to contain the kernel address even for userspace only sampling. Add checks to prevent it.
=20

---
 arch/powerpc/perf/core-book3s.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

=20

arch/powerpc/perf/core-boo= renamed from arch/powerpc/perf/core-book3s.c +0 -0

k3s.c

arch/powerpc/perf/core-book3s.c static inline unsigned long perf_ip_adjust(struct pt_re=

Quoted 4 lines

index 337db5831749..c4525323d691 100644

arch/powerpc/perf/core-book3s.c +0 -0
@@ -95,7 +95,7 @@ static inline unsigned long perf_ip_adjust(struct pt_re=

gs *regs)

arch/powerpc/perf/core-book3s.c static inline unsigned long perf_ip_adjust(struct pt_regs *regs)

Quoted 4 lines
arch/powerpc/perf/core-book3s.c +0 -1
 {
 	return 0;
 }
-static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) =

{ }

arch/powerpc/perf/core-book3s.c static inline unsigned long perf_ip_adjust(struct pt_re= · line 98

Quoted 1 line
arch/powerpc/perf/core-book3s.c +1 -0
+static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp, =

struct perf_event *event) { }

arch/powerpc/perf/core-book3s.c static inline unsigned long perf_ip_adjust(struct pt_=

Quoted 4 lines
arch/powerpc/perf/core-book3s.c +0 -0
 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
 {
 	return 0;
@@ -174,7 +174,7 @@ static inline unsigned long perf_ip_adjust(struct pt_=

regs *regs)

arch/powerpc/perf/core-book3s.c static inline unsigned long perf_ip_adjust(struct pt_= · line 174

Quoted 1 line
arch/powerpc/perf/core-book3s.c +0 -0
  * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, t=

he

arch/powerpc/perf/core-book3s.c static inline unsigned long perf_ip_adjust(struct pt_= · line 175

Quoted 1 line
arch/powerpc/perf/core-book3s.c +0 -0
  * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIE=

R.

arch/powerpc/perf/core-book3s.c static inline unsigned long perf_ip_adjust(struct pt_regs *regs)

Quoted 3 lines
arch/powerpc/perf/core-book3s.c +1 -1
  */
-static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
+static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp, =

struct perf_event *event)

arch/powerpc/perf/core-book3s.c static inline void perf_get_data_addr(struct pt_regs=

Quoted 4 lines
arch/powerpc/perf/core-book3s.c +0 -0
 {
 	unsigned long mmcra =3D regs->dsisr;
 	bool sdar_valid;
@@ -198,6 +198,11 @@ static inline void perf_get_data_addr(struct pt_regs=

*regs, u64 *addrp)

arch/powerpc/perf/core-book3s.c static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) · line 203

Quoted 6 lines

=20

arch/powerpc/perf/core-book3s.c +3 -0
 	if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
 		*addrp =3D mfspr(SPRN_SDAR);
+
+	if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
+		(event->attr.exclude_kernel || event->attr.exclude_hv) &&

I may be missing something, but if !capable(CAP_SYS_ADMIN), should we=20 still check the exclude_kernel/exclude_hv fields in the event attribute? =20 Aren't those user controlled?

- Naveen

arch/powerpc/perf/core-book3s.c static void record_and_restart(struct perf_event *e=

Quoted 6 lines
arch/powerpc/perf/core-book3s.c +2 -0
+		is_kernel_addr(mfspr(SPRN_SDAR)))
+		*addrp =3D 0;
 }

=20
static bool regs_sihv(struct pt_regs *regs)

arch/powerpc/perf/core-book3s.c +0 -0
@@ -2054,7 +2059,7 @@ static void record_and_restart(struct perf_event *e=

vent, unsigned long val,

Quoted 9 lines

=20

arch/powerpc/perf/core-book3s.c +1 -1
 		if (event->attr.sample_type &
 		    (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR))
-			perf_get_data_addr(regs, &data.addr);
+			perf_get_data_addr(regs, &data.addr, event);

=20

arch/powerpc/perf/core-book3s.c +0 -1
 		if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
 			struct cpu_hw_events *cpuhw;
--=20
Quoted 3 lines

2.7.4
=20
=20

=

#3 lore raw

Re: [PATCH 2/2] powerpc/perf: Fix the kernel address leak to userspace via SDAR

Quoted 46 lines · Naveen N. Rao
Quoted 45 lines · Madhavan Srinivasan

Sampled Data Address Register (SDAR) is a 64-bit register that contains the effective address of the storage operand of an instruction that was being executed, possibly out-of-order, at or around the time that the Performance Monitor alert occurred.

In certain scenario SDAR happen to contain the kernel address even for userspace only sampling. Add checks to prevent it.

---
 arch/powerpc/perf/core-book3s.c | 11 ++++++++---

 1 file changed, 8 insertions(+), 3 deletions(-)
arch/powerpc/perf/core-book3s.c +1 -1
index 337db5831749..c4525323d691 100644
@@ -95,7 +95,7 @@ static inline unsigned long perf_ip_adjust(struct 
 pt_regs *regs)
  {
      return 0;
  }
-static inline void perf_get_data_addr(struct pt_regs *regs, u64 
 *addrp) { }
+static inline void perf_get_data_addr(struct pt_regs *regs, u64 
 *addrp, struct perf_event *event) { }

 static inline u32 perf_get_misc_flags(struct pt_regs *regs) {
     return 0;

arch/powerpc/perf/core-book3s.c +1 -1
@@ -174,7 +174,7 @@ static inline unsigned long perf_ip_adjust(struct 
 pt_regs *regs)
   * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
   * [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
   */
-static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
+static inline void perf_get_data_addr(struct pt_regs *regs, u64 
 *addrp, struct perf_event *event)
  {

     unsigned long mmcra = regs->dsisr;
     bool sdar_valid;

arch/powerpc/perf/core-book3s.c +0 -0
@@ -198,6 +198,11 @@ static inline void perf_get_data_addr(struct 
 pt_regs *regs, u64 *addrp)
 
Quoted 8 lines

arch/powerpc/perf/core-book3s.c static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) · line 203

Quoted 5 lines
arch/powerpc/perf/core-book3s.c +3 -0
      if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
          *addrp = mfspr(SPRN_SDAR);
+
+    if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
+        (event->attr.exclude_kernel || event->attr.exclude_hv) &&

I may be missing something, but if !capable(CAP_SYS_ADMIN), should we still check the exclude_kernel/exclude_hv fields in the event attribute?  Aren't those user controlled?

Yes that right. But i also want to handle the case when we sampling only for userspace even with higher privilege level. May be I should handle that as a separate patch. I will respin this patch to check only for the privilege level and change the commit message accordingly.

Thanks for review
Maddy

Quoted 21 lines

- Naveen

Quoted 19 lines
arch/powerpc/perf/core-book3s.c +2 -0
+        is_kernel_addr(mfspr(SPRN_SDAR)))
+        *addrp = 0;
  }
 

 static bool regs_sihv(struct pt_regs *regs)

arch/powerpc/perf/core-book3s.c +0 -0
@@ -2054,7 +2059,7 @@ static void record_and_restart(struct 
 perf_event *event, unsigned long val,
 

         if (event->attr.sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) -            perf_get_data_addr(regs, &data.addr); +            perf_get_data_addr(regs, &data.addr, event);

         if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) { struct cpu_hw_events *cpuhw;

Signature · 1 line
2.7.4
#5 lore raw
In reply to Balbir Singh
Quoted 21 lines · Balbir Singh
Quoted 19 lines · Madhavan Srinivasan

The current Branch History Rolling Buffer (BHRB) code does not check for any privilege levels before updating the data from BHRB. This leaks kernel addresses to userspace even when profiling only with userspace privileges. Add proper checks to prevent it.

---
  arch/powerpc/perf/core-book3s.c | 4 ++++
  1 file changed, 4 insertions(+)
arch/powerpc/perf/core-book3s.c +0 -0
index f89bbd54ecec..337db5831749 100644
@@ -457,6 +457,10 @@ static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
                                 /* invalid entry */
                                 continue;
 
Quoted 7 lines

arch/powerpc/perf/core-book3s.c static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) · line 463

Quoted 4 lines
arch/powerpc/perf/core-book3s.c +4 -0
+                       if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
+                               is_kernel_addr(addr))
+                               continue;
+

Looks good to me. The scope of the leaks concern is KASLR related or something else (figuring out what's in the cache?)

I did not look at it closely. But will get the information.

Thanks for the review
Maddy

Quoted 5 lines

Balbir Singh.