All of lore.kernel.org
 help / color / mirror / Atom feed
* Undefined behavior in libxenvchan
@ 2025-12-14 19:09 Demi Marie Obenour
  2025-12-14 22:50 ` Andrew Cooper
  0 siblings, 1 reply; 6+ messages in thread
From: Demi Marie Obenour @ 2025-12-14 19:09 UTC (permalink / raw)
  To: Xen developer discussion


[-- Attachment #1.1.1: Type: text/plain, Size: 441 bytes --]

I noticed that libxenvchan has undefined behavior: it passes pointers
to guest memory to memcpy() even though they can be concurrently
changed.

Would it make sense to reuse some of Xen's copy_from_guest() code
instead?  There might be a licensing problem (GPL vs LGPL), though.
I think the only approach that isn't UB and has decent performance
is to do the whole copy in assembly.
-- 
Sincerely,
Demi Marie Obenour (she/her/hers)

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 7253 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Undefined behavior in libxenvchan
  2025-12-14 19:09 Undefined behavior in libxenvchan Demi Marie Obenour
@ 2025-12-14 22:50 ` Andrew Cooper
  2025-12-14 23:08   ` Demi Marie Obenour
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Cooper @ 2025-12-14 22:50 UTC (permalink / raw)
  To: Demi Marie Obenour, Xen developer discussion; +Cc: Andrew Cooper

On 14/12/2025 7:09 pm, Demi Marie Obenour wrote:
> I noticed that libxenvchan has undefined behavior: it passes pointers
> to guest memory to memcpy() even though they can be concurrently
> changed.
>
> Would it make sense to reuse some of Xen's copy_from_guest() code
> instead?  There might be a licensing problem (GPL vs LGPL), though.
> I think the only approach that isn't UB and has decent performance
> is to do the whole copy in assembly.

memcpy() is well defined.

The problem is the potential for creating TOCTOU races if suitable
barriers aren't used, due to the compiler being able to optimise through
memcpy().

Xen's copy to/from guest are not appropriate in userspace.  They're
guarding against pagefaults and address ranges not belonging to the
target context.

If more compiler/smp barriers are needed, then that's the appropriate fix.

~Andrew


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Undefined behavior in libxenvchan
  2025-12-14 22:50 ` Andrew Cooper
@ 2025-12-14 23:08   ` Demi Marie Obenour
  2025-12-15  9:41     ` Jan Beulich
  2025-12-15 11:10     ` Teddy Astie
  0 siblings, 2 replies; 6+ messages in thread
From: Demi Marie Obenour @ 2025-12-14 23:08 UTC (permalink / raw)
  To: Andrew Cooper, Xen developer discussion


[-- Attachment #1.1.1: Type: text/plain, Size: 1326 bytes --]

On 12/14/25 17:50, Andrew Cooper wrote:
> On 14/12/2025 7:09 pm, Demi Marie Obenour wrote:
>> I noticed that libxenvchan has undefined behavior: it passes pointers
>> to guest memory to memcpy() even though they can be concurrently
>> changed.
>>
>> Would it make sense to reuse some of Xen's copy_from_guest() code
>> instead?  There might be a licensing problem (GPL vs LGPL), though.
>> I think the only approach that isn't UB and has decent performance
>> is to do the whole copy in assembly.
> 
> memcpy() is well defined.

Rich Felker wrote otherwise on the musl mailing list.  Specifically,
it is undefined behavior if the data is changed while memcpy() is
accessing it, either for reading or for writing.

> The problem is the potential for creating TOCTOU races if suitable
> barriers aren't used, due to the compiler being able to optimise through
> memcpy().

The concern here is about races in memcpy() itself.

> Xen's copy to/from guest are not appropriate in userspace.  They're
> guarding against pagefaults and address ranges not belonging to the
> target context.
> 
> If more compiler/smp barriers are needed, then that's the appropriate fix.

Rich Felker suggested to use an open-coded memcpy() that used volatile
accesses.
-- 
Sincerely,
Demi Marie Obenour (she/her/hers)

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 7253 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Undefined behavior in libxenvchan
  2025-12-14 23:08   ` Demi Marie Obenour
@ 2025-12-15  9:41     ` Jan Beulich
  2025-12-15 11:10     ` Teddy Astie
  1 sibling, 0 replies; 6+ messages in thread
From: Jan Beulich @ 2025-12-15  9:41 UTC (permalink / raw)
  To: Demi Marie Obenour; +Cc: Andrew Cooper, Xen developer discussion

On 15.12.2025 00:08, Demi Marie Obenour wrote:
> On 12/14/25 17:50, Andrew Cooper wrote:
>> On 14/12/2025 7:09 pm, Demi Marie Obenour wrote:
>>> I noticed that libxenvchan has undefined behavior: it passes pointers
>>> to guest memory to memcpy() even though they can be concurrently
>>> changed.
>>>
>>> Would it make sense to reuse some of Xen's copy_from_guest() code
>>> instead?  There might be a licensing problem (GPL vs LGPL), though.
>>> I think the only approach that isn't UB and has decent performance
>>> is to do the whole copy in assembly.
>>
>> memcpy() is well defined.
> 
> Rich Felker wrote otherwise on the musl mailing list.  Specifically,
> it is undefined behavior if the data is changed while memcpy() is
> accessing it, either for reading or for writing.

Aren't you talking about undefined-ness beyond what the C spec uses the
term for? Of course it is always unpredictable what the result of a
function will be when you fiddle with its source behind its back. But
that's not of concern as far as safety is concerned (while the
correctness issue that results is solely a problem for the party doing
the undue modifications).

What we need to guarantee is that whatever copy is made of whatever
in-flight data, any sanity and consistency checking as well as subsequent
use would take only the one, exact same copy of source data. Which, as
Andrew said, may require some extra barriers, while using memcpy() for
the mechanical copying ought to be okay.

Jan

>> The problem is the potential for creating TOCTOU races if suitable
>> barriers aren't used, due to the compiler being able to optimise through
>> memcpy().
> 
> The concern here is about races in memcpy() itself.
> 
>> Xen's copy to/from guest are not appropriate in userspace.  They're
>> guarding against pagefaults and address ranges not belonging to the
>> target context.
>>
>> If more compiler/smp barriers are needed, then that's the appropriate fix.
> 
> Rich Felker suggested to use an open-coded memcpy() that used volatile
> accesses.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Undefined behavior in libxenvchan
  2025-12-14 23:08   ` Demi Marie Obenour
  2025-12-15  9:41     ` Jan Beulich
@ 2025-12-15 11:10     ` Teddy Astie
  2025-12-15 18:17       ` Demi Marie Obenour
  1 sibling, 1 reply; 6+ messages in thread
From: Teddy Astie @ 2025-12-15 11:10 UTC (permalink / raw)
  To: Demi Marie Obenour, Andrew Cooper, Xen developer discussion

Le 15/12/2025 à 00:08, Demi Marie Obenour a écrit :
> On 12/14/25 17:50, Andrew Cooper wrote:
>> On 14/12/2025 7:09 pm, Demi Marie Obenour wrote:
>>> I noticed that libxenvchan has undefined behavior: it passes pointers
>>> to guest memory to memcpy() even though they can be concurrently
>>> changed.
>>>
>>> Would it make sense to reuse some of Xen's copy_from_guest() code
>>> instead?  There might be a licensing problem (GPL vs LGPL), though.
>>> I think the only approach that isn't UB and has decent performance
>>> is to do the whole copy in assembly.
>>
>> memcpy() is well defined.
>
> Rich Felker wrote otherwise on the musl mailing list.  Specifically,
> it is undefined behavior if the data is changed while memcpy() is
> accessing it, either for reading or for writing.
>
>> The problem is the potential for creating TOCTOU races if suitable
>> barriers aren't used, due to the compiler being able to optimise through
>> memcpy().
>
> The concern here is about races in memcpy() itself.
>
>> Xen's copy to/from guest are not appropriate in userspace.  They're
>> guarding against pagefaults and address ranges not belonging to the
>> target context.
>>
>> If more compiler/smp barriers are needed, then that's the appropriate fix.
>
> Rich Felker suggested to use an open-coded memcpy() that used volatile
> accesses.

Do you mean that if a libc uses something like this as a memcpy.

void *memcpy(
     void *restrict dest_str,
     const void *restrict src_str,
     size_t n)
{
     const char *src = src_str;
     char *dest = dest_str;
     size_t i = 0;

     while (i < n)
     {
         dest[i] = src[i];
         i++;
     }

     return dest_str;
}

that the compiler is free to optimize inside this function in ways that
conflict with the "actual volatile-ness" of dest/src ?

Anything said regarding regarding TOCTOU can also happens from within
the memcpy (even though most memcpy() implementations and what compiler
would emit here is very unlikely unaffected by this).

Unfortunately, there is no available volatile memcpy in C, LLVM has a
volatile memcpy, but not usable from C, Rust exposes it through
"unstable" volatile_copy_non_overlapping, which got discussed in [1],
there is also something regarding "atomic memcpy" [2], but I don't know
the exact status of all this.

[1]
https://doc.rust-lang.org/stable/core/intrinsics/fn.volatile_copy_nonoverlapping_memory.html

[2] https://github.com/rust-lang/rfcs/pull/3301

Teddy


--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Undefined behavior in libxenvchan
  2025-12-15 11:10     ` Teddy Astie
@ 2025-12-15 18:17       ` Demi Marie Obenour
  0 siblings, 0 replies; 6+ messages in thread
From: Demi Marie Obenour @ 2025-12-15 18:17 UTC (permalink / raw)
  To: Teddy Astie, Andrew Cooper, Xen developer discussion


[-- Attachment #1.1.1: Type: text/plain, Size: 2845 bytes --]

On 12/15/25 06:10, Teddy Astie wrote:
> Le 15/12/2025 à 00:08, Demi Marie Obenour a écrit :
>> On 12/14/25 17:50, Andrew Cooper wrote:
>>> On 14/12/2025 7:09 pm, Demi Marie Obenour wrote:
>>>> I noticed that libxenvchan has undefined behavior: it passes pointers
>>>> to guest memory to memcpy() even though they can be concurrently
>>>> changed.
>>>>
>>>> Would it make sense to reuse some of Xen's copy_from_guest() code
>>>> instead?  There might be a licensing problem (GPL vs LGPL), though.
>>>> I think the only approach that isn't UB and has decent performance
>>>> is to do the whole copy in assembly.
>>>
>>> memcpy() is well defined.
>>
>> Rich Felker wrote otherwise on the musl mailing list.  Specifically,
>> it is undefined behavior if the data is changed while memcpy() is
>> accessing it, either for reading or for writing.
>>
>>> The problem is the potential for creating TOCTOU races if suitable
>>> barriers aren't used, due to the compiler being able to optimise through
>>> memcpy().
>>
>> The concern here is about races in memcpy() itself.
>>
>>> Xen's copy to/from guest are not appropriate in userspace.  They're
>>> guarding against pagefaults and address ranges not belonging to the
>>> target context.
>>>
>>> If more compiler/smp barriers are needed, then that's the appropriate fix.
>>
>> Rich Felker suggested to use an open-coded memcpy() that used volatile
>> accesses.
> 
> Do you mean that if a libc uses something like this as a memcpy.
> 
> void *memcpy(
>      void *restrict dest_str,
>      const void *restrict src_str,
>      size_t n)
> {
>      const char *src = src_str;
>      char *dest = dest_str;
>      size_t i = 0;
> 
>      while (i < n)
>      {
>          dest[i] = src[i];
>          i++;
>      }
> 
>      return dest_str;
> }
> 
> that the compiler is free to optimize inside this function in ways that 
> conflict with the "actual volatile-ness" of dest/src ?

Correct.

> Anything said regarding regarding TOCTOU can also happens from within 
> the memcpy (even though most memcpy() implementations and what compiler 
> would emit here is very unlikely unaffected by this).
> 
> Unfortunately, there is no available volatile memcpy in C, LLVM has a 
> volatile memcpy, but not usable from C, Rust exposes it through 
> "unstable" volatile_copy_non_overlapping, which got discussed in [1], 
> there is also something regarding "atomic memcpy" [2], but I don't know 
> the exact status of all this.
> 
> [1] 
> https://doc.rust-lang.org/stable/core/intrinsics/fn.volatile_copy_nonoverlapping_memory.html
> 
> [2] https://github.com/rust-lang/rfcs/pull/3301
> 
> Teddy

The C standard expects one to write an open-coded loop and to
not use libc memcpy().
-- 
Sincerely,
Demi Marie Obenour (she/her/hers)

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 7253 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-12-15 18:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-14 19:09 Undefined behavior in libxenvchan Demi Marie Obenour
2025-12-14 22:50 ` Andrew Cooper
2025-12-14 23:08   ` Demi Marie Obenour
2025-12-15  9:41     ` Jan Beulich
2025-12-15 11:10     ` Teddy Astie
2025-12-15 18:17       ` Demi Marie Obenour

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.