Skip to contentSkip to conversation

landlock: Fix REFER in layered domains

PATCH Superseded Günther Noack 4 participants 11 messages Oct 30 – Nov 12

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover landlock: Fix REFER in layered domains

This patchset fixes a bug in Landlock layering, reported by Dwi Siswanto in [1] and [2]. (Thanks!)

The bug
=======

When Landlock initializes layer masks for path walk based checks, it unconditionally adds the LANDLOCK_ACCESS_FS_REFER access right to the handled access rights.

This leads to a wrong layer composition, in cases where the following layers are stacked:

1. A FS layer restricting REFER, but also allowing REFER in a rule (e.g., handle REFER, but allow REFER on /)
2. A non-FS layer (e.g., networking-only layer)

When these rulesets are enforced **individually**, there are REFER operations that are allowed in both rulesets -- the ones that 1 allows.

When these rulesets are enforced **on top of each other**, the non-FS layer starts enforcing REFER implicitly as well, and all REFER operations are denied.

This patchset
=============

This patchset fixes the issue in three commits:

1) The fix, which should be backportable to older kernels and which is CC'd to stable@.
2) Regression selftests.
3) A more elegant fix for newer kernels,
which is intentionally *not* CC'd to stable@, because it would break on older kernels.

In newer kernels, we already add the "REFER" access right to the handled access rights when we merge the ruleset into the new domain, so there is no additional calculation to do for it.

Please let me know what you think!

–Günther

[1] https://github.com/landlock-lsm/go-landlock/issues/66
[2] https://github.com/landlock-lsm/linux/issues/67

Günther Noack (3):
landlock: Fix layer composition of LANDLOCK_ACCESS_FS_REFER selftests/landlock: Test that layering does not break refer
landlock: Simplify landlock_get_fs_access_mask()

 security/landlock/domain.h                 |  9 +++--
 tools/testing/selftests/landlock/fs_test.c | 39 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 3 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/3 landlock: Fix layer composition of LANDLOCK_ACCESS_FS_REFER

In landlock_get_fs_access_mask(), only add the LANDLOCK_ACCESS_FS_REFER right if any filesystem access rights are handled at all. This prevents us from accidentally treating non-filesystem layers as if they handled LANDLOCK_ACCESS_FS_REFER.

landlock_get_fs_access_mask() is used when initializing the layer masks for doing access right checks, so this bug meant that we unintentionally denied "refer" (e.g., link, rename) operations in layered cases, where the same operation would have been accepted if only either one of the two layers had been enforced. If the rulesets individually allow an operation, the composition of these rulesets should also allow it.

The bug only manifests as soon as one other layer handles filesystem access rights. Otherwise, Landlock filesystem path walk is guarded by an early exit for the case that no filesystem-enforcing layer exists.

---
 security/landlock/domain.h | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
security/landlock/domain.h +10 -3
index 428c1d9890db..693e569f5387 100644
@@ -258,9 +258,16 @@ static inline access_mask_t
 landlock_get_fs_access_mask(const struct landlock_domain *const domain,
 			    const u16 layer_level)
 {
-	/* Handles all initially denied by default access rights. */
-	return domain->handled_masks[layer_level].fs |
-	       _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
+	access_mask_t handled = domain->handled_masks[layer_level].fs;
+
+	/*
+	 * Handles all initially denied by default access rights, but only for
+	 * layers handling filesystem accesses.
+	 */
+	if (handled)
+		handled |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
+
+	return handled;
 }
 
 static inline access_mask_t
Signature · 1 line
2.56.0
#5 lore raw

PATCH 2/3 selftests/landlock: Test that layering does not break refer

In reply to Günther Noack

Regression test to exercise that layered Landlock domains do not accidentally deny the LANDLOCK_ACCESS_FS_REFER access right. This used to be the case when layering a non-filesystem ruleset with a filesystem-enforcing ruleset. In that case, REFER operations that would have been allowed for the individual rulesets would get denied when layering the same rulesets.

---
 tools/testing/selftests/landlock/fs_test.c | 39 ++++++++++++++++++++++
 1 file changed, 39 insertions(+)
tools/testing/selftests/landlock/fs_test.c +39 -0
index 6e979cef884d..c319bcea1437 100644
@@ -2619,6 +2619,45 @@ TEST_F_FORK(layout1, refer_denied_by_default4)
 				layer_dir_s1d1_refer);
 }
 
+/*
+ * Regression test for layered domains: The two enforced Landlock layers both do
+ * not restrict the tested "refer" operations on their own, so they should also
+ * not restrict them when they are layered.
+ */
+static void test_layering_does_not_break_refer(
+	struct __test_metadata *const _metadata,
+	const struct landlock_ruleset_attr *const non_fs_attr)
+{
+	ASSERT_EQ(0, unlink(file1_s1d2));
+
+	enforce_fs(_metadata, LANDLOCK_ACCESS_FS_REFER, layer_dir_s1d1_refer);
+
+	drop_access_rights(_metadata, non_fs_attr);
+
+	EXPECT_EQ(0, test_rename(file1_s1d1, file1_s1d2));
+	EXPECT_EQ(0, test_rename(file1_s1d2, file1_s1d1));
+	EXPECT_EQ(0, test_exchange(file2_s1d1, file2_s1d2));
+	EXPECT_EQ(0, test_exchange(file2_s1d2, file2_s1d1));
+}
+
+TEST_F_FORK(layout1, layering_with_net_does_not_break_refer)
+{
+	const struct landlock_ruleset_attr attr = {
+		.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP,
+	};
+
+	test_layering_does_not_break_refer(_metadata, &attr);
+}
+
+TEST_F_FORK(layout1, layering_with_scoped_does_not_break_refer)
+{
+	const struct landlock_ruleset_attr attr = {
+		.scoped = LANDLOCK_SCOPE_SIGNAL,
+	};
+
+	test_layering_does_not_break_refer(_metadata, &attr);
+}
+
 /*
  * Tests walking through a denied root mount.
  */
Signature · 1 line
2.56.0
#7 lore raw

PATCH 3/3 landlock: Simplify landlock_get_fs_access_mask()

In reply to Günther Noack

Return the handled FS access mask as it is, instead of artificially adding LANDLOCK_ACCESS_FS_REFER to it.

Since commit d6c7cf84a24f ("landlock: Simplify initially denied access rights"), the merge operation already bakes the initially denied access rights (LANDLOCK_ACCESS_FS_REFER) into the a layer's handled FS access rights. -- They don't need to be added explicitly any more.

No functional change.

---
 security/landlock/domain.h | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
security/landlock/domain.h +4 -8
index 693e569f5387..1aa85c817d0a 100644
@@ -258,16 +258,12 @@ static inline access_mask_t
 landlock_get_fs_access_mask(const struct landlock_domain *const domain,
 			    const u16 layer_level)
 {
-	access_mask_t handled = domain->handled_masks[layer_level].fs;
-
 	/*
-	 * Handles all initially denied by default access rights, but only for
-	 * layers handling filesystem accesses.
+	 * Initially denied by default access rights are already included for
+	 * layers handling filesystem accesses, see
+	 * landlock_upgrade_handled_access_masks().
 	 */
-	if (handled)
-		handled |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
-
-	return handled;
+	return domain->handled_masks[layer_level].fs;
 }
 
 static inline access_mask_t
Signature · 1 line
2.56.0
#8 lore raw

Re: [PATCH 3/3] landlock: Simplify landlock_get_fs_access_mask()

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

Return the handled FS access mask as it is, instead of artificially adding LANDLOCK_ACCESS_FS_REFER to it.

Since commit d6c7cf84a24f ("landlock: Simplify initially denied access rights"), the merge operation already bakes the initially denied access rights (LANDLOCK_ACCESS_FS_REFER) into the a layer's handled FS access rights. -- They don't need to be added explicitly any more.

No functional change.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#2 lore raw

Re: [PATCH 1/3] landlock: Fix layer composition of LANDLOCK_ACCESS_FS_REFER

In reply to Günther Noack
Sashiko has reviewed this patch and found no issues.
Quoted 17 lines

In landlock_get_fs_access_mask(), only add the LANDLOCK_ACCESS_FS_REFER right if any filesystem access rights are handled at all. This prevents us from accidentally treating non-filesystem layers as if they handled LANDLOCK_ACCESS_FS_REFER.

landlock_get_fs_access_mask() is used when initializing the layer masks for doing access right checks, so this bug meant that we unintentionally denied "refer" (e.g., link, rename) operations in layered cases, where the same operation would have been accepted if only either one of the two layers had been enforced. If the rulesets individually allow an operation, the composition of these rulesets should also allow it.

The bug only manifests as soon as one other layer handles filesystem access rights. Otherwise, Landlock filesystem path walk is guarded by an early exit for the case that no filesystem-enforcing layer exists.

Quoted 6 lines

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#6 lore raw

Re: [PATCH 2/3] selftests/landlock: Test that layering does not break refer

In reply to Günther Noack
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

Regression test to exercise that layered Landlock domains do not accidentally deny the LANDLOCK_ACCESS_FS_REFER access right. This used to be the case when layering a non-filesystem ruleset with a filesystem-enforcing ruleset. In that case, REFER operations that would have been allowed for the individual rulesets would get denied when layering the same rulesets.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#9 lore raw
In reply to Günther Noack
Quoted 7 lines · Günther Noack

This patchset
=============

This patchset fixes the issue in three commits:

1) The fix, which should be backportable to older kernels and which is CC'd to stable@.

To be more precise, the fix needs to go in 7.2, 6.18 and 6.12. Other stable releases predate 6.7 where the bug was introduced.

It does unfortunately not apply cleanly on the stable kernels 7.2, 6.18 and 6.12, but I have backported versions.

(Differences: In the stable versions, the affected helper function was still in ruleset.h instead of domain.h. In 6.12, the constant name did not have a _ prefix yet. In 6.12 in the tests, the drop_access_rights() helper didn't exist.)

Quoted 11 lines

2) Regression selftests.
3) A more elegant fix for newer kernels,
which is intentionally *not* CC'd to stable@,
because it would break on older kernels.

In newer kernels, we already add the "REFER" access right to the handled access rights when we merge the ruleset into the new domain, so there is no additional calculation to do for it.

Please let me know what you think!

P.S. Mickaël: Specifically, I'd like to know whether you think this warrants an erratum. If yes, here is a tentative wording:

Erratum 5: Correct REFER semantics when layering Landlock policies
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This fix addresses an issue where, when multiple Landlock ruleset are layered, Landlock would restrict LANDLOCK_ACCESS_FS_REFER operations which would have been allowed by the same rulesets individually.

This change ensures that a LANDLOCK_ACCESS_FS_REFER operation is allowed in a layered domain if all of the enforced rulesets allow it individually as well.

Impact:

This occurs in situations where one of the layers does not handle any filesystem access rights, but one of the other layers does. In that case, the non-filesystem layer is interpreted to deny the LANDLOCK_ACCESS_FS_REFER right.

–Günther

#3 lore raw

Re: [PATCH 1/3] landlock: Fix layer composition of LANDLOCK_ACCESS_FS_REFER

In reply to Günther Noack

I saw the docs still describe the old one. include/uapi/linux/landlock.h says:

* For historical reasons, the %LANDLOCK_ACCESS_FS_REFER right is always denied
* by default, even when its bit is not set in @handled_access_fs.

and, for LANDLOCK_ACCESS_FS_REFER itself:

* This is the only access right which is denied by default by any ruleset,
* even if the right is not specified as handled at ruleset creation time.

So ruleset that handles no filesystem access right never denies LANDLOCK_ACCESS_FS_REFER -- alone or layered.

Also, the comment above _LANDLOCK_ACCESS_FS_INITIALLY_DENIED in access.h has the same problem:

* All access rights that are denied by default whether they are handled or not
* by a ruleset/layer. This must be ORed with all domain->handled_masks[]
* entries when we need to get the absolute handled access masks, see
* landlock_upgrade_handled_access_masks().

After this patch, the mask only applies to layers that handle filesystem access, and after patch 3, nothing ORs it in at read time. *Someone* who follows the comment could bring the bug back.

security/landlock/domain.h static inline access_mask_t · line 270

Quoted 10 lines
security/landlock/domain.h +10 -0
+	access_mask_t handled = domain->handled_masks[layer_level].fs;
+
+	/*
+	 * Handles all initially denied by default access rights, but only for
+	 * layers handling filesystem accesses.
+	 */
+	if (handled)
+		handled |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
+
+	return handled;

nit: I believe landlock_upgrade_handled_access_masks() already has the same "only if fs is non-zero" rule, so this could be

	return landlock_upgrade_handled_access_masks(
		domain->handled_masks[layer_level]).fs;

Which keep the rule in one place. It matters little if patch 3 goes in on top, and 6.12 doesn't have the helper. What do you think?

Best!
Dwi

#4 lore raw

Re: [PATCH 1/3] landlock: Fix layer composition of LANDLOCK_ACCESS_FS_REFER

Quoted 17 lines · Dwi Siswanto

I saw the docs still describe the old one. include/uapi/linux/landlock.h says:

* For historical reasons, the %LANDLOCK_ACCESS_FS_REFER right is always denied
* by default, even when its bit is not set in @handled_access_fs.

and, for LANDLOCK_ACCESS_FS_REFER itself:

* This is the only access right which is denied by default by any ruleset,
* even if the right is not specified as handled at ruleset creation time.

So ruleset that handles no filesystem access right never denies LANDLOCK_ACCESS_FS_REFER -- alone or layered.

Also, the comment above _LANDLOCK_ACCESS_FS_INITIALLY_DENIED in access.h has the same problem:

Quoted 8 lines

* All access rights that are denied by default whether they are handled or not
* by a ruleset/layer. This must be ORed with all domain->handled_masks[]
* entries when we need to get the absolute handled access masks, see
* landlock_upgrade_handled_access_masks().

After this patch, the mask only applies to layers that handle filesystem access, and after patch 3, nothing ORs it in at read time. *Someone* who follows the comment could bring the bug back.

Thanks for pointing this out, the documentation is indeed not up to date.

All this documentation likely stems from a time when *only* access_fs but none of the other fields existed in struct landlock_ruleset_attr.

It is not very obvious from looking at this patch set alone, but the intended semantics are clearer when you look at merge_ruleset() in domain.c.

This in turn calls landlock_upgrade_handled_access_masks() in access.h to calculcate the updated access masks with the same logic:

if (access_masks.fs)
access_masks.fs |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;

	return access_masks;

The behaviour that we have for non-layered domains is the intended one here -- we only add the REFER right when either one of the other filesystem access rights is handled, but we do not set it when only other access right types are handled.

I'll send a documentation update for this, either in the next patch set revision, or in a follow-up if needed.

Quoted 14 lines

security/landlock/domain.h static inline access_mask_t · line 270

Quoted 10 lines
security/landlock/domain.h +10 -0
+	access_mask_t handled = domain->handled_masks[layer_level].fs;
+
+	/*
+	 * Handles all initially denied by default access rights, but only for
+	 * layers handling filesystem accesses.
+	 */
+	if (handled)
+		handled |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
+
+	return handled;

nit: I believe landlock_upgrade_handled_access_masks() already has the same "only if fs is non-zero" rule, so this could be

Quoted 5 lines
	return landlock_upgrade_handled_access_masks(
		domain->handled_masks[layer_level]).fs;

Which keep the rule in one place. It matters little if patch 3 goes in on top, and 6.12 doesn't have the helper. What do you think?

Good point, I agree, this function already implements this.

As you are saying, the first commit is mainly useful to backporting, and the backport to 6.12 will be easier to compare with the original for stable maintainers if we spell it out instead of using the helper function.

Not feeling strongly about it though; I'd let Mickaël decide what he prefers. In either case, the backporting will require some manual work because the affected code moved around between files.

–Günther

maintainer
#10 lore raw
In reply to Günther Noack
Quoted 31 lines · Günther Noack
Quoted 7 lines · Günther Noack

This patchset
=============

This patchset fixes the issue in three commits:

1) The fix, which should be backportable to older kernels and which is CC'd to stable@.

To be more precise, the fix needs to go in 7.2, 6.18 and 6.12. Other stable releases predate 6.7 where the bug was introduced.

It does unfortunately not apply cleanly on the stable kernels 7.2, 6.18 and 6.12, but I have backported versions.

(Differences: In the stable versions, the affected helper function was still in ruleset.h instead of domain.h. In 6.12, the constant name did not have a _ prefix yet. In 6.12 in the tests, the drop_access_rights() helper didn't exist.)

Quoted 10 lines

2) Regression selftests.
3) A more elegant fix for newer kernels,
which is intentionally *not* CC'd to stable@,
because it would break on older kernels.

In newer kernels, we already add the "REFER" access right to the handled access rights when we merge the ruleset into the new domain, so there is no additional calculation to do for it.

Quoted 4 lines
Quoted 1 line

Please let me know what you think!

P.S. Mickaël: Specifically, I'd like to know whether you think this warrants an erratum. If yes, here is a tentative wording:

Yes, this warrants an erratum because independently stacking filessytem and network or scope policies is legitimate but an unfixed kernel may be overly restrictive by denying FS_REFER operations that every policy allows individually. It can then be useful to know when to tweak such layered policies or when it's fixed.

Quoted 5 lines

Erratum 5: Correct REFER semantics when layering Landlock policies
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 8 lines

This fix addresses an issue where, when multiple Landlock ruleset are layered, Landlock would restrict LANDLOCK_ACCESS_FS_REFER operations which would have been allowed by the same rulesets individually.

This change ensures that a LANDLOCK_ACCESS_FS_REFER operation is allowed in a layered domain if all of the enforced rulesets allow it individually as well.

I think these paragraphs can be merged and simplified a bit.

Quoted 7 lines

Impact:

This occurs in situations where one of the layers does not handle any filesystem access rights, but one of the other layers does. In that case, the non-filesystem layer is interpreted to deny the LANDLOCK_ACCESS_FS_REFER right.

Overall, this looks good to me.

Quoted 4 lines

–Günther