343-cfg80211-limit-scan-results-cache-size.patch 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. From: Johannes Berg <johannes.berg@intel.com>
  2. Date: Tue, 15 Nov 2016 12:05:11 +0100
  3. Subject: [PATCH] cfg80211: limit scan results cache size
  4. It's possible to make scanning consume almost arbitrary amounts
  5. of memory, e.g. by sending beacon frames with random BSSIDs at
  6. high rates while somebody is scanning.
  7. Limit the number of BSS table entries we're willing to cache to
  8. 1000, limiting maximum memory usage to maybe 4-5MB, but lower
  9. in practice - that would be the case for having both full-sized
  10. beacon and probe response frames for each entry; this seems not
  11. possible in practice, so a limit of 1000 entries will likely be
  12. closer to 0.5 MB.
  13. Cc: stable@vger.kernel.org
  14. Signed-off-by: Johannes Berg <johannes.berg@intel.com>
  15. ---
  16. --- a/net/wireless/core.h
  17. +++ b/net/wireless/core.h
  18. @@ -71,6 +71,7 @@ struct cfg80211_registered_device {
  19. struct list_head bss_list;
  20. struct rb_root bss_tree;
  21. u32 bss_generation;
  22. + u32 bss_entries;
  23. struct cfg80211_scan_request *scan_req; /* protected by RTNL */
  24. struct sk_buff *scan_msg;
  25. struct cfg80211_sched_scan_request __rcu *sched_scan_req;
  26. --- a/net/wireless/scan.c
  27. +++ b/net/wireless/scan.c
  28. @@ -57,6 +57,19 @@
  29. * also linked into the probe response struct.
  30. */
  31. +/*
  32. + * Limit the number of BSS entries stored in mac80211. Each one is
  33. + * a bit over 4k at most, so this limits to roughly 4-5M of memory.
  34. + * If somebody wants to really attack this though, they'd likely
  35. + * use small beacons, and only one type of frame, limiting each of
  36. + * the entries to a much smaller size (in order to generate more
  37. + * entries in total, so overhead is bigger.)
  38. + */
  39. +static int bss_entries_limit = 1000;
  40. +module_param(bss_entries_limit, int, 0644);
  41. +MODULE_PARM_DESC(bss_entries_limit,
  42. + "limit to number of scan BSS entries (per wiphy, default 1000)");
  43. +
  44. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  45. static void bss_free(struct cfg80211_internal_bss *bss)
  46. @@ -137,6 +150,10 @@ static bool __cfg80211_unlink_bss(struct
  47. list_del_init(&bss->list);
  48. rb_erase(&bss->rbn, &rdev->bss_tree);
  49. + rdev->bss_entries--;
  50. + WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
  51. + "rdev bss entries[%d]/list[empty:%d] corruption\n",
  52. + rdev->bss_entries, list_empty(&rdev->bss_list));
  53. bss_ref_put(rdev, bss);
  54. return true;
  55. }
  56. @@ -163,6 +180,40 @@ static void __cfg80211_bss_expire(struct
  57. rdev->bss_generation++;
  58. }
  59. +static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
  60. +{
  61. + struct cfg80211_internal_bss *bss, *oldest = NULL;
  62. + bool ret;
  63. +
  64. + lockdep_assert_held(&rdev->bss_lock);
  65. +
  66. + list_for_each_entry(bss, &rdev->bss_list, list) {
  67. + if (atomic_read(&bss->hold))
  68. + continue;
  69. +
  70. + if (!list_empty(&bss->hidden_list) &&
  71. + !bss->pub.hidden_beacon_bss)
  72. + continue;
  73. +
  74. + if (oldest && time_before(oldest->ts, bss->ts))
  75. + continue;
  76. + oldest = bss;
  77. + }
  78. +
  79. + if (WARN_ON(!oldest))
  80. + return false;
  81. +
  82. + /*
  83. + * The callers make sure to increase rdev->bss_generation if anything
  84. + * gets removed (and a new entry added), so there's no need to also do
  85. + * it here.
  86. + */
  87. +
  88. + ret = __cfg80211_unlink_bss(rdev, oldest);
  89. + WARN_ON(!ret);
  90. + return ret;
  91. +}
  92. +
  93. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  94. bool send_message)
  95. {
  96. @@ -689,6 +740,7 @@ static bool cfg80211_combine_bsses(struc
  97. const u8 *ie;
  98. int i, ssidlen;
  99. u8 fold = 0;
  100. + u32 n_entries = 0;
  101. ies = rcu_access_pointer(new->pub.beacon_ies);
  102. if (WARN_ON(!ies))
  103. @@ -712,6 +764,12 @@ static bool cfg80211_combine_bsses(struc
  104. /* This is the bad part ... */
  105. list_for_each_entry(bss, &rdev->bss_list, list) {
  106. + /*
  107. + * we're iterating all the entries anyway, so take the
  108. + * opportunity to validate the list length accounting
  109. + */
  110. + n_entries++;
  111. +
  112. if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
  113. continue;
  114. if (bss->pub.channel != new->pub.channel)
  115. @@ -740,6 +798,10 @@ static bool cfg80211_combine_bsses(struc
  116. new->pub.beacon_ies);
  117. }
  118. + WARN_ONCE(n_entries != rdev->bss_entries,
  119. + "rdev bss entries[%d]/list[len:%d] corruption\n",
  120. + rdev->bss_entries, n_entries);
  121. +
  122. return true;
  123. }
  124. @@ -894,7 +956,14 @@ cfg80211_bss_update(struct cfg80211_regi
  125. }
  126. }
  127. + if (rdev->bss_entries >= bss_entries_limit &&
  128. + !cfg80211_bss_expire_oldest(rdev)) {
  129. + kfree(new);
  130. + goto drop;
  131. + }
  132. +
  133. list_add_tail(&new->list, &rdev->bss_list);
  134. + rdev->bss_entries++;
  135. rb_insert_bss(rdev, new);
  136. found = new;
  137. }