Skip to contentSkip to conversation

__get_user/__put_user

Xenomai Gilles Chanteperdrix 4 participants 23 messages Mar 16, 2013 – May 10, 2013

lore
Share & tools
lore
#0 lore raw

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Thanks in advance.

Signature · 1 line
                                                                Gilles.
#1 lore raw
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130316/3caec3c4/attachment.pgp>

#2 lore raw
Quoted 8 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

Quoted 6 lines
Quoted 3 lines

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Signature · 1 line
                                                                Gilles.
#3 lore raw
Quoted 14 lines · Gilles Chanteperdrix
Quoted 12 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Quoted 6 lines
Quoted 2 lines

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130316/c8d623b0/attachment.pgp>

#4 lore raw
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Signature · 1 line
                                                                Gilles.
#5 lore raw
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off. So, I believe we can simplify __ipipe_uaccess_might_fault to avoid the check for irqs off.

Signature · 1 line
                                                                Gilles.
#6 lore raw
Quoted 27 lines · Gilles Chanteperdrix
Quoted 25 lines · Gilles Chanteperdrix
Quoted 22 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

Quoted 6 lines
Quoted 3 lines

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/63c15c40/attachment.pgp>

#7 lore raw
Quoted 30 lines · Jan Kiszka
Quoted 29 lines · Gilles Chanteperdrix
Quoted 27 lines · Gilles Chanteperdrix
Quoted 24 lines · Jan Kiszka
Quoted 14 lines · Gilles Chanteperdrix
Quoted 12 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Quoted 6 lines
Quoted 2 lines

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

Signature · 1 line
                                                                Gilles.
#8 lore raw
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/0d5976b3/attachment.pgp>

#9 lore raw
Quoted 39 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

Signature · 1 line
                                                                Gilles.
#10 lore raw
Quoted 45 lines · Gilles Chanteperdrix
Quoted 43 lines · Jan Kiszka
Quoted 42 lines · Gilles Chanteperdrix
Quoted 38 lines · Jan Kiszka
Quoted 27 lines · Gilles Chanteperdrix
Quoted 25 lines · Gilles Chanteperdrix
Quoted 22 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

Quoted 6 lines
Quoted 3 lines

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root. After returning from the trap handler, the Linux kernel handles the fault as normal, including the fix-up.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/8f1152ff/attachment.pgp>

#11 lore raw
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Signature · 1 line
                                                                Gilles.
#12 lore raw
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as aliases for __put_user/__get_user, which implementation, for the ARM architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove the call to might_fault() when compiling with CONFIG_IPIPE enabled? Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Maybe.

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/20f67881/attachment.pgp>

#13 lore raw
Quoted 77 lines · Jan Kiszka
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which implementation, for the ARM
architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove
the call to might_fault() when compiling with CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Quoted 5 lines

Maybe.

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Signature · 1 line
                                                                Gilles.
#14 lore raw
Quoted 81 lines · Gilles Chanteperdrix
Quoted 79 lines · Jan Kiszka
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix
Quoted 14 lines · Jan Kiszka

On 2013-03-16 21:19, Gilles Chanteperdrix wrote:

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which implementation, for the ARM
architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove
the call to might_fault() when compiling with CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root
contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Maybe.

Quoted 7 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Right, but the general pattern for this is

	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/8e7e7936/attachment.pgp>

#15 lore raw
Quoted 90 lines · Jan Kiszka
Quoted 81 lines · Gilles Chanteperdrix
Quoted 79 lines · Jan Kiszka
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix

On 03/16/2013 09:25 PM, Jan Kiszka wrote:

On 2013-03-16 21:19, Gilles Chanteperdrix wrote:

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which implementation, for the ARM
architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove
the call to might_fault() when compiling with CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root
contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This
requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Maybe.

Quoted 7 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Quoted 7 lines

Right, but the general pattern for this is

	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()

I can do that, it avoids the issue with __ipipe_uaccess_might_fault, but:
- it is not the same guarantee, nothing tells you that obj is the one associated with the name you passed to xnregistry_fetch, it may have been destroyed and created in the mean time
- I am not sure we do not have some other places where we call copy_from/to_user with irqs off.

Signature · 1 line
                                                                Gilles.
#16 lore raw
Quoted 9 lines · Gilles Chanteperdrix
Quoted 7 lines

Right, but the general pattern for this is

	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()
Quoted 4 lines

I can do that, it avoids the issue with __ipipe_uaccess_might_fault, but:
- it is not the same guarantee, nothing tells you that obj is the one associated with the name you passed to xnregistry_fetch, it may have been destroyed and created in the mean time

See my other reply: user mistake, it should not matter.

Quoted 2 lines

- I am not sure we do not have some other places where we call copy_from/to_user with irqs off.

Might be the case, but I think so far we should have tried to avoid faulting under the nklock as we weren't considering this safe enough or at least good for the worst case latency.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/5563b2b2/attachment.pgp>

#17 lore raw
In reply to Jan Kiszka
Quoted 90 lines · Jan Kiszka
Quoted 81 lines · Gilles Chanteperdrix
Quoted 79 lines · Jan Kiszka
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Gilles Chanteperdrix

On 03/16/2013 09:25 PM, Jan Kiszka wrote:

On 2013-03-16 21:19, Gilles Chanteperdrix wrote:

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which implementation, for the ARM
architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove
the call to might_fault() when compiling with CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root
contexts as problematic.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

atomic meaning with hardware irqs off? or within an irq handler? This
requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Maybe.

Quoted 7 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Quoted 7 lines

Right, but the general pattern for this is

	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()

Seems rather to be get_nklock before xnregistry_fetch.

Signature · 1 line
                                                                Gilles.
#18 lore raw
Quoted 10 lines · Gilles Chanteperdrix
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Right, but the general pattern for this is

Quoted 9 lines
Quoted 5 lines
	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()

Seems rather to be get_nklock before xnregistry_fetch.

Only during heap and queue binding. All other native skin syscall entries fetch from the registry without holding nklock. I suppose the philosophy is that, if user space generates a race around picking up an object and destroying / recreating a different one under the same name, it will get what it deserves.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/3834e7d7/attachment.pgp>

#19 lore raw
In reply to Gilles Chanteperdrix
Quoted 94 lines · Gilles Chanteperdrix
Quoted 92 lines · Jan Kiszka
Quoted 81 lines · Gilles Chanteperdrix
Quoted 79 lines · Jan Kiszka
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 21 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka

On 2013-03-16 21:27, Gilles Chanteperdrix wrote:
On 03/16/2013 09:25 PM, Jan Kiszka wrote:

On 2013-03-16 21:19, Gilles Chanteperdrix wrote:

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which implementation, for the ARM
architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove
the call to might_fault() when compiling with CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root
contexts as problematic.

Quoted 8 lines
Quoted 5 lines

atomic meaning with hardware irqs off? or within an irq handler? This
requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Maybe.

Quoted 7 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Right, but the general pattern for this is

Quoted 10 lines
Quoted 5 lines
	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()

Seems rather to be get_nklock before xnregistry_fetch.

Yes. The other pattern is only used when the fetched handle shall be revalidated before actual use. __rt_bind_helper does not revalidate, so the handle is fetched under lock.

Signature · 1 line
Philippe.
#20 lore raw
Quoted 119 lines · Philippe Gerum
Quoted 118 lines · Gilles Chanteperdrix
Quoted 114 lines · Jan Kiszka
Quoted 97 lines · Gilles Chanteperdrix
Quoted 95 lines · Jan Kiszka
Quoted 84 lines · Gilles Chanteperdrix
Quoted 68 lines · Jan Kiszka
Quoted 59 lines · Gilles Chanteperdrix
Quoted 57 lines · Jan Kiszka
Quoted 55 lines · Gilles Chanteperdrix
Quoted 41 lines · Jan Kiszka
Quoted 40 lines · Gilles Chanteperdrix
Quoted 37 lines · Gilles Chanteperdrix

On 03/16/2013 09:28 PM, Jan Kiszka wrote:

On 2013-03-16 21:27, Gilles Chanteperdrix wrote:
On 03/16/2013 09:25 PM, Jan Kiszka wrote:

On 2013-03-16 21:19, Gilles Chanteperdrix wrote:

Hi,

xenomai asm-generic/syscall.h defines
__xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which
implementation, for the ARM
architecture calls might_fault() which triggers an
ipipe_root_only() check.

So, the question is, what is the best way of avoiding
this issue? Remove
the call to might_fault() when compiling with
CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to
might_fault() ?

Adjust might_fault() in a way that it only considers
atomic non-root
contexts as problematic.

atomic meaning with hardware irqs off? or within an irq
handler? This
requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Quoted 7 lines
Quoted 3 lines

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for
instance, which does some copies from user with hard irqs off.

Hmm, isn't that a real bug? The nucleus should then try to migrate to
Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I
will test this.

Quoted 7 lines
Quoted 1 line

In theory, user space could also provide an invalid pointer.

Yes, this is what I am going to test, but an invalid pointer cause the
fault to be fixed up so that the copy_from_user return -EFAULT.

Quoted 4 lines

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain
and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the
migration to secondary mode.

Quoted 7 lines

I am not sure the fix-up code is completely unsafe to be called over non
root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel
module.

Maybe.

Quoted 8 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while
we access its contents with the xnheap_* accessors.

Right, but the general pattern for this is

obj = xnregistry_fetch(handle)
get_nklock()
if (valid(obj))
do_something_with(obj)
put_nklock()

Quoted 7 lines
Quoted 3 lines

Seems rather to be get_nklock before xnregistry_fetch.

Yes. The other pattern is only used when the fetched handle shall be revalidated before actual use. __rt_bind_helper does not revalidate, so the handle is fetched under lock.

We would have to re-revalidate the handle then - or leave __rt_bind_helper with nklock held so that the caller can follow up with more accesses to the object before releasing the lock again.

It all boils down if we want to relax the constraints for using copy_to/from_user and allow faults inside IRQ-off sections.

Jan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20130509/b48d3aff/attachment.pgp>

#21 lore raw
In reply to Philippe Gerum
Quoted 102 lines · Philippe Gerum
Quoted 101 lines · Gilles Chanteperdrix
Quoted 97 lines · Jan Kiszka
Quoted 81 lines · Gilles Chanteperdrix
Quoted 79 lines · Jan Kiszka
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix
Quoted 29 lines · Gilles Chanteperdrix

On 03/16/2013 09:28 PM, Jan Kiszka wrote:

On 2013-03-16 21:27, Gilles Chanteperdrix wrote:
On 03/16/2013 09:25 PM, Jan Kiszka wrote:

On 2013-03-16 21:19, Gilles Chanteperdrix wrote:

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which implementation, for the ARM
architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove
the call to might_fault() when compiling with CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root
contexts as problematic.

atomic meaning with hardware irqs off? or within an irq handler? This
requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Maybe.

Quoted 7 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Right, but the general pattern for this is

	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()
Quoted 8 lines
Quoted 3 lines

Seems rather to be get_nklock before xnregistry_fetch.

Yes. The other pattern is only used when the fetched handle shall be revalidated before actual use. __rt_bind_helper does not revalidate, so the handle is fetched under lock.

I am not sure I understand what you mean, since __rt_bind_helper seems to revalidate. Is this patch what you have in mind?

ksrc/skins/native/syscall.c +14 -18
index a88a517..b9862e7 100644
@@ -2098,15 +2098,19 @@ static int __rt_queue_bind(struct pt_regs *regs)
 	int err;
 	spl_t s;
 
-	xnlock_get_irqsave(&nklock, s);
-
 	err =
 	    __rt_bind_helper(p, regs, &ph.opaque, XENO_QUEUE_MAGIC,
 			     (void **)&q, 0);
 
 	if (err)
-		goto unlock_and_exit;
+		return err;
+
+	xnlock_get_irqsave(&nklock, s);
+	if (xeno_test_magic(q, XENO_QUEUE_MAGIC) == 0) {
+		xnlock_put_irqrestore(&nklock, s);
 
+		return -EACCES;
+	}
 	ph.opaque2 = &q->bufpool;
 	ph.mapsize = xnheap_extentsize(&q->bufpool);
 	ph.area = xnheap_base_memory(&q->bufpool);
@@ -2123,12 +2127,6 @@ static int __rt_queue_bind(struct pt_regs *regs)
 		xnshadow_relax(0, 0);
 
 	return 0;
-
-      unlock_and_exit:
-
-	xnlock_put_irqrestore(&nklock, s);
-
-	return err;
 }
 
 /*
@@ -2622,15 +2620,19 @@ static int __rt_heap_bind(struct pt_regs *regs)
 	int err;
 	spl_t s;
 
-	xnlock_get_irqsave(&nklock, s);
-
 	err =
 	    __rt_bind_helper(p, regs, &ph.opaque, XENO_HEAP_MAGIC,
 			     (void **)&heap, 0);
 
 	if (err)
-		goto unlock_and_exit;
+		return err;
+
+	xnlock_get_irqsave(&nklock, s);
+	if (xeno_test_magic(heap, XENO_HEAP_MAGIC) == 0) {
+		xnlock_put_irqrestore(&nklock, s);
 
+		return -EACCES;
+	}
 	ph.opaque2 = &heap->heap_base;
 	ph.mapsize = xnheap_extentsize(&heap->heap_base);
 	ph.area = xnheap_base_memory(&heap->heap_base);
@@ -2648,12 +2650,6 @@ static int __rt_heap_bind(struct pt_regs *regs)
 		xnshadow_relax(0, 0);
 
 	return 0;
-
-      unlock_and_exit:
-
-	xnlock_put_irqrestore(&nklock, s);
-
-	return err;
 }
 
 /*
Signature · 1 line
                                                                Gilles.
#22 lore raw
Quoted 184 lines · Gilles Chanteperdrix
Quoted 102 lines · Philippe Gerum
Quoted 101 lines · Gilles Chanteperdrix
Quoted 97 lines · Jan Kiszka
Quoted 81 lines · Gilles Chanteperdrix
Quoted 79 lines · Jan Kiszka
Quoted 70 lines · Gilles Chanteperdrix
Quoted 52 lines · Jan Kiszka
Quoted 51 lines · Gilles Chanteperdrix
Quoted 47 lines · Jan Kiszka
Quoted 38 lines · Gilles Chanteperdrix
Quoted 36 lines · Jan Kiszka
Quoted 34 lines · Gilles Chanteperdrix

On 03/16/2013 09:50 PM, Gilles Chanteperdrix wrote:

On 03/16/2013 09:28 PM, Jan Kiszka wrote:

On 2013-03-16 21:27, Gilles Chanteperdrix wrote:
On 03/16/2013 09:25 PM, Jan Kiszka wrote:

On 2013-03-16 21:19, Gilles Chanteperdrix wrote:

Hi,

xenomai asm-generic/syscall.h defines __xn_put_user/__xn_get_user as
aliases for __put_user/__get_user, which implementation, for the ARM
architecture calls might_fault() which triggers an ipipe_root_only() check.

So, the question is, what is the best way of avoiding this issue? Remove
the call to might_fault() when compiling with CONFIG_IPIPE enabled?
Define __ipipe_safe_put_user which avoids the call to might_fault() ?

Adjust might_fault() in a way that it only considers atomic non-root
contexts as problematic.

atomic meaning with hardware irqs off? or within an irq handler? This
requires xenomai...

Hard irqs off || head domain stalled, I would say.

ok, done, thanks.

Actually, it does not work, the WARN_ON triggers in __rt_heap_bind for
instance, which does some copies from user with hard irqs off.

Quoted 6 lines
Quoted 2 lines

Hmm, isn't that a real bug? The nucleus should then try to migrate to Linux while the nklock is held - ouch...

In theory, the fault should be fixed up without requiring a migration. I will test this.

In theory, user space could also provide an invalid pointer.

Quoted 7 lines
Quoted 3 lines

Yes, this is what I am going to test, but an invalid pointer cause the fault to be fixed up so that the copy_from_user return -EFAULT.

I don't think we fix-up over the head domain. Rather, the nucleus receives the information that a fault has happened over the head domain and migrates to root.

It depends on where the notification happens in the I-pipe patch. You could allow the kernel to fix-up the fault without notifying the nucleus. I thought I did that in the past, but apparently not in the latest patches as a segfault in rt_heap_bind causes a switch to secondary mode, but the nklock is automatically released as part of the migration to secondary mode.

Quoted 5 lines

I am not sure the fix-up code is completely unsafe to be called over non root domain, as it only uses preempt_disable, preempt_enable, list_for_each_entry_rcu and only if the exception is found in a kernel module.

Maybe.

Quoted 7 lines
Quoted 3 lines

But why do we need to hold the nklock across the __rt_bind_helper in __rt_queue_bind at all? Other binding services apparently don't have this need.

I suppose because we do not want the heap vanishing under our feet while we access its contents with the xnheap_* accessors.

Right, but the general pattern for this is

	obj = xnregistry_fetch(handle)
	get_nklock()
	if (valid(obj))
		do_something_with(obj)
	put_nklock()
Quoted 8 lines
Quoted 3 lines

Seems rather to be get_nklock before xnregistry_fetch.

Yes. The other pattern is only used when the fetched handle shall be revalidated before actual use. __rt_bind_helper does not revalidate, so the handle is fetched under lock.

I am not sure I understand what you mean, since __rt_bind_helper seems to revalidate. Is this patch what you have in mind?

ksrc/skins/native/syscall.c +14 -12
index a88a517..b9862e7 100644
@@ -2098,15 +2098,19 @@ static int __rt_queue_bind(struct pt_regs *regs)
 	int err;
 	spl_t s;
 
-	xnlock_get_irqsave(&nklock, s);
-
 	err =
 	    __rt_bind_helper(p, regs, &ph.opaque, XENO_QUEUE_MAGIC,
 			     (void **)&q, 0);
 
 	if (err)
-		goto unlock_and_exit;
+		return err;
+
+	xnlock_get_irqsave(&nklock, s);
+	if (xeno_test_magic(q, XENO_QUEUE_MAGIC) == 0) {
+		xnlock_put_irqrestore(&nklock, s);
 
+		return -EACCES;
+	}
 	ph.opaque2 = &q->bufpool;
 	ph.mapsize = xnheap_extentsize(&q->bufpool);
 	ph.area = xnheap_base_memory(&q->bufpool);
@@ -2123,12 +2127,6 @@ static int __rt_queue_bind(struct pt_regs *regs)
 		xnshadow_relax(0, 0);
 
 	return 0;
-
-      unlock_and_exit:
-
-	xnlock_put_irqrestore(&nklock, s);
-
-	return err;
 }
 
 /*
@@ -2622,15 +2620,19 @@ static int __rt_heap_bind(struct pt_regs *regs)
 	int err;
 	spl_t s;
 
-	xnlock_get_irqsave(&nklock, s);
-
 	err =
 	    __rt_bind_helper(p, regs, &ph.opaque, XENO_HEAP_MAGIC,
 			     (void **)&heap, 0);
 
 	if (err)
-		goto unlock_and_exit;
+		return err;
+
+	xnlock_get_irqsave(&nklock, s);
+	if (xeno_test_magic(heap, XENO_HEAP_MAGIC) == 0) {
+		xnlock_put_irqrestore(&nklock, s);
 
+		return -EACCES;
+	}
 	ph.opaque2 = &heap->heap_base;
 	ph.mapsize = xnheap_extentsize(&heap->heap_base);
 	ph.area = xnheap_base_memory(&heap->heap_base);
@@ -2648,12 +2650,6 @@ static int __rt_heap_bind(struct pt_regs *regs)
 		xnshadow_relax(0, 0);
 

ksrc/skins/native/syscall.c static int __rt_heap_bind(struct pt_regs *regs) · line 2655

Quoted 10 lines
ksrc/skins/native/syscall.c +0 -6
 	return 0;
-
-      unlock_and_exit:
-
-	xnlock_put_irqrestore(&nklock, s);
-
-	return err;
 }
 
 /*

That or returning from __rt_bind_helper with nklock still held. In that case you don't need to revalidate at the caller site as you don't drop the lock before using the object in __rt_queue/heap_bind. But as these are not hotpaths, the above approach is probably cleaner.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux