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[SMK_LABELLEN]; /* TCP peer label */
53 char *smk_inode; /* label of the fso */
54 char *smk_task; /* label of the task */
55 struct mutex smk_lock; /* initialization lock */
56 int smk_flags; /* smack inode flags */
60 char *smk_task; /* label used for access control */
61 char *smk_forked; /* label when forked */
64 #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
67 * A label access rule.
70 struct list_head list;
77 * An entry in the table mapping smack values to
78 * CIPSO level/category-set values.
82 char smk_catset[SMK_LABELLEN];
86 * An entry in the table identifying hosts.
88 struct smk_netlbladdr {
89 struct list_head list;
90 struct sockaddr_in smk_host; /* network address */
91 struct in_addr smk_mask; /* network mask */
92 char *smk_label; /* label */
96 * This is the repository for labels seen so that it is
97 * not necessary to keep allocating tiny chuncks of memory
98 * and so that they can be shared.
100 * Labels are never modified in place. Anytime a label
101 * is imported (e.g. xattrset on a file) the list is checked
102 * for it and it is added if it doesn't exist. The address
103 * is passed out in either case. Entries are added, but
106 * Since labels are hanging around anyway it doesn't
107 * hurt to maintain a secid for those awkward situations
108 * where kernel components that ought to use LSM independent
109 * interfaces don't. The secid should go away when all of
110 * these components have been repaired.
112 * If there is a cipso value associated with the label it
113 * gets stored here, too. This will most likely be rare as
114 * the cipso direct mapping in used internally.
117 struct list_head list;
118 char smk_known[SMK_LABELLEN];
120 struct smack_cipso *smk_cipso;
121 spinlock_t smk_cipsolock; /* for changing cipso map */
127 #define SMK_FSDEFAULT "smackfsdef="
128 #define SMK_FSFLOOR "smackfsfloor="
129 #define SMK_FSHAT "smackfshat="
130 #define SMK_FSROOT "smackfsroot="
132 #define SMACK_CIPSO_OPTION "-CIPSO"
135 * How communications on this socket are treated.
136 * Usually it's determined by the underlying netlabel code
137 * but there are certain cases, including single label hosts
138 * and potentially single label interfaces for which the
139 * treatment can not be known in advance.
141 * The possibility of additional labeling schemes being
142 * introduced in the future exists as well.
144 #define SMACK_UNLABELED_SOCKET 0
145 #define SMACK_CIPSO_SOCKET 1
148 * smackfs magic number
149 * smackfs macic number
151 #define SMACK_MAGIC 0x43415d53 /* "SMAC" */
154 * A limit on the number of entries in the lists
155 * makes some of the list administration easier.
157 #define SMACK_LIST_MAX 10000
162 #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
163 #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
164 #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
165 #define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */
166 #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
167 #define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */
170 * Just to make the common cases easier to deal with
172 #define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
173 #define MAY_ANYREAD (MAY_READ | MAY_EXEC)
174 #define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND)
175 #define MAY_READWRITE (MAY_READ | MAY_WRITE)
179 * Number of access types used by Smack (rwxa)
181 #define SMK_NUM_ACCESS_TYPE 4
184 * Smack audit data; is empty if CONFIG_AUDIT not set
187 struct smk_audit_info {
189 struct common_audit_data a;
193 * These functions are in smack_lsm.c
195 struct inode_smack *new_inode_smack(char *);
198 * These functions are in smack_access.c
200 int smk_access(char *, char *, int, struct smk_audit_info *);
201 int smk_curacc(char *, u32, struct smk_audit_info *);
202 int smack_to_cipso(const char *, struct smack_cipso *);
203 void smack_from_cipso(u32, char *, char *);
204 char *smack_from_secid(const u32);
205 char *smk_import(const char *, int);
206 struct smack_known *smk_import_entry(const char *, int);
207 u32 smack_to_secid(const char *);
212 extern int smack_cipso_direct;
213 extern char *smack_net_ambient;
214 extern char *smack_onlycap;
215 extern const char *smack_cipso_option;
217 extern struct smack_known smack_known_floor;
218 extern struct smack_known smack_known_hat;
219 extern struct smack_known smack_known_huh;
220 extern struct smack_known smack_known_invalid;
221 extern struct smack_known smack_known_star;
222 extern struct smack_known smack_known_web;
224 extern struct list_head smack_known_list;
225 extern struct list_head smack_rule_list;
226 extern struct list_head smk_netlbladdr_list;
228 extern struct security_operations smack_ops;
231 * Stricly for CIPSO level manipulation.
232 * Set the category bit number in a smack label sized buffer.
234 static inline void smack_catset_bit(int cat, char *catsetp)
236 if (cat > SMK_LABELLEN * 8)
239 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
243 * Present a pointer to the smack label in an inode blob.
245 static inline char *smk_of_inode(const struct inode *isp)
247 struct inode_smack *sip = isp->i_security;
248 return sip->smk_inode;
252 * Present a pointer to the smack label in an task blob.
254 static inline char *smk_of_task(const struct task_smack *tsp)
256 return tsp->smk_task;
260 * Present a pointer to the forked smack label in an task blob.
262 static inline char *smk_of_forked(const struct task_smack *tsp)
264 return tsp->smk_forked;
268 * Present a pointer to the smack label in the curren task blob.
270 static inline char *smk_of_current(void)
272 return smk_of_task(current_security());
278 #define SMACK_AUDIT_DENIED 0x1
279 #define SMACK_AUDIT_ACCEPT 0x2
280 extern int log_policy;
282 void smack_log(char *subject_label, char *object_label,
284 int result, struct smk_audit_info *auditdata);
289 * some inline functions to set up audit data
290 * they do nothing if CONFIG_AUDIT is not set
293 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
296 memset(a, 0, sizeof(*a));
298 a->a.smack_audit_data.function = func;
301 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
302 struct task_struct *t)
306 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
309 a->a.u.fs.path.dentry = d;
311 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
314 a->a.u.fs.path.mnt = m;
316 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
321 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
326 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
334 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
338 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
339 struct task_struct *t)
342 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
346 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
350 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
354 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
358 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
364 #endif /* _SECURITY_SMACK_H */