Skip to contentSkip to conversation

landlock: Warn once if a Landlock action is requested while disabled

PATCH In review Mickaël Salaün 3 participants 5 messages Feb 19, 2024 – Feb 26, 2024

Subsystems (3)
lore
Share & tools
lore

Mickaël Salaün

author maintainer
#0 lore raw

PATCH 1/1 landlock: Warn once if a Landlock action is requested while disabled

Because sandboxing can be used as an opportunistic security measure, user space may not log unsupported features. Let the system administrator know if an application tries to use Landlock but failed because it isn't enabled at boot time. This may be caused by bootloader configurations with outdated "lsm" kernel's command-line parameter.

Cc Günther Noack <[email protected]>
Fixes 265885daf3e5 ("landlock: Add syscall implementations")
---
 security/landlock/syscalls.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/syscalls.c +15 -3
index f0bc50003b46..b5b424819dee 100644
@@ -33,6 +33,18 @@
 #include "ruleset.h"
 #include "setup.h"
 
+static bool is_not_initialized(void)
+{
+	if (likely(landlock_initialized))
+		return false;
+
+	pr_warn_once(
+		"Disabled but requested by user space. "
+		"You should enable Landlock at boot time: "
+		"https://docs.kernel.org/userspace-api/landlock.html#kernel-support\n");
+	return true;
+}
+
 /**
  * copy_min_struct_from_user - Safe future-proof argument copying
  *
@@ -173,7 +185,7 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
 	/* Build-time checks. */
 	build_check_abi();
 
-	if (!landlock_initialized)
+	if (is_not_initialized())
 		return -EOPNOTSUPP;
 
 	if (flags) {
@@ -407,7 +419,7 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
 	struct landlock_ruleset *ruleset;
 	int err;
 
-	if (!landlock_initialized)
+	if (is_not_initialized())
 		return -EOPNOTSUPP;
 
 	/* No flag for now. */
@@ -467,7 +479,7 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
 	struct landlock_cred_security *new_llcred;
 	int err;
 
-	if (!landlock_initialized)
+	if (is_not_initialized())
 		return -EOPNOTSUPP;
 
 	/*
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Mickaël Salaün

Because sandboxing can be used as an opportunistic security measure, user space may not log unsupported features. Let the system administrator know if an application tries to use Landlock but failed because it isn't enabled at boot time. This may be caused by bootloader configurations with outdated "lsm" kernel's command-line parameter.

Cc Günther Noack <[email protected]>
Fixes 265885daf3e5 ("landlock: Add syscall implementations")
---
 security/landlock/syscalls.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/syscalls.c +0 -0
index f0bc50003b46..b5b424819dee 100644
@@ -33,6 +33,18 @@
 #include "ruleset.h"
 #include "setup.h"

security/landlock/syscalls.c line 44

Quoted 10 lines
security/landlock/syscalls.c +9 -0
 
+static bool is_not_initialized(void)
+{
+	if (likely(landlock_initialized))
+		return false;
+
+	pr_warn_once(
+		"Disabled but requested by user space. "
+		"You should enable Landlock at boot time: "
+		"https://docs.kernel.org/userspace-api/landlock.html#kernel-support\n");

Perhaps update this docs to be really explicit with a example, maybe...

If landlock is not present in CONFIG_LSM, you can add it. For example, if this was the current config::

  $ zgrep -h ^CONFIG_LSM= /boot/config-$(uname -r) /proc/config.gz 2>/dev/null
  CONFIG_LSM="lockdown,yama,integrity,apparmor"

You can boot with::

lsm=landlock,lockdown,yama,integrity,apparmor

I *still* wish we had the "+" operator for lsm=. It would be SO much easier to say "boot with lsm=+landlock". *shrug*

Signature · 1 line
Kees Cook
author maintainer
#2 lore raw
Quoted 38 lines · Kees Cook
Quoted 21 lines · Mickaël Salaün

Because sandboxing can be used as an opportunistic security measure, user space may not log unsupported features. Let the system administrator know if an application tries to use Landlock but failed because it isn't enabled at boot time. This may be caused by bootloader configurations with outdated "lsm" kernel's command-line parameter.

Cc Günther Noack <[email protected]>
Fixes 265885daf3e5 ("landlock: Add syscall implementations")
---
 security/landlock/syscalls.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/syscalls.c +0 -0
index f0bc50003b46..b5b424819dee 100644
@@ -33,6 +33,18 @@
 #include "ruleset.h"
 #include "setup.h"

security/landlock/syscalls.c line 44

Quoted 10 lines
security/landlock/syscalls.c +9 -0
 
+static bool is_not_initialized(void)
+{
+	if (likely(landlock_initialized))
+		return false;
+
+	pr_warn_once(
+		"Disabled but requested by user space. "
+		"You should enable Landlock at boot time: "
+		"https://docs.kernel.org/userspace-api/landlock.html#kernel-support\n");

Perhaps update this docs to be really explicit with a example, maybe...

If landlock is not present in CONFIG_LSM, you can add it. For example, if this was the current config::

Quoted 7 lines
  $ zgrep -h ^CONFIG_LSM= /boot/config-$(uname -r) /proc/config.gz 2>/dev/null
  CONFIG_LSM="lockdown,yama,integrity,apparmor"

You can boot with::

lsm=landlock,lockdown,yama,integrity,apparmor

Indeed, I'll send a dedicated patch and update the link accordingly.

Quoted 3 lines

I *still* wish we had the "+" operator for lsm=. It would be SO much easier to say "boot with lsm=+landlock". *shrug*

I guess it's still possible and it would need to be backported to be more useful.

Quoted 7 lines
Signature · 1 line
Kees Cook
Reviewed-by on v1 1/1
#3 lore raw
In reply to Mickaël Salaün

Hello!

I think this is a good idea.
Some minor implementation remarks below.

Quoted 19 lines · Mickaël Salaün

Because sandboxing can be used as an opportunistic security measure, user space may not log unsupported features. Let the system administrator know if an application tries to use Landlock but failed because it isn't enabled at boot time. This may be caused by bootloader configurations with outdated "lsm" kernel's command-line parameter.

Cc Günther Noack <[email protected]>
Fixes 265885daf3e5 ("landlock: Add syscall implementations")
---
 security/landlock/syscalls.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/syscalls.c +0 -0
index f0bc50003b46..b5b424819dee 100644

security/landlock/syscalls.c line 39

Quoted 7 lines
security/landlock/syscalls.c +4 -0
@@ -33,6 +33,18 @@
 #include "ruleset.h"
 #include "setup.h"
 
+static bool is_not_initialized(void)
+{
+	if (likely(landlock_initialized))
+		return false;

Optional stylistic remark; I try to avoid predicate functions which have a "negated" meaning, because double negations are slightly more error prone. (We return false here, so Landlock is not not initialized.)

Quoted 12 lines
security/landlock/syscalls.c +8 -0
+
+	pr_warn_once(
+		"Disabled but requested by user space. "
+		"You should enable Landlock at boot time: "
+		"https://docs.kernel.org/userspace-api/landlock.html#kernel-support\n");
+	return true;
+}
+
 /**
  * copy_min_struct_from_user - Safe future-proof argument copying
  *

security/landlock/syscalls.c SYSCALL_DEFINE3(landlock_create_ruleset, · line 189

Quoted 6 lines
security/landlock/syscalls.c +1 -1
@@ -173,7 +185,7 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
 	/* Build-time checks. */
 	build_check_abi();
 
-	if (!landlock_initialized)
+	if (is_not_initialized())
 		return -EOPNOTSUPP;

Technically, any Landlock user needs to go through the landlock_create_ruleset() system call anyway; it might be enough to just add it in that place and leave the other system calls as they were. Then you could also omit the special function.

–Günther

author maintainer
#4 lore raw
Quoted 28 lines · Günther Noack

Hello!

I think this is a good idea.
Some minor implementation remarks below.

Quoted 22 lines · Mickaël Salaün

Because sandboxing can be used as an opportunistic security measure, user space may not log unsupported features. Let the system administrator know if an application tries to use Landlock but failed because it isn't enabled at boot time. This may be caused by bootloader configurations with outdated "lsm" kernel's command-line parameter.

Cc Günther Noack <[email protected]>
Fixes 265885daf3e5 ("landlock: Add syscall implementations")
---
 security/landlock/syscalls.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/syscalls.c +0 -0
index f0bc50003b46..b5b424819dee 100644
@@ -33,6 +33,18 @@
 #include "ruleset.h"
 #include "setup.h"
 
Quoted 9 lines

security/landlock/syscalls.c line 39

Quoted 4 lines
security/landlock/syscalls.c +4 -0
+static bool is_not_initialized(void)
+{
+	if (likely(landlock_initialized))
+		return false;

Optional stylistic remark; I try to avoid predicate functions which have a "negated" meaning, because double negations are slightly more error prone. (We return false here, so Landlock is not not initialized.)

I agree, I was also bothered about this double negation. I'll send a v2 with the same behavior but an is_initialized() helper instead.

Quoted 16 lines
Quoted 16 lines
security/landlock/syscalls.c +8 -0
+
+	pr_warn_once(
+		"Disabled but requested by user space. "
+		"You should enable Landlock at boot time: "
+		"https://docs.kernel.org/userspace-api/landlock.html#kernel-support\n");
+	return true;
+}
+
 /**
  * copy_min_struct_from_user - Safe future-proof argument copying
  *
@@ -173,7 +185,7 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
 	/* Build-time checks. */
 	build_check_abi();
 
Quoted 8 lines

security/landlock/syscalls.c SYSCALL_DEFINE3(landlock_create_ruleset, · line 189

Quoted 3 lines
security/landlock/syscalls.c +1 -1
-	if (!landlock_initialized)
+	if (is_not_initialized())
 		return -EOPNOTSUPP;

Technically, any Landlock user needs to go through the landlock_create_ruleset() system call anyway; it might be enough to just add it in that place and leave the other system calls as they were. Then you could also omit the special function.

True, but we never know. I prefer to cover all entry points the same way. It makes things more consistent and easier to review.

Quoted 5 lines

–Günther