Skip to contentSkip to conversation

Add lockdown mode

PATCH In review Kevin Lampis 3 participants 12 messages May 6, 2025 – May 13, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 3/4 Add lockdown mode

The intention of lockdown mode is to prevent attacks from a rogue dom0 userspace from compromising the system. Lockdown mode can be controlled by a Kconfig option and a command-line parameter. It is also enabled automatically when Secure Boot is enabled and it cannot be disabled in that case.

---
 xen/arch/x86/setup.c       |  1 +
 xen/common/Kconfig         |  8 ++++++
 xen/common/Makefile        |  1 +
 xen/common/kernel.c        |  3 +++
 xen/common/lockdown.c      | 52 ++++++++++++++++++++++++++++++++++++++
 xen/include/xen/lockdown.h |  9 +++++++
 6 files changed, 74 insertions(+)
 create mode 100644 xen/common/lockdown.c
 create mode 100644 xen/include/xen/lockdown.h
xen/arch/x86/setup.c +1 -0
index 2518954124..276957c4ed 100644
@@ -15,6 +15,7 @@
 #include <xen/kexec.h>
 #include <xen/keyhandler.h>
 #include <xen/lib.h>
+#include <xen/lockdown.h>
 #include <xen/multiboot.h>
 #include <xen/nodemask.h>
 #include <xen/numa.h>
xen/common/Kconfig +8 -0
index 4bec78c6f2..63ff37d046 100644
@@ -596,4 +596,12 @@ config BUDDY_ALLOCATOR_SIZE
 	  Amount of memory reserved for the buddy allocator to serve Xen heap,
 	  working alongside the colored one.
 
+config LOCKDOWN_DEFAULT
+	bool "Enable lockdown mode by default"
+	default n
+	help
+	  Lockdown mode prevents attacks from a rogue dom0 userspace from
+	  compromising the system. This is automatically enabled when Secure
+	  Boot is enabled.
+
 endmenu
xen/common/Makefile +1 -0
index 98f0873056..b00a8a925a 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC) += kimage.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o
 obj-$(CONFIG_LLC_COLORING) += llc-coloring.o
+obj-y += lockdown.o
 obj-$(CONFIG_VM_EVENT) += mem_access.o
 obj-y += memory.o
 obj-y += multicall.o
xen/common/kernel.c +3 -0
index 8b63ca55f1..6658db9514 100644
@@ -216,6 +216,9 @@ static void __init _cmdline_parse(const char *cmdline)
  */
 void __init cmdline_parse(const char *cmdline)
 {
+    /* Call this early since it affects command-line parsing */
+    lockdown_init(cmdline);
+
     if ( opt_builtin_cmdline[0] )
     {
         printk("Built-in command line: %s\n", opt_builtin_cmdline);
xen/common/lockdown.c new file +52 -0

52 lines not on this page · raw message

xen/include/xen/lockdown.h new file +9 -0
new file mode 100644
index 0000000000..b2baa31caa
@@ -0,0 +1,9 @@
+#ifndef XEN__LOCKDOWN_H
+#define XEN__LOCKDOWN_H
+
+#include <xen/types.h>
+
+bool is_locked_down(void);
+void lockdown_init(const char *cmdline);
+
+#endif /* XEN__LOCKDOWN_H */
Signature · 1 line
2.42.0
#1 lore raw

Hello Kevin,

Le 06/05/2025 à 18:32, Kevin Lampis a écrit :

Quoted 3 lines

The intention of lockdown mode is to prevent attacks from a rogue dom0 userspace from compromising the system. Lockdown mode can be controlled by a Kconfig option and a command-line parameter.

What is the effective effect of such mode ? How does it protect the hypervisor from Dom0 ?
(I can't find the PATCH 4/4 which seems to give a explanation)

It is also enabled automatically

Quoted 118 lines

when Secure Boot is enabled and it cannot be disabled in that case.

---
  xen/arch/x86/setup.c       |  1 +
  xen/common/Kconfig         |  8 ++++++
  xen/common/Makefile        |  1 +
  xen/common/kernel.c        |  3 +++
  xen/common/lockdown.c      | 52 ++++++++++++++++++++++++++++++++++++++
  xen/include/xen/lockdown.h |  9 +++++++
  6 files changed, 74 insertions(+)

create mode 100644 xen/common/lockdown.c
create mode 100644 xen/include/xen/lockdown.h

xen/arch/x86/setup.c +1 -0
index 2518954124..276957c4ed 100644
@@ -15,6 +15,7 @@
  #include <xen/kexec.h>
  #include <xen/keyhandler.h>
  #include <xen/lib.h>
+#include <xen/lockdown.h>
  #include <xen/multiboot.h>
  #include <xen/nodemask.h>
  #include <xen/numa.h>
xen/common/Kconfig +8 -0
index 4bec78c6f2..63ff37d046 100644
@@ -596,4 +596,12 @@ config BUDDY_ALLOCATOR_SIZE
  	  Amount of memory reserved for the buddy allocator to serve Xen heap,
  	  working alongside the colored one.
 
+config LOCKDOWN_DEFAULT
+	bool "Enable lockdown mode by default"
+	default n
+	help
+	  Lockdown mode prevents attacks from a rogue dom0 userspace from
+	  compromising the system. This is automatically enabled when Secure
+	  Boot is enabled.
+
  endmenu
xen/common/Makefile +1 -0
index 98f0873056..b00a8a925a 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_KEXEC) += kexec.o
  obj-$(CONFIG_KEXEC) += kimage.o
  obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o
  obj-$(CONFIG_LLC_COLORING) += llc-coloring.o
+obj-y += lockdown.o
  obj-$(CONFIG_VM_EVENT) += mem_access.o
  obj-y += memory.o
  obj-y += multicall.o
xen/common/kernel.c +3 -0
index 8b63ca55f1..6658db9514 100644
@@ -216,6 +216,9 @@ static void __init _cmdline_parse(const char *cmdline)
   */
  void __init cmdline_parse(const char *cmdline)
  {
+    /* Call this early since it affects command-line parsing */
+    lockdown_init(cmdline);
+
      if ( opt_builtin_cmdline[0] )
      {
          printk("Built-in command line: %s\n", opt_builtin_cmdline);
xen/common/lockdown.c new file +42 -0
new file mode 100644
index 0000000000..935911dfd0
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/efi.h>
+#include <xen/kernel.h>
+#include <xen/lockdown.h>
+#include <xen/param.h>
+#include <xen/string.h>
+
+static bool __ro_after_init lockdown = IS_ENABLED(CONFIG_LOCKDOWN_DEFAULT);
+ignore_param("lockdown");
+
+bool is_locked_down(void)
+{
+    return lockdown;
+}
+
+void __init lockdown_init(const char *cmdline)
+{
+    if ( efi_secure_boot )
+    {
+        printk("Enabling lockdown mode because Secure Boot is enabled\n");
+        lockdown = true;
+    }
+    else
+    {
+        while ( *cmdline )
+        {
+            size_t param_len, name_len;
+            int ret;
+
+            cmdline += strspn(cmdline, " \n\r\t");
+            param_len = strcspn(cmdline, " \n\r\t");
+            name_len = strcspn(cmdline, "= \n\r\t");
+
+            if ( !strncmp(cmdline, "lockdown", max(name_len, strlen("lockdown"))) ||
+                 !strncmp(cmdline, "no-lockdown", max(name_len, strlen("no-lockdown"))) )
+            {
+                ret = parse_boolean("lockdown", cmdline, cmdline + param_len);
+                if ( ret >= 0 )
+                {
+                    lockdown = ret;
+                    printk("Lockdown mode set from command-line\n");

xen/common/lockdown.c line 52

Quoted 10 lines
xen/common/lockdown.c new file +10 -0
+                    break;
+                }
+            }
+
+            cmdline += param_len;
+        }
+    }
+
+    printk("Lockdown mode is %s\n", lockdown ? "enabled" : "disabled");
+}

With

Quoted 2 lines

Kevin Lampis (1):
Disallow most command-line options when lockdown mode is enabled

I am not convinced of the efficiency of being able to toggle lockdown (including disabling it) mode from command-line. In case the userland can hijack the cmdline, I can't see what would prevent it from setting no-lockdown, which will disable the lockdown mode (also overriding CONFIG_LOCKDOWN_DEFAULT).

Quoted 6 lines
xen/include/xen/lockdown.h new file +0 -0
new file mode 100644
index 0000000000..b2baa31caa

xen/include/xen/lockdown.h line 9

Quoted 9 lines
xen/include/xen/lockdown.h new file +9 -0
@@ -0,0 +1,9 @@
+#ifndef XEN__LOCKDOWN_H
+#define XEN__LOCKDOWN_H
+
+#include <xen/types.h>
+
+bool is_locked_down(void);
+void lockdown_init(const char *cmdline);
+
+#endif /* XEN__LOCKDOWN_H */

Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#2 lore raw
Quoted 1 line · Teddy Astie

(I can't find the PATCH 4/4)

I apologize. The missing patch will be posted as soon as we can.

Quoted 2 lines

I am not convinced of the efficiency of being able to toggle lockdown (including disabling it) mode from command-line.

As you say a malicious userland could hijack the xen command-line arguments. Patch 4 is about ignoring potentially dangerous command line arguments when lockdown mode is enabled.
It is not about disabling lockdown mode itself. Sorry if the description was confusing.

Quoted 1 line

Do we consider Dom0 kernel-space as well (thus Dom0 as a whole)

Dom0 kernel is part of the trusted computing base for Secure Boot so we don't need to worry about that.

Quoted 1 line

what about privcmd device (which can issue hypercalls) ?

We do have a solution for securing hypercalls but I believe it will be part of another patch series.

#3 lore raw
In reply to Kevin Lampis

xen/common/kernel.c static void __init _cmdline_parse(const char *cmdline) · line 220

Quoted 8 lines · Kevin Lampis
xen/common/kernel.c +2 -0
@@ -216,6 +216,9 @@ static void __init _cmdline_parse(const char *cmdline)
  */
 void __init cmdline_parse(const char *cmdline)
 {
+    /* Call this early since it affects command-line parsing */
+    lockdown_init(cmdline);

I can't spot the effect the comment mentions anywhere in this patch. Is the description perhaps lacking some detail? It's rather odd after all to see ...

Quoted 26 lines
xen/common/lockdown.c new file +23 -0
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include <xen/efi.h>
+#include <xen/kernel.h>
+#include <xen/lockdown.h>
+#include <xen/param.h>
+#include <xen/string.h>
+
+static bool __ro_after_init lockdown = IS_ENABLED(CONFIG_LOCKDOWN_DEFAULT);
+ignore_param("lockdown");
+
+bool is_locked_down(void)
+{
+    return lockdown;
+}
+
+void __init lockdown_init(const char *cmdline)
+{
+    if ( efi_secure_boot )
+    {
+        printk("Enabling lockdown mode because Secure Boot is enabled\n");
+        lockdown = true;
+    }

xen/common/lockdown.c line 33

Quoted 10 lines
xen/common/lockdown.c new file +10 -0
+    else
+    {
+        while ( *cmdline )
+        {
+            size_t param_len, name_len;
+            int ret;
+
+            cmdline += strspn(cmdline, " \n\r\t");
+            param_len = strcspn(cmdline, " \n\r\t");
+            name_len = strcspn(cmdline, "= \n\r\t");

... such custom token splitting ahead of normal command line handling.

Jan

#4 lore raw
Quoted 5 lines · Jan Beulich

I can't spot the effect the comment mentions anywhere in this patch. Is the description perhaps lacking some detail? It's rather odd after all to see ...

... such custom token splitting ahead of normal command line handling.

If the UEFI firmware reports that secure boot mode is enabled then Xen lockdown mode will always be enabled.

But we also have a command line argument to enable lockdown mode without secure boot. This is the thing that lockdown_init() is looking for.

It is important to know if we are in lockdown mode or not before parsing any other arguments. Otherwise there will be a race between parsing potentially unsafe arguments and finding the lockdown enable argument.

#5 lore raw
Quoted 10 lines · Kevin Lampis
Quoted 5 lines · Jan Beulich

I can't spot the effect the comment mentions anywhere in this patch. Is the description perhaps lacking some detail? It's rather odd after all to see ...

... such custom token splitting ahead of normal command line handling.

If the UEFI firmware reports that secure boot mode is enabled then Xen lockdown mode will always be enabled.

Quoted 6 lines

But we also have a command line argument to enable lockdown mode without secure boot. This is the thing that lockdown_init() is looking for.

It is important to know if we are in lockdown mode or not before parsing any other arguments. Otherwise there will be a race between parsing potentially unsafe arguments and finding the lockdown enable argument.

Well, there is an alternative: Require the lockdown argument to be absolutely first. (There are further alternatives, but likely less usable.)

Jan

#6 lore raw
Quoted 3 lines · Jan Beulich

Well, there is an alternative: Require the lockdown argument to be absolutely first. (There are further alternatives, but likely less usable.)

Is this your recommendation?

#7 lore raw
Quoted 6 lines · Kevin Lampis
Quoted 3 lines · Jan Beulich

Well, there is an alternative: Require the lockdown argument to be absolutely first. (There are further alternatives, but likely less usable.)

Is this your recommendation?

I would like this to at least be considered. As you likely were able to guess, I don't like that custom command line parsing very much.

Jan

#8 lore raw
Quoted 2 lines · Jan Beulich

I would like this to at least be considered.
I don't like that custom command line parsing very much.

I understand. Parsing code can be risky.

In this case I think the code is small and simple though.

My concern with asking the user to always put the lockdown argument first is that it opens things up to user error. I am not aware of any other project that does this.

#9 lore raw
Quoted 11 lines · Kevin Lampis
Quoted 2 lines · Jan Beulich

I would like this to at least be considered.
I don't like that custom command line parsing very much.

I understand. Parsing code can be risky.

In this case I think the code is small and simple though.

My concern with asking the user to always put the lockdown argument first is that it opens things up to user error. I am not aware of any other project that does this.

Well, it's easily possible to catch that error without any extra parsing.

Jan

#10 lore raw
Quoted 2 lines · Jan Beulich

Well, it's easily possible to catch that error without any extra parsing.

If lockdown is not the first argument then we should print a warning to tell the user that Xen may have already parsed some insecure arguments and lockdown mode will not be effective.

What would be a good way to check if lockdown is or isn't the first argument? I am not sure.

#11 lore raw
Quoted 10 lines · Kevin Lampis
Quoted 2 lines · Jan Beulich

Well, it's easily possible to catch that error without any extra parsing.

If lockdown is not the first argument then we should print a warning to tell the user that Xen may have already parsed some insecure arguments and lockdown mode will not be effective.

What would be a good way to check if lockdown is or isn't the first argument? I am not sure.

It'll be a little hack-ish, I suppose (yet the option is quite special, after all), but I can see various options. For example, have the resulting variable not be boolean, and OR in a separate flag after parsing the first option. Then warn if the flag is clear after all options were parsed.

Jan