From: Ahmad Fatoum <[email protected]>

Security policies will normally be selected after consulting efuses,
secure boot status from the EEPROM or unlock tokens.

For easier experimentation in QEMU, allow setting the security policy
via the command line arguments, e.g.:

  pytest --bootarg barebox.security.policy=lockdown

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 common/boards/qemu-virt/Makefile      |  2 +-
 common/boards/qemu-virt/board.c       |  3 ++
 common/boards/qemu-virt/commandline.c | 74 +++++++++++++++++++++++++++++++++++
 common/boards/qemu-virt/commandline.h |  9 +++++
 test/arm/virt32_secure_defconfig.yaml |  1 +
 5 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/common/boards/qemu-virt/Makefile b/common/boards/qemu-virt/Makefile
index 
2caa6a20c522ac68fd629f38e51fdf1423db4b09..7e1440aecff08942269d60f5d221fc4e69e95ea6
 100644
--- a/common/boards/qemu-virt/Makefile
+++ b/common/boards/qemu-virt/Makefile
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
-obj-y += board.o
+obj-y += board.o commandline.o
 obj-y += qemu-virt-flash.dtbo.o fitimage-pubkey.dtb.o
 ifeq ($(CONFIG_RISCV),y)
 DTC_CPP_FLAGS_qemu-virt-flash.dtbo := -DCONFIG_RISCV
diff --git a/common/boards/qemu-virt/board.c b/common/boards/qemu-virt/board.c
index 
6f88f24b0690c2562b3b3718a56c9f5c46a4455a..6ad35421892703eea32a36a913bc92dbb44acc14
 100644
--- a/common/boards/qemu-virt/board.c
+++ b/common/boards/qemu-virt/board.c
@@ -9,6 +9,7 @@
 #include <deep-probe.h>
 #include <security/policy.h>
 #include "qemu-virt-flash.h"
+#include "commandline.h"
 
 #ifdef CONFIG_64BIT
 #define MACHINE "virt64"
@@ -91,6 +92,8 @@ static int virt_board_driver_init(void)
         * so the test suite can exercise CONFIG_SECURITY_POLICY_PATH.
         */
 
+       qemu_virt_parse_commandline(root);
+
        return 0;
 }
 postcore_initcall(virt_board_driver_init);
diff --git a/common/boards/qemu-virt/commandline.c 
b/common/boards/qemu-virt/commandline.c
new file mode 100644
index 
0000000000000000000000000000000000000000..16e4750e123dee69c612de52c855889372f2cbc3
--- /dev/null
+++ b/common/boards/qemu-virt/commandline.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#define pr_fmt(fmt) "qemu-virt-commandline: " fmt
+
+#include <linux/parser.h>
+#include <of.h>
+#include <string.h>
+#include <security/policy.h>
+#include <xfuncs.h>
+#include <stdio.h>
+#include "commandline.h"
+
+enum {
+       /* String options */
+       Opt_policy,
+       /* Error token */
+       Opt_err
+};
+
+static const match_table_t tokens = {
+       {Opt_policy, "barebox.security.policy=%s"},
+       {Opt_err, NULL}
+};
+
+int qemu_virt_parse_commandline(struct device_node *np)
+{
+       const char *bootargs;
+       char *p, *options, *tmp_options, *policy = NULL;
+       substring_t args[MAX_OPT_ARGS];
+       int ret;
+
+       np = of_get_child_by_name(np, "chosen");
+       if (!np)
+               return -ENOENT;
+
+       ret = of_property_read_string(np, "bootargs", &bootargs);
+       if (ret < 0)
+               return 0;
+
+       options = tmp_options = xstrdup(bootargs);
+
+       while ((p = strsep(&options, " ")) != NULL) {
+               int token;
+
+               if (!*p)
+                       continue;
+
+               token = match_token(p, tokens, args);
+               switch (token) {
+               case Opt_policy:
+                       if (!IS_ENABLED(CONFIG_SECURITY_POLICY)) {
+                               pr_err("CONFIG_SECURITY_POLICY support is 
missing\n");
+                               continue;
+                       }
+
+                       policy = match_strdup(&args[0]);
+                       if (!policy) {
+                               ret = -ENOMEM;
+                               goto out;
+                       }
+                       ret = security_policy_select(policy);
+                       if (ret)
+                               goto out;
+               default:
+                       continue;
+               }
+       }
+
+       ret = 0;
+out:
+       free(policy);
+       free(tmp_options);
+       return ret;
+}
diff --git a/common/boards/qemu-virt/commandline.h 
b/common/boards/qemu-virt/commandline.h
new file mode 100644
index 
0000000000000000000000000000000000000000..8759784e07c57e3492dbabaa8ab9b4d50cc6f73a
--- /dev/null
+++ b/common/boards/qemu-virt/commandline.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef QEMU_VIRT_COMMANDLINE_H_
+#define QEMU_VIRT_COMMANDLINE_H_
+
+struct device_node;
+
+int qemu_virt_parse_commandline(struct device_node *root);
+
+#endif
diff --git a/test/arm/virt32_secure_defconfig.yaml 
b/test/arm/virt32_secure_defconfig.yaml
index 
618cb6a0fb05a4703c1fe25e159a257ed775d7c8..a1537c634811d10957b7fd0cc49d6b66c1b80e06
 100644
--- a/test/arm/virt32_secure_defconfig.yaml
+++ b/test/arm/virt32_secure_defconfig.yaml
@@ -7,6 +7,7 @@ targets:
         cpu: cortex-a7
         memory: 1024M
         kernel: barebox-dt-2nd.img
+        boot_args: barebox.security.policy=devel
         display: qemu-default
       BareboxDriver:
         prompt: 'barebox@[^:]+:[^ ]+ '

-- 
2.39.5


Reply via email to