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

apps/system: replace CONFIG_NSH_LINELEN to LINE_MAX #2918

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

anchao
Copy link
Contributor

@anchao anchao commented Dec 27, 2024

Summary

  1. apps/system: replace CONFIG_NSH_LINELEN to LINE_MAX

Applications should not depend on any properties of nshlib

Signed-off-by: chao an anchao@lixiang.com

  1. nshlib/nshline: move all temp line buffer to heap

Signed-off-by: chao an anchao@lixiang.com

  1. nshlib/traceline: move traceline buffer from stack to heap

Signed-off-by: chao an anchao@lixiang.com

Impact

N/A

Testing

sim/nsh ostest

Signed-off-by: chao an <anchao@lixiang.com>
Signed-off-by: chao an <anchao@lixiang.com>
Signed-off-by: chao an <anchao@lixiang.com>
Applications should not depend on any properties of nshlib

Signed-off-by: chao an <anchao@lixiang.com>
@nuttxpr
Copy link

nuttxpr commented Dec 27, 2024

[Experimental Bot, please feedback here]

No, this PR does not fully meet the NuttX requirements. While it provides a summary of changes, it lacks crucial details.

Here's a breakdown of what's missing:

  • Insufficient Summary: The summary lists what was changed but not why. It needs to explain the rationale behind replacing CONFIG_NSH_LINELEN and moving buffers to the heap. What problem does this solve? What are the benefits? There's no mention of a related NuttX issue.
  • Missing Impact Assessment: Simply stating "N/A" for impact is insufficient. Even seemingly small changes can have unforeseen consequences. The PR needs to explicitly address each impact area (user, build, hardware, documentation, security, compatibility). For example, moving buffers to the heap could impact memory usage. This needs to be acknowledged and discussed.
  • Inadequate Testing Information: "sim/nsh ostest" is not enough. The PR needs to specify the host operating system, CPU architecture, and compiler used for testing. It also needs to specify the target architecture and board configuration used with the simulator. Most importantly, it's missing the actual testing logs before and after the change, which are crucial for demonstrating that the changes work as intended and don't introduce regressions.

Example of how to improve the PR:

Summary:

This PR addresses potential buffer overflows and improves portability in nshlib and related applications. Applications were depending on the internal CONFIG_NSH_LINELEN setting from nshlib, which is not ideal for modularity. This PR replaces the use of CONFIG_NSH_LINELEN with the standard LINE_MAX define in applications. Additionally, temporary line buffers in nshlib/nshline and nshlib/traceline are moved from the stack to the heap. This prevents potential stack overflows when processing very long lines and improves the robustness of nshlib.

Related NuttX Issue: (If one exists, link it here)

Impact:

  • Impact on user: YES. Applications using CONFIG_NSH_LINELEN will need to be recompiled. This should be a straightforward change, as LINE_MAX is a standard define.
  • Impact on build: NO.
  • Impact on hardware: NO.
  • Impact on documentation: YES. Documentation should be updated to reflect the removal of the CONFIG_NSH_LINELEN dependency and the change to heap-allocated buffers. (Indicate if documentation changes are included in the PR).
  • Impact on security: YES. This change mitigates potential stack overflow vulnerabilities when processing long lines.
  • Impact on compatibility: Potentially backwards compatible with applications that don't use extremely long lines. However, applications directly relying on CONFIG_NSH_LINELEN will need modification.
  • Anything else to consider: The change to heap-allocated buffers might slightly increase heap memory usage.

Testing:

  • Build Host: Linux (Ubuntu 20.04), x86_64, GCC 9.4.0
  • Target: sim:qemu-x86_64:nsh

Testing logs before change:

(Paste actual logs here showing the problem being addressed)

Testing logs after change:

(Paste actual logs here demonstrating the correct behavior after the change)

#if (!defined(CONFIG_NSH_DISABLE_MEMDUMP) && defined(NSH_HAVE_WRITEFILE)) || \
!defined(CONFIG_NSH_DISABLEBG) || defined(CONFIG_NSH_PIPELINE) || \
!defined(CONFIG_NSH_DISABLE_WATCH)
char templine[CONFIG_NSH_LINELEN];
Copy link
Contributor

Choose a reason for hiding this comment

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

but this will increase the heap consumption for the temp use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Compared with irregular heap access, shared template and will make it more deterministic

Copy link
Contributor

Choose a reason for hiding this comment

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

could you split the rename from moving buffer to nsh_vtbl?

Copy link
Contributor

Choose a reason for hiding this comment

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

the temp buffer is only used in few command, why move it to nsh_vtbl to extend the life to the whole session?

/* Trace line buffer */

#ifdef CONFIG_SCHED_INSTRUMENTATION_DUMP
char traceline[CONFIG_NSH_LINELEN];
Copy link
Contributor

Choose a reason for hiding this comment

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

but traceline is used only when launching program, isn't good to keep the buffer in the whole nsh life cycle

#if (!defined(CONFIG_NSH_DISABLE_MEMDUMP) && defined(NSH_HAVE_WRITEFILE)) || \
!defined(CONFIG_NSH_DISABLEBG) || defined(CONFIG_NSH_PIPELINE) || \
!defined(CONFIG_NSH_DISABLE_WATCH)
char templine[CONFIG_NSH_LINELEN];
Copy link
Contributor

Choose a reason for hiding this comment

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

the temp buffer is only used in few command, why move it to nsh_vtbl to extend the life to the whole session?

@@ -380,7 +380,7 @@
/* Maximum size of one command line (telnet or serial) */

#ifndef CONFIG_NSH_LINELEN
Copy link
Contributor

Choose a reason for hiding this comment

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

let's remove CONFIG_NSH_LINELEN directly

Copy link

@cederom cederom left a comment

Choose a reason for hiding this comment

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

Thank you @anchao :-) My questions:

  1. Where LINE_MAX should be used and where CONFIG_NSH_LINELEN?
  2. Where is LINE_MAX defined?
  3. Wouldn't that create inconsistency?
  4. This seems to require a documentation update? Or at least mention on the change and what/where is preferred in regard to above two questions?

@cederom cederom requested a review from patacongo January 2, 2025 20:44
@xiaoxiang781216
Copy link
Contributor

Thank you @anchao :-) My questions:

  1. Where LINE_MAX should be used and where CONFIG_NSH_LINELEN?

Yes, the patch plan to replace all XXX_LINELEN to LINE_MAX.

  1. Where is LINE_MAX defined?

here: apache/nuttx#15344
LINE_MAX is POSIX defined macro.

  1. Wouldn't that create inconsistency?

no, it improves the consistency, since all places will use the standard macro defined by POSIX.

  1. This seems to require a documentation update? Or at least mention on the change and what/where is preferred in regard to above two questions?

@patacongo
Copy link
Contributor

3. Where is LINE_MAX defined?

here: apache/nuttx#15344 LINE_MAX is POSIX defined macro.

Specifically, limits.h: https://pubs.opengroup.org/onlinepubs/7908799/xsh/limits.h.html

Minimum value is _POSIX2_LINE_MAX

@cederom
Copy link

cederom commented Jan 3, 2025

  1. Where is LINE_MAX defined?

here: apache/nuttx#15344 LINE_MAX is POSIX defined macro.

Specifically, limits.h: https://pubs.opengroup.org/onlinepubs/7908799/xsh/limits.h.html

Minimum value is _POSIX2_LINE_MAX

Yup, and the latest one https://pubs.opengroup.org/onlinepubs/9799919799/ :D

Thank you @patacongo and @xiaoxiang781216 :-)

Copy link

@cederom cederom left a comment

Choose a reason for hiding this comment

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

Thank you @anchao :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants