smack.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include <linux/lsm_audit.h>
  22. /*
  23. * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
  24. * bigger than can be used, and 24 is the next lower multiple
  25. * of 8, and there are too many issues if there isn't space set
  26. * aside for the terminating null byte.
  27. */
  28. #define SMK_MAXLEN 23
  29. #define SMK_LABELLEN (SMK_MAXLEN+1)
  30. struct superblock_smack {
  31. char *smk_root;
  32. char *smk_floor;
  33. char *smk_hat;
  34. char *smk_default;
  35. int smk_initialized;
  36. spinlock_t smk_sblock; /* for initialization */
  37. };
  38. struct socket_smack {
  39. char *smk_out; /* outbound label */
  40. char *smk_in; /* inbound label */
  41. char smk_packet[SMK_LABELLEN]; /* TCP peer label */
  42. };
  43. /*
  44. * Inode smack data
  45. */
  46. struct inode_smack {
  47. char *smk_inode; /* label of the fso */
  48. struct mutex smk_lock; /* initialization lock */
  49. int smk_flags; /* smack inode flags */
  50. };
  51. #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
  52. /*
  53. * A label access rule.
  54. */
  55. struct smack_rule {
  56. struct list_head list;
  57. char *smk_subject;
  58. char *smk_object;
  59. int smk_access;
  60. };
  61. /*
  62. * An entry in the table mapping smack values to
  63. * CIPSO level/category-set values.
  64. */
  65. struct smack_cipso {
  66. int smk_level;
  67. char smk_catset[SMK_LABELLEN];
  68. };
  69. /*
  70. * An entry in the table identifying hosts.
  71. */
  72. struct smk_netlbladdr {
  73. struct list_head list;
  74. struct sockaddr_in smk_host; /* network address */
  75. struct in_addr smk_mask; /* network mask */
  76. char *smk_label; /* label */
  77. };
  78. /*
  79. * This is the repository for labels seen so that it is
  80. * not necessary to keep allocating tiny chuncks of memory
  81. * and so that they can be shared.
  82. *
  83. * Labels are never modified in place. Anytime a label
  84. * is imported (e.g. xattrset on a file) the list is checked
  85. * for it and it is added if it doesn't exist. The address
  86. * is passed out in either case. Entries are added, but
  87. * never deleted.
  88. *
  89. * Since labels are hanging around anyway it doesn't
  90. * hurt to maintain a secid for those awkward situations
  91. * where kernel components that ought to use LSM independent
  92. * interfaces don't. The secid should go away when all of
  93. * these components have been repaired.
  94. *
  95. * If there is a cipso value associated with the label it
  96. * gets stored here, too. This will most likely be rare as
  97. * the cipso direct mapping in used internally.
  98. */
  99. struct smack_known {
  100. struct list_head list;
  101. char smk_known[SMK_LABELLEN];
  102. u32 smk_secid;
  103. struct smack_cipso *smk_cipso;
  104. spinlock_t smk_cipsolock; /* for changing cipso map */
  105. };
  106. /*
  107. * Mount options
  108. */
  109. #define SMK_FSDEFAULT "smackfsdef="
  110. #define SMK_FSFLOOR "smackfsfloor="
  111. #define SMK_FSHAT "smackfshat="
  112. #define SMK_FSROOT "smackfsroot="
  113. /*
  114. * xattr names
  115. */
  116. #define XATTR_SMACK_SUFFIX "SMACK64"
  117. #define XATTR_SMACK_IPIN "SMACK64IPIN"
  118. #define XATTR_SMACK_IPOUT "SMACK64IPOUT"
  119. #define XATTR_NAME_SMACK XATTR_SECURITY_PREFIX XATTR_SMACK_SUFFIX
  120. #define XATTR_NAME_SMACKIPIN XATTR_SECURITY_PREFIX XATTR_SMACK_IPIN
  121. #define XATTR_NAME_SMACKIPOUT XATTR_SECURITY_PREFIX XATTR_SMACK_IPOUT
  122. #define SMACK_CIPSO_OPTION "-CIPSO"
  123. /*
  124. * How communications on this socket are treated.
  125. * Usually it's determined by the underlying netlabel code
  126. * but there are certain cases, including single label hosts
  127. * and potentially single label interfaces for which the
  128. * treatment can not be known in advance.
  129. *
  130. * The possibility of additional labeling schemes being
  131. * introduced in the future exists as well.
  132. */
  133. #define SMACK_UNLABELED_SOCKET 0
  134. #define SMACK_CIPSO_SOCKET 1
  135. /*
  136. * smackfs magic number
  137. * smackfs macic number
  138. */
  139. #define SMACK_MAGIC 0x43415d53 /* "SMAC" */
  140. /*
  141. * A limit on the number of entries in the lists
  142. * makes some of the list administration easier.
  143. */
  144. #define SMACK_LIST_MAX 10000
  145. /*
  146. * CIPSO defaults.
  147. */
  148. #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
  149. #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
  150. #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
  151. #define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */
  152. #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
  153. #define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */
  154. /*
  155. * Just to make the common cases easier to deal with
  156. */
  157. #define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
  158. #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
  159. #define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND)
  160. #define MAY_READWRITE (MAY_READ | MAY_WRITE)
  161. #define MAY_NOT 0
  162. /*
  163. * Number of access types used by Smack (rwxa)
  164. */
  165. #define SMK_NUM_ACCESS_TYPE 4
  166. /*
  167. * Smack audit data; is empty if CONFIG_AUDIT not set
  168. * to save some stack
  169. */
  170. struct smk_audit_info {
  171. #ifdef CONFIG_AUDIT
  172. struct common_audit_data a;
  173. #endif
  174. };
  175. /*
  176. * These functions are in smack_lsm.c
  177. */
  178. struct inode_smack *new_inode_smack(char *);
  179. /*
  180. * These functions are in smack_access.c
  181. */
  182. int smk_access(char *, char *, int, struct smk_audit_info *);
  183. int smk_curacc(char *, u32, struct smk_audit_info *);
  184. int smack_to_cipso(const char *, struct smack_cipso *);
  185. void smack_from_cipso(u32, char *, char *);
  186. char *smack_from_secid(const u32);
  187. char *smk_import(const char *, int);
  188. struct smack_known *smk_import_entry(const char *, int);
  189. u32 smack_to_secid(const char *);
  190. /*
  191. * Shared data.
  192. */
  193. extern int smack_cipso_direct;
  194. extern char *smack_net_ambient;
  195. extern char *smack_onlycap;
  196. extern const char *smack_cipso_option;
  197. extern struct smack_known smack_known_floor;
  198. extern struct smack_known smack_known_hat;
  199. extern struct smack_known smack_known_huh;
  200. extern struct smack_known smack_known_invalid;
  201. extern struct smack_known smack_known_star;
  202. extern struct smack_known smack_known_web;
  203. extern struct list_head smack_known_list;
  204. extern struct list_head smack_rule_list;
  205. extern struct list_head smk_netlbladdr_list;
  206. extern struct security_operations smack_ops;
  207. /*
  208. * Stricly for CIPSO level manipulation.
  209. * Set the category bit number in a smack label sized buffer.
  210. */
  211. static inline void smack_catset_bit(int cat, char *catsetp)
  212. {
  213. if (cat > SMK_LABELLEN * 8)
  214. return;
  215. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  216. }
  217. /*
  218. * Present a pointer to the smack label in an inode blob.
  219. */
  220. static inline char *smk_of_inode(const struct inode *isp)
  221. {
  222. struct inode_smack *sip = isp->i_security;
  223. return sip->smk_inode;
  224. }
  225. /*
  226. * logging functions
  227. */
  228. #define SMACK_AUDIT_DENIED 0x1
  229. #define SMACK_AUDIT_ACCEPT 0x2
  230. extern int log_policy;
  231. void smack_log(char *subject_label, char *object_label,
  232. int request,
  233. int result, struct smk_audit_info *auditdata);
  234. #ifdef CONFIG_AUDIT
  235. /*
  236. * some inline functions to set up audit data
  237. * they do nothing if CONFIG_AUDIT is not set
  238. *
  239. */
  240. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  241. char type)
  242. {
  243. memset(a, 0, sizeof(*a));
  244. a->a.type = type;
  245. a->a.function = func;
  246. }
  247. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  248. struct task_struct *t)
  249. {
  250. a->a.u.tsk = t;
  251. }
  252. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  253. struct dentry *d)
  254. {
  255. a->a.u.fs.path.dentry = d;
  256. }
  257. static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
  258. struct vfsmount *m)
  259. {
  260. a->a.u.fs.path.mnt = m;
  261. }
  262. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  263. struct inode *i)
  264. {
  265. a->a.u.fs.inode = i;
  266. }
  267. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  268. struct path p)
  269. {
  270. a->a.u.fs.path = p;
  271. }
  272. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  273. struct sock *sk)
  274. {
  275. a->a.u.net.sk = sk;
  276. }
  277. #else /* no AUDIT */
  278. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  279. char type)
  280. {
  281. }
  282. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  283. struct task_struct *t)
  284. {
  285. }
  286. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  287. struct dentry *d)
  288. {
  289. }
  290. static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
  291. struct vfsmount *m)
  292. {
  293. }
  294. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  295. struct inode *i)
  296. {
  297. }
  298. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  299. struct path p)
  300. {
  301. }
  302. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  303. struct sock *sk)
  304. {
  305. }
  306. #endif
  307. #endif /* _SECURITY_SMACK_H */