smack.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 chunks 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. #if IS_ENABLED(CONFIG_IPV6)
  140. /*
  141. * An entry in the table identifying IPv6 hosts.
  142. */
  143. struct smk_net6addr {
  144. struct list_head list;
  145. struct in6_addr smk_host; /* network address */
  146. struct in6_addr smk_mask; /* network mask */
  147. int smk_masks; /* mask size */
  148. struct smack_known *smk_label; /* label */
  149. };
  150. #endif /* CONFIG_IPV6 */
  151. #ifdef SMACK_IPV6_PORT_LABELING
  152. /*
  153. * An entry in the table identifying ports.
  154. */
  155. struct smk_port_label {
  156. struct list_head list;
  157. struct sock *smk_sock; /* socket initialized on */
  158. unsigned short smk_port; /* the port number */
  159. struct smack_known *smk_in; /* inbound label */
  160. struct smack_known *smk_out; /* outgoing label */
  161. short smk_sock_type; /* Socket type */
  162. short smk_can_reuse;
  163. };
  164. #endif /* SMACK_IPV6_PORT_LABELING */
  165. struct smack_known_list_elem {
  166. struct list_head list;
  167. struct smack_known *smk_label;
  168. };
  169. enum {
  170. Opt_error = -1,
  171. Opt_fsdefault = 0,
  172. Opt_fsfloor = 1,
  173. Opt_fshat = 2,
  174. Opt_fsroot = 3,
  175. Opt_fstransmute = 4,
  176. };
  177. #define SMACK_DELETE_OPTION "-DELETE"
  178. #define SMACK_CIPSO_OPTION "-CIPSO"
  179. /*
  180. * CIPSO defaults.
  181. */
  182. #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
  183. #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
  184. #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
  185. #define SMACK_CIPSO_MAPPED_DEFAULT 251 /* Also arbitrary */
  186. #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
  187. /*
  188. * CIPSO 2.2 standard is 239, but Smack wants to use the
  189. * categories in a structured way that limits the value to
  190. * the bits in 23 bytes, hence the unusual number.
  191. */
  192. #define SMACK_CIPSO_MAXCATNUM 184 /* 23 * 8 */
  193. /*
  194. * Ptrace rules
  195. */
  196. #define SMACK_PTRACE_DEFAULT 0
  197. #define SMACK_PTRACE_EXACT 1
  198. #define SMACK_PTRACE_DRACONIAN 2
  199. #define SMACK_PTRACE_MAX SMACK_PTRACE_DRACONIAN
  200. /*
  201. * Flags for untraditional access modes.
  202. * It shouldn't be necessary to avoid conflicts with definitions
  203. * in fs.h, but do so anyway.
  204. */
  205. #define MAY_TRANSMUTE 0x00001000 /* Controls directory labeling */
  206. #define MAY_LOCK 0x00002000 /* Locks should be writes, but ... */
  207. #define MAY_BRINGUP 0x00004000 /* Report use of this rule */
  208. /*
  209. * The policy for delivering signals is configurable.
  210. * It is usually "write", but can be "append".
  211. */
  212. #ifdef CONFIG_SECURITY_SMACK_APPEND_SIGNALS
  213. #define MAY_DELIVER MAY_APPEND /* Signal delivery requires append */
  214. #else
  215. #define MAY_DELIVER MAY_WRITE /* Signal delivery requires write */
  216. #endif
  217. #define SMACK_BRINGUP_ALLOW 1 /* Allow bringup mode */
  218. #define SMACK_UNCONFINED_SUBJECT 2 /* Allow unconfined label */
  219. #define SMACK_UNCONFINED_OBJECT 3 /* Allow unconfined label */
  220. /*
  221. * Just to make the common cases easier to deal with
  222. */
  223. #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
  224. #define MAY_READWRITE (MAY_READ | MAY_WRITE)
  225. #define MAY_NOT 0
  226. /*
  227. * Number of access types used by Smack (rwxatlb)
  228. */
  229. #define SMK_NUM_ACCESS_TYPE 7
  230. /* SMACK data */
  231. struct smack_audit_data {
  232. const char *function;
  233. char *subject;
  234. char *object;
  235. char *request;
  236. int result;
  237. };
  238. /*
  239. * Smack audit data; is empty if CONFIG_AUDIT not set
  240. * to save some stack
  241. */
  242. struct smk_audit_info {
  243. #ifdef CONFIG_AUDIT
  244. struct common_audit_data a;
  245. struct smack_audit_data sad;
  246. #endif
  247. };
  248. /*
  249. * Initialization
  250. */
  251. #if defined(CONFIG_SECURITY_SMACK_NETFILTER)
  252. int smack_nf_ip_init(void);
  253. #else
  254. static inline int smack_nf_ip_init(void)
  255. {
  256. return 0;
  257. }
  258. #endif
  259. int init_smk_fs(void);
  260. int smack_initcall(void);
  261. /*
  262. * These functions are in smack_access.c
  263. */
  264. int smk_access_entry(char *, char *, struct list_head *);
  265. int smk_access(struct smack_known *, struct smack_known *,
  266. int, struct smk_audit_info *);
  267. int smk_tskacc(struct task_smack *, struct smack_known *,
  268. u32, struct smk_audit_info *);
  269. int smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
  270. int smack_str_from_perm(char *string, int access);
  271. struct smack_known *smack_from_secid(const u32);
  272. int smk_parse_label_len(const char *string, int len);
  273. char *smk_parse_smack(const char *string, int len);
  274. int smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
  275. struct smack_known *smk_import_entry(const char *, int);
  276. struct smack_known *smk_import_valid_label(const char *label, int label_len,
  277. gfp_t gfp);
  278. void smk_insert_entry(struct smack_known *skp);
  279. struct smack_known *smk_find_entry(const char *);
  280. bool smack_privileged(int cap);
  281. bool smack_privileged_cred(int cap, const struct cred *cred);
  282. void smk_destroy_label_list(struct list_head *list);
  283. int smack_populate_secattr(struct smack_known *skp);
  284. /*
  285. * Shared data.
  286. */
  287. extern int smack_enabled __initdata;
  288. extern int smack_cipso_direct;
  289. extern int smack_cipso_mapped;
  290. extern struct smack_known *smack_net_ambient;
  291. extern struct smack_known *smack_syslog_label;
  292. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  293. extern struct smack_known *smack_unconfined;
  294. #endif
  295. extern int smack_ptrace_rule;
  296. extern struct lsm_blob_sizes smack_blob_sizes;
  297. extern struct smack_known smack_known_floor;
  298. extern struct smack_known smack_known_hat;
  299. extern struct smack_known smack_known_huh;
  300. extern struct smack_known smack_known_star;
  301. extern struct smack_known smack_known_web;
  302. extern struct mutex smack_known_lock;
  303. extern struct list_head smack_known_list;
  304. extern struct list_head smk_net4addr_list;
  305. #if IS_ENABLED(CONFIG_IPV6)
  306. extern struct list_head smk_net6addr_list;
  307. #endif /* CONFIG_IPV6 */
  308. extern struct mutex smack_onlycap_lock;
  309. extern struct list_head smack_onlycap_list;
  310. #define SMACK_HASH_SLOTS 16
  311. extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
  312. extern struct kmem_cache *smack_rule_cache;
  313. static inline struct task_smack *smack_cred(const struct cred *cred)
  314. {
  315. return cred->security + smack_blob_sizes.lbs_cred;
  316. }
  317. static inline struct smack_known **smack_file(const struct file *file)
  318. {
  319. return (struct smack_known **)(file->f_security +
  320. smack_blob_sizes.lbs_file);
  321. }
  322. static inline struct inode_smack *smack_inode(const struct inode *inode)
  323. {
  324. return inode->i_security + smack_blob_sizes.lbs_inode;
  325. }
  326. static inline struct smack_known **smack_msg_msg(const struct msg_msg *msg)
  327. {
  328. return msg->security + smack_blob_sizes.lbs_msg_msg;
  329. }
  330. static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)
  331. {
  332. return ipc->security + smack_blob_sizes.lbs_ipc;
  333. }
  334. static inline struct superblock_smack *smack_superblock(
  335. const struct super_block *superblock)
  336. {
  337. return superblock->s_security + smack_blob_sizes.lbs_superblock;
  338. }
  339. static inline struct socket_smack *smack_sock(const struct sock *sock)
  340. {
  341. return sock->sk_security + smack_blob_sizes.lbs_sock;
  342. }
  343. #ifdef CONFIG_KEYS
  344. static inline struct smack_known **smack_key(const struct key *key)
  345. {
  346. return key->security + smack_blob_sizes.lbs_key;
  347. }
  348. #endif /* CONFIG_KEYS */
  349. /*
  350. * Is the directory transmuting?
  351. */
  352. static inline int smk_inode_transmutable(const struct inode *isp)
  353. {
  354. struct inode_smack *sip = smack_inode(isp);
  355. return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
  356. }
  357. /*
  358. * Present a pointer to the smack label entry in an inode blob.
  359. */
  360. static inline struct smack_known *smk_of_inode(const struct inode *isp)
  361. {
  362. struct inode_smack *sip = smack_inode(isp);
  363. return sip->smk_inode;
  364. }
  365. /*
  366. * Present a pointer to the smack label entry in an task blob.
  367. */
  368. static inline struct smack_known *smk_of_task(const struct task_smack *tsp)
  369. {
  370. return tsp->smk_task;
  371. }
  372. static inline struct smack_known *smk_of_task_struct_obj(
  373. const struct task_struct *t)
  374. {
  375. struct smack_known *skp;
  376. const struct cred *cred;
  377. rcu_read_lock();
  378. cred = __task_cred(t);
  379. skp = smk_of_task(smack_cred(cred));
  380. rcu_read_unlock();
  381. return skp;
  382. }
  383. /*
  384. * Present a pointer to the forked smack label entry in an task blob.
  385. */
  386. static inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
  387. {
  388. return tsp->smk_forked;
  389. }
  390. /*
  391. * Present a pointer to the smack label in the current task blob.
  392. */
  393. static inline struct smack_known *smk_of_current(void)
  394. {
  395. return smk_of_task(smack_cred(current_cred()));
  396. }
  397. void smack_log(char *subject_label, char *object_label,
  398. int request,
  399. int result, struct smk_audit_info *auditdata);
  400. #ifdef CONFIG_AUDIT
  401. /*
  402. * logging functions
  403. */
  404. #define SMACK_AUDIT_DENIED 0x1
  405. #define SMACK_AUDIT_ACCEPT 0x2
  406. extern int log_policy;
  407. /*
  408. * some inline functions to set up audit data
  409. * they do nothing if CONFIG_AUDIT is not set
  410. *
  411. */
  412. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  413. char type)
  414. {
  415. memset(&a->sad, 0, sizeof(a->sad));
  416. a->a.type = type;
  417. a->a.smack_audit_data = &a->sad;
  418. a->a.smack_audit_data->function = func;
  419. }
  420. static inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
  421. char type, struct lsm_network_audit *net)
  422. {
  423. smk_ad_init(a, func, type);
  424. memset(net, 0, sizeof(*net));
  425. a->a.u.net = net;
  426. }
  427. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  428. struct task_struct *t)
  429. {
  430. a->a.u.tsk = t;
  431. }
  432. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  433. struct dentry *d)
  434. {
  435. a->a.u.dentry = d;
  436. }
  437. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  438. struct inode *i)
  439. {
  440. a->a.u.inode = i;
  441. }
  442. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  443. struct path p)
  444. {
  445. a->a.u.path = p;
  446. }
  447. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  448. struct sock *sk)
  449. {
  450. a->a.u.net->sk = sk;
  451. }
  452. #else /* no AUDIT */
  453. static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
  454. char type)
  455. {
  456. }
  457. static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
  458. struct task_struct *t)
  459. {
  460. }
  461. static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
  462. struct dentry *d)
  463. {
  464. }
  465. static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
  466. struct inode *i)
  467. {
  468. }
  469. static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
  470. struct path p)
  471. {
  472. }
  473. static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
  474. struct sock *sk)
  475. {
  476. }
  477. #endif
  478. #endif /* _SECURITY_SMACK_H */