Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Access vector cache interface for object managers.
0004  *
0005  * Author : Stephen Smalley, <sds@tycho.nsa.gov>
0006  */
0007 #ifndef _SELINUX_AVC_H_
0008 #define _SELINUX_AVC_H_
0009 
0010 #include <linux/stddef.h>
0011 #include <linux/errno.h>
0012 #include <linux/kernel.h>
0013 #include <linux/kdev_t.h>
0014 #include <linux/spinlock.h>
0015 #include <linux/init.h>
0016 #include <linux/audit.h>
0017 #include <linux/lsm_audit.h>
0018 #include <linux/in6.h>
0019 #include "flask.h"
0020 #include "av_permissions.h"
0021 #include "security.h"
0022 
0023 /*
0024  * An entry in the AVC.
0025  */
0026 struct avc_entry;
0027 
0028 struct task_struct;
0029 struct inode;
0030 struct sock;
0031 struct sk_buff;
0032 
0033 /*
0034  * AVC statistics
0035  */
0036 struct avc_cache_stats {
0037     unsigned int lookups;
0038     unsigned int misses;
0039     unsigned int allocations;
0040     unsigned int reclaims;
0041     unsigned int frees;
0042 };
0043 
0044 /*
0045  * We only need this data after we have decided to send an audit message.
0046  */
0047 struct selinux_audit_data {
0048     u32 ssid;
0049     u32 tsid;
0050     u16 tclass;
0051     u32 requested;
0052     u32 audited;
0053     u32 denied;
0054     int result;
0055     struct selinux_state *state;
0056 } __randomize_layout;
0057 
0058 /*
0059  * AVC operations
0060  */
0061 
0062 void __init avc_init(void);
0063 
0064 static inline u32 avc_audit_required(u32 requested,
0065                   struct av_decision *avd,
0066                   int result,
0067                   u32 auditdeny,
0068                   u32 *deniedp)
0069 {
0070     u32 denied, audited;
0071     denied = requested & ~avd->allowed;
0072     if (unlikely(denied)) {
0073         audited = denied & avd->auditdeny;
0074         /*
0075          * auditdeny is TRICKY!  Setting a bit in
0076          * this field means that ANY denials should NOT be audited if
0077          * the policy contains an explicit dontaudit rule for that
0078          * permission.  Take notice that this is unrelated to the
0079          * actual permissions that were denied.  As an example lets
0080          * assume:
0081          *
0082          * denied == READ
0083          * avd.auditdeny & ACCESS == 0 (not set means explicit rule)
0084          * auditdeny & ACCESS == 1
0085          *
0086          * We will NOT audit the denial even though the denied
0087          * permission was READ and the auditdeny checks were for
0088          * ACCESS
0089          */
0090         if (auditdeny && !(auditdeny & avd->auditdeny))
0091             audited = 0;
0092     } else if (result)
0093         audited = denied = requested;
0094     else
0095         audited = requested & avd->auditallow;
0096     *deniedp = denied;
0097     return audited;
0098 }
0099 
0100 int slow_avc_audit(struct selinux_state *state,
0101            u32 ssid, u32 tsid, u16 tclass,
0102            u32 requested, u32 audited, u32 denied, int result,
0103            struct common_audit_data *a);
0104 
0105 /**
0106  * avc_audit - Audit the granting or denial of permissions.
0107  * @state: SELinux state
0108  * @ssid: source security identifier
0109  * @tsid: target security identifier
0110  * @tclass: target security class
0111  * @requested: requested permissions
0112  * @avd: access vector decisions
0113  * @result: result from avc_has_perm_noaudit
0114  * @a:  auxiliary audit data
0115  *
0116  * Audit the granting or denial of permissions in accordance
0117  * with the policy.  This function is typically called by
0118  * avc_has_perm() after a permission check, but can also be
0119  * called directly by callers who use avc_has_perm_noaudit()
0120  * in order to separate the permission check from the auditing.
0121  * For example, this separation is useful when the permission check must
0122  * be performed under a lock, to allow the lock to be released
0123  * before calling the auditing code.
0124  */
0125 static inline int avc_audit(struct selinux_state *state,
0126                 u32 ssid, u32 tsid,
0127                 u16 tclass, u32 requested,
0128                 struct av_decision *avd,
0129                 int result,
0130                 struct common_audit_data *a)
0131 {
0132     u32 audited, denied;
0133     audited = avc_audit_required(requested, avd, result, 0, &denied);
0134     if (likely(!audited))
0135         return 0;
0136     return slow_avc_audit(state, ssid, tsid, tclass,
0137                   requested, audited, denied, result,
0138                   a);
0139 }
0140 
0141 #define AVC_STRICT 1 /* Ignore permissive mode. */
0142 #define AVC_EXTENDED_PERMS 2    /* update extended permissions */
0143 int avc_has_perm_noaudit(struct selinux_state *state,
0144              u32 ssid, u32 tsid,
0145              u16 tclass, u32 requested,
0146              unsigned flags,
0147              struct av_decision *avd);
0148 
0149 int avc_has_perm(struct selinux_state *state,
0150          u32 ssid, u32 tsid,
0151          u16 tclass, u32 requested,
0152          struct common_audit_data *auditdata);
0153 
0154 int avc_has_extended_perms(struct selinux_state *state,
0155                u32 ssid, u32 tsid, u16 tclass, u32 requested,
0156                u8 driver, u8 perm, struct common_audit_data *ad);
0157 
0158 
0159 u32 avc_policy_seqno(struct selinux_state *state);
0160 
0161 #define AVC_CALLBACK_GRANT      1
0162 #define AVC_CALLBACK_TRY_REVOKE     2
0163 #define AVC_CALLBACK_REVOKE     4
0164 #define AVC_CALLBACK_RESET      8
0165 #define AVC_CALLBACK_AUDITALLOW_ENABLE  16
0166 #define AVC_CALLBACK_AUDITALLOW_DISABLE 32
0167 #define AVC_CALLBACK_AUDITDENY_ENABLE   64
0168 #define AVC_CALLBACK_AUDITDENY_DISABLE  128
0169 #define AVC_CALLBACK_ADD_XPERMS     256
0170 
0171 int avc_add_callback(int (*callback)(u32 event), u32 events);
0172 
0173 /* Exported to selinuxfs */
0174 struct selinux_avc;
0175 int avc_get_hash_stats(struct selinux_avc *avc, char *page);
0176 unsigned int avc_get_cache_threshold(struct selinux_avc *avc);
0177 void avc_set_cache_threshold(struct selinux_avc *avc,
0178                  unsigned int cache_threshold);
0179 
0180 /* Attempt to free avc node cache */
0181 void avc_disable(void);
0182 
0183 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
0184 DECLARE_PER_CPU(struct avc_cache_stats, avc_cache_stats);
0185 #endif
0186 
0187 #endif /* _SELINUX_AVC_H_ */
0188