/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Landlock LSM - Ruleset management3*4* Copyright © 2016-2020 Mickaël Salaün <[email protected]>5* Copyright © 2018-2020 ANSSI6*/78#ifndef _SECURITY_LANDLOCK_RULESET_H9#define _SECURITY_LANDLOCK_RULESET_H1011#include <linux/cleanup.h>12#include <linux/err.h>13#include <linux/mutex.h>14#include <linux/rbtree.h>15#include <linux/refcount.h>16#include <linux/workqueue.h>1718#include "access.h"19#include "limits.h"20#include "object.h"2122struct landlock_hierarchy;2324/**25* struct landlock_layer - Access rights for a given layer26*/27struct landlock_layer {28/**29* @level: Position of this layer in the layer stack.30*/31u16 level;32/**33* @access: Bitfield of allowed actions on the kernel object. They are34* relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).35*/36access_mask_t access;37};3839/**40* union landlock_key - Key of a ruleset's red-black tree41*/42union landlock_key {43/**44* @object: Pointer to identify a kernel object (e.g. an inode).45*/46struct landlock_object *object;47/**48* @data: Raw data to identify an arbitrary 32-bit value49* (e.g. a TCP port).50*/51uintptr_t data;52};5354/**55* enum landlock_key_type - Type of &union landlock_key56*/57enum landlock_key_type {58/**59* @LANDLOCK_KEY_INODE: Type of &landlock_ruleset.root_inode's node60* keys.61*/62LANDLOCK_KEY_INODE = 1,63/**64* @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's65* node keys.66*/67LANDLOCK_KEY_NET_PORT,68};6970/**71* struct landlock_id - Unique rule identifier for a ruleset72*/73struct landlock_id {74/**75* @key: Identifies either a kernel object (e.g. an inode) or76* a raw value (e.g. a TCP port).77*/78union landlock_key key;79/**80* @type: Type of a landlock_ruleset's root tree.81*/82const enum landlock_key_type type;83};8485/**86* struct landlock_rule - Access rights tied to an object87*/88struct landlock_rule {89/**90* @node: Node in the ruleset's red-black tree.91*/92struct rb_node node;93/**94* @key: A union to identify either a kernel object (e.g. an inode) or95* a raw data value (e.g. a network socket port). This is used as a key96* for this ruleset element. The pointer is set once and never97* modified. It always points to an allocated object because each rule98* increments the refcount of its object.99*/100union landlock_key key;101/**102* @num_layers: Number of entries in @layers.103*/104u32 num_layers;105/**106* @layers: Stack of layers, from the latest to the newest, implemented107* as a flexible array member (FAM).108*/109struct landlock_layer layers[] __counted_by(num_layers);110};111112/**113* struct landlock_ruleset - Landlock ruleset114*115* This data structure must contain unique entries, be updatable, and quick to116* match an object.117*/118struct landlock_ruleset {119/**120* @root_inode: Root of a red-black tree containing &struct121* landlock_rule nodes with inode object. Once a ruleset is tied to a122* process (i.e. as a domain), this tree is immutable until @usage123* reaches zero.124*/125struct rb_root root_inode;126127#if IS_ENABLED(CONFIG_INET)128/**129* @root_net_port: Root of a red-black tree containing &struct130* landlock_rule nodes with network port. Once a ruleset is tied to a131* process (i.e. as a domain), this tree is immutable until @usage132* reaches zero.133*/134struct rb_root root_net_port;135#endif /* IS_ENABLED(CONFIG_INET) */136137/**138* @hierarchy: Enables hierarchy identification even when a parent139* domain vanishes. This is needed for the ptrace protection.140*/141struct landlock_hierarchy *hierarchy;142union {143/**144* @work_free: Enables to free a ruleset within a lockless145* section. This is only used by146* landlock_put_ruleset_deferred() when @usage reaches zero.147* The fields @lock, @usage, @num_rules, @num_layers and148* @access_masks are then unused.149*/150struct work_struct work_free;151struct {152/**153* @lock: Protects against concurrent modifications of154* @root, if @usage is greater than zero.155*/156struct mutex lock;157/**158* @usage: Number of processes (i.e. domains) or file159* descriptors referencing this ruleset.160*/161refcount_t usage;162/**163* @num_rules: Number of non-overlapping (i.e. not for164* the same object) rules in this ruleset.165*/166u32 num_rules;167/**168* @num_layers: Number of layers that are used in this169* ruleset. This enables to check that all the layers170* allow an access request. A value of 0 identifies a171* non-merged ruleset (i.e. not a domain).172*/173u32 num_layers;174/**175* @access_masks: Contains the subset of filesystem and176* network actions that are restricted by a ruleset.177* A domain saves all layers of merged rulesets in a178* stack (FAM), starting from the first layer to the179* last one. These layers are used when merging180* rulesets, for user space backward compatibility181* (i.e. future-proof), and to properly handle merged182* rulesets without overlapping access rights. These183* layers are set once and never changed for the184* lifetime of the ruleset.185*/186struct access_masks access_masks[];187};188};189};190191struct landlock_ruleset *192landlock_create_ruleset(const access_mask_t access_mask_fs,193const access_mask_t access_mask_net,194const access_mask_t scope_mask);195196void landlock_put_ruleset(struct landlock_ruleset *const ruleset);197void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);198199DEFINE_FREE(landlock_put_ruleset, struct landlock_ruleset *,200if (!IS_ERR_OR_NULL(_T)) landlock_put_ruleset(_T))201202int landlock_insert_rule(struct landlock_ruleset *const ruleset,203const struct landlock_id id,204const access_mask_t access);205206struct landlock_ruleset *207landlock_merge_ruleset(struct landlock_ruleset *const parent,208struct landlock_ruleset *const ruleset);209210const struct landlock_rule *211landlock_find_rule(const struct landlock_ruleset *const ruleset,212const struct landlock_id id);213214static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)215{216if (ruleset)217refcount_inc(&ruleset->usage);218}219220/**221* landlock_union_access_masks - Return all access rights handled in the222* domain223*224* @domain: Landlock ruleset (used as a domain)225*226* Returns: an access_masks result of the OR of all the domain's access masks.227*/228static inline struct access_masks229landlock_union_access_masks(const struct landlock_ruleset *const domain)230{231union access_masks_all matches = {};232size_t layer_level;233234for (layer_level = 0; layer_level < domain->num_layers; layer_level++) {235union access_masks_all layer = {236.masks = domain->access_masks[layer_level],237};238239matches.all |= layer.all;240}241242return matches.masks;243}244245static inline void246landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,247const access_mask_t fs_access_mask,248const u16 layer_level)249{250access_mask_t fs_mask = fs_access_mask & LANDLOCK_MASK_ACCESS_FS;251252/* Should already be checked in sys_landlock_create_ruleset(). */253WARN_ON_ONCE(fs_access_mask != fs_mask);254ruleset->access_masks[layer_level].fs |= fs_mask;255}256257static inline void258landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,259const access_mask_t net_access_mask,260const u16 layer_level)261{262access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;263264/* Should already be checked in sys_landlock_create_ruleset(). */265WARN_ON_ONCE(net_access_mask != net_mask);266ruleset->access_masks[layer_level].net |= net_mask;267}268269static inline void270landlock_add_scope_mask(struct landlock_ruleset *const ruleset,271const access_mask_t scope_mask, const u16 layer_level)272{273access_mask_t mask = scope_mask & LANDLOCK_MASK_SCOPE;274275/* Should already be checked in sys_landlock_create_ruleset(). */276WARN_ON_ONCE(scope_mask != mask);277ruleset->access_masks[layer_level].scope |= mask;278}279280static inline access_mask_t281landlock_get_fs_access_mask(const struct landlock_ruleset *const ruleset,282const u16 layer_level)283{284/* Handles all initially denied by default access rights. */285return ruleset->access_masks[layer_level].fs |286_LANDLOCK_ACCESS_FS_INITIALLY_DENIED;287}288289static inline access_mask_t290landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,291const u16 layer_level)292{293return ruleset->access_masks[layer_level].net;294}295296static inline access_mask_t297landlock_get_scope_mask(const struct landlock_ruleset *const ruleset,298const u16 layer_level)299{300return ruleset->access_masks[layer_level].scope;301}302303bool landlock_unmask_layers(const struct landlock_rule *const rule,304const access_mask_t access_request,305layer_mask_t (*const layer_masks)[],306const size_t masks_array_size);307308access_mask_t309landlock_init_layer_masks(const struct landlock_ruleset *const domain,310const access_mask_t access_request,311layer_mask_t (*const layer_masks)[],312const enum landlock_key_type key_type);313314#endif /* _SECURITY_LANDLOCK_RULESET_H */315316317