1 From d0021b252eaf65ca07ed14f0d66425dd9ccab9a6 Mon Sep 17 00:00:00 2001
2 From: Neil Horman <nhorman@tuxdriver.com>
3 Date: Wed, 3 Mar 2010 08:31:23 +0000
4 Subject: [PATCH] tipc: Fix oops on send prior to entering networked mode (v3)
7 Fix TIPC to disallow sending to remote addresses prior to entering NET_MODE
9 user programs can oops the kernel by sending datagrams via AF_TIPC prior to
10 entering networked mode. The following backtrace has been observed:
12 ID: 13459 TASK: ffff810014640040 CPU: 0 COMMAND: "tipc-client"
13 [exception RIP: tipc_node_select_next_hop+90]
14 RIP: ffffffff8869d3c3 RSP: ffff81002d9a5ab8 RFLAGS: 00010202
15 RAX: 0000000000000001 RBX: 0000000000000001 RCX: 0000000000000001
16 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000001001001
17 RBP: 0000000001001001 R8: 0074736575716552 R9: 0000000000000000
18 R10: ffff81003fbd0680 R11: 00000000000000c8 R12: 0000000000000008
19 R13: 0000000000000001 R14: 0000000000000001 R15: ffff810015c6ca00
20 ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018
21 RIP: 0000003cbd8d49a3 RSP: 00007fffc84e0be8 RFLAGS: 00010206
22 RAX: 000000000000002c RBX: ffffffff8005d116 RCX: 0000000000000000
23 RDX: 0000000000000008 RSI: 00007fffc84e0c00 RDI: 0000000000000003
24 RBP: 0000000000000000 R8: 00007fffc84e0c10 R9: 0000000000000010
25 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
26 R13: 00007fffc84e0d10 R14: 0000000000000000 R15: 00007fffc84e0c30
27 ORIG_RAX: 000000000000002c CS: 0033 SS: 002b
29 What happens is that, when the tipc module in inserted it enters a standalone
30 node mode in which communication to its own address is allowed <0.0.0> but not
31 to other addresses, since the appropriate data structures have not been
32 allocated yet (specifically the tipc_net pointer). There is nothing stopping a
33 client from trying to send such a message however, and if that happens, we
34 attempt to dereference tipc_net.zones while the pointer is still NULL, and
35 explode. The fix is pretty straightforward. Since these oopses all arise from
36 the dereference of global pointers prior to their assignment to allocated
37 values, and since these allocations are small (about 2k total), lets convert
38 these pointers to static arrays of the appropriate size. All the accesses to
39 these bits consider 0/NULL to be a non match when searching, so all the lookups
40 still work properly, and there is no longer a chance of a bad dererence
41 anywhere. As a bonus, this lets us eliminate the setup/teardown routines for
42 those pointers, and elimnates the need to preform any locking around them to
43 prevent access while their being allocated/freed.
45 I've updated the tipc_net structure to behave this way to fix the exact reported
46 problem, and also fixed up the tipc_bearers and media_list arrays to fix an
47 obvious simmilar problem that arises from issuing tipc-config commands to
48 manipulate bearers/links prior to entering networked mode
50 I've tested this for a few hours by running the sanity tests and stress test
51 with the tipcutils suite, and nothing has fallen over. There have been a few
52 lockdep warnings, but those were there before, and can be addressed later, as
53 they didn't actually result in any deadlock.
55 Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
56 CC: Allan Stephens <allan.stephens@windriver.com>
57 CC: David S. Miller <davem@davemloft.net>
58 CC: tipc-discussion@lists.sourceforge.net
60 bearer.c | 37 ++++++-------------------------------
62 net.c | 25 ++++---------------------
63 3 files changed, 11 insertions(+), 53 deletions(-)
64 Signed-off-by: David S. Miller <davem@davemloft.net>
65 Acked-by: Yong Wang <yong.y.wang@intel.com>
67 net/tipc/bearer.c | 37 ++++++-------------------------------
68 net/tipc/bearer.h | 2 +-
69 net/tipc/net.c | 25 ++++---------------------
70 3 files changed, 11 insertions(+), 53 deletions(-)
72 diff --git a/net/tipc/bearer.c b/net/tipc/bearer.c
73 index 327011f..7809137 100644
74 --- a/net/tipc/bearer.c
75 +++ b/net/tipc/bearer.c
78 #define MAX_ADDR_STR 32
80 -static struct media *media_list = NULL;
81 +static struct media media_list[MAX_MEDIA];
82 static u32 media_count = 0;
84 -struct bearer *tipc_bearers = NULL;
85 +struct bearer tipc_bearers[MAX_BEARERS];
88 * media_name_valid - validate media name
89 @@ -108,9 +108,11 @@ int tipc_register_media(u32 media_type,
92 write_lock_bh(&tipc_net_lock);
96 + if (tipc_mode != TIPC_NET_MODE) {
97 + warn("Media <%s> rejected, not in networked mode yet\n", name);
100 if (!media_name_valid(name)) {
101 warn("Media <%s> rejected, illegal name\n", name);
103 @@ -660,33 +662,10 @@ int tipc_disable_bearer(const char *name)
107 -int tipc_bearer_init(void)
111 - write_lock_bh(&tipc_net_lock);
112 - tipc_bearers = kcalloc(MAX_BEARERS, sizeof(struct bearer), GFP_ATOMIC);
113 - media_list = kcalloc(MAX_MEDIA, sizeof(struct media), GFP_ATOMIC);
114 - if (tipc_bearers && media_list) {
117 - kfree(tipc_bearers);
119 - tipc_bearers = NULL;
123 - write_unlock_bh(&tipc_net_lock);
127 void tipc_bearer_stop(void)
134 for (i = 0; i < MAX_BEARERS; i++) {
135 if (tipc_bearers[i].active)
136 tipc_bearers[i].publ.blocked = 1;
137 @@ -695,10 +674,6 @@ void tipc_bearer_stop(void)
138 if (tipc_bearers[i].active)
139 bearer_disable(tipc_bearers[i].publ.name);
141 - kfree(tipc_bearers);
143 - tipc_bearers = NULL;
148 diff --git a/net/tipc/bearer.h b/net/tipc/bearer.h
149 index ca57348..000228e 100644
150 --- a/net/tipc/bearer.h
151 +++ b/net/tipc/bearer.h
152 @@ -114,7 +114,7 @@ struct bearer_name {
156 -extern struct bearer *tipc_bearers;
157 +extern struct bearer tipc_bearers[];
159 void tipc_media_addr_printf(struct print_buf *pb, struct tipc_media_addr *a);
160 struct sk_buff *tipc_media_get_names(void);
161 diff --git a/net/tipc/net.c b/net/tipc/net.c
162 index 7906608..f25b1cd 100644
168 DEFINE_RWLOCK(tipc_net_lock);
169 -struct network tipc_net = { NULL };
170 +struct _zone *tipc_zones[256] = { NULL, };
171 +struct network tipc_net = { tipc_zones };
173 struct tipc_node *tipc_net_select_remote_node(u32 addr, u32 ref)
175 @@ -158,28 +159,12 @@ void tipc_net_send_external_routes(u32 dest)
179 -static int net_init(void)
181 - memset(&tipc_net, 0, sizeof(tipc_net));
182 - tipc_net.zones = kcalloc(tipc_max_zones + 1, sizeof(struct _zone *), GFP_ATOMIC);
183 - if (!tipc_net.zones) {
189 static void net_stop(void)
193 - if (!tipc_net.zones)
196 - for (z_num = 1; z_num <= tipc_max_zones; z_num++) {
197 + for (z_num = 1; z_num <= tipc_max_zones; z_num++)
198 tipc_zone_delete(tipc_net.zones[z_num]);
200 - kfree(tipc_net.zones);
201 - tipc_net.zones = NULL;
204 static void net_route_named_msg(struct sk_buff *buf)
205 @@ -282,9 +267,7 @@ int tipc_net_start(u32 addr)
209 - if ((res = tipc_bearer_init()) ||
210 - (res = net_init()) ||
211 - (res = tipc_cltr_init()) ||
212 + if ((res = tipc_cltr_init()) ||
213 (res = tipc_bclink_init())) {