policy_unpack.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /*
  78. * struct aa_loaddata - buffer of policy raw_data set
  79. *
  80. * there is no loaddata ref for being on ns list, nor a ref from
  81. * d_inode(@dentry) when grab a ref from these, @ns->lock must be held
  82. * && __aa_get_loaddata() needs to be used, and the return value
  83. * checked, if NULL the loaddata is already being reaped and should be
  84. * considered dead.
  85. */
  86. struct aa_loaddata {
  87. struct kref count;
  88. struct list_head list;
  89. struct work_struct work;
  90. struct dentry *dents[AAFS_LOADDATA_NDENTS];
  91. struct aa_ns *ns;
  92. char *name;
  93. size_t size; /* the original size of the payload */
  94. size_t compressed_size; /* the compressed size of the payload */
  95. long revision; /* the ns policy revision this caused */
  96. int abi;
  97. unsigned char *hash;
  98. /* Pointer to payload. If @compressed_size > 0, then this is the
  99. * compressed version of the payload, else it is the uncompressed
  100. * version (with the size indicated by @size).
  101. */
  102. char *data;
  103. };
  104. int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, const char **ns);
  105. /**
  106. * __aa_get_loaddata - get a reference count to uncounted data reference
  107. * @data: reference to get a count on
  108. *
  109. * Returns: pointer to reference OR NULL if race is lost and reference is
  110. * being repeated.
  111. * Requires: @data->ns->lock held, and the return code MUST be checked
  112. *
  113. * Use only from inode->i_private and @data->list found references
  114. */
  115. static inline struct aa_loaddata *
  116. __aa_get_loaddata(struct aa_loaddata *data)
  117. {
  118. if (data && kref_get_unless_zero(&(data->count)))
  119. return data;
  120. return NULL;
  121. }
  122. /**
  123. * aa_get_loaddata - get a reference count from a counted data reference
  124. * @data: reference to get a count on
  125. *
  126. * Returns: point to reference
  127. * Requires: @data to have a valid reference count on it. It is a bug
  128. * if the race to reap can be encountered when it is used.
  129. */
  130. static inline struct aa_loaddata *
  131. aa_get_loaddata(struct aa_loaddata *data)
  132. {
  133. struct aa_loaddata *tmp = __aa_get_loaddata(data);
  134. AA_BUG(data && !tmp);
  135. return tmp;
  136. }
  137. void __aa_loaddata_update(struct aa_loaddata *data, long revision);
  138. bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r);
  139. void aa_loaddata_kref(struct kref *kref);
  140. struct aa_loaddata *aa_loaddata_alloc(size_t size);
  141. static inline void aa_put_loaddata(struct aa_loaddata *data)
  142. {
  143. if (data)
  144. kref_put(&data->count, aa_loaddata_kref);
  145. }
  146. #if IS_ENABLED(CONFIG_KUNIT)
  147. bool aa_inbounds(struct aa_ext *e, size_t size);
  148. size_t aa_unpack_u16_chunk(struct aa_ext *e, char **chunk);
  149. bool aa_unpack_X(struct aa_ext *e, enum aa_code code);
  150. bool aa_unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name);
  151. bool aa_unpack_u32(struct aa_ext *e, u32 *data, const char *name);
  152. bool aa_unpack_u64(struct aa_ext *e, u64 *data, const char *name);
  153. bool aa_unpack_array(struct aa_ext *e, const char *name, u16 *size);
  154. size_t aa_unpack_blob(struct aa_ext *e, char **blob, const char *name);
  155. int aa_unpack_str(struct aa_ext *e, const char **string, const char *name);
  156. int aa_unpack_strdup(struct aa_ext *e, char **string, const char *name);
  157. #endif
  158. #endif /* __POLICY_INTERFACE_H */