smack.h 9.3 KB

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