policy_unpack.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #define PACKED_MODE_USER 4
  33. struct aa_ns;
  34. enum {
  35. AAFS_LOADDATA_ABI = 0,
  36. AAFS_LOADDATA_REVISION,
  37. AAFS_LOADDATA_HASH,
  38. AAFS_LOADDATA_DATA,
  39. AAFS_LOADDATA_COMPRESSED_SIZE,
  40. AAFS_LOADDATA_DIR, /* must be last actual entry */
  41. AAFS_LOADDATA_NDENTS /* count of entries */
  42. };
  43. /*
  44. * The AppArmor interface treats data as a type byte followed by the
  45. * actual data. The interface has the notion of a named entry
  46. * which has a name (AA_NAME typecode followed by name string) followed by
  47. * the entries typecode and data. Named types allow for optional
  48. * elements and extensions to be added and tested for without breaking
  49. * backwards compatibility.
  50. */
  51. enum aa_code {
  52. AA_U8,
  53. AA_U16,
  54. AA_U32,
  55. AA_U64,
  56. AA_NAME, /* same as string except it is items name */
  57. AA_STRING,
  58. AA_BLOB,
  59. AA_STRUCT,
  60. AA_STRUCTEND,
  61. AA_LIST,
  62. AA_LISTEND,
  63. AA_ARRAY,
  64. AA_ARRAYEND,
  65. };
  66. /*
  67. * aa_ext is the read of the buffer containing the serialized profile. The
  68. * data is copied into a kernel buffer in apparmorfs and then handed off to
  69. * the unpack routines.
  70. */
  71. struct aa_ext {
  72. void *start;
  73. void *end;
  74. void *pos; /* pointer to current position in the buffer */
  75. u32 version;
  76. };
  77. /* struct aa_loaddata - buffer of policy raw_data set
  78. * @count: inode/filesystem refcount - use aa_get_i_loaddata()
  79. * @pcount: profile refcount - use aa_get_profile_loaddata()
  80. * @list: list the loaddata is on
  81. * @work: used to do a delayed cleanup
  82. * @dents: refs to dents created in aafs
  83. * @ns: the namespace this loaddata was loaded into
  84. * @name:
  85. * @size: the size of the data that was loaded
  86. * @compressed_size: the size of the data when it is compressed
  87. * @revision: unique revision count that this data was loaded as
  88. * @abi: the abi number the loaddata uses
  89. * @hash: a hash of the loaddata, used to help dedup data
  90. *
  91. * There is no loaddata ref for being on ns->rawdata_list, so
  92. * @ns->lock must be held when walking the list. Dentries and
  93. * inode opens hold refs on @count; profiles hold refs on @pcount.
  94. * When the last @pcount drops, do_ploaddata_rmfs() removes the
  95. * fs entries and drops the associated @count ref.
  96. */
  97. struct aa_loaddata {
  98. struct aa_common_ref count;
  99. struct kref pcount;
  100. struct list_head list;
  101. struct work_struct work;
  102. struct dentry *dents[AAFS_LOADDATA_NDENTS];
  103. struct aa_ns *ns;
  104. char *name;
  105. size_t size; /* the original size of the payload */
  106. size_t compressed_size; /* the compressed size of the payload */
  107. long revision; /* the ns policy revision this caused */
  108. int abi;
  109. unsigned char *hash;
  110. /* Pointer to payload. If @compressed_size > 0, then this is the
  111. * compressed version of the payload, else it is the uncompressed
  112. * version (with the size indicated by @size).
  113. */
  114. char *data;
  115. };
  116. int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, const char **ns);
  117. /**
  118. * aa_get_loaddata - get a reference count from a counted data reference
  119. * @data: reference to get a count on
  120. *
  121. * Returns: pointer to reference
  122. * Requires: @data to have a valid reference count on it. It is a bug
  123. * if the race to reap can be encountered when it is used.
  124. */
  125. static inline struct aa_loaddata *
  126. aa_get_i_loaddata(struct aa_loaddata *data)
  127. {
  128. if (data)
  129. kref_get(&(data->count.count));
  130. return data;
  131. }
  132. /**
  133. * aa_get_profile_loaddata - get a profile reference count on loaddata
  134. * @data: reference to get a count on
  135. *
  136. * Returns: pointer to reference
  137. * Requires: @data to have a valid reference count on it.
  138. */
  139. static inline struct aa_loaddata *
  140. aa_get_profile_loaddata(struct aa_loaddata *data)
  141. {
  142. if (data)
  143. kref_get(&(data->pcount));
  144. return data;
  145. }
  146. void __aa_loaddata_update(struct aa_loaddata *data, long revision);
  147. bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r);
  148. void aa_loaddata_kref(struct kref *kref);
  149. void aa_ploaddata_kref(struct kref *kref);
  150. struct aa_loaddata *aa_loaddata_alloc(size_t size);
  151. static inline void aa_put_i_loaddata(struct aa_loaddata *data)
  152. {
  153. if (data)
  154. kref_put(&data->count.count, aa_loaddata_kref);
  155. }
  156. static inline void aa_put_profile_loaddata(struct aa_loaddata *data)
  157. {
  158. if (data)
  159. kref_put(&data->pcount, aa_ploaddata_kref);
  160. }
  161. #if IS_ENABLED(CONFIG_KUNIT)
  162. bool aa_inbounds(struct aa_ext *e, size_t size);
  163. size_t aa_unpack_u16_chunk(struct aa_ext *e, char **chunk);
  164. bool aa_unpack_X(struct aa_ext *e, enum aa_code code);
  165. bool aa_unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name);
  166. bool aa_unpack_u32(struct aa_ext *e, u32 *data, const char *name);
  167. bool aa_unpack_u64(struct aa_ext *e, u64 *data, const char *name);
  168. bool aa_unpack_array(struct aa_ext *e, const char *name, u16 *size);
  169. size_t aa_unpack_blob(struct aa_ext *e, char **blob, const char *name);
  170. int aa_unpack_str(struct aa_ext *e, const char **string, const char *name);
  171. int aa_unpack_strdup(struct aa_ext *e, char **string, const char *name);
  172. #endif
  173. #endif /* __POLICY_INTERFACE_H */