All of lore.kernel.org
 help / color / mirror / Atom feed
From: Denis Kenzior <denkenz@gmail.com>
To: ell@lists.01.org
Subject: Re: [PATCH 3/5] dbus: Add and validate the UNIX_FDS msg header field
Date: Mon, 02 May 2016 10:10:24 -0500	[thread overview]
Message-ID: <57276DE0.5070902@gmail.com> (raw)
In-Reply-To: <CAOq732LynqbRX=D4Vxwh2u2aAByi4UgBOxNRqjnyn8UrV8pSBQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 4596 bytes --]

Hi Andrew,

On 04/30/2016 09:58 AM, Andrzej Zaborowski wrote:
> On 30 April 2016 at 03:15, Denis Kenzior <denkenz@gmail.com> wrote:
>> On 04/29/2016 06:52 PM, Andrzej Zaborowski wrote:
>>> On 30 April 2016 at 01:17, Denis Kenzior <denkenz@gmail.com> wrote:
>>>>
>>>> On 04/29/2016 05:44 PM, Andrew Zaborowski wrote:
>>>>>
>>>>> @@ -764,11 +764,22 @@ struct l_dbus_message
>>>>> *dbus_message_from_blob(const
>>>>> void *data, size_t size,
>>>>>                   get_header_field(message, DBUS_MESSAGE_FIELD_SIGNATURE,
>>>>>                                           'g', &message->signature);
>>>>>
>>>>> -       if (num_fds > L_ARRAY_SIZE(message->fds)) {
>>>>> -               for (i = L_ARRAY_SIZE(message->fds); i < num_fds; i++)
>>>>> -                       close(fds[i]);
>>>>> +       if (num_fds) {
>>>>> +               uint32_t unix_fds;
>>>>>
>>>>> -               num_fds = L_ARRAY_SIZE(message->fds);
>>>>> +               if (!get_header_field(message,
>>>>> DBUS_MESSAGE_FIELD_UNIX_FDS,
>>>>> +                                       'u', &unix_fds))
>>>>> +                       goto free;
>>>>> +
>>>>> +               if (num_fds > unix_fds)
>>>>> +                       num_fds = unix_fds;
>>>>
>>>>
>>>>
>>>> If num_fds > unix_fds, should all unused fds (e.g. fds[unix_fds ..
>>>> num_fds-1] be closed just in case as well?
>>>
>>>
>>> Ok, let's do that.
>>>
>>>>
>>>> Also, what if unix_fds > num_fds?
>>>
>>>
>>> Well, not sure if there's anything we can do.  As far as I've tried to
>>> understand the sendmsg/recvmsg semantics the message boundaries are
>>> not preserved and I understand FDs may also be delivered in a recvmsg
>>> call different than that in which the message is received when there
>>> are multiple messages in the queue.  The solution would be to keep
>>> buffers of header/body data another buffer with FDs received and pair
>>> them based on the UNIX_FDS headers but perhaps we don't want to
>>> overcomplicate it.  For simplicity I take the minimum of num_fds,
>>> unix_fds and L_ARRAY_SIZE(message->fds).
>>>
>>
>> Isn't this a transport detail?  This function in particular is taking the
>> full dbus-message blob.  Same with dbus_message_build().
>
> Yeah, we should possibly handle that in dbus.c but we'd need to use
> get_header_field etc., and it would get complicated.
>

I'm not following.  So we have dbus_message_from_blob, which takes 
headers, body and the fds array.  If the number of fds specified in the 
header (e.g. unix_fds) doesn't match what the fds[] size is, then we 
should take some action.  This is separate from the transport details 
inside dbus.c.

>>
>> We probably do have a separate problem inside classic_recv_message(). Since
>> the UNIX socket is a stream, the message boundaries are not preserved, as
>> you point out.
>>
>> One way might be to use MSG_WAITALL flag.  Alternatively, allocate a buffer
>> and keep reading into it until enough data is read.
>
> Do you mean in case the read is interrupted by a signal?  I think
> that's easy to handle but I'm not sure it can actually happen if the
> dbus daemon always writes complete messages.

Regardless of whether dbus-daemon writes complete messages, the kernel 
is free to return however many bytes to the process calling recvfrom, 
even if more bytes are actually available in the receive buffer. 
MSG_WAITALL tells the kernel to give us all of the data the recvfrom 
caller is requesting, unless interrupted by a signal.

Without MSG_WAITALL we might start seeing partial reads on a heavily 
loaded system and things will start breaking.

The best way would be to peek at the next header, allocate space for the 
message and keep calling recvfrom until all of the message has been 
read.  Normally this would take 1 recvfrom call, but can take as many as 
needed.  Only once the entire message has been read, we can send it in 
for processing by dbus_message_from_blob or dbus_message_build.  We can 
put this more-complicated scheme on the TODO list for now...

>
>>
>> This reminds me, since we utilize MSG_CMSG_CLOEXEC flag in recvmsg.  Do we
>> still need a separate loop to set the O_CLOEXEC flag?  I suppose its safe to
>> keep it just in case we're running on Linux kernel less than 2.6.23.
>>
>> Is a similar flag implied over kdbus?
>
> Yeah, I'd also noticed we probably didn't need this loop.  But I don't
> think kdbus supports this, there's nothing in the kernel code to set
> this flag.
>

Okay, lets leave it.

Regards,
-Denis

  reply	other threads:[~2016-05-02 15:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-29 22:43 [PATCH 1/5] dbus: Handle the 'h' type in append_arguments Andrew Zaborowski
2016-04-29 22:43 ` [PATCH 2/5] dbus: Remove memcpy and fix setting of FD_CLOEXEC on FDs Andrew Zaborowski
2016-04-29 23:20   ` Denis Kenzior
2016-04-29 22:44 ` [PATCH 3/5] dbus: Add and validate the UNIX_FDS msg header field Andrew Zaborowski
2016-04-29 23:17   ` Denis Kenzior
2016-04-29 23:52     ` Andrzej Zaborowski
2016-04-30  1:15       ` Denis Kenzior
2016-04-30 14:58         ` Andrzej Zaborowski
2016-05-02 15:10           ` Denis Kenzior [this message]
2016-05-02 22:25             ` Andrzej Zaborowski
2016-05-03  3:17               ` Denis Kenzior
2016-04-29 22:44 ` [PATCH 4/5] unit: Add UNIX_FDS header fields in FD test messages Andrew Zaborowski
2016-04-29 23:20   ` Denis Kenzior
2016-04-29 22:44 ` [PATCH 5/5] unit: End to end FD passing test Andrew Zaborowski
2016-04-29 23:21   ` Denis Kenzior
2016-04-29 23:20 ` [PATCH 1/5] dbus: Handle the 'h' type in append_arguments Denis Kenzior

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=57276DE0.5070902@gmail.com \
    --to=denkenz@gmail.com \
    --cc=ell@lists.01.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.