policy_unpack.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor policy loading interface function definitions.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #ifndef __POLICY_INTERFACE_H
  11. #define __POLICY_INTERFACE_H
  12. #include <linux/list.h>
  13. #include <linux/kref.h>
  14. #include <linux/dcache.h>
  15. #include <linux/workqueue.h>
  16. struct aa_load_ent {
  17. struct list_head list;
  18. struct aa_profile *new;
  19. struct aa_profile *old;
  20. struct aa_profile *rename;
  21. const char *ns_name;
  22. };
  23. void aa_load_ent_free(struct aa_load_ent *ent);
  24. struct aa_load_ent *aa_load_ent_alloc(void);
  25. #define PACKED_FLAG_HAT 1
  26. #define PACKED_FLAG_DEBUG1 2
  27. #define PACKED_FLAG_DEBUG2 4
  28. #define PACKED_MODE_ENFORCE 0
  29. #define PACKED_MODE_COMPLAIN 1
  30. #define PACKED_MODE_KILL 2
  31. #define PACKED_MODE_UNCONFINED 3
  32. struct aa_ns;
  33. enum {
  34. AAFS_LOADDATA_ABI = 0,
  35. AAFS_LOADDATA_REVISION,
  36. AAFS_LOADDATA_HASH,
  37. AAFS_LOADDATA_DATA,
  38. AAFS_LOADDATA_COMPRESSED_SIZE,
  39. AAFS_LOADDATA_DIR, /* must be last actual entry */
  40. AAFS_LOADDATA_NDENTS /* count of entries */
  41. };
  42. /*
  43. * The AppArmor interface treats data as a type byte followed by the
  44. * actual data. The interface has the notion of a named entry
  45. * which has a name (AA_NAME typecode followed by name string) followed by
  46. * the entries typecode and data. Named types allow for optional
  47. * elements and extensions to be added and tested for without breaking
  48. * backwards compatibility.
  49. */
  50. enum aa_code {
  51. AA_U8,
  52. AA_U16,
  53. AA_U32,
  54. AA_U64,
  55. AA_NAME, /* same as string except it is items name */
  56. AA_STRING,
  57. AA_BLOB,
  58. AA_STRUCT,
  59. AA_STRUCTEND,
  60. AA_LIST,
  61. AA_LISTEND,
  62. AA_ARRAY,
  63. AA_ARRAYEND,
  64. };
  65. /*
  66. * aa_ext is the read of the buffer containing the serialized profile. The
  67. * data is copied into a kernel buffer in apparmorfs and then handed off to
  68. * the unpack routines.
  69. */
  70. struct aa_ext {
  71. void *start;
  72. void *end;
  73. void *pos; /* pointer to current position in the buffer */
  74. u32 version;
  75. };
  76. /*
  77. * struct aa_loaddata - buffer of policy raw_data set
  78. *
  79. * there is no loaddata ref for being on ns list, nor a ref from
  80. * d_inode(@dentry) when grab a ref from these, @ns->lock must be held
  81. * && __aa_get_loaddata() needs to be used, and the return value
  82. * checked, if NULL the loaddata is already being reaped and should be
  83. * considered dead.
  84. */
  85. struct aa_loaddata {
  86. struct kref count;
  87. struct list_head list;
  88. struct work_struct work;
  89. struct dentry *dents[AAFS_LOADDATA_NDENTS];
  90. struct aa_ns *ns;
  91. char *name;
  92. size_t size; /* the original size of the payload */
  93. size_t compressed_size; /* the compressed size of the payload */
  94. long revision; /* the ns policy revision this caused */
  95. int abi;
  96. unsigned char *hash;
  97. /* Pointer to payload. If @compressed_size > 0, then this is the
  98. * compressed version of the payload, else it is the uncompressed
  99. * version (with the size indicated by @size).
  100. */
  101. char *data;
  102. };
  103. int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, const char **ns);
  104. /**
  105. * __aa_get_loaddata - get a reference count to uncounted data reference
  106. * @data: reference to get a count on
  107. *
  108. * Returns: pointer to reference OR NULL if race is lost and reference is
  109. * being repeated.
  110. * Requires: @data->ns->lock held, and the return code MUST be checked
  111. *
  112. * Use only from inode->i_private and @data->list found references
  113. */
  114. static inline struct aa_loaddata *
  115. __aa_get_loaddata(struct aa_loaddata *data)
  116. {
  117. if (data && kref_get_unless_zero(&(data->count)))
  118. return data;
  119. return NULL;
  120. }
  121. /**
  122. * aa_get_loaddata - get a reference count from a counted data reference
  123. * @data: reference to get a count on
  124. *
  125. * Returns: point to reference
  126. * Requires: @data to have a valid reference count on it. It is a bug
  127. * if the race to reap can be encountered when it is used.
  128. */
  129. static inline struct aa_loaddata *
  130. aa_get_loaddata(struct aa_loaddata *data)
  131. {
  132. struct aa_loaddata *tmp = __aa_get_loaddata(data);
  133. AA_BUG(data && !tmp);
  134. return tmp;
  135. }
  136. void __aa_loaddata_update(struct aa_loaddata *data, long revision);
  137. bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r);
  138. void aa_loaddata_kref(struct kref *kref);
  139. struct aa_loaddata *aa_loaddata_alloc(size_t size);
  140. static inline void aa_put_loaddata(struct aa_loaddata *data)
  141. {
  142. if (data)
  143. kref_put(&data->count, aa_loaddata_kref);
  144. }
  145. #if IS_ENABLED(CONFIG_KUNIT)
  146. bool aa_inbounds(struct aa_ext *e, size_t size);
  147. size_t aa_unpack_u16_chunk(struct aa_ext *e, char **chunk);
  148. bool aa_unpack_X(struct aa_ext *e, enum aa_code code);
  149. bool aa_unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name);
  150. bool aa_unpack_u32(struct aa_ext *e, u32 *data, const char *name);
  151. bool aa_unpack_u64(struct aa_ext *e, u64 *data, const char *name);
  152. size_t aa_unpack_array(struct aa_ext *e, const char *name);
  153. size_t aa_unpack_blob(struct aa_ext *e, char **blob, const char *name);
  154. int aa_unpack_str(struct aa_ext *e, const char **string, const char *name);
  155. int aa_unpack_strdup(struct aa_ext *e, char **string, const char *name);
  156. #endif
  157. #endif /* __POLICY_INTERFACE_H */