smack.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <net/netlabel.h>
  17. /*
  18. * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
  19. * bigger than can be used, and 24 is the next lower multiple
  20. * of 8, and there are too many issues if there isn't space set
  21. * aside for the terminating null byte.
  22. */
  23. #define SMK_MAXLEN 23
  24. #define SMK_LABELLEN (SMK_MAXLEN+1)
  25. /*
  26. * How many kinds of access are there?
  27. * Here's your answer.
  28. */
  29. #define SMK_ACCESSDASH '-'
  30. #define SMK_ACCESSLOW "rwxa"
  31. #define SMK_ACCESSKINDS (sizeof(SMK_ACCESSLOW) - 1)
  32. struct superblock_smack {
  33. char *smk_root;
  34. char *smk_floor;
  35. char *smk_hat;
  36. char *smk_default;
  37. int smk_initialized;
  38. spinlock_t smk_sblock; /* for initialization */
  39. };
  40. struct socket_smack {
  41. char *smk_out; /* outbound label */
  42. char *smk_in; /* inbound label */
  43. char smk_packet[SMK_LABELLEN]; /* TCP peer label */
  44. };
  45. /*
  46. * Inode smack data
  47. */
  48. struct inode_smack {
  49. char *smk_inode; /* label of the fso */
  50. struct mutex smk_lock; /* initialization lock */
  51. int smk_flags; /* smack inode flags */
  52. };
  53. #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
  54. /*
  55. * A label access rule.
  56. */
  57. struct smack_rule {
  58. char *smk_subject;
  59. char *smk_object;
  60. int smk_access;
  61. };
  62. /*
  63. * An entry in the table of permitted label accesses.
  64. */
  65. struct smk_list_entry {
  66. struct smk_list_entry *smk_next;
  67. struct smack_rule smk_rule;
  68. };
  69. /*
  70. * An entry in the table mapping smack values to
  71. * CIPSO level/category-set values.
  72. */
  73. struct smack_cipso {
  74. int smk_level;
  75. char smk_catset[SMK_LABELLEN];
  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 smack_known *smk_next;
  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. /*
  122. * smackfs macic number
  123. */
  124. #define SMACK_MAGIC 0x43415d53 /* "SMAC" */
  125. /*
  126. * A limit on the number of entries in the lists
  127. * makes some of the list administration easier.
  128. */
  129. #define SMACK_LIST_MAX 10000
  130. /*
  131. * CIPSO defaults.
  132. */
  133. #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
  134. #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
  135. #define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */
  136. #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
  137. #define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */
  138. /*
  139. * Just to make the common cases easier to deal with
  140. */
  141. #define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  142. #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
  143. #define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND)
  144. #define MAY_READWRITE (MAY_READ | MAY_WRITE)
  145. #define MAY_NOT 0
  146. /*
  147. * These functions are in smack_lsm.c
  148. */
  149. struct inode_smack *new_inode_smack(char *);
  150. /*
  151. * These functions are in smack_access.c
  152. */
  153. int smk_access(char *, char *, int);
  154. int smk_curacc(char *, u32);
  155. int smack_to_cipso(const char *, struct smack_cipso *);
  156. void smack_from_cipso(u32, char *, char *);
  157. char *smack_from_secid(const u32);
  158. char *smk_import(const char *, int);
  159. struct smack_known *smk_import_entry(const char *, int);
  160. u32 smack_to_secid(const char *);
  161. /*
  162. * Shared data.
  163. */
  164. extern int smack_cipso_direct;
  165. extern int smack_net_nltype;
  166. extern char *smack_net_ambient;
  167. extern struct smack_known *smack_known;
  168. extern struct smack_known smack_known_floor;
  169. extern struct smack_known smack_known_hat;
  170. extern struct smack_known smack_known_huh;
  171. extern struct smack_known smack_known_invalid;
  172. extern struct smack_known smack_known_star;
  173. extern struct smack_known smack_known_unset;
  174. extern struct smk_list_entry *smack_list;
  175. /*
  176. * Stricly for CIPSO level manipulation.
  177. * Set the category bit number in a smack label sized buffer.
  178. */
  179. static inline void smack_catset_bit(int cat, char *catsetp)
  180. {
  181. if (cat > SMK_LABELLEN * 8)
  182. return;
  183. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  184. }
  185. /*
  186. * Present a pointer to the smack label in an inode blob.
  187. */
  188. static inline char *smk_of_inode(const struct inode *isp)
  189. {
  190. struct inode_smack *sip = isp->i_security;
  191. return sip->smk_inode;
  192. }
  193. #endif /* _SECURITY_SMACK_H */