| 1 |
|
| 2 | |
| 3 | * Landlock - 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 domain->handled_masks[]
|
| 23 | * entries when we need to get the absolute handled access masks, see
|
| 24 | * landlock_upgrade_handled_access_masks().
|
| 25 |
|
| 26 |
|
| 27 | #define _LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \
|
| 28 | LANDLOCK_ACCESS_FS_REFER)
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 | #define _LANDLOCK_ACCESS_FS_OPTIONAL ( \
|
| 33 | LANDLOCK_ACCESS_FS_TRUNCATE | \
|
| 34 | LANDLOCK_ACCESS_FS_IOCTL_DEV)
|
| 35 |
|
| 36 |
|
| 37 | typedef u32 access_mask_t;
|
| 38 |
|
| 39 |
|
| 40 | static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
|
| 41 |
|
| 42 | static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
|
| 43 |
|
| 44 | static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
|
| 45 |
|
| 46 | static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
|
| 47 |
|
| 48 |
|
| 49 | struct access_masks {
|
| 50 | access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
|
| 51 | access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
|
| 52 | access_mask_t scope : LANDLOCK_NUM_SCOPE;
|
| 53 | } __packed __aligned(sizeof(u32));
|
| 54 |
|
| 55 | union access_masks_all {
|
| 56 | struct access_masks masks;
|
| 57 | u32 all;
|
| 58 | };
|
| 59 |
|
| 60 |
|
| 61 | static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
|
| 62 | sizeof(typeof_member(union access_masks_all, all)));
|
| 63 |
|
| 64 | #define _LANDLOCK_LAYER_MASK_PADDING \
|
| 65 | (BITS_PER_TYPE(access_mask_t) - LANDLOCK_NUM_ACCESS_MAX - \
|
| 66 | IS_ENABLED(CONFIG_SECURITY_LANDLOCK_LOG))
|
| 67 |
|
| 68 | |
| 69 | * struct layer_mask - The access rights and rule flags for a layer.
|
| 70 | *
|
| 71 | * This has a bit for each access rights and rule flags. During access checks,
|
| 72 | * it is used to represent the access rights for each layer which still need to
|
| 73 | * be fulfilled. When all bits are 0, the access request is considered to be
|
| 74 | * fulfilled.
|
| 75 |
|
| 76 | struct layer_mask {
|
| 77 | |
| 78 | * @access: The unfulfilled access rights for this layer.
|
| 79 |
|
| 80 | access_mask_t access : LANDLOCK_NUM_ACCESS_MAX;
|
| 81 | #ifdef CONFIG_SECURITY_LANDLOCK_LOG
|
| 82 | |
| 83 | * @quiet: Whether we have encountered a rule with the quiet flag for
|
| 84 | * this layer. Used to control logging.
|
| 85 |
|
| 86 | access_mask_t quiet : 1;
|
| 87 | #endif
|
| 88 | |
| 89 | * @__pad: Padding for the compiler's bitfield initialization.
|
| 90 |
|
| 91 | access_mask_t __pad : _LANDLOCK_LAYER_MASK_PADDING;
|
| 92 | } __packed __aligned(sizeof(access_mask_t));
|
| 93 |
|
| 94 | |
| 95 | * Make sure that we don't increase the size of struct layer_mask when storing
|
| 96 | * rule flags.
|
| 97 |
|
| 98 | static_assert(sizeof(struct layer_mask) == sizeof(access_mask_t));
|
| 99 |
|
| 100 | |
| 101 | * struct layer_masks - An array of struct layer_mask, one per layer.
|
| 102 |
|
| 103 | struct layer_masks {
|
| 104 | |
| 105 | * @layers: The unfulfilled access rights for each layer.
|
| 106 |
|
| 107 | struct layer_mask layers[LANDLOCK_MAX_NUM_LAYERS];
|
| 108 | };
|
| 109 |
|
| 110 | |
| 111 | * Tracks domains responsible of a denied access. This avoids storing in each
|
| 112 | * object the full matrix of per-layer unfulfilled access rights, which is
|
| 113 | * required by update_request().
|
| 114 | *
|
| 115 | * Each nibble represents the layer index of the newest layer which denied a
|
| 116 | * certain access right. For file system access rights, the upper four bits are
|
| 117 | * the index of the layer which denies LANDLOCK_ACCESS_FS_IOCTL_DEV and the
|
| 118 | * lower nibble represents LANDLOCK_ACCESS_FS_TRUNCATE.
|
| 119 |
|
| 120 | typedef u8 deny_masks_t;
|
| 121 |
|
| 122 | |
| 123 | * Makes sure all optional access rights can be tied to a layer index (cf.
|
| 124 | * get_deny_mask).
|
| 125 |
|
| 126 | static_assert(BITS_PER_TYPE(deny_masks_t) >=
|
| 127 | (HWEIGHT(LANDLOCK_MAX_NUM_LAYERS - 1) *
|
| 128 | HWEIGHT(_LANDLOCK_ACCESS_FS_OPTIONAL)));
|
| 129 |
|
| 130 |
|
| 131 | static_assert(HWEIGHT(LANDLOCK_MAX_NUM_LAYERS) == 1);
|
| 132 |
|
| 133 |
|
| 134 | static inline struct access_masks
|
| 135 | landlock_upgrade_handled_access_masks(struct access_masks access_masks)
|
| 136 | {
|
| 137 | |
| 138 | * All access rights that are denied by default whether they are
|
| 139 | * explicitly handled or not.
|
| 140 |
|
| 141 | if (access_masks.fs)
|
| 142 | access_masks.fs |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
|
| 143 |
|
| 144 | return access_masks;
|
| 145 | }
|
| 146 |
|
| 147 |
|
| 148 | static inline bool access_mask_subset(access_mask_t subset,
|
| 149 | access_mask_t superset)
|
| 150 | {
|
| 151 | return (subset | superset) == superset;
|
| 152 | }
|
| 153 |
|
| 154 |
|
| 155 | typedef u8 optional_access_t;
|
| 156 | static_assert(BITS_PER_TYPE(optional_access_t) >=
|
| 157 | HWEIGHT(_LANDLOCK_ACCESS_FS_OPTIONAL));
|
| 158 |
|
| 159 | #endif
|