Skip to content
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
merged 1 commit into from
Aug 16, 2013
Merged

Don't dereference NULL ipaddr in netif_set_ipaddr() #35

merged 1 commit into from
Aug 16, 2013

Conversation

adamgreen
Copy link
Contributor

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.

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()
@bogdanm bogdanm merged commit 73b1687 into ARMmbed:master Aug 16, 2013
@adamgreen adamgreen deleted the fixNullIpAddrBug branch August 16, 2013 08:22
@adamgreen
Copy link
Contributor Author

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
@OPpuolitaival OPpuolitaival mentioned this pull request Sep 1, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants