gfp flags for security_inode_alloc()?
[linux-2.6-block.git] / security / smack / smack.h
CommitLineData
e114e473
CS
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
13#ifndef _SECURITY_SMACK_H
14#define _SECURITY_SMACK_H
15
16#include <linux/capability.h>
17#include <linux/spinlock.h>
076c54c5 18#include <linux/security.h>
6d3dc07c 19#include <linux/in.h>
e114e473 20#include <net/netlabel.h>
7198e2ee
EB
21#include <linux/list.h>
22#include <linux/rculist.h>
ecfcc53f 23#include <linux/lsm_audit.h>
e114e473
CS
24
25/*
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.
30 */
31#define SMK_MAXLEN 23
32#define SMK_LABELLEN (SMK_MAXLEN+1)
33
e114e473
CS
34struct superblock_smack {
35 char *smk_root;
36 char *smk_floor;
37 char *smk_hat;
38 char *smk_default;
39 int smk_initialized;
40 spinlock_t smk_sblock; /* for initialization */
41};
42
43struct socket_smack {
272cd7a8
CS
44 char *smk_out; /* outbound label */
45 char *smk_in; /* inbound label */
46 char *smk_packet; /* TCP peer label */
e114e473
CS
47};
48
49/*
50 * Inode smack data
51 */
52struct inode_smack {
53 char *smk_inode; /* label of the fso */
676dac4b 54 char *smk_task; /* label of the task */
7898e1f8 55 char *smk_mmap; /* label of the mmap domain */
e114e473
CS
56 struct mutex smk_lock; /* initialization lock */
57 int smk_flags; /* smack inode flags */
58};
59
676dac4b 60struct task_smack {
7898e1f8
CS
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 */
676dac4b
CS
65};
66
e114e473 67#define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
5c6d1125 68#define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */
2267b13a 69#define SMK_INODE_CHANGED 0x04 /* smack was transmuted */
e114e473
CS
70
71/*
72 * A label access rule.
73 */
74struct smack_rule {
7198e2ee
EB
75 struct list_head list;
76 char *smk_subject;
77 char *smk_object;
78 int smk_access;
e114e473
CS
79};
80
81/*
82 * An entry in the table mapping smack values to
83 * CIPSO level/category-set values.
84 */
85struct smack_cipso {
86 int smk_level;
87 char smk_catset[SMK_LABELLEN];
88};
89
6d3dc07c
CS
90/*
91 * An entry in the table identifying hosts.
92 */
93struct smk_netlbladdr {
7198e2ee 94 struct list_head list;
6d3dc07c
CS
95 struct sockaddr_in smk_host; /* network address */
96 struct in_addr smk_mask; /* network mask */
97 char *smk_label; /* label */
98};
99
e114e473
CS
100/*
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.
104 *
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
109 * never deleted.
110 *
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.
116 *
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.
272cd7a8
CS
120 *
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
123 * time.
e114e473
CS
124 */
125struct smack_known {
7198e2ee 126 struct list_head list;
e114e473
CS
127 char smk_known[SMK_LABELLEN];
128 u32 smk_secid;
129 struct smack_cipso *smk_cipso;
272cd7a8
CS
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 */
e114e473
CS
133};
134
135/*
136 * Mount options
137 */
138#define SMK_FSDEFAULT "smackfsdef="
139#define SMK_FSFLOOR "smackfsfloor="
140#define SMK_FSHAT "smackfshat="
141#define SMK_FSROOT "smackfsroot="
142
4303154e
EB
143#define SMACK_CIPSO_OPTION "-CIPSO"
144
e114e473 145/*
6d3dc07c
CS
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.
151 *
152 * The possibility of additional labeling schemes being
153 * introduced in the future exists as well.
154 */
155#define SMACK_UNLABELED_SOCKET 0
156#define SMACK_CIPSO_SOCKET 1
157
158/*
159 * smackfs magic number
e114e473
CS
160 */
161#define SMACK_MAGIC 0x43415d53 /* "SMAC" */
162
e114e473
CS
163/*
164 * CIPSO defaults.
165 */
166#define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
6d3dc07c 167#define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
e114e473
CS
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 */
172
5c6d1125
JS
173/*
174 * Flag for transmute access
175 */
176#define MAY_TRANSMUTE 64
e114e473
CS
177/*
178 * Just to make the common cases easier to deal with
179 */
e114e473 180#define MAY_ANYREAD (MAY_READ | MAY_EXEC)
e114e473
CS
181#define MAY_READWRITE (MAY_READ | MAY_WRITE)
182#define MAY_NOT 0
183
ecfcc53f 184/*
ce8a4321 185 * Number of access types used by Smack (rwxat)
ecfcc53f 186 */
ce8a4321 187#define SMK_NUM_ACCESS_TYPE 5
ecfcc53f 188
3b3b0e4f
EP
189/* SMACK data */
190struct smack_audit_data {
191 const char *function;
192 char *subject;
193 char *object;
194 char *request;
195 int result;
196};
197
ecfcc53f
EB
198/*
199 * Smack audit data; is empty if CONFIG_AUDIT not set
200 * to save some stack
201 */
202struct smk_audit_info {
203#ifdef CONFIG_AUDIT
204 struct common_audit_data a;
3b3b0e4f 205 struct smack_audit_data sad;
ecfcc53f
EB
206#endif
207};
e114e473
CS
208/*
209 * These functions are in smack_lsm.c
210 */
211struct inode_smack *new_inode_smack(char *);
212
213/*
214 * These functions are in smack_access.c
215 */
7898e1f8 216int smk_access_entry(char *, char *, struct list_head *);
ecfcc53f
EB
217int smk_access(char *, char *, int, struct smk_audit_info *);
218int smk_curacc(char *, u32, struct smk_audit_info *);
e114e473 219int smack_to_cipso(const char *, struct smack_cipso *);
272cd7a8 220char *smack_from_cipso(u32, char *);
e114e473 221char *smack_from_secid(const u32);
0e94ae17 222void smk_parse_smack(const char *string, int len, char *smack);
e114e473
CS
223char *smk_import(const char *, int);
224struct smack_known *smk_import_entry(const char *, int);
272cd7a8 225struct smack_known *smk_find_entry(const char *);
e114e473
CS
226u32 smack_to_secid(const char *);
227
228/*
229 * Shared data.
230 */
231extern int smack_cipso_direct;
e114e473 232extern char *smack_net_ambient;
15446235 233extern char *smack_onlycap;
4303154e 234extern const char *smack_cipso_option;
e114e473 235
e114e473
CS
236extern struct smack_known smack_known_floor;
237extern struct smack_known smack_known_hat;
238extern struct smack_known smack_known_huh;
239extern struct smack_known smack_known_invalid;
240extern struct smack_known smack_known_star;
6d3dc07c 241extern struct smack_known smack_known_web;
e114e473 242
7198e2ee 243extern struct list_head smack_known_list;
7198e2ee
EB
244extern struct list_head smk_netlbladdr_list;
245
076c54c5 246extern struct security_operations smack_ops;
e114e473
CS
247
248/*
249 * Stricly for CIPSO level manipulation.
250 * Set the category bit number in a smack label sized buffer.
251 */
252static inline void smack_catset_bit(int cat, char *catsetp)
253{
254 if (cat > SMK_LABELLEN * 8)
255 return;
256
257 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
258}
259
5c6d1125
JS
260/*
261 * Is the directory transmuting?
262 */
263static inline int smk_inode_transmutable(const struct inode *isp)
264{
265 struct inode_smack *sip = isp->i_security;
266 return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
267}
268
e114e473
CS
269/*
270 * Present a pointer to the smack label in an inode blob.
271 */
272static inline char *smk_of_inode(const struct inode *isp)
273{
274 struct inode_smack *sip = isp->i_security;
275 return sip->smk_inode;
276}
277
676dac4b
CS
278/*
279 * Present a pointer to the smack label in an task blob.
280 */
281static inline char *smk_of_task(const struct task_smack *tsp)
282{
283 return tsp->smk_task;
284}
285
286/*
287 * Present a pointer to the forked smack label in an task blob.
288 */
289static inline char *smk_of_forked(const struct task_smack *tsp)
290{
291 return tsp->smk_forked;
292}
293
294/*
5c6d1125 295 * Present a pointer to the smack label in the current task blob.
676dac4b
CS
296 */
297static inline char *smk_of_current(void)
298{
299 return smk_of_task(current_security());
300}
301
ecfcc53f
EB
302/*
303 * logging functions
304 */
305#define SMACK_AUDIT_DENIED 0x1
306#define SMACK_AUDIT_ACCEPT 0x2
307extern int log_policy;
308
309void smack_log(char *subject_label, char *object_label,
310 int request,
311 int result, struct smk_audit_info *auditdata);
312
313#ifdef CONFIG_AUDIT
314
315/*
316 * some inline functions to set up audit data
317 * they do nothing if CONFIG_AUDIT is not set
318 *
319 */
320static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
321 char type)
322{
323 memset(a, 0, sizeof(*a));
324 a->a.type = type;
3b3b0e4f
EP
325 a->a.smack_audit_data = &a->sad;
326 a->a.smack_audit_data->function = func;
ecfcc53f
EB
327}
328
48c62af6
EP
329static inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
330 char type, struct lsm_network_audit *net)
331{
332 smk_ad_init(a, func, type);
333 memset(net, 0, sizeof(*net));
334 a->a.u.net = net;
335}
336
ecfcc53f
EB
337static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
338 struct task_struct *t)
339{
340 a->a.u.tsk = t;
341}
342static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
343 struct dentry *d)
344{
a269434d 345 a->a.u.dentry = d;
ecfcc53f
EB
346}
347static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
348 struct inode *i)
349{
f48b7399 350 a->a.u.inode = i;
ecfcc53f
EB
351}
352static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
353 struct path p)
354{
f48b7399 355 a->a.u.path = p;
ecfcc53f
EB
356}
357static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
358 struct sock *sk)
359{
48c62af6 360 a->a.u.net->sk = sk;
ecfcc53f
EB
361}
362
363#else /* no AUDIT */
364
365static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
366 char type)
367{
368}
369static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
370 struct task_struct *t)
371{
372}
373static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
374 struct dentry *d)
375{
376}
377static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
378 struct vfsmount *m)
379{
380}
381static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
382 struct inode *i)
383{
384}
385static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
386 struct path p)
387{
388}
389static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
390 struct sock *sk)
391{
392}
393#endif
394
e114e473 395#endif /* _SECURITY_SMACK_H */