/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Landlock LSM - Object management3*4* Copyright © 2016-2020 Mickaël Salaün <[email protected]>5* Copyright © 2018-2020 ANSSI6*/78#ifndef _SECURITY_LANDLOCK_OBJECT_H9#define _SECURITY_LANDLOCK_OBJECT_H1011#include <linux/compiler_types.h>12#include <linux/refcount.h>13#include <linux/spinlock.h>1415struct landlock_object;1617/**18* struct landlock_object_underops - Operations on an underlying object19*/20struct landlock_object_underops {21/**22* @release: Releases the underlying object (e.g. iput() for an inode).23*/24void (*release)(struct landlock_object *const object)25__releases(object->lock);26};2728/**29* struct landlock_object - Security blob tied to a kernel object30*31* The goal of this structure is to enable to tie a set of ephemeral access32* rights (pertaining to different domains) to a kernel object (e.g an inode)33* in a safe way. This implies to handle concurrent use and modification.34*35* The lifetime of a &struct landlock_object depends on the rules referring to36* it.37*/38struct landlock_object {39/**40* @usage: This counter is used to tie an object to the rules matching41* it or to keep it alive while adding a new rule. If this counter42* reaches zero, this struct must not be modified, but this counter can43* still be read from within an RCU read-side critical section. When44* adding a new rule to an object with a usage counter of zero, we must45* wait until the pointer to this object is set to NULL (or recycled).46*/47refcount_t usage;48/**49* @lock: Protects against concurrent modifications. This lock must be50* held from the time @usage drops to zero until any weak references51* from @underobj to this object have been cleaned up.52*53* Lock ordering: inode->i_lock nests inside this.54*/55spinlock_t lock;56/**57* @underobj: Used when cleaning up an object and to mark an object as58* tied to its underlying kernel structure. This pointer is protected59* by @lock. Cf. landlock_release_inodes() and release_inode().60*/61void *underobj;62union {63/**64* @rcu_free: Enables lockless use of @usage, @lock and65* @underobj from within an RCU read-side critical section.66* @rcu_free and @underops are only used by67* landlock_put_object().68*/69struct rcu_head rcu_free;70/**71* @underops: Enables landlock_put_object() to release the72* underlying object (e.g. inode).73*/74const struct landlock_object_underops *underops;75};76};7778struct landlock_object *79landlock_create_object(const struct landlock_object_underops *const underops,80void *const underobj);8182void landlock_put_object(struct landlock_object *const object);8384static inline void landlock_get_object(struct landlock_object *const object)85{86if (object)87refcount_inc(&object->usage);88}8990#endif /* _SECURITY_LANDLOCK_OBJECT_H */919293