blob: a2e2cdfab4ef2004c4e2820ba79f4a438abc8194 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
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>
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +020018#include <linux/security.h>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050019#include <linux/in.h>
Casey Schauflere114e472008-02-04 22:29:50 -080020#include <net/netlabel.h>
Etienne Basset7198e2e2009-03-24 20:53:24 +010021#include <linux/list.h>
22#include <linux/rculist.h>
Etienne Bassetecfcc532009-04-08 20:40:06 +020023#include <linux/lsm_audit.h>
Casey Schauflere114e472008-02-04 22:29:50 -080024
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
Casey Schauflere114e472008-02-04 22:29:50 -080034struct 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 */
Casey Schaufler676dac42010-12-02 06:43:39 -080054 char *smk_task; /* label of the task */
Casey Schauflere114e472008-02-04 22:29:50 -080055 struct mutex smk_lock; /* initialization lock */
56 int smk_flags; /* smack inode flags */
57};
58
Casey Schaufler676dac42010-12-02 06:43:39 -080059struct task_smack {
60 char *smk_task; /* label used for access control */
61 char *smk_forked; /* label when forked */
62};
63
Casey Schauflere114e472008-02-04 22:29:50 -080064#define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
65
66/*
67 * A label access rule.
68 */
69struct smack_rule {
Etienne Basset7198e2e2009-03-24 20:53:24 +010070 struct list_head list;
71 char *smk_subject;
72 char *smk_object;
73 int smk_access;
Casey Schauflere114e472008-02-04 22:29:50 -080074};
75
76/*
77 * An entry in the table mapping smack values to
78 * CIPSO level/category-set values.
79 */
80struct smack_cipso {
81 int smk_level;
82 char smk_catset[SMK_LABELLEN];
83};
84
85/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -050086 * An entry in the table identifying hosts.
87 */
88struct smk_netlbladdr {
Etienne Basset7198e2e2009-03-24 20:53:24 +010089 struct list_head list;
Casey Schaufler6d3dc072008-12-31 12:54:12 -050090 struct sockaddr_in smk_host; /* network address */
91 struct in_addr smk_mask; /* network mask */
92 char *smk_label; /* label */
93};
94
95/*
Casey Schauflere114e472008-02-04 22:29:50 -080096 * 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.
99 *
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
104 * never deleted.
105 *
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.
111 *
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.
115 */
116struct smack_known {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100117 struct list_head list;
Casey Schauflere114e472008-02-04 22:29:50 -0800118 char smk_known[SMK_LABELLEN];
119 u32 smk_secid;
120 struct smack_cipso *smk_cipso;
121 spinlock_t smk_cipsolock; /* for changing cipso map */
122};
123
124/*
125 * Mount options
126 */
127#define SMK_FSDEFAULT "smackfsdef="
128#define SMK_FSFLOOR "smackfsfloor="
129#define SMK_FSHAT "smackfshat="
130#define SMK_FSROOT "smackfsroot="
131
Etienne Basset43031542009-03-27 17:11:01 -0400132#define SMACK_CIPSO_OPTION "-CIPSO"
133
Casey Schauflere114e472008-02-04 22:29:50 -0800134/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500135 * 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.
140 *
141 * The possibility of additional labeling schemes being
142 * introduced in the future exists as well.
143 */
144#define SMACK_UNLABELED_SOCKET 0
145#define SMACK_CIPSO_SOCKET 1
146
147/*
148 * smackfs magic number
Casey Schauflere114e472008-02-04 22:29:50 -0800149 * smackfs macic number
150 */
151#define SMACK_MAGIC 0x43415d53 /* "SMAC" */
152
153/*
154 * A limit on the number of entries in the lists
155 * makes some of the list administration easier.
156 */
157#define SMACK_LIST_MAX 10000
158
159/*
160 * CIPSO defaults.
161 */
162#define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500163#define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
Casey Schauflere114e472008-02-04 22:29:50 -0800164#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 */
168
169/*
170 * Just to make the common cases easier to deal with
171 */
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)
176#define MAY_NOT 0
177
178/*
Etienne Bassetecfcc532009-04-08 20:40:06 +0200179 * Number of access types used by Smack (rwxa)
180 */
181#define SMK_NUM_ACCESS_TYPE 4
182
183/*
184 * Smack audit data; is empty if CONFIG_AUDIT not set
185 * to save some stack
186 */
187struct smk_audit_info {
188#ifdef CONFIG_AUDIT
189 struct common_audit_data a;
190#endif
191};
192/*
Casey Schauflere114e472008-02-04 22:29:50 -0800193 * These functions are in smack_lsm.c
194 */
195struct inode_smack *new_inode_smack(char *);
196
197/*
198 * These functions are in smack_access.c
199 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200200int smk_access(char *, char *, int, struct smk_audit_info *);
201int smk_curacc(char *, u32, struct smk_audit_info *);
Casey Schauflere114e472008-02-04 22:29:50 -0800202int smack_to_cipso(const char *, struct smack_cipso *);
203void smack_from_cipso(u32, char *, char *);
204char *smack_from_secid(const u32);
205char *smk_import(const char *, int);
206struct smack_known *smk_import_entry(const char *, int);
207u32 smack_to_secid(const char *);
208
209/*
210 * Shared data.
211 */
212extern int smack_cipso_direct;
Casey Schauflere114e472008-02-04 22:29:50 -0800213extern char *smack_net_ambient;
Casey Schaufler15446232008-07-30 15:37:11 -0700214extern char *smack_onlycap;
Etienne Basset43031542009-03-27 17:11:01 -0400215extern const char *smack_cipso_option;
Casey Schauflere114e472008-02-04 22:29:50 -0800216
Casey Schauflere114e472008-02-04 22:29:50 -0800217extern struct smack_known smack_known_floor;
218extern struct smack_known smack_known_hat;
219extern struct smack_known smack_known_huh;
220extern struct smack_known smack_known_invalid;
221extern struct smack_known smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500222extern struct smack_known smack_known_web;
Casey Schauflere114e472008-02-04 22:29:50 -0800223
Etienne Basset7198e2e2009-03-24 20:53:24 +0100224extern struct list_head smack_known_list;
225extern struct list_head smack_rule_list;
226extern struct list_head smk_netlbladdr_list;
227
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +0200228extern struct security_operations smack_ops;
Casey Schauflere114e472008-02-04 22:29:50 -0800229
230/*
231 * Stricly for CIPSO level manipulation.
232 * Set the category bit number in a smack label sized buffer.
233 */
234static inline void smack_catset_bit(int cat, char *catsetp)
235{
236 if (cat > SMK_LABELLEN * 8)
237 return;
238
239 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
240}
241
242/*
243 * Present a pointer to the smack label in an inode blob.
244 */
245static inline char *smk_of_inode(const struct inode *isp)
246{
247 struct inode_smack *sip = isp->i_security;
248 return sip->smk_inode;
249}
250
Etienne Bassetecfcc532009-04-08 20:40:06 +0200251/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800252 * Present a pointer to the smack label in an task blob.
253 */
254static inline char *smk_of_task(const struct task_smack *tsp)
255{
256 return tsp->smk_task;
257}
258
259/*
260 * Present a pointer to the forked smack label in an task blob.
261 */
262static inline char *smk_of_forked(const struct task_smack *tsp)
263{
264 return tsp->smk_forked;
265}
266
267/*
268 * Present a pointer to the smack label in the curren task blob.
269 */
270static inline char *smk_of_current(void)
271{
272 return smk_of_task(current_security());
273}
274
275/*
Etienne Bassetecfcc532009-04-08 20:40:06 +0200276 * logging functions
277 */
278#define SMACK_AUDIT_DENIED 0x1
279#define SMACK_AUDIT_ACCEPT 0x2
280extern int log_policy;
281
282void smack_log(char *subject_label, char *object_label,
283 int request,
284 int result, struct smk_audit_info *auditdata);
285
286#ifdef CONFIG_AUDIT
287
288/*
289 * some inline functions to set up audit data
290 * they do nothing if CONFIG_AUDIT is not set
291 *
292 */
293static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
294 char type)
295{
296 memset(a, 0, sizeof(*a));
297 a->a.type = type;
Thomas Liud4131de2009-07-09 10:00:30 -0400298 a->a.smack_audit_data.function = func;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200299}
300
301static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
302 struct task_struct *t)
303{
304 a->a.u.tsk = t;
305}
306static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
307 struct dentry *d)
308{
309 a->a.u.fs.path.dentry = d;
310}
311static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
312 struct vfsmount *m)
313{
314 a->a.u.fs.path.mnt = m;
315}
316static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
317 struct inode *i)
318{
319 a->a.u.fs.inode = i;
320}
321static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
322 struct path p)
323{
324 a->a.u.fs.path = p;
325}
326static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
327 struct sock *sk)
328{
329 a->a.u.net.sk = sk;
330}
331
332#else /* no AUDIT */
333
334static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
335 char type)
336{
337}
338static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
339 struct task_struct *t)
340{
341}
342static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
343 struct dentry *d)
344{
345}
346static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
347 struct vfsmount *m)
348{
349}
350static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
351 struct inode *i)
352{
353}
354static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
355 struct path p)
356{
357}
358static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
359 struct sock *sk)
360{
361}
362#endif
363
Casey Schauflere114e472008-02-04 22:29:50 -0800364#endif /* _SECURITY_SMACK_H */