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