smack.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 2.
  7. *
  8. * Author:
  9. * Casey Schaufler <casey@schaufler-ca.com>
  10. *
  11. */
  12. #ifndef _SECURITY_SMACK_H
  13. #define _SECURITY_SMACK_H
  14. #include <linux/capability.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/security.h>
  17. #include <linux/in.h>
  18. #include <net/netlabel.h>
  19. /*
  20. * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
  21. * bigger than can be used, and 24 is the next lower multiple
  22. * of 8, and there are too many issues if there isn't space set
  23. * aside for the terminating null byte.
  24. */
  25. #define SMK_MAXLEN 23
  26. #define SMK_LABELLEN (SMK_MAXLEN+1)
  27. struct superblock_smack {
  28. char *smk_root;
  29. char *smk_floor;
  30. char *smk_hat;
  31. char *smk_default;
  32. int smk_initialized;
  33. spinlock_t smk_sblock; /* for initialization */
  34. };
  35. struct socket_smack {
  36. char *smk_out; /* outbound label */
  37. char *smk_in; /* inbound label */
  38. int smk_labeled; /* label scheme */
  39. char smk_packet[SMK_LABELLEN]; /* TCP peer label */
  40. };
  41. /*
  42. * Inode smack data
  43. */
  44. struct inode_smack {
  45. char *smk_inode; /* label of the fso */
  46. struct mutex smk_lock; /* initialization lock */
  47. int smk_flags; /* smack inode flags */
  48. };
  49. #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
  50. /*
  51. * A label access rule.
  52. */
  53. struct smack_rule {
  54. char *smk_subject;
  55. char *smk_object;
  56. int smk_access;
  57. };
  58. /*
  59. * An entry in the table of permitted label accesses.
  60. */
  61. struct smk_list_entry {
  62. struct smk_list_entry *smk_next;
  63. struct smack_rule smk_rule;
  64. };
  65. /*
  66. * An entry in the table mapping smack values to
  67. * CIPSO level/category-set values.
  68. */
  69. struct smack_cipso {
  70. int smk_level;
  71. char smk_catset[SMK_LABELLEN];
  72. };
  73. /*
  74. * An entry in the table identifying hosts.
  75. */
  76. struct smk_netlbladdr {
  77. struct smk_netlbladdr *smk_next;
  78. struct sockaddr_in smk_host; /* network address */
  79. struct in_addr smk_mask; /* network mask */
  80. char *smk_label; /* label */
  81. };
  82. /*
  83. * This is the repository for labels seen so that it is
  84. * not necessary to keep allocating tiny chuncks of memory
  85. * and so that they can be shared.
  86. *
  87. * Labels are never modified in place. Anytime a label
  88. * is imported (e.g. xattrset on a file) the list is checked
  89. * for it and it is added if it doesn't exist. The address
  90. * is passed out in either case. Entries are added, but
  91. * never deleted.
  92. *
  93. * Since labels are hanging around anyway it doesn't
  94. * hurt to maintain a secid for those awkward situations
  95. * where kernel components that ought to use LSM independent
  96. * interfaces don't. The secid should go away when all of
  97. * these components have been repaired.
  98. *
  99. * If there is a cipso value associated with the label it
  100. * gets stored here, too. This will most likely be rare as
  101. * the cipso direct mapping in used internally.
  102. */
  103. struct smack_known {
  104. struct smack_known *smk_next;
  105. char smk_known[SMK_LABELLEN];
  106. u32 smk_secid;
  107. struct smack_cipso *smk_cipso;
  108. spinlock_t smk_cipsolock; /* for changing cipso map */
  109. };
  110. /*
  111. * Mount options
  112. */
  113. #define SMK_FSDEFAULT "smackfsdef="
  114. #define SMK_FSFLOOR "smackfsfloor="
  115. #define SMK_FSHAT "smackfshat="
  116. #define SMK_FSROOT "smackfsroot="
  117. /*
  118. * xattr names
  119. */
  120. #define XATTR_SMACK_SUFFIX "SMACK64"
  121. #define XATTR_SMACK_IPIN "SMACK64IPIN"
  122. #define XATTR_SMACK_IPOUT "SMACK64IPOUT"
  123. #define XATTR_NAME_SMACK XATTR_SECURITY_PREFIX XATTR_SMACK_SUFFIX
  124. #define XATTR_NAME_SMACKIPIN XATTR_SECURITY_PREFIX XATTR_SMACK_IPIN
  125. #define XATTR_NAME_SMACKIPOUT XATTR_SECURITY_PREFIX XATTR_SMACK_IPOUT
  126. /*
  127. * How communications on this socket are treated.
  128. * Usually it's determined by the underlying netlabel code
  129. * but there are certain cases, including single label hosts
  130. * and potentially single label interfaces for which the
  131. * treatment can not be known in advance.
  132. *
  133. * The possibility of additional labeling schemes being
  134. * introduced in the future exists as well.
  135. */
  136. #define SMACK_UNLABELED_SOCKET 0
  137. #define SMACK_CIPSO_SOCKET 1
  138. /*
  139. * smackfs magic number
  140. * smackfs macic number
  141. */
  142. #define SMACK_MAGIC 0x43415d53 /* "SMAC" */
  143. /*
  144. * A limit on the number of entries in the lists
  145. * makes some of the list administration easier.
  146. */
  147. #define SMACK_LIST_MAX 10000
  148. /*
  149. * CIPSO defaults.
  150. */
  151. #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
  152. #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
  153. #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
  154. #define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */
  155. #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
  156. #define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */
  157. /*
  158. * Just to make the common cases easier to deal with
  159. */
  160. #define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  161. #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
  162. #define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND)
  163. #define MAY_READWRITE (MAY_READ | MAY_WRITE)
  164. #define MAY_NOT 0
  165. /*
  166. * These functions are in smack_lsm.c
  167. */
  168. struct inode_smack *new_inode_smack(char *);
  169. /*
  170. * These functions are in smack_access.c
  171. */
  172. int smk_access(char *, char *, int);
  173. int smk_curacc(char *, u32);
  174. int smack_to_cipso(const char *, struct smack_cipso *);
  175. void smack_from_cipso(u32, char *, char *);
  176. char *smack_from_secid(const u32);
  177. char *smk_import(const char *, int);
  178. struct smack_known *smk_import_entry(const char *, int);
  179. u32 smack_to_secid(const char *);
  180. /*
  181. * Shared data.
  182. */
  183. extern int smack_cipso_direct;
  184. extern char *smack_net_ambient;
  185. extern char *smack_onlycap;
  186. extern struct smack_known *smack_known;
  187. extern struct smack_known smack_known_floor;
  188. extern struct smack_known smack_known_hat;
  189. extern struct smack_known smack_known_huh;
  190. extern struct smack_known smack_known_invalid;
  191. extern struct smack_known smack_known_star;
  192. extern struct smack_known smack_known_web;
  193. extern struct smk_list_entry *smack_list;
  194. extern struct smk_netlbladdr *smack_netlbladdrs;
  195. extern struct security_operations smack_ops;
  196. /*
  197. * Stricly for CIPSO level manipulation.
  198. * Set the category bit number in a smack label sized buffer.
  199. */
  200. static inline void smack_catset_bit(int cat, char *catsetp)
  201. {
  202. if (cat > SMK_LABELLEN * 8)
  203. return;
  204. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  205. }
  206. /*
  207. * Present a pointer to the smack label in an inode blob.
  208. */
  209. static inline char *smk_of_inode(const struct inode *isp)
  210. {
  211. struct inode_smack *sip = isp->i_security;
  212. return sip->smk_inode;
  213. }
  214. #endif /* _SECURITY_SMACK_H */