All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Beulich <jbeulich@suse.com>
To: Demi Marie Obenour <demiobenour@gmail.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
	Xen developer discussion <xen-devel@lists.xenproject.org>
Subject: Re: Undefined behavior in libxenvchan
Date: Mon, 15 Dec 2025 10:41:54 +0100	[thread overview]
Message-ID: <7beb1c09-dcbf-4cc8-ae90-cd9151ca97e1@suse.com> (raw)
In-Reply-To: <bdf22b1a-49b5-43d4-8dfc-178c0076c917@gmail.com>

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.



  reply	other threads:[~2025-12-15  9:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2025-12-15 11:10     ` Teddy Astie
2025-12-15 18:17       ` Demi Marie Obenour

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7beb1c09-dcbf-4cc8-ae90-cd9151ca97e1@suse.com \
    --to=jbeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=demiobenour@gmail.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.