<div dir="ltr">Hi Felix, thanks for your patience.<div><br><div class="gmail_quote"><div dir="ltr">On Tue, Feb 16, 2016 at 12:00 PM Felix Fietkau <<a href="mailto:nbd@openwrt.org">nbd@openwrt.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2016-02-16 10:06, Eyal Birger wrote:<br>
> Hi Felix,<br>
><br>
> Thanks again for your responses.<br>
><br>
> On Mon, Feb 15, 2016 at 2:14 PM Felix Fietkau <<a href="mailto:nbd@openwrt.org" target="_blank">nbd@openwrt.org</a><br>
> <mailto:<a href="mailto:nbd@openwrt.org" target="_blank">nbd@openwrt.org</a>>> wrote:<br>
><br>
> On 2016-02-15 12:54, Eyal Birger wrote:<br>
> > > > if (offset < sizeof(ub->hdr)) {<br>
> > > > - iov[0].iov_base = ((char *) &ub->hdr) +<br>
> offset;<br>
> > > > - iov[0].iov_len = sizeof(ub->hdr) - offset;<br>
> > > > + struct ubus_msghdr hdr;<br>
> > > > +<br>
> > > > + hdr.version = ub->hdr.version;<br>
> > > > + hdr.type = ub->hdr.type;<br>
> > > > + hdr.seq = cpu_to_be16(ub->hdr.seq);<br>
> > > > + hdr.peer = cpu_to_be32(ub->hdr.peer);<br>
> > > > +<br>
> > > > + iov[0].iov_base = ((char *) &hdr) + offset;<br>
> > > > + iov[0].iov_len = sizeof(hdr) - offset;<br>
> > The corner case is this: You changed the iov to point at stack<br>
> space<br>
> > instead of ub->hdr. If the code receives a part of the header<br>
> in one<br>
> > call, and another one in the next (offset > 0), the contents<br>
> of hdr will<br>
> > be corrupt, as it will be a mix of uninitialized stack space + the<br>
> > received data from the last call.<br>
> > Interesting... I initialize the iov_base every time to a newly<br>
> created and<br>
> > calculated hdr variable before the sendmsg() call, and iov is<br>
> never used<br>
> > otherwise - so I wonder how it could be reused in subsequent calls?<br>
> Before your change, iov[0].iov_base points at ub->hdr, which is on heap<br>
> and is preserved across calls.<br>
> After your change, iov[0].iov_base points at the on-stack struct hdr,<br>
> which is not preserved across calls.<br>
><br>
><br>
> The thing is, I don't see why the area pointed to in iov_base is<br>
> required to be preserved between calls - the sendmsg() call never uses<br>
> the cached values in iov. They are always re-armed with new pointers.<br>
<br>
You're still confused, so please forget about iov for a second and only<br>
focus on struct ubus_msghdr hdr. Think about what happens if the first<br>
call only receives a few bytes of it, and the next call fills in the rest.<br></blockquote><div><br></div><div>Ok, I'm fine with the premise that I'm confused :)</div><div><br></div><div>so I'll try to explain my confusion:</div><div><br></div><div>On the first call to ubus_msg_writev(offset = 0):</div><div> offset is less than sizeof(ub->hdr)</div><div> hdr is created on the stack, passed to sendmsg()</div><div> sendmsg() returns. No more references to hdr exist anywhere - </div><div> hdr contents is copied to the kernel socket for consumption.</div><div> but sendmsg() did not write all of the hdr (e.g. only 3 bytes).</div><div> ubus_msg_writev() returns with return value '3'.</div><div><br></div><div>Second call to ubus_msg_writev(offset = 3):</div><div> offset is still less than sizeof(ub->hdr)</div><div> hdr is recreated on the stack, it's a different hdr now that is sent</div><div> to sendmsg(), but contains the remaining bytes to be sent.</div><div><br></div><div>That's the point I don't understand - the old pointer to hdr is never used</div><div>again. So the only way this would create problems is if somehow the hdr</div><div>pointer is retained after the sendmsg() call exits. But calling sendmsg() with iov's</div><div>pointing to stack area is legitimate as far as I can tell.</div><div><br></div><div>Eyal.</div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
- Felix<br>
</blockquote></div></div></div>