blob: 513dc1aa16dd1ff5594745b463dd67118648705a [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
Etienne Bassetecfcc532009-04-08 20:40:06 +020062/*
63 * what events do we log
64 * can be overwritten at run-time by /smack/logging
65 */
66int log_policy = SMACK_AUDIT_DENIED;
67
Casey Schauflere114e472008-02-04 22:29:50 -080068/**
69 * smk_access - determine if a subject has a specific access to an object
70 * @subject_label: a pointer to the subject's Smack label
71 * @object_label: a pointer to the object's Smack label
72 * @request: the access requested, in "MAY" format
Etienne Bassetecfcc532009-04-08 20:40:06 +020073 * @a : a pointer to the audit data
Casey Schauflere114e472008-02-04 22:29:50 -080074 *
75 * This function looks up the subject/object pair in the
76 * access rule list and returns 0 if the access is permitted,
77 * non zero otherwise.
78 *
79 * Even though Smack labels are usually shared on smack_list
80 * labels that come in off the network can't be imported
81 * and added to the list for locking reasons.
82 *
83 * Therefore, it is necessary to check the contents of the labels,
84 * not just the pointer values. Of course, in most cases the labels
85 * will be on the list, so checking the pointers may be a worthwhile
86 * optimization.
87 */
Etienne Bassetecfcc532009-04-08 20:40:06 +020088int smk_access(char *subject_label, char *object_label, int request,
89 struct smk_audit_info *a)
Casey Schauflere114e472008-02-04 22:29:50 -080090{
91 u32 may = MAY_NOT;
Casey Schauflere114e472008-02-04 22:29:50 -080092 struct smack_rule *srp;
Etienne Bassetecfcc532009-04-08 20:40:06 +020093 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -080094
95 /*
96 * Hardcoded comparisons.
97 *
98 * A star subject can't access any object.
99 */
100 if (subject_label == smack_known_star.smk_known ||
Etienne Bassetecfcc532009-04-08 20:40:06 +0200101 strcmp(subject_label, smack_known_star.smk_known) == 0) {
102 rc = -EACCES;
103 goto out_audit;
104 }
Casey Schauflere114e472008-02-04 22:29:50 -0800105 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500106 * An internet object can be accessed by any subject.
107 * Tasks cannot be assigned the internet label.
108 * An internet subject can access any object.
109 */
110 if (object_label == smack_known_web.smk_known ||
111 subject_label == smack_known_web.smk_known ||
112 strcmp(object_label, smack_known_web.smk_known) == 0 ||
113 strcmp(subject_label, smack_known_web.smk_known) == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200114 goto out_audit;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500115 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800116 * A star object can be accessed by any subject.
117 */
118 if (object_label == smack_known_star.smk_known ||
119 strcmp(object_label, smack_known_star.smk_known) == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200120 goto out_audit;
Casey Schauflere114e472008-02-04 22:29:50 -0800121 /*
122 * An object can be accessed in any way by a subject
123 * with the same label.
124 */
125 if (subject_label == object_label ||
126 strcmp(subject_label, object_label) == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200127 goto out_audit;
Casey Schauflere114e472008-02-04 22:29:50 -0800128 /*
129 * A hat subject can read any object.
130 * A floor object can be read by any subject.
131 */
132 if ((request & MAY_ANYREAD) == request) {
133 if (object_label == smack_known_floor.smk_known ||
134 strcmp(object_label, smack_known_floor.smk_known) == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200135 goto out_audit;
Casey Schauflere114e472008-02-04 22:29:50 -0800136 if (subject_label == smack_known_hat.smk_known ||
137 strcmp(subject_label, smack_known_hat.smk_known) == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200138 goto out_audit;
Casey Schauflere114e472008-02-04 22:29:50 -0800139 }
140 /*
141 * Beyond here an explicit relationship is required.
142 * If the requested access is contained in the available
143 * access (e.g. read is included in readwrite) it's
144 * good.
145 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100146 rcu_read_lock();
147 list_for_each_entry_rcu(srp, &smack_rule_list, list) {
Casey Schauflere114e472008-02-04 22:29:50 -0800148 if (srp->smk_subject == subject_label ||
149 strcmp(srp->smk_subject, subject_label) == 0) {
150 if (srp->smk_object == object_label ||
151 strcmp(srp->smk_object, object_label) == 0) {
152 may = srp->smk_access;
153 break;
154 }
155 }
156 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100157 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800158 /*
159 * This is a bit map operation.
160 */
161 if ((request & may) == request)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200162 goto out_audit;
Casey Schauflere114e472008-02-04 22:29:50 -0800163
Etienne Bassetecfcc532009-04-08 20:40:06 +0200164 rc = -EACCES;
165out_audit:
166#ifdef CONFIG_AUDIT
167 if (a)
168 smack_log(subject_label, object_label, request, rc, a);
169#endif
170 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800171}
172
173/**
174 * smk_curacc - determine if current has a specific access to an object
Randy Dunlap251a2a92009-02-18 11:42:33 -0800175 * @obj_label: a pointer to the object's Smack label
176 * @mode: the access requested, in "MAY" format
Etienne Bassetecfcc532009-04-08 20:40:06 +0200177 * @a : common audit data
Casey Schauflere114e472008-02-04 22:29:50 -0800178 *
179 * This function checks the current subject label/object label pair
180 * in the access rule list and returns 0 if the access is permitted,
Casey Schaufler15446232008-07-30 15:37:11 -0700181 * non zero otherwise. It allows that current may have the capability
Casey Schauflere114e472008-02-04 22:29:50 -0800182 * to override the rules.
183 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200184int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
Casey Schauflere114e472008-02-04 22:29:50 -0800185{
186 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200187 char *sp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800188
Etienne Bassetecfcc532009-04-08 20:40:06 +0200189 rc = smk_access(sp, obj_label, mode, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -0800190 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200191 goto out_audit;
Casey Schauflere114e472008-02-04 22:29:50 -0800192
Casey Schaufler15446232008-07-30 15:37:11 -0700193 /*
194 * Return if a specific label has been designated as the
195 * only one that gets privilege and current does not
196 * have that label.
197 */
David Howellsb6dff3e2008-11-14 10:39:16 +1100198 if (smack_onlycap != NULL && smack_onlycap != current->cred->security)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200199 goto out_audit;
Casey Schaufler15446232008-07-30 15:37:11 -0700200
Casey Schauflere114e472008-02-04 22:29:50 -0800201 if (capable(CAP_MAC_OVERRIDE))
202 return 0;
203
Etienne Bassetecfcc532009-04-08 20:40:06 +0200204out_audit:
205#ifdef CONFIG_AUDIT
206 if (a)
207 smack_log(sp, obj_label, mode, rc, a);
208#endif
Casey Schauflere114e472008-02-04 22:29:50 -0800209 return rc;
210}
211
Etienne Bassetecfcc532009-04-08 20:40:06 +0200212#ifdef CONFIG_AUDIT
213/**
214 * smack_str_from_perm : helper to transalate an int to a
215 * readable string
216 * @string : the string to fill
217 * @access : the int
218 *
219 */
220static inline void smack_str_from_perm(char *string, int access)
221{
222 int i = 0;
223 if (access & MAY_READ)
224 string[i++] = 'r';
225 if (access & MAY_WRITE)
226 string[i++] = 'w';
227 if (access & MAY_EXEC)
228 string[i++] = 'x';
229 if (access & MAY_APPEND)
230 string[i++] = 'a';
231 string[i] = '\0';
232}
233/**
234 * smack_log_callback - SMACK specific information
235 * will be called by generic audit code
236 * @ab : the audit_buffer
237 * @a : audit_data
238 *
239 */
240static void smack_log_callback(struct audit_buffer *ab, void *a)
241{
242 struct common_audit_data *ad = a;
243 struct smack_audit_data *sad = &ad->lsm_priv.smack_audit_data;
244 audit_log_format(ab, "lsm=SMACK fn=%s action=%s", ad->function,
245 sad->result ? "denied" : "granted");
246 audit_log_format(ab, " subject=");
247 audit_log_untrustedstring(ab, sad->subject);
248 audit_log_format(ab, " object=");
249 audit_log_untrustedstring(ab, sad->object);
250 audit_log_format(ab, " requested=%s", sad->request);
251}
252
253/**
254 * smack_log - Audit the granting or denial of permissions.
255 * @subject_label : smack label of the requester
256 * @object_label : smack label of the object being accessed
257 * @request: requested permissions
258 * @result: result from smk_access
259 * @a: auxiliary audit data
260 *
261 * Audit the granting or denial of permissions in accordance
262 * with the policy.
263 */
264void smack_log(char *subject_label, char *object_label, int request,
265 int result, struct smk_audit_info *ad)
266{
267 char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
268 struct smack_audit_data *sad;
269 struct common_audit_data *a = &ad->a;
270
271 /* check if we have to log the current event */
272 if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
273 return;
274 if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
275 return;
276
277 if (a->function == NULL)
278 a->function = "unknown";
279
280 /* end preparing the audit data */
281 sad = &a->lsm_priv.smack_audit_data;
282 smack_str_from_perm(request_buffer, request);
283 sad->subject = subject_label;
284 sad->object = object_label;
285 sad->request = request_buffer;
286 sad->result = result;
287 a->lsm_pre_audit = smack_log_callback;
288
289 common_lsm_audit(a);
290}
291#else /* #ifdef CONFIG_AUDIT */
292void smack_log(char *subject_label, char *object_label, int request,
293 int result, struct smk_audit_info *ad)
294{
295}
296#endif
297
Casey Schauflere114e472008-02-04 22:29:50 -0800298static DEFINE_MUTEX(smack_known_lock);
299
300/**
301 * smk_import_entry - import a label, return the list entry
302 * @string: a text string that might be a Smack label
303 * @len: the maximum size, or zero if it is NULL terminated.
304 *
305 * Returns a pointer to the entry in the label list that
306 * matches the passed string, adding it if necessary.
307 */
308struct smack_known *smk_import_entry(const char *string, int len)
309{
310 struct smack_known *skp;
311 char smack[SMK_LABELLEN];
312 int found;
313 int i;
314
315 if (len <= 0 || len > SMK_MAXLEN)
316 len = SMK_MAXLEN;
317
318 for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
319 if (found)
320 smack[i] = '\0';
321 else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
Etienne Bassetecfcc532009-04-08 20:40:06 +0200322 string[i] == '/' || string[i] == '"' ||
323 string[i] == '\\' || string[i] == '\'') {
Casey Schauflere114e472008-02-04 22:29:50 -0800324 smack[i] = '\0';
325 found = 1;
326 } else
327 smack[i] = string[i];
328 }
329
330 if (smack[0] == '\0')
331 return NULL;
332
333 mutex_lock(&smack_known_lock);
334
Etienne Basset7198e2e2009-03-24 20:53:24 +0100335 found = 0;
336 list_for_each_entry_rcu(skp, &smack_known_list, list) {
337 if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
338 found = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800339 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100340 }
341 }
Casey Schauflere114e472008-02-04 22:29:50 -0800342
Etienne Basset7198e2e2009-03-24 20:53:24 +0100343 if (found == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800344 skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
345 if (skp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800346 strncpy(skp->smk_known, smack, SMK_MAXLEN);
347 skp->smk_secid = smack_next_secid++;
348 skp->smk_cipso = NULL;
349 spin_lock_init(&skp->smk_cipsolock);
350 /*
351 * Make sure that the entry is actually
352 * filled before putting it on the list.
353 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100354 list_add_rcu(&skp->list, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800355 }
356 }
357
358 mutex_unlock(&smack_known_lock);
359
360 return skp;
361}
362
363/**
364 * smk_import - import a smack label
365 * @string: a text string that might be a Smack label
366 * @len: the maximum size, or zero if it is NULL terminated.
367 *
368 * Returns a pointer to the label in the label list that
369 * matches the passed string, adding it if necessary.
370 */
371char *smk_import(const char *string, int len)
372{
373 struct smack_known *skp;
374
Etienne Basset43031542009-03-27 17:11:01 -0400375 /* labels cannot begin with a '-' */
376 if (string[0] == '-')
377 return NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800378 skp = smk_import_entry(string, len);
379 if (skp == NULL)
380 return NULL;
381 return skp->smk_known;
382}
383
384/**
385 * smack_from_secid - find the Smack label associated with a secid
386 * @secid: an integer that might be associated with a Smack label
387 *
388 * Returns a pointer to the appropraite Smack label if there is one,
389 * otherwise a pointer to the invalid Smack label.
390 */
391char *smack_from_secid(const u32 secid)
392{
393 struct smack_known *skp;
394
Etienne Basset7198e2e2009-03-24 20:53:24 +0100395 rcu_read_lock();
396 list_for_each_entry_rcu(skp, &smack_known_list, list) {
397 if (skp->smk_secid == secid) {
398 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800399 return skp->smk_known;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100400 }
401 }
Casey Schauflere114e472008-02-04 22:29:50 -0800402
403 /*
404 * If we got this far someone asked for the translation
405 * of a secid that is not on the list.
406 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100407 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800408 return smack_known_invalid.smk_known;
409}
410
411/**
412 * smack_to_secid - find the secid associated with a Smack label
413 * @smack: the Smack label
414 *
415 * Returns the appropriate secid if there is one,
416 * otherwise 0
417 */
418u32 smack_to_secid(const char *smack)
419{
420 struct smack_known *skp;
421
Etienne Basset7198e2e2009-03-24 20:53:24 +0100422 rcu_read_lock();
423 list_for_each_entry_rcu(skp, &smack_known_list, list) {
424 if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
425 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800426 return skp->smk_secid;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100427 }
428 }
429 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800430 return 0;
431}
432
433/**
434 * smack_from_cipso - find the Smack label associated with a CIPSO option
435 * @level: Bell & LaPadula level from the network
436 * @cp: Bell & LaPadula categories from the network
437 * @result: where to put the Smack value
438 *
439 * This is a simple lookup in the label table.
440 *
441 * This is an odd duck as far as smack handling goes in that
442 * it sends back a copy of the smack label rather than a pointer
443 * to the master list. This is done because it is possible for
444 * a foreign host to send a smack label that is new to this
445 * machine and hence not on the list. That would not be an
446 * issue except that adding an entry to the master list can't
447 * be done at that point.
448 */
449void smack_from_cipso(u32 level, char *cp, char *result)
450{
451 struct smack_known *kp;
452 char *final = NULL;
453
Etienne Basset7198e2e2009-03-24 20:53:24 +0100454 rcu_read_lock();
455 list_for_each_entry(kp, &smack_known_list, list) {
Casey Schauflere114e472008-02-04 22:29:50 -0800456 if (kp->smk_cipso == NULL)
457 continue;
458
459 spin_lock_bh(&kp->smk_cipsolock);
460
461 if (kp->smk_cipso->smk_level == level &&
462 memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
463 final = kp->smk_known;
464
465 spin_unlock_bh(&kp->smk_cipsolock);
466 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100467 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800468 if (final == NULL)
469 final = smack_known_huh.smk_known;
470 strncpy(result, final, SMK_MAXLEN);
471 return;
472}
473
474/**
475 * smack_to_cipso - find the CIPSO option to go with a Smack label
476 * @smack: a pointer to the smack label in question
477 * @cp: where to put the result
478 *
479 * Returns zero if a value is available, non-zero otherwise.
480 */
481int smack_to_cipso(const char *smack, struct smack_cipso *cp)
482{
483 struct smack_known *kp;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100484 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800485
Etienne Basset7198e2e2009-03-24 20:53:24 +0100486 rcu_read_lock();
487 list_for_each_entry_rcu(kp, &smack_known_list, list) {
Casey Schauflere114e472008-02-04 22:29:50 -0800488 if (kp->smk_known == smack ||
Etienne Basset7198e2e2009-03-24 20:53:24 +0100489 strcmp(kp->smk_known, smack) == 0) {
490 found = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800491 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100492 }
493 }
494 rcu_read_unlock();
Casey Schauflere114e472008-02-04 22:29:50 -0800495
Etienne Basset7198e2e2009-03-24 20:53:24 +0100496 if (found == 0 || kp->smk_cipso == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800497 return -ENOENT;
498
499 memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));
500 return 0;
501}