Smack: Transmute labels on specified directories
[deliverable/linux.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 {
44 char *smk_out; /* outbound label */
45 char *smk_in; /* inbound label */
46 char smk_packet[SMK_LABELLEN]; /* TCP peer label */
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 */
e114e473
CS
55 struct mutex smk_lock; /* initialization lock */
56 int smk_flags; /* smack inode flags */
57};
58
676dac4b
CS
59struct task_smack {
60 char *smk_task; /* label used for access control */
61 char *smk_forked; /* label when forked */
62};
63
e114e473 64#define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
5c6d1125 65#define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */
e114e473
CS
66
67/*
68 * A label access rule.
69 */
70struct smack_rule {
7198e2ee
EB
71 struct list_head list;
72 char *smk_subject;
73 char *smk_object;
74 int smk_access;
e114e473
CS
75};
76
77/*
78 * An entry in the table mapping smack values to
79 * CIPSO level/category-set values.
80 */
81struct smack_cipso {
82 int smk_level;
83 char smk_catset[SMK_LABELLEN];
84};
85
6d3dc07c
CS
86/*
87 * An entry in the table identifying hosts.
88 */
89struct smk_netlbladdr {
7198e2ee 90 struct list_head list;
6d3dc07c
CS
91 struct sockaddr_in smk_host; /* network address */
92 struct in_addr smk_mask; /* network mask */
93 char *smk_label; /* label */
94};
95
e114e473
CS
96/*
97 * This is the repository for labels seen so that it is
98 * not necessary to keep allocating tiny chuncks of memory
99 * and so that they can be shared.
100 *
101 * Labels are never modified in place. Anytime a label
102 * is imported (e.g. xattrset on a file) the list is checked
103 * for it and it is added if it doesn't exist. The address
104 * is passed out in either case. Entries are added, but
105 * never deleted.
106 *
107 * Since labels are hanging around anyway it doesn't
108 * hurt to maintain a secid for those awkward situations
109 * where kernel components that ought to use LSM independent
110 * interfaces don't. The secid should go away when all of
111 * these components have been repaired.
112 *
113 * If there is a cipso value associated with the label it
114 * gets stored here, too. This will most likely be rare as
115 * the cipso direct mapping in used internally.
116 */
117struct smack_known {
7198e2ee 118 struct list_head list;
e114e473
CS
119 char smk_known[SMK_LABELLEN];
120 u32 smk_secid;
121 struct smack_cipso *smk_cipso;
122 spinlock_t smk_cipsolock; /* for changing cipso map */
123};
124
125/*
126 * Mount options
127 */
128#define SMK_FSDEFAULT "smackfsdef="
129#define SMK_FSFLOOR "smackfsfloor="
130#define SMK_FSHAT "smackfshat="
131#define SMK_FSROOT "smackfsroot="
132
4303154e
EB
133#define SMACK_CIPSO_OPTION "-CIPSO"
134
e114e473 135/*
6d3dc07c
CS
136 * How communications on this socket are treated.
137 * Usually it's determined by the underlying netlabel code
138 * but there are certain cases, including single label hosts
139 * and potentially single label interfaces for which the
140 * treatment can not be known in advance.
141 *
142 * The possibility of additional labeling schemes being
143 * introduced in the future exists as well.
144 */
145#define SMACK_UNLABELED_SOCKET 0
146#define SMACK_CIPSO_SOCKET 1
147
148/*
149 * smackfs magic number
e114e473
CS
150 * smackfs macic number
151 */
152#define SMACK_MAGIC 0x43415d53 /* "SMAC" */
153
154/*
155 * A limit on the number of entries in the lists
156 * makes some of the list administration easier.
157 */
158#define SMACK_LIST_MAX 10000
159
160/*
161 * CIPSO defaults.
162 */
163#define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
6d3dc07c 164#define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
e114e473
CS
165#define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
166#define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */
167#define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
168#define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */
169
5c6d1125
JS
170/*
171 * Flag for transmute access
172 */
173#define MAY_TRANSMUTE 64
e114e473
CS
174/*
175 * Just to make the common cases easier to deal with
176 */
177#define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
178#define MAY_ANYREAD (MAY_READ | MAY_EXEC)
179#define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND)
180#define MAY_READWRITE (MAY_READ | MAY_WRITE)
181#define MAY_NOT 0
182
ecfcc53f
EB
183/*
184 * Number of access types used by Smack (rwxa)
185 */
186#define SMK_NUM_ACCESS_TYPE 4
187
188/*
189 * Smack audit data; is empty if CONFIG_AUDIT not set
190 * to save some stack
191 */
192struct smk_audit_info {
193#ifdef CONFIG_AUDIT
194 struct common_audit_data a;
195#endif
196};
e114e473
CS
197/*
198 * These functions are in smack_lsm.c
199 */
200struct inode_smack *new_inode_smack(char *);
201
202/*
203 * These functions are in smack_access.c
204 */
5c6d1125 205int smk_access_entry(char *, char *);
ecfcc53f
EB
206int smk_access(char *, char *, int, struct smk_audit_info *);
207int smk_curacc(char *, u32, struct smk_audit_info *);
e114e473
CS
208int smack_to_cipso(const char *, struct smack_cipso *);
209void smack_from_cipso(u32, char *, char *);
210char *smack_from_secid(const u32);
211char *smk_import(const char *, int);
212struct smack_known *smk_import_entry(const char *, int);
213u32 smack_to_secid(const char *);
214
215/*
216 * Shared data.
217 */
218extern int smack_cipso_direct;
e114e473 219extern char *smack_net_ambient;
15446235 220extern char *smack_onlycap;
4303154e 221extern const char *smack_cipso_option;
e114e473 222
e114e473
CS
223extern struct smack_known smack_known_floor;
224extern struct smack_known smack_known_hat;
225extern struct smack_known smack_known_huh;
226extern struct smack_known smack_known_invalid;
227extern struct smack_known smack_known_star;
6d3dc07c 228extern struct smack_known smack_known_web;
e114e473 229
7198e2ee
EB
230extern struct list_head smack_known_list;
231extern struct list_head smack_rule_list;
232extern struct list_head smk_netlbladdr_list;
233
076c54c5 234extern struct security_operations smack_ops;
e114e473
CS
235
236/*
237 * Stricly for CIPSO level manipulation.
238 * Set the category bit number in a smack label sized buffer.
239 */
240static inline void smack_catset_bit(int cat, char *catsetp)
241{
242 if (cat > SMK_LABELLEN * 8)
243 return;
244
245 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
246}
247
5c6d1125
JS
248/*
249 * Is the directory transmuting?
250 */
251static inline int smk_inode_transmutable(const struct inode *isp)
252{
253 struct inode_smack *sip = isp->i_security;
254 return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
255}
256
e114e473
CS
257/*
258 * Present a pointer to the smack label in an inode blob.
259 */
260static inline char *smk_of_inode(const struct inode *isp)
261{
262 struct inode_smack *sip = isp->i_security;
263 return sip->smk_inode;
264}
265
676dac4b
CS
266/*
267 * Present a pointer to the smack label in an task blob.
268 */
269static inline char *smk_of_task(const struct task_smack *tsp)
270{
271 return tsp->smk_task;
272}
273
274/*
275 * Present a pointer to the forked smack label in an task blob.
276 */
277static inline char *smk_of_forked(const struct task_smack *tsp)
278{
279 return tsp->smk_forked;
280}
281
282/*
5c6d1125 283 * Present a pointer to the smack label in the current task blob.
676dac4b
CS
284 */
285static inline char *smk_of_current(void)
286{
287 return smk_of_task(current_security());
288}
289
ecfcc53f
EB
290/*
291 * logging functions
292 */
293#define SMACK_AUDIT_DENIED 0x1
294#define SMACK_AUDIT_ACCEPT 0x2
295extern int log_policy;
296
297void smack_log(char *subject_label, char *object_label,
298 int request,
299 int result, struct smk_audit_info *auditdata);
300
301#ifdef CONFIG_AUDIT
302
303/*
304 * some inline functions to set up audit data
305 * they do nothing if CONFIG_AUDIT is not set
306 *
307 */
308static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
309 char type)
310{
311 memset(a, 0, sizeof(*a));
312 a->a.type = type;
d4131ded 313 a->a.smack_audit_data.function = func;
ecfcc53f
EB
314}
315
316static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
317 struct task_struct *t)
318{
319 a->a.u.tsk = t;
320}
321static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
322 struct dentry *d)
323{
324 a->a.u.fs.path.dentry = d;
325}
326static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
327 struct vfsmount *m)
328{
329 a->a.u.fs.path.mnt = m;
330}
331static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
332 struct inode *i)
333{
334 a->a.u.fs.inode = i;
335}
336static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
337 struct path p)
338{
339 a->a.u.fs.path = p;
340}
341static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
342 struct sock *sk)
343{
344 a->a.u.net.sk = sk;
345}
346
347#else /* no AUDIT */
348
349static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
350 char type)
351{
352}
353static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
354 struct task_struct *t)
355{
356}
357static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
358 struct dentry *d)
359{
360}
361static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
362 struct vfsmount *m)
363{
364}
365static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
366 struct inode *i)
367{
368}
369static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
370 struct path p)
371{
372}
373static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
374 struct sock *sk)
375{
376}
377#endif
378
e114e473 379#endif /* _SECURITY_SMACK_H */
This page took 0.207216 seconds and 5 git commands to generate.