smack.h 5.6 KB

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