From: Eric Blake <eblake@redhat.com>
To: Corey Bryant <coreyb@linux.vnet.ibm.com>
Cc: kwolf@redhat.com, aliguori@us.ibm.com,
stefanha@linux.vnet.ibm.com, libvir-list@redhat.com,
qemu-devel@nongnu.org, lcapitulino@redhat.com,
pbonzini@redhat.com
Subject: Re: [Qemu-devel] [PATCH v3 3/5] osdep: Enable qemu_open to dup pre-opened fd
Date: Fri, 15 Jun 2012 12:42:32 -0600 [thread overview]
Message-ID: <4FDB8218.1010804@redhat.com> (raw)
In-Reply-To: <4FDB7C1B.8020802@linux.vnet.ibm.com>
[-- Attachment #1: Type: text/plain, Size: 4466 bytes --]
On 06/15/2012 12:16 PM, Corey Bryant wrote:
>> I think you need to honor flags so that the end use of the fd will be as
>> if qemu had directly opened the file, rather than just doing a blind dup
>> with a resulting fd that is in a different state than the caller
>> expected. I can think of at least the following cases (there may be
>> more):
>
> I was thinking libvirt would handle all the flag settings on open
> (obviously since that's how I coded it). I think you're right with this
> approach though as QEMU will re-open the same file various times with
> different flags.
How will libvirt know whether qemu wanted to open a file with O_TRUNCATE
vs. reusing an entire file, or with O_NONBLOCK or not, and so forth? I
think there are just too many qmeu_open() calls with different flags to
expect libvirt to know how to pre-open all possible fds in such a manner
that /dev/fd/nnn will be a valid replacement for what qemu would have
done, in the cases where qemu can instead correct flags itself.
>
> There are some flags that I don't think we'll be able to change. For
> example: O_RDONLY, O_WRONLY, O_RDWR. I assume libvirt would open all
> files O_RDWR.
I agree that this can't be changed, so this is one case where libvirt
will have to know what the file will used for. But it is also a case
where qemu can at least check whether the fd passed in has sufficient
permissions (fcntl(F_GETFL)) for what qemu wanted to use, and should
error out here rather than silently succeed here then have a weird EBADF
failure down the road when the dup'd fd is finally used. You are right
that libvirt should always be safe in passing in an O_RDWR fd for
whatever qemu wants, although that may represent security holes (there's
reasons for O_RDONLY); and in cases where libvirt is instead passing in
one end of a pipe, the fd will necessarily be O_RDONLY or O_WRONLY
(since you can't have an O_RDWR pipe).
>> Oh, and are we using MSG_CMSG_CLOEXEC where possible (and where not
>> possible, falling back to fcntl(F_GETFD/F_SETFD) to set FD_CLOEXEC) on
>> all fds received by 'getfd' and 'pass-fd'? I can't think of any reason
>> why 'migrate fd:name' would need to be inheritable, and in the case of
>> /dev/fd/ parsing, while the dup() result may need to be inheritable, the
>> original that we are dup'ing from should most certainly be cloexec.
>
> It doesn't look like we use MSG_CMSG_CLOEXEC anywhere in QEMU. I don't
> think we can modify getfd at this point (compatibility) but we could
> update pass-fd to set it. It may make more sense to set it with
> fcntl(FD_CLOEXEC) in qmp_pass_fd().
That's not atomic. If we don't care about atomicity (for example, if we
can guarantee that no other thread in qemu can possibly fork/exec in
between the monitor thread receiving an fd then converting it to
cloexec, based on how we hold a mutex), then that's fine. OR, if we
make sure _all_ fork() calls sanitize themselves, by closing all fds in
the getfd/pass-fd list prior to calling exec(), then we don't have to
even worry about cloexec (but then you have to worry about locking the
getfd name list, since locking a list to iterate it is not an async-safe
action and probably can't be done between fork() and exec()).
Otherwise, using MSG_CMSG_CLOEXEC is the only safe way to avoid leaking
unintended fds into another thread's child process.
>
>>
>> if (flags & O_NONBLOCK)
>> use fcntl(F_GETFL/F_SETFL) to set O_NONBLOCK
>> else
>> use fcntl(F_GETFL/F_SETFL) to clear O_NONBLOCK
>>
>> or maybe we document that callers of pass-fd must always pass fds with
>> O_NONBLOCK clear instead of clearing it ourselves. Or maybe we make
>> sure part of the process of tying name with fd in the lookup list of
>> named fds is determining the current O_NONBLOCK state in case future
>> qemu_open() need it in the opposite state.
>
> Just documenting it seems error-prone. Why not just set/clear it based
> on the flag passed to qemu_open?
Yep, back to my argument that making libvirt have to know every context
that qemu will want to open, and what flags it would be using in those
contexts, is painful compared to having qemu just do the right thing in
the first place, or gracefully erroring out right away at the point of
the qemu_open(/dev/fd/nnn).
--
Eric Blake eblake@redhat.com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 620 bytes --]
next prev parent reply other threads:[~2012-06-15 18:42 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-14 15:55 [Qemu-devel] [PATCH v3 0/5] file descriptor passing using pass-fd Corey Bryant
2012-06-14 15:55 ` [Qemu-devel] [PATCH v3 1/5] qapi: Convert getfd and closefd Corey Bryant
2012-06-14 15:55 ` [Qemu-devel] [PATCH v3 2/5] qapi: Add pass-fd QMP command Corey Bryant
2012-06-15 14:32 ` Luiz Capitulino
2012-06-15 15:04 ` Corey Bryant
2012-06-15 15:14 ` Luiz Capitulino
2012-06-15 15:29 ` Corey Bryant
2012-06-15 16:26 ` Luiz Capitulino
2012-06-14 15:55 ` [Qemu-devel] [PATCH v3 3/5] osdep: Enable qemu_open to dup pre-opened fd Corey Bryant
2012-06-15 15:16 ` Eric Blake
2012-06-15 18:16 ` Corey Bryant
2012-06-15 18:42 ` Eric Blake [this message]
2012-06-15 19:02 ` Corey Bryant
2012-06-15 18:46 ` Kevin Wolf
2012-06-15 19:19 ` Corey Bryant
2012-06-15 20:00 ` Eric Blake
2012-06-15 20:49 ` Corey Bryant
2012-06-18 8:10 ` Kevin Wolf
2012-06-19 13:59 ` Corey Bryant
2012-06-14 15:55 ` [Qemu-devel] [PATCH v3 4/5] block: Convert open calls to qemu_open Corey Bryant
2012-06-15 14:36 ` Luiz Capitulino
2012-06-15 15:10 ` Corey Bryant
2012-06-15 15:21 ` Eric Blake
2012-06-15 18:32 ` Corey Bryant
2012-06-14 15:55 ` [Qemu-devel] [PATCH v3 5/5] block: Prevent /dev/fd/X filename from being detected as floppy Corey Bryant
2012-06-15 14:38 ` Luiz Capitulino
2012-06-15 15:12 ` Corey Bryant
2012-06-19 15:46 ` [Qemu-devel] [PATCH v3 0/5] file descriptor passing using pass-fd Eric Blake
2012-06-19 15:57 ` Kevin Wolf
2012-06-19 16:14 ` Eric Blake
2012-06-20 7:25 ` Kevin Wolf
2012-06-20 8:31 ` Daniel P. Berrange
2012-06-20 11:24 ` Eric Blake
2012-06-20 13:31 ` Corey Bryant
2012-06-20 14:53 ` Eric Blake
2012-06-20 16:24 ` Corey Bryant
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=4FDB8218.1010804@redhat.com \
--to=eblake@redhat.com \
--cc=aliguori@us.ibm.com \
--cc=coreyb@linux.vnet.ibm.com \
--cc=kwolf@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=libvir-list@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@linux.vnet.ibm.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).