security/landlock/ruleset.c
Source file repositories/reference/linux-study-clean/security/landlock/ruleset.c
File Facts
- System
- Linux kernel
- Corpus path
security/landlock/ruleset.c- Extension
.c- Size
- 19952 bytes
- Lines
- 745
- Domain
- Core OS
- Bucket
- Security And Isolation
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bits.hlinux/bug.hlinux/cleanup.hlinux/compiler_types.hlinux/err.hlinux/errno.hlinux/kernel.hlinux/lockdep.hlinux/mutex.hlinux/overflow.hlinux/rbtree.hlinux/refcount.hlinux/slab.hlinux/spinlock.hlinux/workqueue.huapi/linux/landlock.haccess.hdomain.hlimits.hobject.hruleset.h
Detected Declarations
function landlock_create_rulesetfunction build_check_rulefunction is_object_pointerfunction create_rulefunction free_rulefunction build_check_rulesetfunction insert_rulefunction build_check_layerfunction landlock_insert_rulefunction merge_treefunction merge_rulesetfunction inherit_treefunction inherit_rulesetfunction free_rulesetfunction landlock_put_rulesetfunction free_ruleset_workfunction landlock_put_ruleset_deferredfunction landlock_merge_rulesetfunction landlock_find_rulefunction offunction landlock_init_layer_masks
Annotated Snippet
if (this->key.data != id.key.data) {
parent_node = *walker_node;
if (this->key.data < id.key.data)
walker_node = &((*walker_node)->rb_right);
else
walker_node = &((*walker_node)->rb_left);
continue;
}
/* Only a single-level layer should match an existing rule. */
if (WARN_ON_ONCE(num_layers != 1))
return -EINVAL;
/* If there is a matching rule, updates it. */
if ((*layers)[0].level == 0) {
/*
* Extends access rights when the request comes from
* landlock_add_rule(2), i.e. @ruleset is not a domain.
*/
if (WARN_ON_ONCE(this->num_layers != 1))
return -EINVAL;
if (WARN_ON_ONCE(this->layers[0].level != 0))
return -EINVAL;
this->layers[0].access |= (*layers)[0].access;
this->layers[0].flags.quiet |= (*layers)[0].flags.quiet;
return 0;
}
if (WARN_ON_ONCE(this->layers[0].level == 0))
return -EINVAL;
/*
* Intersects access rights when it is a merge between a
* ruleset and a domain.
*/
new_rule = create_rule(id, &this->layers, this->num_layers,
&(*layers)[0]);
if (IS_ERR(new_rule))
return PTR_ERR(new_rule);
rb_replace_node(&this->node, &new_rule->node, root);
free_rule(this, id.type);
return 0;
}
/* There is no match for @id. */
build_check_ruleset();
if (ruleset->num_rules >= LANDLOCK_MAX_NUM_RULES)
return -E2BIG;
new_rule = create_rule(id, layers, num_layers, NULL);
if (IS_ERR(new_rule))
return PTR_ERR(new_rule);
rb_link_node(&new_rule->node, parent_node, walker_node);
rb_insert_color(&new_rule->node, root);
ruleset->num_rules++;
return 0;
}
static void build_check_layer(void)
{
const struct landlock_layer layer = {
.level = ~0,
.access = ~0,
};
/*
* Checks that .level and .access are large enough to contain their expected
* maximum values.
*/
BUILD_BUG_ON(layer.level < LANDLOCK_MAX_NUM_LAYERS);
BUILD_BUG_ON(layer.access < LANDLOCK_MASK_ACCESS_FS);
}
/* @ruleset must be locked by the caller. */
int landlock_insert_rule(struct landlock_ruleset *const ruleset,
const struct landlock_id id,
const access_mask_t access, const u32 flags)
{
struct landlock_layer layers[] = { {
.access = access,
/* When @level is zero, insert_rule() extends @ruleset. */
.level = 0,
.flags = {
.quiet = !!(flags & LANDLOCK_ADD_RULE_QUIET),
},
} };
build_check_layer();
return insert_rule(ruleset, id, &layers, ARRAY_SIZE(layers));
}
Annotation
- Immediate include surface: `linux/bits.h`, `linux/bug.h`, `linux/cleanup.h`, `linux/compiler_types.h`, `linux/err.h`, `linux/errno.h`, `linux/kernel.h`, `linux/lockdep.h`.
- Detected declarations: `function landlock_create_ruleset`, `function build_check_rule`, `function is_object_pointer`, `function create_rule`, `function free_rule`, `function build_check_ruleset`, `function insert_rule`, `function build_check_layer`, `function landlock_insert_rule`, `function merge_tree`.
- Atlas domain: Core OS / Security And Isolation.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.