From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 2/5] hush: export source_script()
Date: Fri, 30 Oct 2026 15:13:29 +0100 [thread overview]
Message-ID: <20261030-devboot-v1-2-ab9dd3a92c66@pengutronix.de> (raw)
In-Reply-To: <20261030-devboot-v1-0-ab9dd3a92c66@pengutronix.de>
C code that wants to source a shell script has to go through
run_command("source ..."), which parses the command line and looks the
script up in the PATH.
Export source_script() instead. As the simple parser can't source
scripts, it gets a stub returning -ENOSYS. Unlike its callers in hush,
source_script() can now be called directly, so check the security policy
in it, like run_command() and execute_script() do.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
common/hush.c | 17 +++++++++++++++--
include/shell.h | 8 ++++++++
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/common/hush.c b/common/hush.c
index ac885fbfe7..c5809b32cd 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -329,7 +329,6 @@ static char **make_list_in(char **inp, char *name);
static char *insert_var_value(char *inp);
static int set_local_var(const char *s, int flg_export);
static int execute_script(const char *path, int argc, char *argv[]);
-static int source_script(const char *path, int argc, char *argv[]);
static int b_check_space(o_string *o, int len)
{
@@ -1975,12 +1974,26 @@ static int execute_script(const char *path, int argc, char *argv[])
return ret;
}
-static int source_script(const char *path, int argc, char *argv[])
+/**
+ * source_script - execute a shell script in the current environment
+ * @path: path of the script
+ * @argc: number of positional parameters, including $0
+ * @argv: positional parameters, starting with $0
+ *
+ * Unlike executing a script, variables set by it remain set in the current
+ * environment context after it returns.
+ *
+ * Return: exit status of the last command executed, or a negative error code
+ */
+int source_script(const char *path, int argc, char *argv[])
{
struct p_context ctx = {};
char *script;
int ret;
+ if (!IS_ALLOWED(SCONFIG_SHELL))
+ return -EPERM;
+
initialize_context(&ctx, true);
ctx.global_argc = argc;
diff --git a/include/shell.h b/include/shell.h
index b399564ff3..a0c3414fa6 100644
--- a/include/shell.h
+++ b/include/shell.h
@@ -6,17 +6,25 @@
#ifndef __SHELL_H__
#define __SHELL_H__
+#include <linux/errno.h>
+
int shell_get_last_return_code(void);
int run_shell(void);
#ifdef CONFIG_SHELL_HUSH
char *shell_expand(char *str);
+int source_script(const char *path, int argc, char *argv[]);
#else
static inline char *shell_expand(char *str)
{
return strdup(str);
}
+
+static inline int source_script(const char *path, int argc, char *argv[])
+{
+ return -ENOSYS;
+}
#endif
#ifdef CONFIG_BAREBOX_CMDLINE
--
2.47.3
next prev parent reply other threads:[~2026-10-30 14:15 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 14:13 [PATCH 0/5] boot: implement devboot in C Sascha Hauer
2026-10-30 14:13 ` [PATCH 1/5] boot: report the exit status of failing boot scripts Sascha Hauer
2026-10-30 14:13 ` Sascha Hauer [this message]
2026-10-30 14:13 ` [PATCH 3/5] boot: hand overrides on to nested boots and the bootm command Sascha Hauer
2026-10-30 14:13 ` [PATCH 4/5] boot: implement devboot in C Sascha Hauer
2026-10-30 14:13 ` [PATCH 5/5] sandbox: defconfig: enable boot overrides and devboot Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261030-devboot-v1-2-ab9dd3a92c66@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox