diff mbox

rose_route_frame() NULL pointer dereference kernel panic

Message ID 56D5FD7A.5080104@free.fr
State RFC, archived
Delegated to: David Miller
Headers show

Commit Message

Bernard Pidoux March 1, 2016, 8:37 p.m. UTC
Hi David, Ralf,

David is absolutely right about my unappropriate patch.
Although I had searched functions calling rose_route_frame(), I did not
notice rose_xmit() was involved. Shame on me !

Then, David precisely located the source of the issue we are facing.

When rose_xmit() calls rose_route_frame() with NULL as second argument,
there is always a null pointer dereference when rose_route_frame() calls 
ax25cmp().

Here is the explanation :

When rose_route_frame() is called by rose_xmit() with NULL *ax25 argument
the following comparison (rose_route.c , line 883)

if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&

always has a pointer dereference leading to a kernel panic.

I noticed, using a few printk, that during rose normal operations 
rose_xmit() was never called
when ax25ipd sends an UDP frame. Otherwise, this bug would have been 
found earlier.
It is only because FPAC application asked for a connection to an address 
without defined route and
gateway that rose_xmit() was activated.

I am not sure I understood well the purpose of the NULL second argument.
I only guess it was intended to have ax25->dest_addr empty in order to 
make the comparison
with all possible rose_neigh->callsign always false.

I built the following patch in order to obtain the same result without 
NULL pointer.

                 return NETDEV_TX_OK;

Could Ralf or David please check if above code syntax is correct.
I tested the patch and found rose was working correctly with no more 
panic nor
unwanted effects on rose_route_frame() normal operations.

Bernard Pidoux

Comments

David Miller March 3, 2016, 10:02 p.m. UTC | #1
From: f6bvp <f6bvp@free.fr>
Date: Tue, 1 Mar 2016 21:37:14 +0100

> I built the following patch in order to obtain the same result without
> NULL pointer.

But it will cause every packet to be dropped because rose_route_frame() won't
find a matching neighbour, and therefore return 0 to rose_xmit().
Bernard Pidoux March 5, 2016, 3:32 p.m. UTC | #2
David,

I understand I did not explain clearly or completely things.

I agree that each time patched rose_xmit() is calling rose_route_frame() 
it will
get a 0 return.
And I think this is what was intended by the author of rose_xmit().
He wrote a null argument in order to obtain this result but this situation
was never reached until I configurerd a secondary network with the
following attributes (lack of route gateway) and thus the bug had not
been detected before.

/sbin/ifconfig enp4s0:1 44.168.19.22 netmask 255.255.255.240

With original rose_route_frame(),  when ax25cmp() was called with a NULL
argument it always got a null dereference pointer and a kernel panic
occured.

I conducted a few trials with printks in rose functions and patched
rose_xmit() otherwise kernel panic would have occured.
For my setting of rose network, I configured a device axudp to send
encapsulated AX.25 frames into UDP frames via ax25ipd daemon.
First, I set ax25ipd configuration with a rose neighbour with a local
network address.

# Route HAMNET
# F6BVP-10/11
route f6bvp-10 44.168.19.19 udp 10093
route f6bvp-11 44.168.19.19 udp 10093 b
#

kernel route looks like:

Destination     Passerelle      Genmask         Indic Metric Ref Use Iface
0.0.0.0         192.168.0.254   0.0.0.0         UG    10 0        0 enp4s0
44.168.19.16    0.0.0.0         255.255.255.240 U     0 0        0 enp4s0
169.254.0.0     0.0.0.0         255.255.0.0     U     10 0        0 enp4s0
192.168.0.0     0.0.0.0         255.255.255.0   U     10 0        0 enp4s0

Those are printks when starting AX.25, and  ROSE fpac node application:

[  410.759423] NET: Registered protocol family 3
[  410.784477] mkiss: AX.25 Multikiss, Hans Albas PE1AYX
[  410.785506] mkiss: ax0: crc mode is auto.
[  410.786135] IPv6: ADDRCONF(NETDEV_CHANGE): ax0: link becomes ready
[  411.011461] ROSE: rose_setup()
[  411.012685] ROSE: rose_setup()
[  411.014506] ROSE: rose_setup()
[  411.016902] ROSE: rose_setup()
[  411.021736] ROSE: rose_setup()
[  411.023884] ROSE: rose_setup()
[  411.026132] ROSE: rose_setup()
[  411.028349] ROSE: rose_setup()
[  411.030648] ROSE: rose_setup()
[  411.032975] ROSE: rose_setup()
[  411.033688] NET: Registered protocol family 11
[  411.037511] ROSE: rose_set_mac_address()
[  411.037987] ROSE: rose_open()
[  412.041361] ROSE: rose_connect()
[  414.053240] ROSE: rose_connect()
[  414.053599] mkiss: ax0: Trying crc-smack
[  414.058881] mkiss: ax0: Trying crc-flexnet
[  414.086670] ROSE: rose_route_frame()
[  414.152265] ROSE: rose_route_frame()
[  471.414622] ROSE: rose_connect()
[  471.449136] ROSE: rose_route_frame()
[  471.694472] ROSE: rose_route_frame()
[  471.695823] ROSE: rose_recvmsg()

Application fpacnode client shows that node is connected to local 
neighbour and
application works normally.

Next configuration trial was with ax25ipd.conf configured for and a 
remote subnet
rose neighbour:

# Route HAMNET
# F6BVP-10/11
route f6cnb-9 44.168.12.18 udp 10092 b
route f6cnb-11 44.168.12.20 udp 10092 b
#

Kernel route table is the same as before, i.e. still without any gateway
for 44.0.0.0 route.

This time printks show a different scenario when starting rose:

  1863.750045] mkiss: AX.25 Multikiss, Hans Albas PE1AYX
[ 1863.751165] mkiss: ax0: crc mode is auto.
[ 1863.755760] IPv6: ADDRCONF(NETDEV_CHANGE): ax0: link becomes ready
[ 1863.792418] ROSE: rose_set_mac_address()
[ 1863.795116] ROSE: rose_open()
[ 1864.797375] ROSE: rose_connect()
[ 1866.809240] ROSE: rose_connect()
[ 1866.809662] mkiss: ax0: Trying crc-smack
[ 1866.811740] ROSE: rose_connect()
[ 1866.811983] ROSE: rose_header()
[ 1866.811990] ROSE: rose_xmit()
[ 1866.811990] ROSE: rose_route_frame()
[ 1866.811992] rose_route : unknown neighbour or device '*'
[ 1866.813647] mkiss: ax0: Trying crc-flexnet
[ 1866.815623] ROSE: rose_header()
[ 1866.817228] ROSE: rose_xmit()
[ 1866.818808] ROSE: rose_route_frame()
[ 1866.820411] rose_route : unknown neighbour or device '*'
[ 1876.832984] ROSE: rose_header()
[ 1876.834572] ROSE: rose_xmit()
[ 1876.836093] ROSE: rose_route_frame()
[ 1876.837614] rose_route : unknown neighbour or device '*'
[ 1876.839574] ROSE: rose_header()
[ 1876.841099] ROSE: rose_xmit()
[ 1876.842586] ROSE: rose_route_frame()
[ 1876.844083] rose_route : unknown neighbour or device '*'

With the patch ax25cmp() comparison fails and message
unknown neighbor or device '*'
is correctly displayed for rose_neigh == NULL

Of course, because there is no route or gateway toward this subnetwork
in kernel route table, neighbour node is not connected and fpacnode 
application
is informed about it. Thus system operator knows there is something
wrong in rose network configuration.

Next trial was performed after adding a gateway toward remote rose neighbour
sub net.

/sbin/route add -net 44.0.0.0/8 gw 44.168.19.17

This time starting rose shows no more rose_xmit():

  9871.374021] mkiss: AX.25 Multikiss, Hans Albas PE1AYX
[ 9871.375418] mkiss: ax0: crc mode is auto.
[ 9871.376747] IPv6: ADDRCONF(NETDEV_CHANGE): ax0: link becomes ready
[ 9871.479231] ROSE: rose_set_mac_address()
[ 9871.479795] ROSE: rose_open()
[ 9872.481622] ROSE: rose_connect()
[ 9874.494286] ROSE: rose_connect()
[ 9874.494712] mkiss: ax0: Trying crc-smack
[ 9874.496477] ROSE: rose_connect()
[ 9874.497012] mkiss: ax0: Trying crc-flexnet
[10029.645910] ROSE: rose_connect()
[10029.656025] ROSE: rose_sendmsg()
[10029.666022] ROSE: rose_recvmsg()
[10029.666390] ROSE: rose_sendmsg()
[10029.676014] ROSE: rose_recvmsg()
[10029.686035] ROSE: rose_recvmsg()
[10039.632987] ROSE: rose_connect()
[10039.643081] ROSE: rose_sendmsg()
[10039.653027] ROSE: rose_recvmsg()
[10039.653453] ROSE: rose_sendmsg()
[10039.663022] ROSE: rose_recvmsg()

And a final trial was after removing again subnet route and gateway:

/sbin/route del -net 44.0.0.0/8

[ 5707.295943] mkiss: AX.25 Multikiss, Hans Albas PE1AYX
[ 5707.297031] mkiss: ax0: crc mode is auto.
[ 5707.299063] IPv6: ADDRCONF(NETDEV_CHANGE): ax0: link becomes ready
[ 5707.339840] ROSE: rose_set_mac_address()
[ 5707.340550] ROSE: rose_open()
[ 5707.345682] ROSE: rose_connect()
[ 5709.358367] ROSE: rose_connect()
[ 5709.358785] mkiss: ax0: Trying crc-smack
[ 5709.360589] ROSE: rose_connect()
[ 5709.361116] mkiss: ax0: Trying crc-flexnet
[ 5709.361553] ROSE: rose_header()
[ 5709.361558] ROSE: rose_xmit()
[ 5709.361559] ROSE: rose_route_frame()
[ 5709.361560] rose_route : unknown neighbour or device '*'
[ 5709.369564] ROSE: rose_header()
[ 5709.371134] ROSE: rose_xmit()
[ 5709.372695] ROSE: rose_route_frame()
[ 5709.374258] rose_route : unknown neighbour or device '*'
[ 5719.392472] ROSE: rose_header()
[ 5719.394032] ROSE: rose_xmit()
[ 5719.395536] ROSE: rose_route_frame()
[ 5719.395538] rose_route : unknown neighbour or device '*'
[ 5719.396035] ROSE: rose_header()
[ 5719.396040] ROSE: rose_xmit()
[ 5719.396041] ROSE: rose_route_frame()
[ 5719.396042] rose_route : unknown neighbour or device '*'

Conclusion : rose_header and rose_xmit() are only activated when a route
to a rose_neighbour is not found in kernel table.

This is initialized in rose_setup() that sets

dev->netdev_ops         = &rose_netdev_ops;

and we have :

static const struct net_device_ops rose_netdev_ops = {
         .ndo_open               = rose_open,
         .ndo_stop               = rose_close,
         .ndo_start_xmit         = rose_xmit,
         .ndo_set_mac_address    = rose_set_mac_address,
};

I guess, there is no need for more demonstrations of this patch utility.
It avoids kernel panic in case of uncomplete subnet route setting
(lack of gateway) and triggers error message (when proper console
printk level is set).

One last remark. Patch uses an ax25_cb parameter for called
rose_route_device()  is looking for an  ax25->dest_address
in ax25_cb structure

Bernard Pidoux


Le 03/03/2016 23:02, David Miller a écrit :
> From: f6bvp <f6bvp@free.fr>
> Date: Tue, 1 Mar 2016 21:37:14 +0100
>
>> I built the following patch in order to obtain the same result without
>> NULL pointer.
> But it will cause every packet to be dropped because rose_route_frame() won't
> find a matching neighbour, and therefore return 0 to rose_xmit().
David Miller March 5, 2016, 4:22 p.m. UTC | #3
From: f6bvp <f6bvp@free.fr>
Date: Sat, 5 Mar 2016 16:32:42 +0100

> I understand I did not explain clearly or completely things.
> 
> I agree that each time patched rose_xmit() is calling
> rose_route_frame() it will
> get a 0 return.
> And I think this is what was intended by the author of rose_xmit().

If that's what he intended he would have implemented the entirety of
rose_xmit() as "kfree_skb(skb)".  But that's obviously not the case.

The author meant the packet to be sent in some way, perhaps using a
default path or something like that.

So please stop telling me over and over again that this function
is meant to simply drop all packets, it's not true.
Bernard Pidoux March 5, 2016, 5:32 p.m. UTC | #4
Le 05/03/2016 17:22, David Miller a écrit :
> From: f6bvp <f6bvp@free.fr>
> Date: Sat, 5 Mar 2016 16:32:42 +0100
> 
>> I understand I did not explain clearly or completely things.
>>
>> I agree that each time patched rose_xmit() is calling
>> rose_route_frame() it will
>> get a 0 return.
>> And I think this is what was intended by the author of rose_xmit().
> 
> If that's what he intended he would have implemented the entirety of
> rose_xmit() as "kfree_skb(skb)".  But that's obviously not the case.
> 
> The author meant the packet to be sent in some way, perhaps using a
> default path or something like that.

Via a NULL pointer ?
I don't see how it could work.

> 
> So please stop telling me over and over again that this function
> is meant to simply drop all packets, it's not true.
> 
I am just making hypothesis and trying to infer some deductions from the
behaviour of program when there is no more kernel panic.

If there is a situation leading to a kernel panic, I thought code should
be changed ?
What is the problem replacing a NULL argument by an array of 0 ?
Francois Romieu March 5, 2016, 7:57 p.m. UTC | #5
f6bvp <f6bvp@free.fr> :
> Le 05/03/2016 17:22, David Miller a écrit :
[...]
> > If that's what he intended he would have implemented the entirety of
> > rose_xmit() as "kfree_skb(skb)".  But that's obviously not the case.
> > 
> > The author meant the packet to be sent in some way, perhaps using a
> > default path or something like that.
> 
> Via a NULL pointer ?
> I don't see how it could work.

Ask G4KLX what he meant when he wrote rose_rebuild_header (since that's
where Eric B. took rose_xmit from) back in the 2.1.9 era ?

See https://git.kernel.org/cgit/linux/kernel/git/history/history.git/commit/?id=d75df542864496c92ff705d7d072a58b0119a4ff
Bernard Pidoux March 6, 2016, 10:58 a.m. UTC | #6
François,

Thank you for providing information about rose_rebuild_header history.
I was not able to find Jonathan Naylor G4KLX email.
However as rose_xmit() has been recently changed by Eric Biederman, he
may have more precise information about rose_route_frame()
NULL argument.
Meanwhile, I browsed into rose_route.c release 001 you referenced and found
an interesting comment before rose_route_frame() :
+/*
+ * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
+ * indicates an internally generated frame.
+ */
+int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)

Now, I hope that ROSE guru will explain us what means an "internally
generated frame" and if, by chance, it means that it is not to be sent
via a net device, but rather to be used locally ?

Bernard
diff mbox

Patch

--- a/net/rose/rose_dev.c       2016-02-25 21:01:36.000000000 +0100
+++ b/net/rose/rose_dev.c  2016-03-01 14:08:29.911389078 +0100
@@ -101,13 +101,16 @@  static netdev_tx_t rose_xmit(struct sk_b
  {
         struct net_device_stats *stats = &dev->stats;
         unsigned int len = skb->len;
+       struct ax25_cb ax25;

+       memset(&ax25, 0, sizeof(struct ax25_cb));
+
         if (!netif_running(dev)) {
                 printk(KERN_ERR "ROSE: rose_xmit - called when iface is 
down\n");
                 return NETDEV_TX_BUSY;
         }

-       if (!rose_route_frame(skb, NULL)) {
+       if (!rose_route_frame(skb, &ax25)) {
                 dev_kfree_skb(skb);
                 stats->tx_errors++;