Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Landlock LSM - Object management
0004  *
0005  * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
0006  * Copyright © 2018-2020 ANSSI
0007  */
0008 
0009 #ifndef _SECURITY_LANDLOCK_OBJECT_H
0010 #define _SECURITY_LANDLOCK_OBJECT_H
0011 
0012 #include <linux/compiler_types.h>
0013 #include <linux/refcount.h>
0014 #include <linux/spinlock.h>
0015 
0016 struct landlock_object;
0017 
0018 /**
0019  * struct landlock_object_underops - Operations on an underlying object
0020  */
0021 struct landlock_object_underops {
0022     /**
0023      * @release: Releases the underlying object (e.g. iput() for an inode).
0024      */
0025     void (*release)(struct landlock_object *const object)
0026         __releases(object->lock);
0027 };
0028 
0029 /**
0030  * struct landlock_object - Security blob tied to a kernel object
0031  *
0032  * The goal of this structure is to enable to tie a set of ephemeral access
0033  * rights (pertaining to different domains) to a kernel object (e.g an inode)
0034  * in a safe way.  This implies to handle concurrent use and modification.
0035  *
0036  * The lifetime of a &struct landlock_object depends on the rules referring to
0037  * it.
0038  */
0039 struct landlock_object {
0040     /**
0041      * @usage: This counter is used to tie an object to the rules matching
0042      * it or to keep it alive while adding a new rule.  If this counter
0043      * reaches zero, this struct must not be modified, but this counter can
0044      * still be read from within an RCU read-side critical section.  When
0045      * adding a new rule to an object with a usage counter of zero, we must
0046      * wait until the pointer to this object is set to NULL (or recycled).
0047      */
0048     refcount_t usage;
0049     /**
0050      * @lock: Protects against concurrent modifications.  This lock must be
0051      * held from the time @usage drops to zero until any weak references
0052      * from @underobj to this object have been cleaned up.
0053      *
0054      * Lock ordering: inode->i_lock nests inside this.
0055      */
0056     spinlock_t lock;
0057     /**
0058      * @underobj: Used when cleaning up an object and to mark an object as
0059      * tied to its underlying kernel structure.  This pointer is protected
0060      * by @lock.  Cf. landlock_release_inodes() and release_inode().
0061      */
0062     void *underobj;
0063     union {
0064         /**
0065          * @rcu_free: Enables lockless use of @usage, @lock and
0066          * @underobj from within an RCU read-side critical section.
0067          * @rcu_free and @underops are only used by
0068          * landlock_put_object().
0069          */
0070         struct rcu_head rcu_free;
0071         /**
0072          * @underops: Enables landlock_put_object() to release the
0073          * underlying object (e.g. inode).
0074          */
0075         const struct landlock_object_underops *underops;
0076     };
0077 };
0078 
0079 struct landlock_object *
0080 landlock_create_object(const struct landlock_object_underops *const underops,
0081                void *const underobj);
0082 
0083 void landlock_put_object(struct landlock_object *const object);
0084 
0085 static inline void landlock_get_object(struct landlock_object *const object)
0086 {
0087     if (object)
0088         refcount_inc(&object->usage);
0089 }
0090 
0091 #endif /* _SECURITY_LANDLOCK_OBJECT_H */