smack.h 14 KB

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