Skip to content

Commit 622e2f5

Browse files
committed
landlock: Move access types
Move LANDLOCK_ACCESS_FS_INITIALLY_DENIED, access_mask_t, struct access_mask, and struct access_masks_all to a dedicated access.h file. Rename LANDLOCK_ACCESS_FS_INITIALLY_DENIED to _LANDLOCK_ACCESS_FS_INITIALLY_DENIED to make it clear that it's not part of UAPI. Add some newlines when appropriate. This file will be extended with following commits, and it will help to avoid dependency loops. Cc: Günther Noack <[email protected]> Link: https://lore.kernel.org/r/[email protected] [mic: Fix rebase conflict because of the new cleanup headers] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent 924f440 commit 622e2f5

File tree

5 files changed

+68
-46
lines changed

5 files changed

+68
-46
lines changed

security/landlock/access.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Landlock LSM - Access types and helpers
4+
*
5+
* Copyright © 2016-2020 Mickaël Salaün <[email protected]>
6+
* Copyright © 2018-2020 ANSSI
7+
* Copyright © 2024-2025 Microsoft Corporation
8+
*/
9+
10+
#ifndef _SECURITY_LANDLOCK_ACCESS_H
11+
#define _SECURITY_LANDLOCK_ACCESS_H
12+
13+
#include <linux/bitops.h>
14+
#include <linux/build_bug.h>
15+
#include <linux/kernel.h>
16+
#include <uapi/linux/landlock.h>
17+
18+
#include "limits.h"
19+
20+
/*
21+
* All access rights that are denied by default whether they are handled or not
22+
* by a ruleset/layer. This must be ORed with all ruleset->access_masks[]
23+
* entries when we need to get the absolute handled access masks.
24+
*/
25+
/* clang-format off */
26+
#define _LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \
27+
LANDLOCK_ACCESS_FS_REFER)
28+
/* clang-format on */
29+
30+
typedef u16 access_mask_t;
31+
32+
/* Makes sure all filesystem access rights can be stored. */
33+
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
34+
/* Makes sure all network access rights can be stored. */
35+
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
36+
/* Makes sure all scoped rights can be stored. */
37+
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
38+
/* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
39+
static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
40+
41+
/* Ruleset access masks. */
42+
struct access_masks {
43+
access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
44+
access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
45+
access_mask_t scope : LANDLOCK_NUM_SCOPE;
46+
};
47+
48+
union access_masks_all {
49+
struct access_masks masks;
50+
u32 all;
51+
};
52+
53+
/* Makes sure all fields are covered. */
54+
static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
55+
sizeof(typeof_member(union access_masks_all, all)));
56+
57+
typedef u16 layer_mask_t;
58+
59+
/* Makes sure all layers can be checked. */
60+
static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
61+
62+
#endif /* _SECURITY_LANDLOCK_ACCESS_H */

security/landlock/fs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <uapi/linux/fiemap.h>
3737
#include <uapi/linux/landlock.h>
3838

39+
#include "access.h"
3940
#include "common.h"
4041
#include "cred.h"
4142
#include "fs.h"
@@ -393,7 +394,7 @@ get_handled_fs_accesses(const struct landlock_ruleset *const domain)
393394
{
394395
/* Handles all initially denied by default access rights. */
395396
return landlock_union_access_masks(domain).fs |
396-
LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
397+
_LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
397398
}
398399

399400
static const struct access_masks any_fs = {

security/landlock/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/init.h>
1414
#include <linux/rcupdate.h>
1515

16+
#include "access.h"
1617
#include "ruleset.h"
1718
#include "setup.h"
1819

security/landlock/ruleset.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/spinlock.h>
2323
#include <linux/workqueue.h>
2424

25+
#include "access.h"
2526
#include "limits.h"
2627
#include "object.h"
2728
#include "ruleset.h"

security/landlock/ruleset.h

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,17 @@
99
#ifndef _SECURITY_LANDLOCK_RULESET_H
1010
#define _SECURITY_LANDLOCK_RULESET_H
1111

12-
#include <linux/bitops.h>
13-
#include <linux/build_bug.h>
1412
#include <linux/cleanup.h>
1513
#include <linux/err.h>
16-
#include <linux/kernel.h>
1714
#include <linux/mutex.h>
1815
#include <linux/rbtree.h>
1916
#include <linux/refcount.h>
2017
#include <linux/workqueue.h>
21-
#include <uapi/linux/landlock.h>
2218

19+
#include "access.h"
2320
#include "limits.h"
2421
#include "object.h"
2522

26-
/*
27-
* All access rights that are denied by default whether they are handled or not
28-
* by a ruleset/layer. This must be ORed with all ruleset->access_masks[]
29-
* entries when we need to get the absolute handled access masks.
30-
*/
31-
/* clang-format off */
32-
#define LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \
33-
LANDLOCK_ACCESS_FS_REFER)
34-
/* clang-format on */
35-
36-
typedef u16 access_mask_t;
37-
/* Makes sure all filesystem access rights can be stored. */
38-
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
39-
/* Makes sure all network access rights can be stored. */
40-
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
41-
/* Makes sure all scoped rights can be stored. */
42-
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
43-
/* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
44-
static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
45-
46-
/* Ruleset access masks. */
47-
struct access_masks {
48-
access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
49-
access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
50-
access_mask_t scope : LANDLOCK_NUM_SCOPE;
51-
};
52-
53-
union access_masks_all {
54-
struct access_masks masks;
55-
u32 all;
56-
};
57-
58-
/* Makes sure all fields are covered. */
59-
static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
60-
sizeof(typeof_member(union access_masks_all, all)));
61-
62-
typedef u16 layer_mask_t;
63-
/* Makes sure all layers can be checked. */
64-
static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
65-
6623
/**
6724
* struct landlock_layer - Access rights for a given layer
6825
*/
@@ -371,7 +328,7 @@ landlock_get_fs_access_mask(const struct landlock_ruleset *const ruleset,
371328
{
372329
/* Handles all initially denied by default access rights. */
373330
return ruleset->access_masks[layer_level].fs |
374-
LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
331+
_LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
375332
}
376333

377334
static inline access_mask_t

0 commit comments

Comments
 (0)