2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
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.
9 * Casey Schaufler <casey@schaufler-ca.com>
13 #ifndef _SECURITY_SMACK_H
14 #define _SECURITY_SMACK_H
16 #include <linux/capability.h>
17 #include <linux/spinlock.h>
18 #include <linux/security.h>
20 #include <net/netlabel.h>
21 #include <linux/list.h>
22 #include <linux/rculist.h>
23 #include <linux/lsm_audit.h>
26 * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
27 * bigger than can be used, and 24 is the next lower multiple
28 * of 8, and there are too many issues if there isn't space set
29 * aside for the terminating null byte.
32 #define SMK_LABELLEN (SMK_MAXLEN+1)
34 struct superblock_smack {
40 spinlock_t smk_sblock; /* for initialization */
44 char *smk_out; /* outbound label */
45 char *smk_in; /* inbound label */
46 char *smk_packet; /* TCP peer label */
53 char *smk_inode; /* label of the fso */
54 char *smk_task; /* label of the task */
55 char *smk_mmap; /* label of the mmap domain */
56 struct mutex smk_lock; /* initialization lock */
57 int smk_flags; /* smack inode flags */
61 char *smk_task; /* label for access control */
62 char *smk_forked; /* label when forked */
63 struct list_head smk_rules; /* per task access rules */
64 struct mutex smk_rules_lock; /* lock for the rules */
67 #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
68 #define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */
69 #define SMK_INODE_CHANGED 0x04 /* smack was transmuted */
72 * A label access rule.
75 struct list_head list;
82 * An entry in the table mapping smack values to
83 * CIPSO level/category-set values.
87 char smk_catset[SMK_LABELLEN];
91 * An entry in the table identifying hosts.
93 struct smk_netlbladdr {
94 struct list_head list;
95 struct sockaddr_in smk_host; /* network address */
96 struct in_addr smk_mask; /* network mask */
97 char *smk_label; /* label */
101 * This is the repository for labels seen so that it is
102 * not necessary to keep allocating tiny chuncks of memory
103 * and so that they can be shared.
105 * Labels are never modified in place. Anytime a label
106 * is imported (e.g. xattrset on a file) the list is checked
107 * for it and it is added if it doesn't exist. The address
108 * is passed out in either case. Entries are added, but
111 * Since labels are hanging around anyway it doesn't
112 * hurt to maintain a secid for those awkward situations
113 * where kernel components that ought to use LSM independent
114 * interfaces don't. The secid should go away when all of
115 * these components have been repaired.
117 * If there is a cipso value associated with the label it
118 * gets stored here, too. This will most likely be rare as
119 * the cipso direct mapping in used internally.
121 * Keep the access rules for this subject label here so that
122 * the entire set of rules does not need to be examined every
126 struct list_head list;
127 char smk_known[SMK_LABELLEN];
129 struct smack_cipso *smk_cipso;
130 spinlock_t smk_cipsolock; /* for changing cipso map */
131 struct list_head smk_rules; /* access rules */
132 struct mutex smk_rules_lock; /* lock for the rules */
138 #define SMK_FSDEFAULT "smackfsdef="
139 #define SMK_FSFLOOR "smackfsfloor="
140 #define SMK_FSHAT "smackfshat="
141 #define SMK_FSROOT "smackfsroot="
143 #define SMACK_CIPSO_OPTION "-CIPSO"
146 * How communications on this socket are treated.
147 * Usually it's determined by the underlying netlabel code
148 * but there are certain cases, including single label hosts
149 * and potentially single label interfaces for which the
150 * treatment can not be known in advance.
152 * The possibility of additional labeling schemes being
153 * introduced in the future exists as well.
155 #define SMACK_UNLABELED_SOCKET 0
156 #define SMACK_CIPSO_SOCKET 1
159 * smackfs magic number
161 #define SMACK_MAGIC 0x43415d53 /* "SMAC" */
166 #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
167 #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
168 #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
169 #define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */
170 #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
171 #define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */
174 * Flag for transmute access
176 #define MAY_TRANSMUTE 64
178 * Just to make the common cases easier to deal with
180 #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
181 #define MAY_READWRITE (MAY_READ | MAY_WRITE)
185 * Number of access types used by Smack (rwxat)
187 #define SMK_NUM_ACCESS_TYPE 5
190 * Smack audit data; is empty if CONFIG_AUDIT not set
193 struct smk_audit_info {
195 struct common_audit_data a;
199 * These functions are in smack_lsm.c
201 struct inode_smack *new_inode_smack(char *);
204 * These functions are in smack_access.c
206 int smk_access_entry(char *, char *, struct list_head *);
207 int smk_access(char *, char *, int, struct smk_audit_info *);
208 int smk_curacc(char *, u32, struct smk_audit_info *);
209 int smack_to_cipso(const char *, struct smack_cipso *);
210 char *smack_from_cipso(u32, char *);
211 char *smack_from_secid(const u32);
212 void smk_parse_smack(const char *string, int len, char *smack);
213 char *smk_import(const char *, int);
214 struct smack_known *smk_import_entry(const char *, int);
215 struct smack_known *smk_find_entry(const char *);
216 u32 smack_to_secid(const char *);
221 extern int smack_cipso_direct;
222 extern char *smack_net_ambient;
223 extern char *smack_onlycap;
224 extern const char *smack_cipso_option;
226 extern struct smack_known smack_known_floor;
227 extern struct smack_known smack_known_hat;
228 extern struct smack_known smack_known_huh;
229 extern struct smack_known smack_known_invalid;
230 extern struct smack_known smack_known_star;
231 extern struct smack_known smack_known_web;
233 extern struct list_head smack_known_list;
234 extern struct list_head smk_netlbladdr_list;
236 extern struct security_operations smack_ops;
239 * Stricly for CIPSO level manipulation.
240 * Set the category bit number in a smack label sized buffer.
242 static inline void smack_catset_bit(int cat, char *catsetp)
244 if (cat > SMK_LABELLEN * 8)
247 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
251 * Is the directory transmuting?
253 static inline int smk_inode_transmutable(const struct inode *isp)
255 struct inode_smack *sip = isp->i_security;
256 return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
260 * Present a pointer to the smack label in an inode blob.
262 static inline char *smk_of_inode(const struct inode *isp)
264 struct inode_smack *sip = isp->i_security;
265 return sip->smk_inode;
269 * Present a pointer to the smack label in an task blob.
271 static inline char *smk_of_task(const struct task_smack *tsp)
273 return tsp->smk_task;
277 * Present a pointer to the forked smack label in an task blob.
279 static inline char *smk_of_forked(const struct task_smack *tsp)
281 return tsp->smk_forked;
285 * Present a pointer to the smack label in the current task blob.
287 static inline char *smk_of_current(void)
289 return smk_of_task(current_security());
295 #define SMACK_AUDIT_DENIED 0x1
296 #define SMACK_AUDIT_ACCEPT 0x2
297 extern int log_policy;
299 void smack_log(char *subject_label, char *object_label,
301 int result, struct smk_audit_info *auditdata);
306 * some inline functions to set up audit data
307 * they do nothing if CONFIG_AUDIT is not set
310 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
313 memset(a, 0, sizeof(*a));
315 a->a.smack_audit_data.function = func;
318 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
319 struct task_struct *t)
323 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
326 a->a.u.fs.path.dentry = d;
328 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
331 a->a.u.fs.path.mnt = m;
333 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
338 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
343 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
351 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
355 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
356 struct task_struct *t)
359 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
363 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
367 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
371 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
375 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
381 #endif /* _SECURITY_SMACK_H */