smack.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. #if IS_ENABLED(CONFIG_IPV6)
  19. #include <linux/in6.h>
  20. #endif /* CONFIG_IPV6 */
  21. #include <net/netlabel.h>
  22. #include <linux/list.h>
  23. #include <linux/rculist.h>
  24. #include <linux/lsm_audit.h>
  25. /*
  26. * Use IPv6 port labeling if IPv6 is enabled and secmarks
  27. * are not being used.
  28. */
  29. #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
  30. #define SMACK_IPV6_PORT_LABELING 1
  31. #endif
  32. #if IS_ENABLED(CONFIG_IPV6) && defined(CONFIG_SECURITY_SMACK_NETFILTER)
  33. #define SMACK_IPV6_SECMARK_LABELING 1
  34. #endif
  35. /*
  36. * Smack labels were limited to 23 characters for a long time.
  37. */
  38. #define SMK_LABELLEN 24
  39. #define SMK_LONGLABEL 256
  40. /*
  41. * This is the repository for labels seen so that it is
  42. * not necessary to keep allocating tiny chuncks of memory
  43. * and so that they can be shared.
  44. *
  45. * Labels are never modified in place. Anytime a label
  46. * is imported (e.g. xattrset on a file) the list is checked
  47. * for it and it is added if it doesn't exist. The address
  48. * is passed out in either case. Entries are added, but
  49. * never deleted.
  50. *
  51. * Since labels are hanging around anyway it doesn't
  52. * hurt to maintain a secid for those awkward situations
  53. * where kernel components that ought to use LSM independent
  54. * interfaces don't. The secid should go away when all of
  55. * these components have been repaired.
  56. *
  57. * The cipso value associated with the label gets stored here, too.
  58. *
  59. * Keep the access rules for this subject label here so that
  60. * the entire set of rules does not need to be examined every
  61. * time.
  62. */
  63. struct smack_known {
  64. struct list_head list;
  65. struct hlist_node smk_hashed;
  66. char *smk_known;
  67. u32 smk_secid;
  68. struct netlbl_lsm_secattr smk_netlabel; /* on wire labels */
  69. struct list_head smk_rules; /* access rules */
  70. struct mutex smk_rules_lock; /* lock for rules */
  71. };
  72. /*
  73. * Maximum number of bytes for the levels in a CIPSO IP option.
  74. * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
  75. * bigger than can be used, and 24 is the next lower multiple
  76. * of 8, and there are too many issues if there isn't space set
  77. * aside for the terminating null byte.
  78. */
  79. #define SMK_CIPSOLEN 24
  80. struct superblock_smack {
  81. struct smack_known *smk_root;
  82. struct smack_known *smk_floor;
  83. struct smack_known *smk_hat;
  84. struct smack_known *smk_default;
  85. int smk_flags;
  86. };
  87. /*
  88. * Superblock flags
  89. */
  90. #define SMK_SB_INITIALIZED 0x01
  91. #define SMK_SB_UNTRUSTED 0x02
  92. struct socket_smack {
  93. struct smack_known *smk_out; /* outbound label */
  94. struct smack_known *smk_in; /* inbound label */
  95. struct smack_known *smk_packet; /* TCP peer label */
  96. };
  97. /*
  98. * Inode smack data
  99. */
  100. struct inode_smack {
  101. struct smack_known *smk_inode; /* label of the fso */
  102. struct smack_known *smk_task; /* label of the task */
  103. struct smack_known *smk_mmap; /* label of the mmap domain */
  104. struct mutex smk_lock; /* initialization lock */
  105. int smk_flags; /* smack inode flags */
  106. struct rcu_head smk_rcu; /* for freeing inode_smack */
  107. };
  108. struct task_smack {
  109. struct smack_known *smk_task; /* label for access control */
  110. struct smack_known *smk_forked; /* label when forked */
  111. struct list_head smk_rules; /* per task access rules */
  112. struct mutex smk_rules_lock; /* lock for the rules */
  113. struct list_head smk_relabel; /* transit allowed labels */
  114. };
  115. #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
  116. #define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */
  117. #define SMK_INODE_CHANGED 0x04 /* smack was transmuted */
  118. #define SMK_INODE_IMPURE 0x08 /* involved in an impure transaction */
  119. /*
  120. * A label access rule.
  121. */
  122. struct smack_rule {
  123. struct list_head list;
  124. struct smack_known *smk_subject;
  125. struct smack_known *smk_object;
  126. int smk_access;
  127. };
  128. /*
  129. * An entry in the table identifying IPv4 hosts.
  130. */
  131. struct smk_net4addr {
  132. struct list_head list;
  133. struct in_addr smk_host; /* network address */
  134. struct in_addr smk_mask; /* network mask */
  135. int smk_masks; /* mask size */
  136. struct smack_known *smk_label; /* label */
  137. };
  138. #if IS_ENABLED(CONFIG_IPV6)
  139. /*
  140. * An entry in the table identifying IPv6 hosts.
  141. */
  142. struct smk_net6addr {
  143. struct list_head list;
  144. struct in6_addr smk_host; /* network address */
  145. struct in6_addr smk_mask; /* network mask */
  146. int smk_masks; /* mask size */
  147. struct smack_known *smk_label; /* label */
  148. };
  149. #endif /* CONFIG_IPV6 */
  150. #ifdef SMACK_IPV6_PORT_LABELING
  151. /*
  152. * An entry in the table identifying ports.
  153. */
  154. struct smk_port_label {
  155. struct list_head list;
  156. struct sock *smk_sock; /* socket initialized on */
  157. unsigned short smk_port; /* the port number */
  158. struct smack_known *smk_in; /* inbound label */
  159. struct smack_known *smk_out; /* outgoing label */
  160. short smk_sock_type; /* Socket type */
  161. short smk_can_reuse;
  162. };
  163. #endif /* SMACK_IPV6_PORT_LABELING */
  164. struct smack_known_list_elem {
  165. struct list_head list;
  166. struct smack_known *smk_label;
  167. };
  168. /* Super block security struct flags for mount options */
  169. #define FSDEFAULT_MNT 0x01
  170. #define FSFLOOR_MNT 0x02
  171. #define FSHAT_MNT 0x04
  172. #define FSROOT_MNT 0x08
  173. #define FSTRANS_MNT 0x10
  174. #define NUM_SMK_MNT_OPTS 5
  175. enum {
  176. Opt_error = -1,
  177. Opt_fsdefault = 1,
  178. Opt_fsfloor = 2,
  179. Opt_fshat = 3,
  180. Opt_fsroot = 4,
  181. Opt_fstransmute = 5,
  182. };
  183. /*
  184. * Mount options
  185. */
  186. #define SMK_FSDEFAULT "smackfsdef="
  187. #define SMK_FSFLOOR "smackfsfloor="
  188. #define SMK_FSHAT "smackfshat="
  189. #define SMK_FSROOT "smackfsroot="
  190. #define SMK_FSTRANS "smackfstransmute="
  191. #define SMACK_DELETE_OPTION "-DELETE"
  192. #define SMACK_CIPSO_OPTION "-CIPSO"
  193. /*
  194. * How communications on this socket are treated.
  195. * Usually it's determined by the underlying netlabel code
  196. * but there are certain cases, including single label hosts
  197. * and potentially single label interfaces for which the
  198. * treatment can not be known in advance.
  199. *
  200. * The possibility of additional labeling schemes being
  201. * introduced in the future exists as well.
  202. */
  203. #define SMACK_UNLABELED_SOCKET 0
  204. #define SMACK_CIPSO_SOCKET 1
  205. /*
  206. * CIPSO defaults.
  207. */
  208. #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
  209. #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
  210. #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
  211. #define SMACK_CIPSO_MAPPED_DEFAULT 251 /* Also arbitrary */
  212. #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
  213. /*
  214. * CIPSO 2.2 standard is 239, but Smack wants to use the
  215. * categories in a structured way that limits the value to
  216. * the bits in 23 bytes, hence the unusual number.
  217. */
  218. #define SMACK_CIPSO_MAXCATNUM 184 /* 23 * 8 */
  219. /*
  220. * Ptrace rules
  221. */
  222. #define SMACK_PTRACE_DEFAULT 0
  223. #define SMACK_PTRACE_EXACT 1
  224. #define SMACK_PTRACE_DRACONIAN 2
  225. #define SMACK_PTRACE_MAX SMACK_PTRACE_DRACONIAN
  226. /*
  227. * Flags for untraditional access modes.
  228. * It shouldn't be necessary to avoid conflicts with definitions
  229. * in fs.h, but do so anyway.
  230. */
  231. #define MAY_TRANSMUTE 0x00001000 /* Controls directory labeling */
  232. #define MAY_LOCK 0x00002000 /* Locks should be writes, but ... */
  233. #define MAY_BRINGUP 0x00004000 /* Report use of this rule */
  234. /*
  235. * The policy for delivering signals is configurable.
  236. * It is usually "write", but can be "append".
  237. */
  238. #ifdef CONFIG_SECURITY_SMACK_APPEND_SIGNALS
  239. #define MAY_DELIVER MAY_APPEND /* Signal delivery requires append */
  240. #else
  241. #define MAY_DELIVER MAY_WRITE /* Signal delivery requires write */
  242. #endif
  243. #define SMACK_BRINGUP_ALLOW 1 /* Allow bringup mode */
  244. #define SMACK_UNCONFINED_SUBJECT 2 /* Allow unconfined label */
  245. #define SMACK_UNCONFINED_OBJECT 3 /* Allow unconfined label */
  246. /*
  247. * Just to make the common cases easier to deal with
  248. */
  249. #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
  250. #define MAY_READWRITE (MAY_READ | MAY_WRITE)
  251. #define MAY_NOT 0
  252. /*
  253. * Number of access types used by Smack (rwxatlb)
  254. */
  255. #define SMK_NUM_ACCESS_TYPE 7
  256. /* SMACK data */
  257. struct smack_audit_data {
  258. const char *function;
  259. char *subject;
  260. char *object;
  261. char *request;
  262. int result;
  263. };
  264. /*
  265. * Smack audit data; is empty if CONFIG_AUDIT not set
  266. * to save some stack
  267. */
  268. struct smk_audit_info {
  269. #ifdef CONFIG_AUDIT
  270. struct common_audit_data a;
  271. struct smack_audit_data sad;
  272. #endif
  273. };
  274. /*
  275. * These functions are in smack_access.c
  276. */
  277. int smk_access_entry(char *, char *, struct list_head *);
  278. int smk_access(struct smack_known *, struct smack_known *,
  279. int, struct smk_audit_info *);
  280. int smk_tskacc(struct task_smack *, struct smack_known *,
  281. u32, struct smk_audit_info *);
  282. int smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
  283. struct smack_known *smack_from_secid(const u32);
  284. char *smk_parse_smack(const char *string, int len);
  285. int smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
  286. struct smack_known *smk_import_entry(const char *, int);
  287. void smk_insert_entry(struct smack_known *skp);
  288. struct smack_known *smk_find_entry(const char *);
  289. bool smack_privileged(int cap);
  290. void smk_destroy_label_list(struct list_head *list);
  291. /*
  292. * Shared data.
  293. */
  294. extern int smack_enabled;
  295. extern int smack_cipso_direct;
  296. extern int smack_cipso_mapped;
  297. extern struct smack_known *smack_net_ambient;
  298. extern struct smack_known *smack_syslog_label;
  299. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  300. extern struct smack_known *smack_unconfined;
  301. #endif
  302. extern int smack_ptrace_rule;
  303. extern struct smack_known smack_known_floor;
  304. extern struct smack_known smack_known_hat;
  305. extern struct smack_known smack_known_huh;
  306. extern struct smack_known smack_known_star;
  307. extern struct smack_known smack_known_web;
  308. extern struct mutex smack_known_lock;
  309. extern struct list_head smack_known_list;
  310. extern struct list_head smk_net4addr_list;
  311. #if IS_ENABLED(CONFIG_IPV6)
  312. extern struct list_head smk_net6addr_list;
  313. #endif /* CONFIG_IPV6 */
  314. extern struct mutex smack_onlycap_lock;
  315. extern struct list_head smack_onlycap_list;
  316. #define SMACK_HASH_SLOTS 16
  317. extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
  318. /*
  319. * Is the directory transmuting?
  320. */
  321. static inline int smk_inode_transmutable(const struct inode *isp)
  322. {
  323. struct inode_smack *sip = isp->i_security;
  324. return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
  325. }
  326. /*
  327. * Present a pointer to the smack label entry in an inode blob.
  328. */
  329. static inline struct smack_known *smk_of_inode(const struct inode *isp)
  330. {
  331. struct inode_smack *sip = isp->i_security;
  332. return sip->smk_inode;
  333. }
  334. /*
  335. * Present a pointer to the smack label entry in an task blob.
  336. */
  337. static inline struct smack_known *smk_of_task(const struct task_smack *tsp)
  338. {
  339. return tsp->smk_task;
  340. }
  341. static inline struct smack_known *smk_of_task_struct(const struct task_struct *t)
  342. {
  343. struct smack_known *skp;
  344. rcu_read_lock();
  345. skp = smk_of_task(__task_cred(t)->security);
  346. rcu_read_unlock();
  347. return skp;
  348. }
  349. /*
  350. * Present a pointer to the forked smack label entry in an task blob.
  351. */
  352. static inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
  353. {
  354. return tsp->smk_forked;
  355. }
  356. /*
  357. * Present a pointer to the smack label in the current task blob.
  358. */
  359. static inline struct smack_known *smk_of_current(void)
  360. {
  361. return smk_of_task(current_security());
  362. }
  363. /*
  364. * logging functions
  365. */
  366. #define SMACK_AUDIT_DENIED 0x1
  367. #define SMACK_AUDIT_ACCEPT 0x2
  368. extern int log_policy;
  369. void smack_log(char *subject_label, char *object_label,
  370. int request,
  371. int result, struct smk_audit_info *auditdata);
  372. #ifdef CONFIG_AUDIT
  373. /*
  374. * some inline functions to set up audit data
  375. * they do nothing if CONFIG_AUDIT is not set
  376. *
  377. */
  378. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  379. char type)
  380. {
  381. memset(&a->sad, 0, sizeof(a->sad));
  382. a->a.type = type;
  383. a->a.smack_audit_data = &a->sad;
  384. a->a.smack_audit_data->function = func;
  385. }
  386. static inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
  387. char type, struct lsm_network_audit *net)
  388. {
  389. smk_ad_init(a, func, type);
  390. memset(net, 0, sizeof(*net));
  391. a->a.u.net = net;
  392. }
  393. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  394. struct task_struct *t)
  395. {
  396. a->a.u.tsk = t;
  397. }
  398. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  399. struct dentry *d)
  400. {
  401. a->a.u.dentry = d;
  402. }
  403. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  404. struct inode *i)
  405. {
  406. a->a.u.inode = i;
  407. }
  408. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  409. struct path p)
  410. {
  411. a->a.u.path = p;
  412. }
  413. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  414. struct sock *sk)
  415. {
  416. a->a.u.net->sk = sk;
  417. }
  418. #else /* no AUDIT */
  419. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  420. char type)
  421. {
  422. }
  423. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  424. struct task_struct *t)
  425. {
  426. }
  427. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  428. struct dentry *d)
  429. {
  430. }
  431. static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
  432. struct vfsmount *m)
  433. {
  434. }
  435. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  436. struct inode *i)
  437. {
  438. }
  439. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  440. struct path p)
  441. {
  442. }
  443. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  444. struct sock *sk)
  445. {
  446. }
  447. #endif
  448. #endif /* _SECURITY_SMACK_H */