Allow overriding commands
cover Allow overriding commands
This patch series allows user-defined functions to hook commands by creating a function that is the same name as a command. Commands may be executed by prefixing the command name with the "builtin" keyword, as in bash. This allows functions to modify the invocation of commands. For instance, I use this to concatenate an extra initrd file to an existing one created by my OS, by hooking initrd and adding the extra file. As an aside, in the future, I'd like to dynamically generate this extra initrd file within GRUB.
This patch series is not "lockdown" aware. And I'm not entirely sure it needs to be, but I have a strong suspicion it does. Commands that are locked down obviously won't all of a sudden work. However, my concern is that this feature could prevent essential commands from running. Not being entirely familiar with secureboot, I don't feel I'm in the best position to judge the security implications . If it is determined that this feature does need to be lockdown aware, I believe its a fairly simple change, and can modify the patch series. I'll await feedback before I make that change, as it may be deemed unnecessary.
Glenn
Glenn Washburn (2):
script: Allow user-defined functions to override builtins
script: Add script language construct "builtin"
Makefile.util.def | 12 ++++++++++ grub-core/script/execute.c | 45 ++++++++++++++++++++++------------- tests/grub_cmd_builtin.in | 37 ++++++++++++++++++++++++++++ tests/grub_script_override.in | 33 +++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 tests/grub_cmd_builtin.in create mode 100644 tests/grub_script_override.in
Signature · 1 line
2.27.0
PATCH 1/2 script: Allow user-defined functions to override builtins
When sourcing a grub script file that you do not control, it can be useful to be able to hook in to certain built in commands to modify behavior of those built in commands.
This is a preparatory patch in that it does not provide access to overridden built in commands. So it is of limited value by itself.
--- Makefile.util.def | 6 ++++++ grub-core/script/execute.c | 22 ++++++++++++++-------- tests/grub_script_override.in | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 tests/grub_script_override.in
Makefile.util.def +6 -0
@@ -1031,6 +1031,12 @@ script = { common = tests/grub_script_setparams.in; }; +script = { + testcase; + name = grub_script_override; + common = tests/grub_script_override.in; +}; + script = { testcase; name = grub_script_return;
grub-core/script/execute.c +14 -8
@@ -984,14 +984,15 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) args = argv.args +2 ; cmdname = argv.args[1 ]; } - grubcmd =grub_command_find (cmdname); -if (! grubcmd) +/* Allow user functions to override built in commands. */ + func =grub_script_function_find (cmdname); +if (! func) { grub_errno =GRUB_ERR_NONE ; -/* It's not a GRUB command, try all functions. */ - func =grub_script_function_find (cmdname); -if (! func) +/* It's not a function, check if GRUB command. */ + grubcmd =grub_command_find (cmdname); +if (! grubcmd) {/* As a last resort, try if it is an assignment. */ char *assign =grub_strdup (cmdname);
@@ -1007,6 +1008,11 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) eq++;grub_script_env_set (assign, eq); } +else + { +/* Not an assignment, notify the user that the command was not found */ +grub_error (GRUB_ERR_UNKNOWN_COMMAND ,N_ ("can't find command `%s'" ), cmdname); + }grub_free (assign);grub_snprintf (errnobuf,sizeof (errnobuf),"%d" , grub_errno);
@@ -1020,7 +1026,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) }/* Execute the GRUB command or function. */ -if (grubcmd) +if (func) + ret =grub_script_function_call (func, argc, args); +else {if (grub_extractor_level && !(grubcmd->flags &GRUB_COMMAND_FLAG_EXTRACTOR ))
@@ -1033,8 +1041,6 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)else ret = (grubcmd->func) (grubcmd, argc, args); } -else - ret =grub_script_function_call (func, argc, args);if (invert) {
tests/grub_script_override.in new file +33 -0
@@ -0,0 +1,33 @@ +#! @builddir@/grub-shell-tester + +# Run GRUB script in a Qemu instance +# Copyright (C) 2010 Free Software Foundation, Inc. +# +# GRUB is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# GRUB is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GRUB. If not, see <http://www.gnu.org/licenses/>. + +echo override builtin function +function help { + echo "This is not the help you're looking for... $@" +} + +help +help module +help -module +help module1 module2 + +function insmod { + echo "Ignoring all module insertions: $@" +} + +insmod module1 module2
Signature · 1 line
2.27.0
PATCH 2/2 script: Add script language construct "builtin"
In reply to Glenn WashburnProvide a way to access builtin commands which have been hidden by a user- defined function of the same name.
--- Makefile.util.def | 6 ++++++ grub-core/script/execute.c | 27 ++++++++++++++++----------- tests/grub_cmd_builtin.in | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 tests/grub_cmd_builtin.in
Makefile.util.def +6 -0
@@ -1043,6 +1043,12 @@ script = { common = tests/grub_script_return.in; }; +script = { + testcase; + name = grub_cmd_builtin; + common = tests/grub_cmd_builtin.in; +}; + script = { testcase; name = grub_cmd_regexp;
grub-core/script/execute.c +16 -11
@@ -931,7 +931,7 @@ grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd) {struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd; -grub_command_t grubcmd; +grub_command_t grubcmd =0 ;grub_err_t ret =0 ;grub_script_function_t func =0 ;char errnobuf[18 ];
@@ -940,6 +940,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)unsigned int i;char **args;int invert; +int builtin;struct grub_script_argv argv = {0 ,0 ,0 };/* Lookup the command. */
@@ -966,11 +967,10 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) cmdstring[cmdlen -1 ] ='\0' ;grub_verify_string (cmdstring,GRUB_VERIFY_COMMAND );grub_free (cmdstring); - invert =0 ; - argc = argv.argc -1 ; - args = argv.args +1 ; cmdname = argv.args[0 ]; -if (grub_strcmp (cmdname,"!" ) ==0 ) + invert = !grub_strcmp (cmdname,"!" ); + builtin = !grub_strcmp (cmdname,"builtin" ); +if (invert || builtin) {if (argv.argc <2 || ! argv.args[1 ]) {
@@ -979,14 +979,19 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) N_("no command is specified")); } - invert =1 ; - argc = argv.argc -2 ; - args = argv.args +2 ; - cmdname = argv.args[1 ]; + argc = argv.argc - (1 + invert + builtin); + args = argv.args +1 + invert + builtin; + cmdname = argv.args[invert + builtin]; + } +else + { + argc = argv.argc -1 ; + args = argv.args +1 ; }/* Allow user functions to override built in commands. */ - func =grub_script_function_find (cmdname); -if (! func) +if (! builtin) + func =grub_script_function_find (cmdname); +if (! func || builtin) { grub_errno =GRUB_ERR_NONE ;
tests/grub_cmd_builtin.in new file +37 -0
@@ -0,0 +1,37 @@ +#! @BUILD_SHEBANG@ +set -e + +. "@builddir@/grub-core/modinfo.sh" + +function hook_insmod { + @builddir@/grub-shell <<EOF +insmod test +function insmod { + if [ "\$1" != "parttool" ]; then + builtin insmod "\$1" + fi +} +insmod "$1" +lsmod +EOF +} + +lsmod_original_file=`mktemp "${TMPDIR:-/tmp}/tmp.lsmod_original.XXXXXXXXXX"` || exit 1 +lsmod_hooked_file=`mktemp "${TMPDIR:-/tmp}/tmp.lsmod_hooked.XXXXXXXXXX"` || exit 1 + +echo 'insmod test; insmod luks; lsmod' | @builddir@/grub-shell >$lsmod_original_file +hook_insmod luks >$lsmod_hooked_file + +if ! cmp $lsmod_original_file $lsmod_hooked_file; then + echo "lsmod output not identical:" + diff -u $lsmod_original_file $lsmod_hooked_file + exit 1 +fi + + +hook_insmod parttool >$lsmod_hooked_file + +if grep -q ^parttool $lsmod_hooked_file; then + echo "parttool module was inserted, but should not have been" + exit 1 +fi
Signature · 1 line
2.27.0