345-v4.16-netfilter-nf_flow_offload-fix-use-after-free-and-a-r.patch 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Date: Wed, 7 Feb 2018 09:23:25 +0100
  3. Subject: [PATCH] netfilter: nf_flow_offload: fix use-after-free and a resource
  4. leak
  5. flow_offload_del frees the flow, so all associated resource must be
  6. freed before.
  7. Since the ct entry in struct flow_offload_entry was allocated by
  8. flow_offload_alloc, it should be freed by flow_offload_free to take care
  9. of the error handling path when flow_offload_add fails.
  10. While at it, make flow_offload_del static, since it should never be
  11. called directly, only from the gc step
  12. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  13. ---
  14. --- a/include/net/netfilter/nf_flow_table.h
  15. +++ b/include/net/netfilter/nf_flow_table.h
  16. @@ -90,7 +90,6 @@ struct flow_offload *flow_offload_alloc(
  17. void flow_offload_free(struct flow_offload *flow);
  18. int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow);
  19. -void flow_offload_del(struct nf_flowtable *flow_table, struct flow_offload *flow);
  20. struct flow_offload_tuple_rhash *flow_offload_lookup(struct nf_flowtable *flow_table,
  21. struct flow_offload_tuple *tuple);
  22. int nf_flow_table_iterate(struct nf_flowtable *flow_table,
  23. --- a/net/netfilter/nf_flow_table.c
  24. +++ b/net/netfilter/nf_flow_table.c
  25. @@ -125,7 +125,9 @@ void flow_offload_free(struct flow_offlo
  26. dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache);
  27. dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache);
  28. e = container_of(flow, struct flow_offload_entry, flow);
  29. - kfree(e);
  30. + nf_ct_delete(e->ct, 0, 0);
  31. + nf_ct_put(e->ct);
  32. + kfree_rcu(e, rcu_head);
  33. }
  34. EXPORT_SYMBOL_GPL(flow_offload_free);
  35. @@ -149,11 +151,9 @@ int flow_offload_add(struct nf_flowtable
  36. }
  37. EXPORT_SYMBOL_GPL(flow_offload_add);
  38. -void flow_offload_del(struct nf_flowtable *flow_table,
  39. - struct flow_offload *flow)
  40. +static void flow_offload_del(struct nf_flowtable *flow_table,
  41. + struct flow_offload *flow)
  42. {
  43. - struct flow_offload_entry *e;
  44. -
  45. rhashtable_remove_fast(&flow_table->rhashtable,
  46. &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
  47. *flow_table->type->params);
  48. @@ -161,10 +161,8 @@ void flow_offload_del(struct nf_flowtabl
  49. &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
  50. *flow_table->type->params);
  51. - e = container_of(flow, struct flow_offload_entry, flow);
  52. - kfree_rcu(e, rcu_head);
  53. + flow_offload_free(flow);
  54. }
  55. -EXPORT_SYMBOL_GPL(flow_offload_del);
  56. struct flow_offload_tuple_rhash *
  57. flow_offload_lookup(struct nf_flowtable *flow_table,
  58. @@ -175,15 +173,6 @@ flow_offload_lookup(struct nf_flowtable
  59. }
  60. EXPORT_SYMBOL_GPL(flow_offload_lookup);
  61. -static void nf_flow_release_ct(const struct flow_offload *flow)
  62. -{
  63. - struct flow_offload_entry *e;
  64. -
  65. - e = container_of(flow, struct flow_offload_entry, flow);
  66. - nf_ct_delete(e->ct, 0, 0);
  67. - nf_ct_put(e->ct);
  68. -}
  69. -
  70. int nf_flow_table_iterate(struct nf_flowtable *flow_table,
  71. void (*iter)(struct flow_offload *flow, void *data),
  72. void *data)
  73. @@ -259,10 +248,8 @@ static int nf_flow_offload_gc_step(struc
  74. flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
  75. if (nf_flow_has_expired(flow) ||
  76. - nf_flow_is_dying(flow)) {
  77. + nf_flow_is_dying(flow))
  78. flow_offload_del(flow_table, flow);
  79. - nf_flow_release_ct(flow);
  80. - }
  81. }
  82. out:
  83. rhashtable_walk_stop(&hti);