diff mbox series

fix kernel menuconfig failure due to shell composition fault in cml1.bbclass

Message ID CAOj-5WA1uD=aTC19Osn4ZHf2-+AyYpXOTWtWxGeK_4O=rv2CAg@mail.gmail.com
State New
Headers show
Series fix kernel menuconfig failure due to shell composition fault in cml1.bbclass | expand

Commit Message

Sam Liddicott Sept. 12, 2024, 10:41 a.m. UTC
Function do_menuconfig in cml1.bbclass has a problem with the composition
of the shell command, manifest when KCONFIG_CONFIG_COMMAND contains " which
it often may, e.g.
    KCONFIG_CONFIG_COMMAND="menuconfig CC=\"ccache x86_64-sublime-linux-gcc
  -fuse-ld=bfd\" ..."

Without the fix, this command fails for me on kirkstone:
    bitbake linux-intel -c menuconfig

which generates a hidden error that requires a degree in archeology to
unearth:
    0: linux-intel-5.15.94+... do_menuconfig - 0s (pid 3056478)
    Trying to run: screen -r devshell_3056478
      GEN     Makefile
    ccache: unknown compiler
    scripts/Kconfig.include:44: Sorry, this compiler is not supported.
    make[1]: *** [.../kernel-source/scripts/kconfig/Makefile:48:
menuconfig] Error 1
    make: *** [.../kernel-source/Makefile:620: menuconfig] Error 2
    screen is terminating]

which is because the invoked command was effectively:
    make menuconfig "CC=ccache" x86_64-sublime-linux-gcc ...
and as extra args are ignored, it is the same as:
    make menuconfig "CC=ccache"

kernel menuconfig can succeed in some manner for other kernels and
architectures and open embedded releases, although it is still subject to
ignoring most of KCONFIG_CONFIG_COMMAND, perhaps depending whether or not a
kernel has previously been build (avoiding the need to invoke a broken
compiler for menuconfig)

As mentioned, the problem is that a " can terminate the value of the -c
argument in this line:
    oe_terminal("sh -c \"make %s; ....
causing the rest of KCONFIG_CONFIG_COMMAND to become additional (and
ignored) numeric arguments from $0 onwards.

This also means that the error handler "Command failed.' and 'Press any key
to continue..." doesn't work.

I attach three alternative patches, depending which approach the
maintainers want to take.

Patch 1 uses the shell to dynamically compose the shell command by means of
"$@" which is a great trick I've used for years when the arguments already
exist as a shell-safe list. This means that there is no need to escape the
make arguments.

Patch 2 uses shlex.quote which of course requires python >= 3.3 so one of
the other patches may be needed for some of older open embedded releases

Patch 3 uses .replace() and is likely inadequate but could be a preferred
starting point for maintainers if they don't like patch 1 and want to avoid
shlex in patch 2, but would rather roll their own.

Comments

Sam Liddicott Sept. 12, 2024, 11:41 a.m. UTC | #1
grovelling apologies, this was the wrong mailing list
diff mbox series

Patch

From 6fea0b20d62b7ccd844754aebef76bd675d72165 Mon Sep 17 00:00:00 2001
From: Sam Liddicott <sam@liddicott.com>
Date: Thu, 12 Sep 2024 10:09:37 +0100
Subject: escape " in KCONFIG_CONFIG_COMMAND for do_menuconfig

The problem with composing shell frgaments in python like this:
    oe_terminal("sh -c \"make %s ..." % args ...
is that if args contains " this can terminate the command that is
passed to sh -c, and the remainder of args will become additional
arguments to the shell command, available as $0 onwards.

That also has the effect of breaking the "Command failed" and
the "Press any key to continue..." messages.

This patch escapes all " and \ in args with a backslash, suitable
for composing with python.
    oe_terminal("sh -c \"make %s ..." % args.replace("\"", "\\\"") ...
all instances of " become \" in the command invocation.

However those escapes may be buggy, and I'm not confident that they
are sufficient.

---
 meta/classes/cml1.bbclass | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meta/classes/cml1.bbclass b/meta/classes/cml1.bbclass
index d319d66ab2..f6727c5c33 100644
--- a/meta/classes/cml1.bbclass
+++ b/meta/classes/cml1.bbclass
@@ -48,7 +48,7 @@  python do_menuconfig() {
     # ensure that environment variables are overwritten with this tasks 'd' values
     d.appendVar("OE_TERMINAL_EXPORTS", " PKG_CONFIG_DIR PKG_CONFIG_PATH PKG_CONFIG_LIBDIR PKG_CONFIG_SYSROOT_DIR")
 
-    oe_terminal("sh -c \"make %s; if [ \\$? -ne 0 ]; then echo 'Command failed.'; printf 'Press any key to continue... '; read r; fi\"" % d.getVar('KCONFIG_CONFIG_COMMAND'),
+    oe_terminal("sh -c \"make %s; if [ \\$? -ne 0 ]; then echo 'Command failed.'; printf 'Press any key to continue... '; read r; fi\"" % d.getVar('KCONFIG_CONFIG_COMMAND').replace("\\", "\\\\").replace("\"", "\\\""),
                 d.getVar('PN') + ' Configuration', d)
 
     # FIXME this check can be removed when the minimum bitbake version has been bumped
-- 
2.43.0