smack.h 10 KB

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