Skip to content

Commit 6f68cd6

Browse files
pskrgagdavem330
authored andcommitted
net: batman-adv: fix error handling
Syzbot reported ODEBUG warning in batadv_nc_mesh_free(). The problem was in wrong error handling in batadv_mesh_init(). Before this patch batadv_mesh_init() was calling batadv_mesh_free() in case of any batadv_*_init() calls failure. This approach may work well, when there is some kind of indicator, which can tell which parts of batadv are initialized; but there isn't any. All written above lead to cleaning up uninitialized fields. Even if we hide ODEBUG warning by initializing bat_priv->nc.work, syzbot was able to hit GPF in batadv_nc_purge_paths(), because hash pointer in still NULL. [1] To fix these bugs we can unwind batadv_*_init() calls one by one. It is good approach for 2 reasons: 1) It fixes bugs on error handling path 2) It improves the performance, since we won't call unneeded batadv_*_free() functions. So, this patch makes all batadv_*_init() clean up all allocated memory before returning with an error to no call correspoing batadv_*_free() and open-codes batadv_mesh_free() with proper order to avoid touching uninitialized fields. Link: https://lore.kernel.org/netdev/000000000000c87fbd05cef6bcb0@google.com/ [1] Reported-and-tested-by: syzbot+28b0702ada0bf7381f58@syzkaller.appspotmail.com Fixes: c6c8fea ("net: Add batman-adv meshing protocol") Signed-off-by: Pavel Skripkin <paskripkin@gmail.com> Acked-by: Sven Eckelmann <sven@narfation.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent fa40d97 commit 6f68cd6

4 files changed

Lines changed: 52 additions & 20 deletions

File tree

‎net/batman-adv/bridge_loop_avoidance.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,10 +1560,14 @@ int batadv_bla_init(struct batadv_priv *bat_priv)
15601560
return 0;
15611561

15621562
bat_priv->bla.claim_hash = batadv_hash_new(128);
1563-
bat_priv->bla.backbone_hash = batadv_hash_new(32);
1563+
if (!bat_priv->bla.claim_hash)
1564+
return -ENOMEM;
15641565

1565-
if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1566+
bat_priv->bla.backbone_hash = batadv_hash_new(32);
1567+
if (!bat_priv->bla.backbone_hash) {
1568+
batadv_hash_destroy(bat_priv->bla.claim_hash);
15661569
return -ENOMEM;
1570+
}
15671571

15681572
batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
15691573
&batadv_claim_hash_lock_class_key);

‎net/batman-adv/main.c‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,29 +190,41 @@ int batadv_mesh_init(struct net_device *soft_iface)
190190

191191
bat_priv->gw.generation = 0;
192192

193-
ret = batadv_v_mesh_init(bat_priv);
194-
if (ret < 0)
195-
goto err;
196-
197193
ret = batadv_originator_init(bat_priv);
198-
if (ret < 0)
199-
goto err;
194+
if (ret < 0) {
195+
atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
196+
goto err_orig;
197+
}
200198

201199
ret = batadv_tt_init(bat_priv);
202-
if (ret < 0)
203-
goto err;
200+
if (ret < 0) {
201+
atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
202+
goto err_tt;
203+
}
204+
205+
ret = batadv_v_mesh_init(bat_priv);
206+
if (ret < 0) {
207+
atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
208+
goto err_v;
209+
}
204210

205211
ret = batadv_bla_init(bat_priv);
206-
if (ret < 0)
207-
goto err;
212+
if (ret < 0) {
213+
atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
214+
goto err_bla;
215+
}
208216

209217
ret = batadv_dat_init(bat_priv);
210-
if (ret < 0)
211-
goto err;
218+
if (ret < 0) {
219+
atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
220+
goto err_dat;
221+
}
212222

213223
ret = batadv_nc_mesh_init(bat_priv);
214-
if (ret < 0)
215-
goto err;
224+
if (ret < 0) {
225+
atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
226+
goto err_nc;
227+
}
216228

217229
batadv_gw_init(bat_priv);
218230
batadv_mcast_init(bat_priv);
@@ -222,8 +234,20 @@ int batadv_mesh_init(struct net_device *soft_iface)
222234

223235
return 0;
224236

225-
err:
226-
batadv_mesh_free(soft_iface);
237+
err_nc:
238+
batadv_dat_free(bat_priv);
239+
err_dat:
240+
batadv_bla_free(bat_priv);
241+
err_bla:
242+
batadv_v_mesh_free(bat_priv);
243+
err_v:
244+
batadv_tt_free(bat_priv);
245+
err_tt:
246+
batadv_originator_free(bat_priv);
247+
err_orig:
248+
batadv_purge_outstanding_packets(bat_priv, NULL);
249+
atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
250+
227251
return ret;
228252
}
229253

‎net/batman-adv/network-coding.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
152152
&batadv_nc_coding_hash_lock_class_key);
153153

154154
bat_priv->nc.decoding_hash = batadv_hash_new(128);
155-
if (!bat_priv->nc.decoding_hash)
155+
if (!bat_priv->nc.decoding_hash) {
156+
batadv_hash_destroy(bat_priv->nc.coding_hash);
156157
goto err;
158+
}
157159

158160
batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
159161
&batadv_nc_decoding_hash_lock_class_key);

‎net/batman-adv/translation-table.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4162,8 +4162,10 @@ int batadv_tt_init(struct batadv_priv *bat_priv)
41624162
return ret;
41634163

41644164
ret = batadv_tt_global_init(bat_priv);
4165-
if (ret < 0)
4165+
if (ret < 0) {
4166+
batadv_tt_local_table_free(bat_priv);
41664167
return ret;
4168+
}
41674169

41684170
batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
41694171
batadv_tt_tvlv_unicast_handler_v1,

0 commit comments

Comments
 (0)