Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * AppArmor security module
0004  *
0005  * This file contains AppArmor policy dfa matching engine definitions.
0006  *
0007  * Copyright (C) 1998-2008 Novell/SUSE
0008  * Copyright 2009-2012 Canonical Ltd.
0009  */
0010 
0011 #ifndef __AA_MATCH_H
0012 #define __AA_MATCH_H
0013 
0014 #include <linux/kref.h>
0015 
0016 #define DFA_NOMATCH         0
0017 #define DFA_START           1
0018 
0019 
0020 /**
0021  * The format used for transition tables is based on the GNU flex table
0022  * file format (--tables-file option; see Table File Format in the flex
0023  * info pages and the flex sources for documentation). The magic number
0024  * used in the header is 0x1B5E783D instead of 0xF13C57B1 though, because
0025  * new tables have been defined and others YY_ID_CHK (check) and YY_ID_DEF
0026  * (default) tables are used slightly differently (see the apparmor-parser
0027  * package).
0028  *
0029  *
0030  * The data in the packed dfa is stored in network byte order, and the tables
0031  * are arranged for flexibility.  We convert the table data to host native
0032  * byte order.
0033  *
0034  * The dfa begins with a table set header, and is followed by the actual
0035  * tables.
0036  */
0037 
0038 #define YYTH_MAGIC  0x1B5E783D
0039 #define YYTH_FLAG_DIFF_ENCODE   1
0040 #define YYTH_FLAG_OOB_TRANS 2
0041 #define YYTH_FLAGS (YYTH_FLAG_DIFF_ENCODE | YYTH_FLAG_OOB_TRANS)
0042 
0043 #define MAX_OOB_SUPPORTED   1
0044 
0045 struct table_set_header {
0046     u32 th_magic;       /* YYTH_MAGIC */
0047     u32 th_hsize;
0048     u32 th_ssize;
0049     u16 th_flags;
0050     char th_version[];
0051 };
0052 
0053 /* The YYTD_ID are one less than flex table mappings.  The flex id
0054  * has 1 subtracted at table load time, this allows us to directly use the
0055  * ID's as indexes.
0056  */
0057 #define YYTD_ID_ACCEPT  0
0058 #define YYTD_ID_BASE    1
0059 #define YYTD_ID_CHK 2
0060 #define YYTD_ID_DEF 3
0061 #define YYTD_ID_EC  4
0062 #define YYTD_ID_META    5
0063 #define YYTD_ID_ACCEPT2 6
0064 #define YYTD_ID_NXT 7
0065 #define YYTD_ID_TSIZE   8
0066 #define YYTD_ID_MAX 8
0067 
0068 #define YYTD_DATA8  1
0069 #define YYTD_DATA16 2
0070 #define YYTD_DATA32 4
0071 #define YYTD_DATA64 8
0072 
0073 /* ACCEPT & ACCEPT2 tables gets 6 dedicated flags, YYTD_DATAX define the
0074  * first flags
0075  */
0076 #define ACCEPT1_FLAGS(X) ((X) & 0x3f)
0077 #define ACCEPT2_FLAGS(X) ACCEPT1_FLAGS((X) >> YYTD_ID_ACCEPT2)
0078 #define TO_ACCEPT1_FLAG(X) ACCEPT1_FLAGS(X)
0079 #define TO_ACCEPT2_FLAG(X) (ACCEPT1_FLAGS(X) << YYTD_ID_ACCEPT2)
0080 #define DFA_FLAG_VERIFY_STATES 0x1000
0081 
0082 struct table_header {
0083     u16 td_id;
0084     u16 td_flags;
0085     u32 td_hilen;
0086     u32 td_lolen;
0087     char td_data[];
0088 };
0089 
0090 #define DEFAULT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_DEF]->td_data))
0091 #define BASE_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_BASE]->td_data))
0092 #define NEXT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_NXT]->td_data))
0093 #define CHECK_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_CHK]->td_data))
0094 #define EQUIV_TABLE(DFA) ((u8 *)((DFA)->tables[YYTD_ID_EC]->td_data))
0095 #define ACCEPT_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT]->td_data))
0096 #define ACCEPT_TABLE2(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT2]->td_data))
0097 
0098 struct aa_dfa {
0099     struct kref count;
0100     u16 flags;
0101     u32 max_oob;
0102     struct table_header *tables[YYTD_ID_TSIZE];
0103 };
0104 
0105 extern struct aa_dfa *nulldfa;
0106 extern struct aa_dfa *stacksplitdfa;
0107 
0108 #define byte_to_byte(X) (X)
0109 
0110 #define UNPACK_ARRAY(TABLE, BLOB, LEN, TTYPE, BTYPE, NTOHX) \
0111     do { \
0112         typeof(LEN) __i; \
0113         TTYPE *__t = (TTYPE *) TABLE; \
0114         BTYPE *__b = (BTYPE *) BLOB; \
0115         for (__i = 0; __i < LEN; __i++) { \
0116             __t[__i] = NTOHX(__b[__i]); \
0117         } \
0118     } while (0)
0119 
0120 static inline size_t table_size(size_t len, size_t el_size)
0121 {
0122     return ALIGN(sizeof(struct table_header) + len * el_size, 8);
0123 }
0124 
0125 int aa_setup_dfa_engine(void);
0126 void aa_teardown_dfa_engine(void);
0127 
0128 struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags);
0129 unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
0130                   const char *str, int len);
0131 unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
0132               const char *str);
0133 unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
0134              const char c);
0135 unsigned int aa_dfa_outofband_transition(struct aa_dfa *dfa,
0136                      unsigned int state);
0137 unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
0138                 const char *str, const char **retpos);
0139 unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
0140                  const char *str, int n, const char **retpos);
0141 
0142 void aa_dfa_free_kref(struct kref *kref);
0143 
0144 #define WB_HISTORY_SIZE 24
0145 struct match_workbuf {
0146     unsigned int count;
0147     unsigned int pos;
0148     unsigned int len;
0149     unsigned int size;  /* power of 2, same as history size */
0150     unsigned int history[WB_HISTORY_SIZE];
0151 };
0152 #define DEFINE_MATCH_WB(N)      \
0153 struct match_workbuf N = {      \
0154     .count = 0,         \
0155     .pos = 0,           \
0156     .len = 0,           \
0157 }
0158 
0159 unsigned int aa_dfa_leftmatch(struct aa_dfa *dfa, unsigned int start,
0160                   const char *str, unsigned int *count);
0161 
0162 /**
0163  * aa_get_dfa - increment refcount on dfa @p
0164  * @dfa: dfa  (MAYBE NULL)
0165  *
0166  * Returns: pointer to @dfa if @dfa is NULL will return NULL
0167  * Requires: @dfa must be held with valid refcount when called
0168  */
0169 static inline struct aa_dfa *aa_get_dfa(struct aa_dfa *dfa)
0170 {
0171     if (dfa)
0172         kref_get(&(dfa->count));
0173 
0174     return dfa;
0175 }
0176 
0177 /**
0178  * aa_put_dfa - put a dfa refcount
0179  * @dfa: dfa to put refcount   (MAYBE NULL)
0180  *
0181  * Requires: if @dfa != NULL that a valid refcount be held
0182  */
0183 static inline void aa_put_dfa(struct aa_dfa *dfa)
0184 {
0185     if (dfa)
0186         kref_put(&dfa->count, aa_dfa_free_kref);
0187 }
0188 
0189 #define MATCH_FLAG_DIFF_ENCODE 0x80000000
0190 #define MARK_DIFF_ENCODE 0x40000000
0191 #define MATCH_FLAG_OOB_TRANSITION 0x20000000
0192 #define MATCH_FLAGS_MASK 0xff000000
0193 #define MATCH_FLAGS_VALID (MATCH_FLAG_DIFF_ENCODE | MATCH_FLAG_OOB_TRANSITION)
0194 #define MATCH_FLAGS_INVALID (MATCH_FLAGS_MASK & ~MATCH_FLAGS_VALID)
0195 
0196 #endif /* __AA_MATCH_H */