diff mbox series

fully fix kernel menuconfig shell composition fault in cml1.bbclass

Message ID CAOj-5WCRbrpNsHMCox8k-sc3Dr604-pVTt5=iDjaXhf85Ha_Dg@mail.gmail.com
State New
Headers show
Series fully fix kernel menuconfig shell composition fault in cml1.bbclass | expand

Commit Message

Sam Liddicott Sept. 12, 2024, 4:23 p.m. UTC
I mistakenly posted bad patches to bitbake-devel earlier today.

These are 2 alternative patches (pick only one). They apply on master
branch but should backport easily. I tested them and generated them on
kirkstone (and then updated the path and checked application on master).

This is a full fix for a problem partially addressed by Jaeyoon Jung (
jaeyoon.jung@lge.com Thu Sep 21 17:27:10 2023 +0200
https://lists.openembedded.org/g/openembedded-core/message/186885)

That fix swapped " for ' which instead causes the failure to occur when
KCONFIG_CONFIG_COMMAND contained ' (single quotes) whereas before it
occurred when KCONFIG_CONFIG_COMMAND contained " (double quotes).

The problem only arises because of the extra "sh -c" needed in cml1.bbclass
to execute multiple shell statements, and because of the existence of a
prepended "exec" by terminal.bbclass function emit_terminal_func.

This means that the implicitly shell-ready KCONFIG_CONFIG_COMMAND needs
escaping for insertion into the extra "sh -c". Although cml1.bbclass
escaped the shell code in the source, the KCONFIG_CONFIG_COMMAND was not
escaped, and so either " or ' was going to fall victim at some point.

These two alternative patches directly address the need to escape
KCONFIG_CONFIG_COMMAND.

The first patch is the simplest, and causes KCONFIG_CONFIG_COMMAND to be
passed as additional arguments to the inner "sh -c", entirely avoiding the
need to shell escape, on this principle:

    -"sh -c 'make %s ...'" % ${KCONFIG_CONFIG_COMMAND}"
    +"sh -c 'make \"$@\" ...' dummy-arg0 %s" % ${KCONFIG_CONFIG_COMMAND}

The second fix uses shlex.quote (suitable for python 3.3 or above) to quote
the entire argument to "sh -c" including KCONFIG_CONFIG_COMMAND, avoiding
also the need to manually escape any of the nested shell.

I think that the first fix is more portable, but they both require scrutiny
to ensure fixing the problem properly is worth the trouble.

For application in kirkstone and some
others, meta/classes-recipe/cml1.bbclass needs changing to
meta/classes/cml1.bbclass

I know that we have a good-enough-for-now fix from Jaeyoon Jung, so this
isn't urgent, and maybe not even important.

Sam

Comments

Sam Liddicott Sept. 13, 2024, 9:51 p.m. UTC | #1
TL;DR: Applying the Jaeyoon Jung patch (with fixups) to dunfell or thud
does not fix the core problem but breaks even more (thud and dunfell use '
), but the attached patches do.

I'm testing with extra config:
    KCONFIG_CONFIG_COMMAND_prepend=' X="1 2" '
    KCONFIG_CONFIG_COMMAND_prepend=" Y='a b' "
and then:
    bitbake virtual/kernel -c menuconfig

The result can then be examined by using ps to locate the make process, and
then
    tr '\0' '\n' < /proc/$PID/cmdline
and each line will show one of the "make" arguments, and it is easy to tell
whether multi-word arguments have been properly preserved and whether any
are missing.

I've also tested by adding a false target to KCONFIG_CONFIG_COMMAND
    KCONFIG_CONFIG_COMMAND_prepend=' zox X="1 2" '
    KCONFIG_CONFIG_COMMAND_prepend=" Y='a b' "
and ensured that it properly fails with:
    Command failed.
    Press any key to continue...

The biggest surprise was that without any patch, my
extra KCONFIG_CONFIG_COMMAND debug lines causes menuconfig to make the
kernel and skip menuconfig (because the command line was truncated), for
both thud and dunfell.

On thud and dunfell, a patch initially appears un-needed because (at least
in my setup) thud is using single quotes for KCONFIG_CONFIG_COMMAND:
    menuconfig HOSTLDFLAGS='-L.../...'
and so double quotes don't exist to terminate that string early. Of course,
that means they would both be broken by the original patch, or if double
quotes were used in a custom configuration.

I've attached a patch suitable for applying to thud, and another for
dunfell -- so that single or double quotes can be used.

Sam
diff mbox series

Patch

From a96f41f6d6f0d8308d74a74d736d5a35dc54f0fa Mon Sep 17 00:00:00 2001
From: Sam Liddicott <sam@liddicott.com>
Date: Thu, 12 Sep 2024 17:18:37 +0100
Subject: use shlex.quote on KCONFIG_CONFIG_COMMAND for do_menuconfig

Although KCONFIG_CONFIG_COMMAND is shell-ready, it needs escaping
when composed as part of some shell which is then embedded into an
outer "sh -c" in cml1.bbclass

This patch uses shlex.quote to escape the entire shell fragment
when it is composed in the inner "sh -c"

Signed-off-by: Sam Liddicott <sam@liddicott.com>
---
 meta/classes-recipe/cml1.bbclass | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meta/classes-recipe/cml1.bbclass b/meta/classes-recipe/cml1.bbclass
index d319d66ab2..f6727c5c33 100644
--- a/meta/classes-recipe/cml1.bbclass
+++ b/meta/classes-recipe/cml1.bbclass
@@ -36,4 +36,5 @@  KCONFIG_CONFIG_ENABLE_MENUCONFIG ??= "true"
 KCONFIG_CONFIG_ROOTDIR ??= "${B}"
 python do_menuconfig() {
     import shutil
+    import shlex
 
@@ -48,5 +49,5 @@  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 %s' % shlex.quote("make %s; if [ $? -ne 0 ]; then echo 'Command failed.'; printf 'Press any key to continue... '; read r; fi" % d.getVar('KCONFIG_CONFIG_COMMAND')),
                 d.getVar('PN') + ' Configuration', d)
-- 
2.43.0