match.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor policy dfa matching engine definitions.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2012 Canonical Ltd.
  9. */
  10. #ifndef __AA_MATCH_H
  11. #define __AA_MATCH_H
  12. #include <linux/kref.h>
  13. #define DFA_NOMATCH 0
  14. #define DFA_START 1
  15. /**
  16. * The format used for transition tables is based on the GNU flex table
  17. * file format (--tables-file option; see Table File Format in the flex
  18. * info pages and the flex sources for documentation). The magic number
  19. * used in the header is 0x1B5E783D instead of 0xF13C57B1 though, because
  20. * new tables have been defined and others YY_ID_CHK (check) and YY_ID_DEF
  21. * (default) tables are used slightly differently (see the apparmor-parser
  22. * package).
  23. *
  24. *
  25. * The data in the packed dfa is stored in network byte order, and the tables
  26. * are arranged for flexibility. We convert the table data to host native
  27. * byte order.
  28. *
  29. * The dfa begins with a table set header, and is followed by the actual
  30. * tables.
  31. */
  32. #define YYTH_MAGIC 0x1B5E783D
  33. #define YYTH_FLAG_DIFF_ENCODE 1
  34. #define YYTH_FLAG_OOB_TRANS 2
  35. #define YYTH_FLAGS (YYTH_FLAG_DIFF_ENCODE | YYTH_FLAG_OOB_TRANS)
  36. #define MAX_OOB_SUPPORTED 1
  37. struct table_set_header {
  38. u32 th_magic; /* YYTH_MAGIC */
  39. u32 th_hsize;
  40. u32 th_ssize;
  41. u16 th_flags;
  42. char th_version[];
  43. };
  44. /* The YYTD_ID are one less than flex table mappings. The flex id
  45. * has 1 subtracted at table load time, this allows us to directly use the
  46. * ID's as indexes.
  47. */
  48. #define YYTD_ID_ACCEPT 0
  49. #define YYTD_ID_BASE 1
  50. #define YYTD_ID_CHK 2
  51. #define YYTD_ID_DEF 3
  52. #define YYTD_ID_EC 4
  53. #define YYTD_ID_META 5
  54. #define YYTD_ID_ACCEPT2 6
  55. #define YYTD_ID_NXT 7
  56. #define YYTD_ID_TSIZE 8
  57. #define YYTD_ID_MAX 8
  58. #define YYTD_DATA8 1
  59. #define YYTD_DATA16 2
  60. #define YYTD_DATA32 4
  61. #define YYTD_DATA64 8
  62. /* ACCEPT & ACCEPT2 tables gets 6 dedicated flags, YYTD_DATAX define the
  63. * first flags
  64. */
  65. #define ACCEPT1_FLAGS(X) ((X) & 0x3f)
  66. #define ACCEPT2_FLAGS(X) ACCEPT1_FLAGS((X) >> YYTD_ID_ACCEPT2)
  67. #define TO_ACCEPT1_FLAG(X) ACCEPT1_FLAGS(X)
  68. #define TO_ACCEPT2_FLAG(X) (ACCEPT1_FLAGS(X) << YYTD_ID_ACCEPT2)
  69. #define DFA_FLAG_VERIFY_STATES 0x1000
  70. struct table_header {
  71. u16 td_id;
  72. u16 td_flags;
  73. u32 td_hilen;
  74. u32 td_lolen;
  75. char td_data[];
  76. };
  77. #define DEFAULT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_DEF]->td_data))
  78. #define BASE_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_BASE]->td_data))
  79. #define NEXT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_NXT]->td_data))
  80. #define CHECK_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_CHK]->td_data))
  81. #define EQUIV_TABLE(DFA) ((u8 *)((DFA)->tables[YYTD_ID_EC]->td_data))
  82. #define ACCEPT_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT]->td_data))
  83. #define ACCEPT_TABLE2(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT2]->td_data))
  84. struct aa_dfa {
  85. struct kref count;
  86. u16 flags;
  87. u32 max_oob;
  88. struct table_header *tables[YYTD_ID_TSIZE];
  89. };
  90. #define byte_to_byte(X) (X)
  91. #define UNPACK_ARRAY(TABLE, BLOB, LEN, TTYPE, BTYPE, NTOHX) \
  92. do { \
  93. typeof(LEN) __i; \
  94. TTYPE *__t = (TTYPE *) TABLE; \
  95. BTYPE *__b = (BTYPE *) BLOB; \
  96. for (__i = 0; __i < LEN; __i++) { \
  97. __t[__i] = NTOHX(__b[__i]); \
  98. } \
  99. } while (0)
  100. static inline size_t table_size(size_t len, size_t el_size)
  101. {
  102. return ALIGN(sizeof(struct table_header) + len * el_size, 8);
  103. }
  104. #define aa_state_t unsigned int
  105. struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags);
  106. aa_state_t aa_dfa_match_len(struct aa_dfa *dfa, aa_state_t start,
  107. const char *str, int len);
  108. aa_state_t aa_dfa_match(struct aa_dfa *dfa, aa_state_t start,
  109. const char *str);
  110. aa_state_t aa_dfa_next(struct aa_dfa *dfa, aa_state_t state, const char c);
  111. aa_state_t aa_dfa_outofband_transition(struct aa_dfa *dfa, aa_state_t state);
  112. aa_state_t aa_dfa_match_until(struct aa_dfa *dfa, aa_state_t start,
  113. const char *str, const char **retpos);
  114. aa_state_t aa_dfa_matchn_until(struct aa_dfa *dfa, aa_state_t start,
  115. const char *str, int n, const char **retpos);
  116. void aa_dfa_free_kref(struct kref *kref);
  117. /* This needs to be a power of 2 */
  118. #define WB_HISTORY_SIZE 32
  119. struct match_workbuf {
  120. unsigned int pos;
  121. unsigned int len;
  122. aa_state_t history[WB_HISTORY_SIZE];
  123. };
  124. #define DEFINE_MATCH_WB(N) \
  125. struct match_workbuf N = { \
  126. .pos = 0, \
  127. .len = 0, \
  128. }
  129. aa_state_t aa_dfa_leftmatch(struct aa_dfa *dfa, aa_state_t start,
  130. const char *str, unsigned int *count);
  131. /**
  132. * aa_get_dfa - increment refcount on dfa @p
  133. * @dfa: dfa (MAYBE NULL)
  134. *
  135. * Returns: pointer to @dfa if @dfa is NULL will return NULL
  136. * Requires: @dfa must be held with valid refcount when called
  137. */
  138. static inline struct aa_dfa *aa_get_dfa(struct aa_dfa *dfa)
  139. {
  140. if (dfa)
  141. kref_get(&(dfa->count));
  142. return dfa;
  143. }
  144. /**
  145. * aa_put_dfa - put a dfa refcount
  146. * @dfa: dfa to put refcount (MAYBE NULL)
  147. *
  148. * Requires: if @dfa != NULL that a valid refcount be held
  149. */
  150. static inline void aa_put_dfa(struct aa_dfa *dfa)
  151. {
  152. if (dfa)
  153. kref_put(&dfa->count, aa_dfa_free_kref);
  154. }
  155. #define MATCH_FLAG_DIFF_ENCODE 0x80000000
  156. #define MARK_DIFF_ENCODE 0x40000000
  157. #define MATCH_FLAG_OOB_TRANSITION 0x20000000
  158. #define MATCH_FLAGS_MASK 0xff000000
  159. #define MATCH_FLAGS_VALID (MATCH_FLAG_DIFF_ENCODE | MATCH_FLAG_OOB_TRANSITION)
  160. #define MATCH_FLAGS_INVALID (MATCH_FLAGS_MASK & ~MATCH_FLAGS_VALID)
  161. #endif /* __AA_MATCH_H */