-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't dereference NULL ipaddr in netif_set_ipaddr() #35
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The code in netif_set_ipaddr would read the memory pointed to by its ipaddr parameter, even if it was NULL on this line: if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) { On the Cortex-M3, it is typically OK to read from address 0 so this code will actually compare the reset stack pointer value to the current value in netif->ip_addr. Later in the code, this same pointer will be used for a second read: ip_addr_set(&(netif->ip_addr), ipaddr); The ip_addr_set call will first check to see if the ipaddr is NULL and if so, treats it like IP_ADDR_ANY (4 bytes of 0). /** Safely copy one IP address to another (src may be NULL) */ #define ip_addr_set(dest, src) ((dest)->addr = \ ((src) == NULL ? 0 : \ (src)->addr)) The issue here is that when GCC optimizes this code, it assumes that the first dereference of ipaddr would have thrown an invalid memory access exception and execution would never make it to this second dereference. Therefore it optimizes out the NULL check in ip_addr_set. The -fno-delete-null-pointer-checks will disable this optimization and is a good thing to use with GCC in general on Cortex-M parts. I will let the mbed guys make that change to their build system. I have however corrected the code so that the intent of how to handle a NULL ipaddr is more obvious and gets rid of the potential NULL dereference. By the way, this bug caused connect() to fail in obtaining an address from DHCP. If I recall correctly from when I first debugged this issue (late last year), I actually saw the initial value of the stack pointer being used in the DHCP request as an IP address which caused it to be rejected.
bogdanm
added a commit
that referenced
this pull request
Aug 16, 2013
Don't dereference NULL ipaddr in netif_set_ipaddr()
Heh guys, thanks for letting me know that the networking code was still not working and pulling in my latest fix. |
SeppoTakalo
pushed a commit
that referenced
this pull request
Nov 9, 2016
Adding missing stubs, and added check for nullpointer to coap message process callback.
pan-
added a commit
to pan-/mbed
that referenced
this pull request
May 15, 2018
BLE: improve permission and signing strategy
Merged
yossi2le
pushed a commit
to yossi2le/mbed-os
that referenced
this pull request
Jan 2, 2019
arm_uc_paal_update_api.h file has been moved to ARMmbed/update-client-paal
geky
pushed a commit
to geky/mbed
that referenced
this pull request
Jan 17, 2019
Added a short section about the callback functions, based on the answers to issue ARMmbed#35 and ARMmbed#36
pan-
added a commit
to pan-/mbed
that referenced
this pull request
May 29, 2020
Change variable name
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The code in netif_set_ipaddr would read the memory pointed to by its
ipaddr parameter, even if it was NULL on this line:
if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
On the Cortex-M3, it is typically OK to read from address 0 so this
code will actually compare the reset stack pointer value to the
current value in netif->ip_addr.
Later in the code, this same pointer will be used for a second read:
ip_addr_set(&(netif->ip_addr), ipaddr);
The ip_addr_set call will first check to see if the ipaddr is NULL and
if so, treats it like IP_ADDR_ANY (4 bytes of 0).
/** Safely copy one IP address to another (src may be NULL) */
#define ip_addr_set(dest, src) ((dest)->addr =
((src) == NULL ? 0 :
(src)->addr))
The issue here is that when GCC optimizes this code, it assumes that
the first dereference of ipaddr would have thrown an invalid memory
access exception and execution would never make it to this second
dereference. Therefore it optimizes out the NULL check in ip_addr_set.
The -fno-delete-null-pointer-checks will disable this optimization and
is a good thing to use with GCC in general on Cortex-M parts. I will
let the mbed guys make that change to their build system.
I have however corrected the code so that the intent of how to handle a
NULL ipaddr is more obvious and gets rid of the potential NULL
dereference.
By the way, this bug caused connect() to fail in obtaining an
address from DHCP. If I recall correctly from when I first debugged
this issue (late last year), I actually saw the initial value of the
stack pointer being used in the DHCP request as an IP address which
caused it to be rejected.