blob: ac0a2707f6d41583e9e325819ab2265483320853 [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#include <linux/types.h>
14#include <linux/fs.h>
15#include <linux/sched.h>
16#include "smack.h"
17
Casey Schauflere114e472008-02-04 22:29:50 -080018struct smack_known smack_known_huh = {
Casey Schauflere114e472008-02-04 22:29:50 -080019 .smk_known = "?",
20 .smk_secid = 2,
21 .smk_cipso = NULL,
22};
23
24struct smack_known smack_known_hat = {
Casey Schauflere114e472008-02-04 22:29:50 -080025 .smk_known = "^",
26 .smk_secid = 3,
27 .smk_cipso = NULL,
28};
29
30struct smack_known smack_known_star = {
Casey Schauflere114e472008-02-04 22:29:50 -080031 .smk_known = "*",
32 .smk_secid = 4,
33 .smk_cipso = NULL,
34};
35
36struct smack_known smack_known_floor = {
Casey Schauflere114e472008-02-04 22:29:50 -080037 .smk_known = "_",
38 .smk_secid = 5,
39 .smk_cipso = NULL,
40};
41
42struct smack_known smack_known_invalid = {
Casey Schauflere114e472008-02-04 22:29:50 -080043 .smk_known = "",
44 .smk_secid = 6,
45 .smk_cipso = NULL,
46};
47
Casey Schaufler6d3dc072008-12-31 12:54:12 -050048struct smack_known smack_known_web = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -050049 .smk_known = "@",
50 .smk_secid = 7,
51 .smk_cipso = NULL,
52};
53
Etienne Basset7198e2e2009-03-24 20:53:24 +010054LIST_HEAD(smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -080055
56/*
57 * The initial value needs to be bigger than any of the
58 * known values above.
59 */
60static u32 smack_next_secid = 10;
61
62/**
63 * smk_access - determine if a subject has a specific access to an object
64 * @subject_label: a pointer to the subject's Smack label
65 * @object_label: a pointer to the object's Smack label
66 * @request: the access requested, in "MAY" format
67 *
68 * This function looks up the subject/object pair in the
69 * access rule list and returns 0 if the access is permitted,
70 * non zero otherwise.
71 *
72 * Even though Smack labels are usually shared on smack_list
73 * labels that come in off the network can't be imported
74 * and added to the list for locking reasons.
75 *
76 * Therefore, it is necessary to check the contents of the labels,
77 * not just the pointer values. Of course, in most cases the labels
78 * will be on the list, so checking the pointers may be a worthwhile
79 * optimization.
80 */
81int smk_access(char *subject_label, char *object_label, int request)
82{
83 u32 may = MAY_NOT;
Casey Schauflere114e472008-02-04 22:29:50 -080084 struct smack_rule *srp;
85
86 /*
87 * Hardcoded comparisons.
88 *
89 * A star subject can't access any object.
90 */
91 if (subject_label == smack_known_star.smk_known ||
92 strcmp(subject_label, smack_known_star.smk_known) == 0)
93 return -EACCES;
94 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -050095 * An internet object can be accessed by any subject.
96 * Tasks cannot be assigned the internet label.
97 * An internet subject can access any object.
98 */
99 if (object_label == smack_known_web.smk_known ||
100 subject_label == smack_known_web.smk_known ||
101 strcmp(object_label, smack_known_web.smk_known) == 0 ||
102 strcmp(subject_label, smack_known_web.smk_known) == 0)
103 return 0;
104 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800105 * A star object can be accessed by any subject.
106 */
107 if (object_label == smack_known_star.smk_known ||
108 strcmp(object_label, smack_known_star.smk_known) == 0)
109 return 0;
110 /*
111 * An object can be accessed in any way by a subject
112 * with the same label.
113 */
114 if (subject_label == object_label ||
115 strcmp(subject_label, object_label) == 0)
116 return 0;
117 /*
118 * A hat subject can read any object.
119 * A floor object can be read by any subject.
120 */
121 if ((request & MAY_ANYREAD) == request) {
122 if (object_label == smack_known_floor.smk_known ||
123 strcmp(object_label, smack_known_floor.smk_known) == 0)
124 return 0;
125 if (subject_label == smack_known_hat.smk_known ||
126 strcmp(subject_label, smack_known_hat.smk_known) == 0)
127 return 0;
128 }
129 /*
130 * Beyond here an explicit relationship is required.
131 * If the requested access is contained in the available
132 * access (e.g. read is included in readwrite) it's
133 * good.
134 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100135 rcu_read_lock();
136 list_for_each_entry_rcu(srp, &smack_rule_list, list) {
Casey Schauflere114e472008-02-04 22:29:50 -0800137 if (srp->smk_subject == subject_label ||
138 strcmp(srp->smk_subject, subject_label) == 0) {
139 if (srp->smk_object == object_label ||
140 strcmp(srp->smk_object, object_label) == 0) {
141 may = srp->smk_access;
142 break;
143 }
144 }
145 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100146 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800147 /*
148 * This is a bit map operation.
149 */
150 if ((request & may) == request)
151 return 0;
152
153 return -EACCES;
154}
155
156/**
157 * smk_curacc - determine if current has a specific access to an object
Randy Dunlap251a2a92009-02-18 11:42:33 -0800158 * @obj_label: a pointer to the object's Smack label
159 * @mode: the access requested, in "MAY" format
Casey Schauflere114e472008-02-04 22:29:50 -0800160 *
161 * This function checks the current subject label/object label pair
162 * in the access rule list and returns 0 if the access is permitted,
Casey Schaufler15446232008-07-30 15:37:11 -0700163 * non zero otherwise. It allows that current may have the capability
Casey Schauflere114e472008-02-04 22:29:50 -0800164 * to override the rules.
165 */
166int smk_curacc(char *obj_label, u32 mode)
167{
168 int rc;
169
David Howells86a264a2008-11-14 10:39:18 +1100170 rc = smk_access(current_security(), obj_label, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800171 if (rc == 0)
172 return 0;
173
Casey Schaufler15446232008-07-30 15:37:11 -0700174 /*
175 * Return if a specific label has been designated as the
176 * only one that gets privilege and current does not
177 * have that label.
178 */
David Howellsb6dff3e2008-11-14 10:39:16 +1100179 if (smack_onlycap != NULL && smack_onlycap != current->cred->security)
Casey Schaufler15446232008-07-30 15:37:11 -0700180 return rc;
181
Casey Schauflere114e472008-02-04 22:29:50 -0800182 if (capable(CAP_MAC_OVERRIDE))
183 return 0;
184
185 return rc;
186}
187
188static DEFINE_MUTEX(smack_known_lock);
189
190/**
191 * smk_import_entry - import a label, return the list entry
192 * @string: a text string that might be a Smack label
193 * @len: the maximum size, or zero if it is NULL terminated.
194 *
195 * Returns a pointer to the entry in the label list that
196 * matches the passed string, adding it if necessary.
197 */
198struct smack_known *smk_import_entry(const char *string, int len)
199{
200 struct smack_known *skp;
201 char smack[SMK_LABELLEN];
202 int found;
203 int i;
204
205 if (len <= 0 || len > SMK_MAXLEN)
206 len = SMK_MAXLEN;
207
208 for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
209 if (found)
210 smack[i] = '\0';
211 else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
212 string[i] == '/') {
213 smack[i] = '\0';
214 found = 1;
215 } else
216 smack[i] = string[i];
217 }
218
219 if (smack[0] == '\0')
220 return NULL;
221
222 mutex_lock(&smack_known_lock);
223
Etienne Basset7198e2e2009-03-24 20:53:24 +0100224 found = 0;
225 list_for_each_entry_rcu(skp, &smack_known_list, list) {
226 if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
227 found = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800228 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100229 }
230 }
Casey Schauflere114e472008-02-04 22:29:50 -0800231
Etienne Basset7198e2e2009-03-24 20:53:24 +0100232 if (found == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800233 skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
234 if (skp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800235 strncpy(skp->smk_known, smack, SMK_MAXLEN);
236 skp->smk_secid = smack_next_secid++;
237 skp->smk_cipso = NULL;
238 spin_lock_init(&skp->smk_cipsolock);
239 /*
240 * Make sure that the entry is actually
241 * filled before putting it on the list.
242 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100243 list_add_rcu(&skp->list, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800244 }
245 }
246
247 mutex_unlock(&smack_known_lock);
248
249 return skp;
250}
251
252/**
253 * smk_import - import a smack label
254 * @string: a text string that might be a Smack label
255 * @len: the maximum size, or zero if it is NULL terminated.
256 *
257 * Returns a pointer to the label in the label list that
258 * matches the passed string, adding it if necessary.
259 */
260char *smk_import(const char *string, int len)
261{
262 struct smack_known *skp;
263
Etienne Basset43031542009-03-27 17:11:01 -0400264 /* labels cannot begin with a '-' */
265 if (string[0] == '-')
266 return NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800267 skp = smk_import_entry(string, len);
268 if (skp == NULL)
269 return NULL;
270 return skp->smk_known;
271}
272
273/**
274 * smack_from_secid - find the Smack label associated with a secid
275 * @secid: an integer that might be associated with a Smack label
276 *
277 * Returns a pointer to the appropraite Smack label if there is one,
278 * otherwise a pointer to the invalid Smack label.
279 */
280char *smack_from_secid(const u32 secid)
281{
282 struct smack_known *skp;
283
Etienne Basset7198e2e2009-03-24 20:53:24 +0100284 rcu_read_lock();
285 list_for_each_entry_rcu(skp, &smack_known_list, list) {
286 if (skp->smk_secid == secid) {
287 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800288 return skp->smk_known;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100289 }
290 }
Casey Schauflere114e472008-02-04 22:29:50 -0800291
292 /*
293 * If we got this far someone asked for the translation
294 * of a secid that is not on the list.
295 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100296 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800297 return smack_known_invalid.smk_known;
298}
299
300/**
301 * smack_to_secid - find the secid associated with a Smack label
302 * @smack: the Smack label
303 *
304 * Returns the appropriate secid if there is one,
305 * otherwise 0
306 */
307u32 smack_to_secid(const char *smack)
308{
309 struct smack_known *skp;
310
Etienne Basset7198e2e2009-03-24 20:53:24 +0100311 rcu_read_lock();
312 list_for_each_entry_rcu(skp, &smack_known_list, list) {
313 if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
314 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800315 return skp->smk_secid;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100316 }
317 }
318 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800319 return 0;
320}
321
322/**
323 * smack_from_cipso - find the Smack label associated with a CIPSO option
324 * @level: Bell & LaPadula level from the network
325 * @cp: Bell & LaPadula categories from the network
326 * @result: where to put the Smack value
327 *
328 * This is a simple lookup in the label table.
329 *
330 * This is an odd duck as far as smack handling goes in that
331 * it sends back a copy of the smack label rather than a pointer
332 * to the master list. This is done because it is possible for
333 * a foreign host to send a smack label that is new to this
334 * machine and hence not on the list. That would not be an
335 * issue except that adding an entry to the master list can't
336 * be done at that point.
337 */
338void smack_from_cipso(u32 level, char *cp, char *result)
339{
340 struct smack_known *kp;
341 char *final = NULL;
342
Etienne Basset7198e2e2009-03-24 20:53:24 +0100343 rcu_read_lock();
344 list_for_each_entry(kp, &smack_known_list, list) {
Casey Schauflere114e472008-02-04 22:29:50 -0800345 if (kp->smk_cipso == NULL)
346 continue;
347
348 spin_lock_bh(&kp->smk_cipsolock);
349
350 if (kp->smk_cipso->smk_level == level &&
351 memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
352 final = kp->smk_known;
353
354 spin_unlock_bh(&kp->smk_cipsolock);
355 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100356 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800357 if (final == NULL)
358 final = smack_known_huh.smk_known;
359 strncpy(result, final, SMK_MAXLEN);
360 return;
361}
362
363/**
364 * smack_to_cipso - find the CIPSO option to go with a Smack label
365 * @smack: a pointer to the smack label in question
366 * @cp: where to put the result
367 *
368 * Returns zero if a value is available, non-zero otherwise.
369 */
370int smack_to_cipso(const char *smack, struct smack_cipso *cp)
371{
372 struct smack_known *kp;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100373 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800374
Etienne Basset7198e2e2009-03-24 20:53:24 +0100375 rcu_read_lock();
376 list_for_each_entry_rcu(kp, &smack_known_list, list) {
Casey Schauflere114e472008-02-04 22:29:50 -0800377 if (kp->smk_known == smack ||
Etienne Basset7198e2e2009-03-24 20:53:24 +0100378 strcmp(kp->smk_known, smack) == 0) {
379 found = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800380 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100381 }
382 }
383 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800384
Etienne Basset7198e2e2009-03-24 20:53:24 +0100385 if (found == 0 || kp->smk_cipso == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800386 return -ENOENT;
387
388 memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));
389 return 0;
390}