/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Landlock - Access types and helpers3*4* Copyright © 2016-2020 Mickaël Salaün <[email protected]>5* Copyright © 2018-2020 ANSSI6* Copyright © 2024-2025 Microsoft Corporation7*/89#ifndef _SECURITY_LANDLOCK_ACCESS_H10#define _SECURITY_LANDLOCK_ACCESS_H1112#include <linux/bitops.h>13#include <linux/build_bug.h>14#include <linux/kernel.h>15#include <uapi/linux/landlock.h>1617#include "limits.h"1819/*20* All access rights that are denied by default whether they are handled or not21* by a ruleset/layer. This must be ORed with all ruleset->access_masks[]22* entries when we need to get the absolute handled access masks, see23* landlock_upgrade_handled_access_masks().24*/25/* clang-format off */26#define _LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \27LANDLOCK_ACCESS_FS_REFER)28/* clang-format on */2930/* clang-format off */31#define _LANDLOCK_ACCESS_FS_OPTIONAL ( \32LANDLOCK_ACCESS_FS_TRUNCATE | \33LANDLOCK_ACCESS_FS_IOCTL_DEV)34/* clang-format on */3536typedef u16 access_mask_t;3738/* Makes sure all filesystem access rights can be stored. */39static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);40/* Makes sure all network access rights can be stored. */41static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);42/* Makes sure all scoped rights can be stored. */43static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);44/* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */45static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));4647/* Ruleset access masks. */48struct access_masks {49access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;50access_mask_t net : LANDLOCK_NUM_ACCESS_NET;51access_mask_t scope : LANDLOCK_NUM_SCOPE;52};5354union access_masks_all {55struct access_masks masks;56u32 all;57};5859/* Makes sure all fields are covered. */60static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==61sizeof(typeof_member(union access_masks_all, all)));6263typedef u16 layer_mask_t;6465/* Makes sure all layers can be checked. */66static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);6768/*69* Tracks domains responsible of a denied access. This is required to avoid70* storing in each object the full layer_masks[] required by update_request().71*/72typedef u8 deny_masks_t;7374/*75* Makes sure all optional access rights can be tied to a layer index (cf.76* get_deny_mask).77*/78static_assert(BITS_PER_TYPE(deny_masks_t) >=79(HWEIGHT(LANDLOCK_MAX_NUM_LAYERS - 1) *80HWEIGHT(_LANDLOCK_ACCESS_FS_OPTIONAL)));8182/* LANDLOCK_MAX_NUM_LAYERS must be a power of two (cf. deny_masks_t assert). */83static_assert(HWEIGHT(LANDLOCK_MAX_NUM_LAYERS) == 1);8485/* Upgrades with all initially denied by default access rights. */86static inline struct access_masks87landlock_upgrade_handled_access_masks(struct access_masks access_masks)88{89/*90* All access rights that are denied by default whether they are91* explicitly handled or not.92*/93if (access_masks.fs)94access_masks.fs |= _LANDLOCK_ACCESS_FS_INITIALLY_DENIED;9596return access_masks;97}9899#endif /* _SECURITY_LANDLOCK_ACCESS_H */100101102