Skip to contentSkip to conversation

arm/xen: Enable user access to the kernel before issuing a privcmd call

PATCH In review Julien Grall 3 participants 17 messages Sep 11, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm/xen: Enable user access to the kernel before issuing a privcmd call

When Xen is copyin data to/from the guest it will check if the kernel has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access anymore the user space by default. This will result to fail on every hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

I didn't find generic helpers to do a these operations, so the change is only arm32 specific.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Russell King <[email protected]>

ARM64 doesn't seem to have priviledge no-access support yet so there is nothing to do for now.

I haven't look x86 at all.

---
 arch/arm/xen/Makefile    |  1 +
 arch/arm/xen/hypercall.S |  4 ++--
 arch/arm/xen/privcmd.c   | 25 +++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 arch/arm/xen/privcmd.c
arch/arm/xen/Makefile +1 -0
index 1296952..d8d088a 100644
@@ -1 +1,2 @@
 obj-y		:= enlighten.o hypercall.o grant-table.o p2m.o mm.o
+obj-y		+= privcmd.o
arch/arm/xen/hypercall.S +2 -2
index f00e080..56e7181 100644
@@ -91,7 +91,7 @@ HYPERCALL3(vcpu_op);
 HYPERCALL1(tmem_op);
 HYPERCALL2(multicall);
 
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)
 	stmdb sp!, {r4}
 	mov r12, r0
 	mov r0, r1
@@ -102,4 +102,4 @@ ENTRY(privcmd_call)
 	__HVC(XEN_IMM)
 	ldm sp!, {r4}
 	ret lr
-ENDPROC(privcmd_call);
+ENDPROC(__privcmd_call);
arch/arm/xen/privcmd.c new file +25 -0
new file mode 100644
index 0000000..97f502a
@@ -0,0 +1,25 @@
+#include <linux/uaccess.h>
+#include <asm/xen/hypervisor.h>
+
+/* Forward declaration for the assembly function living in hypercall.S */
+long __privcmd_call(unsigned call, unsigned int long a1,
+		    unsigned long a2, unsigned long a3,
+		    unsigned long a4, unsigned long a5);
+
+long privcmd_call(unsigned call, unsigned int long a1,
+		  unsigned long a2, unsigned long a3,
+		  unsigned long a4, unsigned long a5)
+{
+	long ret;
+	/*
+	 * Privcmd calls are issued by the userspace. We need to allow the
+	 * kernel to access the userspace memory before issuing the hypercall.
+	 */
+	unsigned int ua_flags = uaccess_save_and_enable();
+
+	ret = __privcmd_call(call, a1, a2, a3, a4, a5);
+
+	uaccess_restore(ua_flags);
+
+	return ret;
+}
Signature · 1 line
2.1.4
#1 lore raw
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 5 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 5 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which could be used right here directly I think? That would be preferable to wrapping I think.

Ian.

#2 lore raw
Quoted 27 lines · Ian Campbell
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 5 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

Quoted 3 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

Quoted 6 lines

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 2 lines
arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which could be used right here directly I think? That would be preferable to wrapping I think.

Looking to the uaccess_save macro:

.macro uaccess_save, tmp
#ifdef CONFIG_CPU_SW_DOMAIN_PAN
mrc p15, 0, \tmp, c3, c0, 0
str \tmp, [sp, #S_FRAME_SIZE]
#endif
.endm

It's saving the register on the Stack with an offset S_FRAME_SIZE. AFAICT, S_FRAME_SIZE is the size of the pt_regs structure.

So it looks like to me that they are unusable for any assembly functions but entry point.

I though about using a static inline for privcmd_call but it was introducing changes on the arm64 in order to decouple hypercall.h (it's common right now).

Regards,

Signature · 1 line
Julien Grall
#3 lore raw
Quoted 37 lines · Julien Grall
Quoted 28 lines · Ian Campbell
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 6 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

Quoted 3 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

Quoted 7 lines

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 2 lines
arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which
could be used right here directly I think? That would be preferable to wrapping I think.

Quoted 1 line

Looking to the uaccess_save macro:

I was thinking more about uaccess_enable/disable.

Ian.

#4 lore raw
Quoted 38 lines · Ian Campbell
Quoted 37 lines · Julien Grall
Quoted 35 lines · Ian Campbell
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 6 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 5 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which
could be used right here directly I think? That would be preferable to wrapping I think.

Quoted 3 lines
Quoted 1 line

Looking to the uaccess_save macro:

I was thinking more about uaccess_enable/disable.

Well, we can't assume that the function will be called with uaccess disabled. So we have to save the state and restore it after issuing the hypercall.

Regards,

Signature · 1 line
Julien Grall
#5 lore raw
Quoted 2 lines · Julien Grall

Well, we can't assume that the function will be called with uaccess disabled.

Please explain your reasoning.

The reason copy_from_user() et.al. need to save and restore the DACR is because the DACR may be in one of two states on older ARM architectures. It may have set the kernel domain to 'manager' mode, to allow these accessors to work on kernel memory, or the kernel domain may be in 'client' mode, thereby preventing the accessors from touching kernel memory.

Unless the code path is reachable with the kernel domain in manager mode, (iow, a set_fs(KERNEL_DS) or set_fs(get_ds()) has been done) then it should be safe to use uaccess_disable/uaccess_enable.

Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#6 lore raw
Quoted 5 lines · Russell King - ARM Linux
Quoted 2 lines · Julien Grall

Well, we can't assume that the function will be called with uaccess disabled.

Please explain your reasoning.

I think I was confused about the usage of uaccess. Thank you for the explanation.

Quoted 10 lines

The reason copy_from_user() et.al. need to save and restore the DACR is because the DACR may be in one of two states on older ARM architectures. It may have set the kernel domain to 'manager' mode, to allow these accessors to work on kernel memory, or the kernel domain may be in 'client' mode, thereby preventing the accessors from touching kernel memory.

Unless the code path is reachable with the kernel domain in manager mode, (iow, a set_fs(KERNEL_DS) or set_fs(get_ds()) has been done) then it should be safe to use uaccess_disable/uaccess_enable.

I believe that our privcmd driver doesn't made any usage of set_fs(...), so I will use the uaccess_{disable,enable} macro.

Regards,

Signature · 1 line
Julien Grall
#7 lore raw
In reply to Russell King - ARM Linux
Quoted 5 lines · Russell King - ARM Linux
Quoted 2 lines · Julien Grall

Well, we can't assume that the function will be called with uaccess disabled.

Please explain your reasoning.

I think I was confused about the usage of uaccess. Thank you for the explanation.

Quoted 10 lines

The reason copy_from_user() et.al. need to save and restore the DACR is because the DACR may be in one of two states on older ARM architectures. It may have set the kernel domain to 'manager' mode, to allow these accessors to work on kernel memory, or the kernel domain may be in 'client' mode, thereby preventing the accessors from touching kernel memory.

Unless the code path is reachable with the kernel domain in manager mode, (iow, a set_fs(KERNEL_DS) or set_fs(get_ds()) has been done) then it should be safe to use uaccess_disable/uaccess_enable.

I believe that our privcmd driver doesn't made any usage of set_fs(...), so I will use the uaccess_{disable,enable} macro.

Regards,

Signature · 1 line
Julien Grall
#8 lore raw
In reply to Julien Grall
Quoted 2 lines · Julien Grall

Well, we can't assume that the function will be called with uaccess disabled.

Please explain your reasoning.

The reason copy_from_user() et.al. need to save and restore the DACR is because the DACR may be in one of two states on older ARM architectures. It may have set the kernel domain to 'manager' mode, to allow these accessors to work on kernel memory, or the kernel domain may be in 'client' mode, thereby preventing the accessors from touching kernel memory.

Unless the code path is reachable with the kernel domain in manager mode, (iow, a set_fs(KERNEL_DS) or set_fs(get_ds()) has been done) then it should be safe to use uaccess_disable/uaccess_enable.

Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#9 lore raw
In reply to Ian Campbell
Quoted 38 lines · Ian Campbell
Quoted 37 lines · Julien Grall
Quoted 35 lines · Ian Campbell
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 6 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 5 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which
could be used right here directly I think? That would be preferable to wrapping I think.

Quoted 3 lines
Quoted 1 line

Looking to the uaccess_save macro:

I was thinking more about uaccess_enable/disable.

Well, we can't assume that the function will be called with uaccess disabled. So we have to save the state and restore it after issuing the hypercall.

Regards,

Signature · 1 line
Julien Grall
#10 lore raw
In reply to Julien Grall
Quoted 37 lines · Julien Grall
Quoted 28 lines · Ian Campbell
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 6 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

Quoted 3 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

Quoted 7 lines

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 2 lines
arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which
could be used right here directly I think? That would be preferable to wrapping I think.

Quoted 1 line

Looking to the uaccess_save macro:

I was thinking more about uaccess_enable/disable.

Ian.

#11 lore raw
In reply to Julien Grall
Quoted 10 lines · Julien Grall

Looking to the uaccess_save macro:

.macro uaccess_save, tmp
#ifdef CONFIG_CPU_SW_DOMAIN_PAN
mrc p15, 0, \tmp, c3, c0, 0
str \tmp, [sp, #S_FRAME_SIZE]
#endif
.endm

Quoted 2 lines

It's saving the register on the Stack with an offset S_FRAME_SIZE. AFAICT, S_FRAME_SIZE is the size of the pt_regs structure.

While in the kernel, the uaccess state will be disabled except for the specific sites reading or writing userspace. That means the user-access part of the DACR is well known.

What isn't known is whether we're executing here in the kernel segment (set_fs(get_ds())) or not - which allows the kernel to use the userspace accessors to safely read from its own memory space.

If we can assume that's not in effect, then the DACR value is fully known at this point, and you can just do a:

uaccess_enable rtmp
HVC call
uaccess_disable rtmp

where rtmp is a register you can afford to be changed by the macro. I suspect 'ip' would be a good choice there.

Please put the macros as close to __HVC(XEN_IMM) as possible.

Thanks.

Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#12 lore raw
In reply to Julien Grall
Quoted 10 lines · Julien Grall

Looking to the uaccess_save macro:

.macro uaccess_save, tmp
#ifdef CONFIG_CPU_SW_DOMAIN_PAN
mrc p15, 0, \tmp, c3, c0, 0
str \tmp, [sp, #S_FRAME_SIZE]
#endif
.endm

Quoted 2 lines

It's saving the register on the Stack with an offset S_FRAME_SIZE. AFAICT, S_FRAME_SIZE is the size of the pt_regs structure.

While in the kernel, the uaccess state will be disabled except for the specific sites reading or writing userspace. That means the user-access part of the DACR is well known.

What isn't known is whether we're executing here in the kernel segment (set_fs(get_ds())) or not - which allows the kernel to use the userspace accessors to safely read from its own memory space.

If we can assume that's not in effect, then the DACR value is fully known at this point, and you can just do a:

uaccess_enable rtmp
HVC call
uaccess_disable rtmp

where rtmp is a register you can afford to be changed by the macro. I suspect 'ip' would be a good choice there.

Please put the macros as close to __HVC(XEN_IMM) as possible.

Thanks.

Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#13 lore raw
In reply to Ian Campbell
Quoted 27 lines · Ian Campbell
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 5 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

Quoted 3 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

Quoted 6 lines

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 2 lines
arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which could be used right here directly I think? That would be preferable to wrapping I think.

Looking to the uaccess_save macro:

.macro uaccess_save, tmp
#ifdef CONFIG_CPU_SW_DOMAIN_PAN
mrc p15, 0, \tmp, c3, c0, 0
str \tmp, [sp, #S_FRAME_SIZE]
#endif
.endm

It's saving the register on the Stack with an offset S_FRAME_SIZE. AFAICT, S_FRAME_SIZE is the size of the pt_regs structure.

So it looks like to me that they are unusable for any assembly functions but entry point.

I though about using a static inline for privcmd_call but it was introducing changes on the arm64 in order to decouple hypercall.h (it's common right now).

Regards,

Signature · 1 line
Julien Grall
#14 lore raw
In reply to Ian Campbell

Hi Ian,

Quoted 4 lines · Ian Campbell
Quoted 2 lines

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

That was a typo in the commit title of the patch. So I won't fix this one.

All the others will be fixed on the next version.

Regards,

Signature · 1 line
Julien Grall
#15 lore raw
In reply to Ian Campbell

Hi Ian,

Quoted 4 lines · Ian Campbell
Quoted 2 lines

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

That was a typo in the commit title of the patch. So I won't fix this one.

All the others will be fixed on the next version.

Regards,

Signature · 1 line
Julien Grall
#16 lore raw
In reply to Julien Grall
Quoted 1 line · Julien Grall

When Xen is copyin data to/from the guest it will check if the kernel

"copying"

Quoted 5 lines

has the right to do the access. If not, the hypercall will return an error.

After the commit a5e090acbf545c0a3b04080f8a488b17ec41fe02 "ARM:
software-based priviledged-no-access support", the kernel can't access

"privileged"

Quoted 1 line

anymore the user space by default. This will result to fail on every

"any more" (or "any longer")

Quoted 4 lines

hypercall made by the userspace (i.e via privcmd).

We have to enable the userspace access and then restore the correct permission everytime the privmcd is used to made an hypercall.

"every time" and "privcmd"

arch/arm/xen/hypercall.S HYPERCALL3(vcpu_op); · line 94

Quoted 5 lines

HYPERCALL1(tmem_op);
HYPERCALL2(multicall);

arch/arm/xen/hypercall.S +1 -1
-ENTRY(privcmd_call)
+ENTRY(__privcmd_call)

arch/arm/include/asm/assembler.h seems to contain uaccess_* macros which could be used right here directly I think? That would be preferable to wrapping I think.

Ian.