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

eswifi: Fix possible buffer overflows #63074

Merged
merged 2 commits into from
Oct 10, 2023
Merged

Conversation

ceolin
Copy link
Member

@ceolin ceolin commented Sep 25, 2023

Fix buffer overflows when copying ssid.

Limit the number of the copied ssid to WIFI_SSID_MAX_LEN
and avoid a possible one byte overflow.

Signed-off-by: Flavio Ceolin <[email protected]>
d3zd3z
d3zd3z previously requested changes Sep 25, 2023
strcpy(status->ssid, sta->ssid);
status->ssid_len = strlen(sta->ssid);
status->ssid_len = strnlen(sta->ssid, WIFI_SSID_MAX_LEN);
strncpy(status->ssid, sta->ssid, sta->ssid_len);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost certainly not what we want to do, as the result will not ever have a nul termination on it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I saw several places assuming that the string may be not nul terminate and allocating WIFI_SSID_MAX_LEN + 1 to accommodate the \0 e.g:

shell_fprintf(sh, SHELL_NORMAL, "SSID: %-32s\n", status.ssid)

if (params->ssid_length > WIFI_SSID_MAX_LEN) {

other drivers too (e.g drivers/wifi/esp32/src/esp_wifi_drv.c)

       strncpy(status->ssid, data->status.ssid, WIFI_SSID_MAX_LEN);
       status->ssid_len = strnlen(data->status.ssid, WIFI_SSID_MAX_LEN);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory SSID can have \0 in it so using strncpy() will probably not work as expected. On the other hand I doubt anybody would use \0 in the SSID as it would make connecting quite difficult. So it would make sense to allocate one extra \0 byte at the end of SSID array to make sure the value is null terminated even if the SSID is max length (32 bytes).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the point is that upper layer is whether or not assuming that ssid is nul terminate ? I didn't dig too much, but my impression is that places manipulating struct wifi_iface_status.ssid are assuming that this is not nul terminate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that should be the case, we should not assume there is \0 at the end. I am just pondering here that it might make sense to add one at the end anyway (just in case).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without really now the context and reasons why they are not nul terminate, I'd say that generally speaking this can be a huge source of mistakes since most common APIs to deal with strings expects a nul terminator.
That's said, this is not particular to this driver, I think we should merge this fix (if it is correct)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loicpoulain , do you have any input about this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IEEE 802.11-2020: 9.4.22

When the UTF-8 SSID subfield of the Extended Capabilities element is equal to 1 in the frame that includes
the SSID element, or the Extended Capabilities of the source of the SSID information is known to include
the UTF-8 SSID capability based on a previously received Extended Capabilities element, the SSID is a
sequence of UTF-8 encoded code points. Otherwise, the character encoding of the octets in this SSID
element is unspecified.
NOTE—If the SSID is a sequence of UTF-8 encoded code points, a terminating null might or might not be present

TL;DR, Null termination of SSID isn't guaranteed, so, if the stored SSID is used as a string it should be ssid[33] to accomodate NULL character.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WiFi controller reports NULL terminated strings and we convert that into what the WiFi susbsystem is expecting, so... it seems ok to merge.

drivers/wifi/eswifi/eswifi_shell.c Outdated Show resolved Hide resolved
Limit the copied data to the buffer's size.

Signed-off-by: Flavio Ceolin <[email protected]>
@ceolin ceolin added the bug The issue is a bug, or the PR is fixing a bug label Oct 2, 2023
@ceolin ceolin added this to the v3.5.0 milestone Oct 2, 2023
strcpy(status->ssid, sta->ssid);
status->ssid_len = strlen(sta->ssid);
status->ssid_len = strnlen(sta->ssid, WIFI_SSID_MAX_LEN);
strncpy(status->ssid, sta->ssid, sta->ssid_len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loicpoulain , do you have any input about this?

@ceolin ceolin dismissed d3zd3z’s stale review October 9, 2023 19:38

the concern about non-null terminated strings was explained.

Copy link
Member

@jhedberg jhedberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jukkar this still needs your approval (as the assignee) before it can be merged

@carlescufi carlescufi merged commit ea109f6 into zephyrproject-rtos:main Oct 10, 2023
2 checks passed
@jhedberg
Copy link
Member

I'm seeing this CI failure in #63703

FAILED: zephyr/drivers/wifi/CMakeFiles/drivers__wifi.dir/eswifi/eswifi_core.c.obj 
ccache /opt/toolchains/zephyr-sdk-0.16.3/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -DCORE_CM4 -DHSE_VALUE=8000000 -DKERNEL -DMBEDTLS_CONFIG_FILE=\"config-tls-generic.h\" -DPICOLIBC_DOUBLE_PRINTF_SCANF -DSTM32L475xx -DUSE_FULL_LL_DRIVER -DUSE_HAL_DRIVER -D_FORTIFY_SOURCE=1 -D_POSIX_C_SOURCE=200809 -D__LINUX_ERRNO_EXTENSIONS__ -D__PROGRAM_START -D__ZEPHYR_SUPERVISOR__ -D__ZEPHYR__=1 -I/__w/zephyr/zephyr/subsys/net/ip -I/__w/zephyr/zephyr/include -I/__w/zephyr/zephyr/twister-out/disco_l475_iot1/samples/net/cloud/tagoio_http_post/sample.net.cloud.tagoio_http_post.wifi/zephyr/include/generated -I/__w/zephyr/zephyr/soc/arm/st_stm32/stm32l4 -I/__w/zephyr/zephyr/lib/posix/getopt/. -I/__w/zephyr/zephyr/drivers -I/__w/zephyr/zephyr/soc/arm/st_stm32/common -I/__w/zephyr/zephyr/subsys/net/lib/sockets/. -I/__w/zephyr/zephyr/subsys/net/lib/dns/. -I/__w/zephyr/modules/hal/cmsis/CMSIS/Core/Include -I/__w/zephyr/zephyr/modules/cmsis/. -I/__w/zephyr/modules/hal/stm32/stm32cube/stm32l4xx/soc -I/__w/zephyr/modules/hal/stm32/stm32cube/stm32l4xx/drivers/include -I/__w/zephyr/modules/hal/stm32/stm32cube/stm32l4xx/drivers/include/Legacy -I/__w/zephyr/modules/hal/stm32/stm32cube/common_ll/include -I/__w/zephyr/modules/crypto/mbedtls/include -I/__w/zephyr/zephyr/modules/mbedtls/configs -I/__w/zephyr/zephyr/modules/mbedtls/include -Wshadow -fno-strict-aliasing -Werror -Os -imacros /__w/zephyr/zephyr/twister-out/disco_l475_iot1/samples/net/cloud/tagoio_http_post/sample.net.cloud.tagoio_http_post.wifi/zephyr/include/generated/autoconf.h -fno-common -g -gdwarf-4 -fdiagnostics-color=always -mcpu=cortex-m4 -mthumb -mabi=aapcs -mfp16-format=ieee --sysroot=/opt/toolchains/zephyr-sdk-0.16.3/arm-zephyr-eabi/arm-zephyr-eabi -imacros /__w/zephyr/zephyr/include/zephyr/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-pic -fno-pie -fno-asynchronous-unwind-tables -ftls-model=local-exec -fno-reorder-functions --param=min-pagesize=0 -fno-defer-pop -fmacro-prefix-map=/__w/zephyr/zephyr/samples/net/cloud/tagoio_http_post=CMAKE_SOURCE_DIR -fmacro-prefix-map=/__w/zephyr/zephyr=ZEPHYR_BASE -fmacro-prefix-map=/__w/zephyr=WEST_TOPDIR -ffunction-sections -fdata-sections --specs=picolibc.specs -D_POSIX_THREADS -std=c99 -MD -MT zephyr/drivers/wifi/CMakeFiles/drivers__wifi.dir/eswifi/eswifi_core.c.obj -MF zephyr/drivers/wifi/CMakeFiles/drivers__wifi.dir/eswifi/eswifi_core.c.obj.d -o zephyr/drivers/wifi/CMakeFiles/drivers__wifi.dir/eswifi/eswifi_core.c.obj -c /__w/zephyr/zephyr/drivers/wifi/eswifi/eswifi_core.c
In file included from /opt/toolchains/zephyr-sdk-0.16.3/arm-zephyr-eabi/picolibc/include/string.h:215,
                 from /__w/zephyr/zephyr/include/zephyr/sys/mpsc_packet.h:9,
                 from /__w/zephyr/zephyr/include/zephyr/logging/log_msg.h:10,
                 from /__w/zephyr/zephyr/include/zephyr/logging/log_core.h:9,
                 from /__w/zephyr/zephyr/include/zephyr/logging/log.h:11,
                 from /__w/zephyr/zephyr/drivers/wifi/eswifi/eswifi_log.h:13,
                 from /__w/zephyr/zephyr/drivers/wifi/eswifi/eswifi_core.c:13:
/__w/zephyr/zephyr/drivers/wifi/eswifi/eswifi_core.c: In function 'eswifi_mgmt_iface_status':
/__w/zephyr/zephyr/drivers/wifi/eswifi/eswifi_core.c:494:45: error: 'struct eswifi_sta' has no member named 'ssid_len'
  494 |         strncpy(status->ssid, sta->ssid, sta->ssid_len);
      |                                             ^~
/__w/zephyr/zephyr/drivers/wifi/eswifi/eswifi_core.c:494:45: error: 'struct eswifi_sta' has no member named 'ssid_len'
  494 |         strncpy(status->ssid, sta->ssid, sta->ssid_len);
      |                                             ^~
[275/310] Linking C static library modules/mbedtls/libmbedTLSBase.a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Wi-Fi Wi-Fi bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants