Skip to contentSkip to conversation

cfg80211: Add ies_allocated check before copying ies

RFC PATCH In review Michael Buesch 2 participants 4 messages Apr 25, 2009 – Apr 26, 2009 802.11 (including cfg80211/NL80211)

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cfg80211: Add ies_allocated check before copying ies

I don't fully understand the code, but let's imagine the following situation:

- cfg80211_bss_update was called and the ie was stored to allocated space.
- The information_elements pointer is changed to the allocated space.
- cfg80211_bss_update is called again, but now the ie fits into the space after the "found" structure.
- But the information_elements pointer still points to the allocated space. So it may overrun the buffer and crash.

Is this scenario possible?
If yes, please consider the following patch.

---
 net/wireless/scan.c |    5 +++++
 1 file changed, 5 insertions(+)
net/wireless/scan.c +5 -0
@@ -378,6 +378,11 @@ cfg80211_bss_update(struct cfg80211_regi
 			size_t ielen = res->pub.len_information_elements;
 
 			if (ksize(found) >= used + ielen) {
+				if (found->ies_allocated) {
+					kfree(found->pub.information_elements);
+					found->ies_allocated = false;
+					found->pub.information_elements = (u8 *)found + used;
+				}
 				memcpy(found->pub.information_elements,
 				       res->pub.information_elements, ielen);
 				found->pub.len_information_elements = ielen;
Signature · 1 line
Greetings, Michael.
maintainer
#1 lore raw
Quoted 11 lines · Michael Buesch

I don't fully understand the code, but let's imagine the following situation:

- cfg80211_bss_update was called and the ie was stored to allocated space.
- The information_elements pointer is changed to the allocated space.
- cfg80211_bss_update is called again, but now the ie fits into the space after the "found" structure.
- But the information_elements pointer still points to the allocated space. So it may overrun the buffer and crash.

Is this scenario possible?
If yes, please consider the following patch.

Yeah, looks like a bug, good catch. I don't think the fix is correct though -- you lose the data in this case.

I think it should simply be:

net/wireless/scan.c cfg80211_bss_update(struct cfg80211_regi · line 388

Quoted 8 lines
net/wireless/scan.c +1 -1
 			size_t ielen = res->pub.len_information_elements;
 
-			if (ksize(found) >= used + ielen) {
+			if (!found->ies_allocated && ksize(found) >= used + ielen) {
 				memcpy(found->pub.information_elements,
 				       res->pub.information_elements, ielen);
 				found->pub.len_information_elements = ielen;

so that the else branch gets a chance to reallocate if necessary, would you agree?

johannes

#2 lore raw
Quoted 20 lines · On Sunday 26 April 2009 10:32:32 Johannes Berg wrote:
Quoted 11 lines · Michael Buesch

I don't fully understand the code, but let's imagine the following situation:

- cfg80211_bss_update was called and the ie was stored to allocated space.
- The information_elements pointer is changed to the allocated space.
- cfg80211_bss_update is called again, but now the ie fits into the space after the "found" structure.
- But the information_elements pointer still points to the allocated space. So it may overrun the buffer and crash.

Is this scenario possible?
If yes, please consider the following patch.

Yeah, looks like a bug, good catch. I don't think the fix is correct though -- you lose the data in this case.

I think it should simply be:

Quoted 2 lines

size_t ielen = res->pub.len_information_elements;

Quoted 9 lines

net/wireless/scan.c cfg80211_bss_update(struct cfg80211_regi · line 388

Quoted 6 lines
net/wireless/scan.c +1 -1
-			if (ksize(found) >= used + ielen) {
+			if (!found->ies_allocated && ksize(found) >= used + ielen) {
 				memcpy(found->pub.information_elements,
 				       res->pub.information_elements, ielen);
 				found->pub.len_information_elements = ielen;

so that the else branch gets a chance to reallocate if necessary, would you agree?

Yeah I first also considered this option, but I thought that the code likes to prefer putting the stuff into the "found" tail, if it fits.
But yes, your fix is perfectly fine. If it was allocated once, it will always be allocated (and grown) from then on.
It won't shrink the buffer anymore for future ies that are smaller, but I guess that's OK.

Signature · 1 line
Greetings, Michael.
maintainer
#3 lore raw
Quoted 14 lines · Michael Buesch
Quoted 4 lines

I think it should simply be:

Quoted 2 lines

size_t ielen = res->pub.len_information_elements;

Quoted 9 lines

net/wireless/scan.c cfg80211_bss_update(struct cfg80211_regi · line 388

Quoted 6 lines
net/wireless/scan.c +1 -1
-			if (ksize(found) >= used + ielen) {
+			if (!found->ies_allocated && ksize(found) >= used + ielen) {
 				memcpy(found->pub.information_elements,
 				       res->pub.information_elements, ielen);
 				found->pub.len_information_elements = ielen;

so that the else branch gets a chance to reallocate if necessary, would you agree?

Quoted 5 lines

Yeah I first also considered this option, but I thought that the code likes to prefer putting the stuff into the "found" tail, if it fits.
But yes, your fix is perfectly fine. If it was allocated once, it will always be allocated (and grown) from then on.
It won't shrink the buffer anymore for future ies that are smaller, but I guess that's OK.

Ah, yes, but then the correct fix would be to kfree the allocated IEs and use the old buffer again, not discard the data :) I don't think it's worth bothering, the entire structures are usually freed fairly quickly anyway. Do you want to redo the patch or would you prefer if I sent it?

johannes