* [RFC] endianness bugs in net/batman-adv/
@ 2012-04-13 19:46 Al Viro
[not found] ` <20120413194629.GP6589-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
0 siblings, 1 reply; 3+ messages in thread
From: Al Viro @ 2012-04-13 19:46 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: netdev
Let's start with net/batman-adv/translation-table.c:send_tt_request().
Its tt_crc argument gets stored into skb as-is and skb goes on the wire.
OK, so it's fixed-endian, right?
That sucker comes straight from the (only) caller - tt_update_orig().
There it gets compared with ->tt_crc of struct orig_node instances. Fine,
except that just prior to that comparison we assign to ->tt_crc the return
value of tt_global_crc(). Which is built on crc16_byte() and clearly
returns a host-endian value. Additionally, orig_node ->tt_crc is getting
compared to tt_request->tt_data in send_other_tt_response(), which ultimately
comes from recv_tt_query() where it's flipped from net-endian to host-endian.
It gets even funnier - we have 3 structures with ->tt_crc in them;
one is struct orig_node (see above), another is struct batman_ogv_packet and
then there's the weirdest one - struct bat_priv. Where ->tt_crc is
atomic_t, of all things. With exactly two things ever done to it:
batman_ogm_packet->tt_crc = htons((uint16_t)
atomic_read(&bat_priv->tt_crc));
in bat_iv_ogm_schedule() and
atomic_set(&bat_priv->tt_crc, tt_local_crc(bat_priv));
in prepare_packet_buffer(). What the hell does that have to do with atomic_t?
At least that one is definitely host-endian all along (tt_local_crc() is
the same kind of built-on-crc16_byte() thing).
And then there's batman_ogv_packet, where we flip the damn field
from net-endian to host-endian and back. That's where the argument of
tt_update_orig() comes from, AFAICS always in host-endian form.
IOW, unless I'm misreading that code we have
bat_priv ->tt_crc: host-endian, no need to make it atomic_t
orig_node ->tt_crc: host-endian
tt_update_orig()/send_tt_request() tt_crc argument: host-endian
the value put into the packet in send_tt_request(): broken; should be
net-endian, in reality it's host-endian. Missing htons() at the very
least.
Could somebody familiar with that code comment on that?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] endianness bugs in net/batman-adv/
[not found] ` <20120413194629.GP6589-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
@ 2012-04-14 9:34 ` Antonio Quartulli
[not found] ` <20120414093404.GA5300-E/2OGukznS5g9hUCZPvPmw@public.gmane.org>
0 siblings, 1 reply; 3+ messages in thread
From: Antonio Quartulli @ 2012-04-14 9:34 UTC (permalink / raw)
To: Al Viro
Cc: netdev-u79uwXL29TY76Z2rM5mHXA,
b.a.t.m.a.n-ZwoEplunGu2X36UT3dwllkB+6BGkLq7r
[-- Attachment #1: Type: text/plain, Size: 3133 bytes --]
On Fri, Apr 13, 2012 at 08:46:29 +0100, Al Viro wrote:
> Let's start with net/batman-adv/translation-table.c:send_tt_request().
> Its tt_crc argument gets stored into skb as-is and skb goes on the wire.
> OK, so it's fixed-endian, right?
ok, it's a bug. The tt_crc field must be stored in the skb by using htons().
>
> That sucker comes straight from the (only) caller - tt_update_orig().
> There it gets compared with ->tt_crc of struct orig_node instances. Fine,
> except that just prior to that comparison we assign to ->tt_crc the return
> value of tt_global_crc(). Which is built on crc16_byte() and clearly
> returns a host-endian value. Additionally, orig_node ->tt_crc is getting
> compared to tt_request->tt_data in send_other_tt_response(), which ultimately
> comes from recv_tt_query() where it's flipped from net-endian to host-endian.
>
We want to do every computation using host-endian data. There is no "fixed
endian" anywhere. As I wrote above, we forgot to use htons() before sending the
tt_crc over the wire.
> It gets even funnier - we have 3 structures with ->tt_crc in them;
> one is struct orig_node (see above), another is struct batman_ogv_packet and
> then there's the weirdest one - struct bat_priv. Where ->tt_crc is
> atomic_t, of all things. With exactly two things ever done to it:
> batman_ogm_packet->tt_crc = htons((uint16_t)
> atomic_read(&bat_priv->tt_crc));
> in bat_iv_ogm_schedule() and
> atomic_set(&bat_priv->tt_crc, tt_local_crc(bat_priv));
> in prepare_packet_buffer(). What the hell does that have to do with atomic_t?
Thank you for spotting this. It was defined as atomic_t in the early development
phase of this new tt framework, but, then, I'd say that we forgot to convert it
to uint16_t once atomic_t was not needed anymore.
> At least that one is definitely host-endian all along (tt_local_crc() is
> the same kind of built-on-crc16_byte() thing).
>
> And then there's batman_ogv_packet, where we flip the damn field
> from net-endian to host-endian and back. That's where the argument of
> tt_update_orig() comes from, AFAICS always in host-endian form.
>
> IOW, unless I'm misreading that code we have
> bat_priv ->tt_crc: host-endian, no need to make it atomic_t
I agree.
> orig_node ->tt_crc: host-endian
I agree. As I said before we want to use host-endian everywhere. We just want to
convert the data to net-endian before sending it over the wire (like people
should normally do).
> tt_update_orig()/send_tt_request() tt_crc argument: host-endian
I agree.
> the value put into the packet in send_tt_request(): broken; should be
> net-endian, in reality it's host-endian. Missing htons() at the very
> least.
>
Exactly. This is the bug I was talking about in my first inline response.
All the problems come from a missing htons() before sending the tt_request
packet.
Thank you very much. I'll fix it.
Best regards,
--
Antonio Quartulli
..each of us alone is worth nothing..
Ernesto "Che" Guevara
[-- Attachment #2: Type: application/pgp-signature, Size: 490 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] endianness bugs in net/batman-adv/
[not found] ` <20120414093404.GA5300-E/2OGukznS5g9hUCZPvPmw@public.gmane.org>
@ 2012-04-14 13:07 ` Antonio Quartulli
0 siblings, 0 replies; 3+ messages in thread
From: Antonio Quartulli @ 2012-04-14 13:07 UTC (permalink / raw)
To: Al Viro
Cc: netdev-u79uwXL29TY76Z2rM5mHXA,
b.a.t.m.a.n-ZwoEplunGu2X36UT3dwllkB+6BGkLq7r
[-- Attachment #1: Type: text/plain, Size: 570 bytes --]
Hello,
On Sat, Apr 14, 2012 at 11:34:05AM +0200, Antonio Quartulli wrote:
....
>
> Thank you very much. I'll fix it.
I sent two patches to the batman-adv mailing list that will fix the bugs you
have found.
If you would like to have a look at the patches, you can find them on the list
archive:
https://lists.open-mesh.org/pipermail/b.a.t.m.a.n/2012-April/006692.html
https://lists.open-mesh.org/pipermail/b.a.t.m.a.n/2012-April/006693.html
Regards,
--
Antonio Quartulli
..each of us alone is worth nothing..
Ernesto "Che" Guevara
[-- Attachment #2: Type: application/pgp-signature, Size: 490 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-04-14 13:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-04-13 19:46 [RFC] endianness bugs in net/batman-adv/ Al Viro
[not found] ` <20120413194629.GP6589-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
2012-04-14 9:34 ` Antonio Quartulli
[not found] ` <20120414093404.GA5300-E/2OGukznS5g9hUCZPvPmw@public.gmane.org>
2012-04-14 13:07 ` Antonio Quartulli
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).