match.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor policy dfa matching engine definitions.
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #ifndef __AA_MATCH_H
  15. #define __AA_MATCH_H
  16. #include <linux/kref.h>
  17. #include <linux/workqueue.h>
  18. #define DFA_NOMATCH 0
  19. #define DFA_START 1
  20. #define DFA_VALID_PERM_MASK 0xffffffff
  21. #define DFA_VALID_PERM2_MASK 0xffffffff
  22. /**
  23. * The format used for transition tables is based on the GNU flex table
  24. * file format (--tables-file option; see Table File Format in the flex
  25. * info pages and the flex sources for documentation). The magic number
  26. * used in the header is 0x1B5E783D instead of 0xF13C57B1 though, because
  27. * the YY_ID_CHK (check) and YY_ID_DEF (default) tables are used
  28. * slightly differently (see the apparmor-parser package).
  29. */
  30. #define YYTH_MAGIC 0x1B5E783D
  31. #define YYTH_DEF_RECURSE 0x1 /* DEF Table is recursive */
  32. struct table_set_header {
  33. u32 th_magic; /* YYTH_MAGIC */
  34. u32 th_hsize;
  35. u32 th_ssize;
  36. u16 th_flags;
  37. char th_version[];
  38. };
  39. /* The YYTD_ID are one less than flex table mappings. The flex id
  40. * has 1 subtracted at table load time, this allows us to directly use the
  41. * ID's as indexes.
  42. */
  43. #define YYTD_ID_ACCEPT 0
  44. #define YYTD_ID_BASE 1
  45. #define YYTD_ID_CHK 2
  46. #define YYTD_ID_DEF 3
  47. #define YYTD_ID_EC 4
  48. #define YYTD_ID_META 5
  49. #define YYTD_ID_ACCEPT2 6
  50. #define YYTD_ID_NXT 7
  51. #define YYTD_ID_TSIZE 8
  52. #define YYTD_DATA8 1
  53. #define YYTD_DATA16 2
  54. #define YYTD_DATA32 4
  55. #define YYTD_DATA64 8
  56. /* Each ACCEPT2 table gets 6 dedicated flags, YYTD_DATAX define the
  57. * first flags
  58. */
  59. #define ACCEPT1_FLAGS(X) ((X) & 0x3f)
  60. #define ACCEPT2_FLAGS(X) ACCEPT1_FLAGS((X) >> YYTD_ID_ACCEPT2)
  61. #define TO_ACCEPT1_FLAG(X) ACCEPT1_FLAGS(X)
  62. #define TO_ACCEPT2_FLAG(X) (ACCEPT1_FLAGS(X) << YYTD_ID_ACCEPT2)
  63. #define DFA_FLAG_VERIFY_STATES 0x1000
  64. struct table_header {
  65. u16 td_id;
  66. u16 td_flags;
  67. u32 td_hilen;
  68. u32 td_lolen;
  69. char td_data[];
  70. };
  71. #define DEFAULT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_DEF]->td_data))
  72. #define BASE_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_BASE]->td_data))
  73. #define NEXT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_NXT]->td_data))
  74. #define CHECK_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_CHK]->td_data))
  75. #define EQUIV_TABLE(DFA) ((u8 *)((DFA)->tables[YYTD_ID_EC]->td_data))
  76. #define ACCEPT_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT]->td_data))
  77. #define ACCEPT_TABLE2(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT2]->td_data))
  78. struct aa_dfa {
  79. struct kref count;
  80. u16 flags;
  81. struct table_header *tables[YYTD_ID_TSIZE];
  82. };
  83. #define byte_to_byte(X) (X)
  84. #define UNPACK_ARRAY(TABLE, BLOB, LEN, TYPE, NTOHX) \
  85. do { \
  86. typeof(LEN) __i; \
  87. TYPE *__t = (TYPE *) TABLE; \
  88. TYPE *__b = (TYPE *) BLOB; \
  89. for (__i = 0; __i < LEN; __i++) { \
  90. __t[__i] = NTOHX(__b[__i]); \
  91. } \
  92. } while (0)
  93. static inline size_t table_size(size_t len, size_t el_size)
  94. {
  95. return ALIGN(sizeof(struct table_header) + len * el_size, 8);
  96. }
  97. struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags);
  98. unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
  99. const char *str, int len);
  100. unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
  101. const char *str);
  102. unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
  103. const char c);
  104. void aa_dfa_free_kref(struct kref *kref);
  105. /**
  106. * aa_put_dfa - put a dfa refcount
  107. * @dfa: dfa to put refcount (MAYBE NULL)
  108. *
  109. * Requires: if @dfa != NULL that a valid refcount be held
  110. */
  111. static inline void aa_put_dfa(struct aa_dfa *dfa)
  112. {
  113. if (dfa)
  114. kref_put(&dfa->count, aa_dfa_free_kref);
  115. }
  116. #endif /* __AA_MATCH_H */