blob: 129c4eb8ffb10cbba9dce59ef4e5761624d19f7e [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 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020065#define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */
Casey Schauflere114e472008-02-04 22:29:50 -080066
67/*
68 * A label access rule.
69 */
70struct smack_rule {
Etienne Basset7198e2e2009-03-24 20:53:24 +010071 struct list_head list;
72 char *smk_subject;
73 char *smk_object;
74 int smk_access;
Casey Schauflere114e472008-02-04 22:29:50 -080075};
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
86/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -050087 * An entry in the table identifying hosts.
88 */
89struct smk_netlbladdr {
Etienne Basset7198e2e2009-03-24 20:53:24 +010090 struct list_head list;
Casey Schaufler6d3dc072008-12-31 12:54:12 -050091 struct sockaddr_in smk_host; /* network address */
92 struct in_addr smk_mask; /* network mask */
93 char *smk_label; /* label */
94};
95
96/*
Casey Schauflere114e472008-02-04 22:29:50 -080097 * 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 {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100118 struct list_head list;
Casey Schauflere114e472008-02-04 22:29:50 -0800119 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
Etienne Basset43031542009-03-27 17:11:01 -0400133#define SMACK_CIPSO_OPTION "-CIPSO"
134
Casey Schauflere114e472008-02-04 22:29:50 -0800135/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500136 * 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
Casey Schauflere114e472008-02-04 22:29:50 -0800150 * 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 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500164#define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
Casey Schauflere114e472008-02-04 22:29:50 -0800165#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
170/*
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200171 * Flag for transmute access
172 */
173#define MAY_TRANSMUTE 64
174/*
Casey Schauflere114e472008-02-04 22:29:50 -0800175 * 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
183/*
Etienne Bassetecfcc532009-04-08 20:40:06 +0200184 * 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};
197/*
Casey Schauflere114e472008-02-04 22:29:50 -0800198 * 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 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200205int smk_access_entry(char *, char *);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200206int smk_access(char *, char *, int, struct smk_audit_info *);
207int smk_curacc(char *, u32, struct smk_audit_info *);
Casey Schauflere114e472008-02-04 22:29:50 -0800208int 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;
Casey Schauflere114e472008-02-04 22:29:50 -0800219extern char *smack_net_ambient;
Casey Schaufler15446232008-07-30 15:37:11 -0700220extern char *smack_onlycap;
Etienne Basset43031542009-03-27 17:11:01 -0400221extern const char *smack_cipso_option;
Casey Schauflere114e472008-02-04 22:29:50 -0800222
Casey Schauflere114e472008-02-04 22:29:50 -0800223extern 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;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500228extern struct smack_known smack_known_web;
Casey Schauflere114e472008-02-04 22:29:50 -0800229
Etienne Basset7198e2e2009-03-24 20:53:24 +0100230extern struct list_head smack_known_list;
231extern struct list_head smack_rule_list;
232extern struct list_head smk_netlbladdr_list;
233
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +0200234extern struct security_operations smack_ops;
Casey Schauflere114e472008-02-04 22:29:50 -0800235
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
248/*
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200249 * 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
257/*
Casey Schauflere114e472008-02-04 22:29:50 -0800258 * 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
Etienne Bassetecfcc532009-04-08 20:40:06 +0200266/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800267 * 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/*
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200283 * Present a pointer to the smack label in the current task blob.
Casey Schaufler676dac42010-12-02 06:43:39 -0800284 */
285static inline char *smk_of_current(void)
286{
287 return smk_of_task(current_security());
288}
289
290/*
Etienne Bassetecfcc532009-04-08 20:40:06 +0200291 * 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;
Thomas Liud4131de2009-07-09 10:00:30 -0400313 a->a.smack_audit_data.function = func;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200314}
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
Casey Schauflere114e472008-02-04 22:29:50 -0800379#endif /* _SECURITY_SMACK_H */