This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 2f2350513e0a50605596185daa90725e47096c76 Author: Marco Casaroli <[email protected]> AuthorDate: Mon Oct 12 05:32:29 2026 +0200 binfmt/script: Limit the number of nested interpreters. The interpreter of a script can be a script, which the handler loads through load_module() again. Nothing stopped this: a script that named itself as its interpreter recursed without end in the kernel, and the system stopped responding. Allow four levels, as Linux does, and fail with ELOOP after that. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- binfmt/script.c | 14 ++++++++++++++ include/nuttx/binfmt/binfmt.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/binfmt/script.c b/binfmt/script.c index 4e14af5fe0d..67a985fed15 100644 --- a/binfmt/script.c +++ b/binfmt/script.c @@ -44,6 +44,12 @@ #define SCRIPT_HEADER_MAX 128 +/* The interpreter of a script can be a script, up to four levels, as in + * Linux. + */ + +#define SCRIPT_DEPTH_MAX 4 + /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -188,6 +194,12 @@ static int script_loadbinary(FAR struct binary_s *binp, return -ENOEXEC; } + if (binp->scriptdepth >= SCRIPT_DEPTH_MAX) + { + berr("ERROR: Too many script interpreters for %s\n", filename); + return -ELOOP; + } + /* Terminate at newline */ end = strchr(header + 2, '\n'); @@ -255,7 +267,9 @@ static int script_loadbinary(FAR struct binary_s *binp, return ret; } + binp->scriptdepth++; ret = load_module(binp, interp, exports, nexports); + binp->scriptdepth--; if (ret < 0) { kmm_free(binp->argv); diff --git a/include/nuttx/binfmt/binfmt.h b/include/nuttx/binfmt/binfmt.h index 669357557a7..156dcac9afd 100644 --- a/include/nuttx/binfmt/binfmt.h +++ b/include/nuttx/binfmt/binfmt.h @@ -116,6 +116,9 @@ struct binary_s * example the interpreter of a * script, its argument and the * script. Freed by unload_module() */ +#ifdef CONFIG_BINFMT_SCRIPT + uint8_t scriptdepth; /* Scripts being loaded */ +#endif /* Unload module callback */
