smack.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/lsm_hooks.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. struct hlist_node smk_hashed;
  53. char *smk_known;
  54. u32 smk_secid;
  55. struct netlbl_lsm_secattr smk_netlabel; /* on wire labels */
  56. struct list_head smk_rules; /* access rules */
  57. struct mutex smk_rules_lock; /* lock for rules */
  58. };
  59. /*
  60. * Maximum number of bytes for the levels in a CIPSO IP option.
  61. * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
  62. * bigger than can be used, and 24 is the next lower multiple
  63. * of 8, and there are too many issues if there isn't space set
  64. * aside for the terminating null byte.
  65. */
  66. #define SMK_CIPSOLEN 24
  67. struct superblock_smack {
  68. struct smack_known *smk_root;
  69. struct smack_known *smk_floor;
  70. struct smack_known *smk_hat;
  71. struct smack_known *smk_default;
  72. int smk_initialized;
  73. };
  74. struct socket_smack {
  75. struct smack_known *smk_out; /* outbound label */
  76. struct smack_known *smk_in; /* inbound label */
  77. struct smack_known *smk_packet; /* TCP peer label */
  78. };
  79. /*
  80. * Inode smack data
  81. */
  82. struct inode_smack {
  83. struct smack_known *smk_inode; /* label of the fso */
  84. struct smack_known *smk_task; /* label of the task */
  85. struct smack_known *smk_mmap; /* label of the mmap domain */
  86. struct mutex smk_lock; /* initialization lock */
  87. int smk_flags; /* smack inode flags */
  88. };
  89. struct task_smack {
  90. struct smack_known *smk_task; /* label for access control */
  91. struct smack_known *smk_forked; /* label when forked */
  92. struct list_head smk_rules; /* per task access rules */
  93. struct mutex smk_rules_lock; /* lock for the rules */
  94. };
  95. #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
  96. #define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */
  97. #define SMK_INODE_CHANGED 0x04 /* smack was transmuted */
  98. #define SMK_INODE_IMPURE 0x08 /* involved in an impure transaction */
  99. /*
  100. * A label access rule.
  101. */
  102. struct smack_rule {
  103. struct list_head list;
  104. struct smack_known *smk_subject;
  105. struct smack_known *smk_object;
  106. int smk_access;
  107. };
  108. /*
  109. * An entry in the table identifying hosts.
  110. */
  111. struct smk_netlbladdr {
  112. struct list_head list;
  113. struct sockaddr_in smk_host; /* network address */
  114. struct in_addr smk_mask; /* network mask */
  115. struct smack_known *smk_label; /* label */
  116. };
  117. /*
  118. * An entry in the table identifying ports.
  119. */
  120. struct smk_port_label {
  121. struct list_head list;
  122. struct sock *smk_sock; /* socket initialized on */
  123. unsigned short smk_port; /* the port number */
  124. struct smack_known *smk_in; /* inbound label */
  125. struct smack_known *smk_out; /* outgoing label */
  126. };
  127. struct smack_onlycap {
  128. struct list_head list;
  129. struct smack_known *smk_label;
  130. };
  131. /* Super block security struct flags for mount options */
  132. #define FSDEFAULT_MNT 0x01
  133. #define FSFLOOR_MNT 0x02
  134. #define FSHAT_MNT 0x04
  135. #define FSROOT_MNT 0x08
  136. #define FSTRANS_MNT 0x10
  137. #define NUM_SMK_MNT_OPTS 5
  138. enum {
  139. Opt_error = -1,
  140. Opt_fsdefault = 1,
  141. Opt_fsfloor = 2,
  142. Opt_fshat = 3,
  143. Opt_fsroot = 4,
  144. Opt_fstransmute = 5,
  145. };
  146. /*
  147. * Mount options
  148. */
  149. #define SMK_FSDEFAULT "smackfsdef="
  150. #define SMK_FSFLOOR "smackfsfloor="
  151. #define SMK_FSHAT "smackfshat="
  152. #define SMK_FSROOT "smackfsroot="
  153. #define SMK_FSTRANS "smackfstransmute="
  154. #define SMACK_CIPSO_OPTION "-CIPSO"
  155. /*
  156. * How communications on this socket are treated.
  157. * Usually it's determined by the underlying netlabel code
  158. * but there are certain cases, including single label hosts
  159. * and potentially single label interfaces for which the
  160. * treatment can not be known in advance.
  161. *
  162. * The possibility of additional labeling schemes being
  163. * introduced in the future exists as well.
  164. */
  165. #define SMACK_UNLABELED_SOCKET 0
  166. #define SMACK_CIPSO_SOCKET 1
  167. /*
  168. * CIPSO defaults.
  169. */
  170. #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
  171. #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
  172. #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
  173. #define SMACK_CIPSO_MAPPED_DEFAULT 251 /* Also arbitrary */
  174. #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
  175. /*
  176. * CIPSO 2.2 standard is 239, but Smack wants to use the
  177. * categories in a structured way that limits the value to
  178. * the bits in 23 bytes, hence the unusual number.
  179. */
  180. #define SMACK_CIPSO_MAXCATNUM 184 /* 23 * 8 */
  181. /*
  182. * Ptrace rules
  183. */
  184. #define SMACK_PTRACE_DEFAULT 0
  185. #define SMACK_PTRACE_EXACT 1
  186. #define SMACK_PTRACE_DRACONIAN 2
  187. #define SMACK_PTRACE_MAX SMACK_PTRACE_DRACONIAN
  188. /*
  189. * Flags for untraditional access modes.
  190. * It shouldn't be necessary to avoid conflicts with definitions
  191. * in fs.h, but do so anyway.
  192. */
  193. #define MAY_TRANSMUTE 0x00001000 /* Controls directory labeling */
  194. #define MAY_LOCK 0x00002000 /* Locks should be writes, but ... */
  195. #define MAY_BRINGUP 0x00004000 /* Report use of this rule */
  196. #define SMACK_BRINGUP_ALLOW 1 /* Allow bringup mode */
  197. #define SMACK_UNCONFINED_SUBJECT 2 /* Allow unconfined label */
  198. #define SMACK_UNCONFINED_OBJECT 3 /* Allow unconfined label */
  199. /*
  200. * Just to make the common cases easier to deal with
  201. */
  202. #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
  203. #define MAY_READWRITE (MAY_READ | MAY_WRITE)
  204. #define MAY_NOT 0
  205. /*
  206. * Number of access types used by Smack (rwxatlb)
  207. */
  208. #define SMK_NUM_ACCESS_TYPE 7
  209. /* SMACK data */
  210. struct smack_audit_data {
  211. const char *function;
  212. char *subject;
  213. char *object;
  214. char *request;
  215. int result;
  216. };
  217. /*
  218. * Smack audit data; is empty if CONFIG_AUDIT not set
  219. * to save some stack
  220. */
  221. struct smk_audit_info {
  222. #ifdef CONFIG_AUDIT
  223. struct common_audit_data a;
  224. struct smack_audit_data sad;
  225. #endif
  226. };
  227. /*
  228. * These functions are in smack_lsm.c
  229. */
  230. struct inode_smack *new_inode_smack(struct smack_known *);
  231. /*
  232. * These functions are in smack_access.c
  233. */
  234. int smk_access_entry(char *, char *, struct list_head *);
  235. int smk_access(struct smack_known *, struct smack_known *,
  236. int, struct smk_audit_info *);
  237. int smk_tskacc(struct task_smack *, struct smack_known *,
  238. u32, struct smk_audit_info *);
  239. int smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
  240. struct smack_known *smack_from_secid(const u32);
  241. char *smk_parse_smack(const char *string, int len);
  242. int smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
  243. struct smack_known *smk_import_entry(const char *, int);
  244. void smk_insert_entry(struct smack_known *skp);
  245. struct smack_known *smk_find_entry(const char *);
  246. int smack_privileged(int cap);
  247. /*
  248. * Shared data.
  249. */
  250. extern int smack_enabled;
  251. extern int smack_cipso_direct;
  252. extern int smack_cipso_mapped;
  253. extern struct smack_known *smack_net_ambient;
  254. extern struct smack_known *smack_syslog_label;
  255. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  256. extern struct smack_known *smack_unconfined;
  257. #endif
  258. extern struct smack_known smack_cipso_option;
  259. extern int smack_ptrace_rule;
  260. extern struct smack_known smack_known_floor;
  261. extern struct smack_known smack_known_hat;
  262. extern struct smack_known smack_known_huh;
  263. extern struct smack_known smack_known_invalid;
  264. extern struct smack_known smack_known_star;
  265. extern struct smack_known smack_known_web;
  266. extern struct mutex smack_known_lock;
  267. extern struct list_head smack_known_list;
  268. extern struct list_head smk_netlbladdr_list;
  269. extern struct mutex smack_onlycap_lock;
  270. extern struct list_head smack_onlycap_list;
  271. #define SMACK_HASH_SLOTS 16
  272. extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
  273. /*
  274. * Is the directory transmuting?
  275. */
  276. static inline int smk_inode_transmutable(const struct inode *isp)
  277. {
  278. struct inode_smack *sip = isp->i_security;
  279. return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
  280. }
  281. /*
  282. * Present a pointer to the smack label entry in an inode blob.
  283. */
  284. static inline struct smack_known *smk_of_inode(const struct inode *isp)
  285. {
  286. struct inode_smack *sip = isp->i_security;
  287. return sip->smk_inode;
  288. }
  289. /*
  290. * Present a pointer to the smack label entry in an task blob.
  291. */
  292. static inline struct smack_known *smk_of_task(const struct task_smack *tsp)
  293. {
  294. return tsp->smk_task;
  295. }
  296. static inline struct smack_known *smk_of_task_struct(const struct task_struct *t)
  297. {
  298. struct smack_known *skp;
  299. rcu_read_lock();
  300. skp = smk_of_task(__task_cred(t)->security);
  301. rcu_read_unlock();
  302. return skp;
  303. }
  304. /*
  305. * Present a pointer to the forked smack label entry in an task blob.
  306. */
  307. static inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
  308. {
  309. return tsp->smk_forked;
  310. }
  311. /*
  312. * Present a pointer to the smack label in the current task blob.
  313. */
  314. static inline struct smack_known *smk_of_current(void)
  315. {
  316. return smk_of_task(current_security());
  317. }
  318. /*
  319. * logging functions
  320. */
  321. #define SMACK_AUDIT_DENIED 0x1
  322. #define SMACK_AUDIT_ACCEPT 0x2
  323. extern int log_policy;
  324. void smack_log(char *subject_label, char *object_label,
  325. int request,
  326. int result, struct smk_audit_info *auditdata);
  327. #ifdef CONFIG_AUDIT
  328. /*
  329. * some inline functions to set up audit data
  330. * they do nothing if CONFIG_AUDIT is not set
  331. *
  332. */
  333. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  334. char type)
  335. {
  336. memset(&a->sad, 0, sizeof(a->sad));
  337. a->a.type = type;
  338. a->a.smack_audit_data = &a->sad;
  339. a->a.smack_audit_data->function = func;
  340. }
  341. static inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
  342. char type, struct lsm_network_audit *net)
  343. {
  344. smk_ad_init(a, func, type);
  345. memset(net, 0, sizeof(*net));
  346. a->a.u.net = net;
  347. }
  348. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  349. struct task_struct *t)
  350. {
  351. a->a.u.tsk = t;
  352. }
  353. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  354. struct dentry *d)
  355. {
  356. a->a.u.dentry = d;
  357. }
  358. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  359. struct inode *i)
  360. {
  361. a->a.u.inode = i;
  362. }
  363. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  364. struct path p)
  365. {
  366. a->a.u.path = p;
  367. }
  368. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  369. struct sock *sk)
  370. {
  371. a->a.u.net->sk = sk;
  372. }
  373. #else /* no AUDIT */
  374. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  375. char type)
  376. {
  377. }
  378. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  379. struct task_struct *t)
  380. {
  381. }
  382. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  383. struct dentry *d)
  384. {
  385. }
  386. static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
  387. struct vfsmount *m)
  388. {
  389. }
  390. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  391. struct inode *i)
  392. {
  393. }
  394. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  395. struct path p)
  396. {
  397. }
  398. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  399. struct sock *sk)
  400. {
  401. }
  402. #endif
  403. #endif /* _SECURITY_SMACK_H */