[components][serial] Harden RT-Smart serial ioctl arguments - #11733
[components][serial] Harden RT-Smart serial ioctl arguments#11733BernardXiong wants to merge 3 commits into
Conversation
在 v1/v2 串口文件边界复制已知用户参数,补齐 v2 unread count 和 termios 缓冲区保护。 在 imx6ull UART 编程前校验波特率,并在硬件配置成功后提交串口状态。
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_driver_serial_v2Reviewers: @Ryan-CW-Code Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-08-23 11:32 CST)
📝 Review Instructions
|
|
There was a problem hiding this comment.
Pull request overview
Harden serial ioctl handling at RT-Smart user/kernel boundaries and validate UART configuration. / 加固 RT-Smart 串口 ioctl 的用户态/内核态边界并校验 UART 配置。
Changes:
- Marshal known v1/v2 ioctl payloads. / 封送已知的 v1/v2 ioctl 参数。
- Validate baud rates and defer configuration commits. / 校验波特率并延后配置提交。
- Reject user-space callback registration. / 拒绝用户态回调注册。
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
components/drivers/serial/dev_serial.c |
Adds v1 ioctl marshalling and configuration validation. |
components/drivers/serial/dev_serial_v2.c |
Adds v2 ioctl marshalling and safer configuration updates. |
bsp/nxp/imx/imx6ull-smart/drivers/drv_uart.c |
Rejects unsupported baud rates before register writes. |
Suppressed comments (2)
components/drivers/serial/dev_serial_v2.c:229
- 🟠 [Bug/错误]:
TCSETS*commands never reach the v2 termios setter /TCSETS*命令不会进入 v2 termios 设置逻辑
English: rt_serial_control() handles only TCSETA, TCSETAW, and TCSETAF; these TCSETS* commands fall through to the BSP control callback and return -RT_EINVAL on imx6ull-smart. Consequently Musl's tcsetattr() cannot configure this serial device. Add these aliases to the v2 termios setter cases.
中文:rt_serial_control() 仅处理 TCSETA、TCSETAW 和 TCSETAF;这些 TCSETS* 命令会落入 BSP control 回调,并在 imx6ull-smart 上返回 -RT_EINVAL。因此 Musl 的 tcsetattr() 无法配置该串口设备。请将这些别名加入 v2 termios 设置分支。
case TCSETS:
case TCSETSW:
case TCSETSF:
arg_size = sizeof(karg.termios);
kptr = &karg.termios;
components/drivers/serial/dev_serial.c:187
- 🔴 [Security/安全]:
TCGETAstill copies uninitialized kernel data /TCGETA仍会复制未初始化的内核数据
English: Although this boundary now uses a kernel buffer, the downstream v1 TCGETA handler declares an uninitialized struct termios tmp and _termios_to_termio() copies its unset c_line and c_cc fields into this output buffer. The subsequent copyout still leaks kernel stack bytes. Initialize that temporary structure before populating and converting it.
中文:虽然此边界现在使用内核缓冲区,但后续 v1 TCGETA 处理器声明了未初始化的 struct termios tmp,随后 _termios_to_termio() 将其中未设置的 c_line 和 c_cc 字段复制到该输出缓冲区。最终 copyout 仍会泄露内核栈字节。请在填充和转换前初始化该临时结构。
case TCGETA:
arg_size = sizeof(karg.termio);
kptr = &karg.termio;
copy_out = RT_TRUE;
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| #endif | ||
| rt_uint16_t oflag; | ||
| rt_size_t unread_bytes; | ||
| } karg; |
| rt_size_t unread_bytes; | ||
| rt_ssize_t unread_count; | ||
| rt_int32_t timeout; | ||
| } karg; |
| case FIONREAD: | ||
| arg_size = sizeof(karg.unread_bytes); | ||
| kptr = &karg.unread_bytes; | ||
| copy_out = RT_TRUE; |
| case FIONREAD: | ||
| arg_size = sizeof(karg.unread_bytes); | ||
| kptr = &karg.unread_bytes; | ||
| copy_out = RT_TRUE; |
| case TCGETA: | ||
| case TCGETS: | ||
| arg_size = sizeof(karg.termios); | ||
| kptr = &karg.termios; | ||
| copy_out = RT_TRUE; |
| case TCSETA: | ||
| case TCSETAW: | ||
| case TCSETAF: | ||
| arg_size = sizeof(karg.termio); | ||
| kptr = &karg.termio; | ||
| copy_in = RT_TRUE; |
| case TCSETA: | ||
| case TCSETAW: | ||
| case TCSETAF: |
|
Addressed the review findings in commit
The original PR validation limitation remains unchanged: the full imx6ull-smart build cannot enter compilation because the existing configuration selects Newlib while |
Description
Follow up on #11453 to harden the remaining RT-Smart serial ioctl paths.
RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNTand use the correctstruct termiosbuffer for v2 termios commands.Validation
arm-none-eabi-gcc -fsyntax-onlyfor v1/v2 serial sources with LWP disabled.arm-linux-musleabi-gcc -fsyntax-onlyfor v1/v2 serial sources and imx6ull UART driver with a temporary Musl/LWP configuration.components/lwp/lwp.h.Changes
components/drivers/serial/dev_serial.ccomponents/drivers/serial/dev_serial_v2.cbsp/nxp/imx/imx6ull-smart/drivers/drv_uart.c