smack.h 6.3 KB

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