2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
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.
9 * Casey Schaufler <casey@schaufler-ca.com>
13 #include <linux/types.h>
14 #include <linux/slab.h>
16 #include <linux/sched.h>
19 struct smack_known smack_known_huh = {
24 struct smack_known smack_known_hat = {
29 struct smack_known smack_known_star = {
34 struct smack_known smack_known_floor = {
39 struct smack_known smack_known_invalid = {
44 struct smack_known smack_known_web = {
49 LIST_HEAD(smack_known_list);
52 * The initial value needs to be bigger than any of the
55 static u32 smack_next_secid = 10;
58 * are we running in permissive mode?
59 * can be overwritten at run-time by /smack/permissive
61 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
62 int permissive_mode = SMACK_PERMISSIVE_ALLOWED;
66 * what events do we log
67 * can be overwritten at run-time by /smack/logging
69 int log_policy = SMACK_AUDIT_DENIED;
72 * smk_access_entry - look up matching access rule
73 * @subject_label: a pointer to the subject's Smack label
74 * @object_label: a pointer to the object's Smack label
75 * @rule_list: the list of rules to search
77 * This function looks up the subject/object pair in the
78 * access rule list and returns the access mode. If no
79 * entry is found returns -ENOENT.
83 * Earlier versions of this function allowed for labels that
84 * were not on the label list. This was done to allow for
85 * labels to come over the network that had never been seen
86 * before on this host. Unless the receiving socket has the
87 * star label this will always result in a failure check. The
88 * star labeled socket case is now handled in the networking
89 * hooks so there is no case where the label is not on the
90 * label list. Checking to see if the address of two labels
91 * is the same is now a reliable test.
93 * Do the object check first because that is more
96 * Allowing write access implies allowing locking.
98 int smk_access_entry(char *subject_label, char *object_label,
99 struct list_head *rule_list)
102 struct smack_rule *srp;
104 list_for_each_entry_rcu(srp, rule_list, list) {
105 if (srp->smk_object == object_label &&
106 srp->smk_subject->smk_known == subject_label) {
107 may = srp->smk_access;
113 * MAY_WRITE implies MAY_LOCK.
115 if ((may & MAY_WRITE) == MAY_WRITE)
121 * smk_access - determine if a subject has a specific access to an object
122 * @subject_known: a pointer to the subject's Smack label entry
123 * @object_label: a pointer to the object's Smack label
124 * @request: the access requested, in "MAY" format
125 * @a : a pointer to the audit data
127 * This function looks up the subject/object pair in the
128 * access rule list and returns 0 if the access is permitted,
129 * non zero otherwise.
131 * Smack labels are shared on smack_list
133 int smk_access(struct smack_known *subject_known, char *object_label,
134 int request, struct smk_audit_info *a)
140 * Hardcoded comparisons.
142 * A star subject can't access any object.
144 if (subject_known == &smack_known_star) {
149 * An internet object can be accessed by any subject.
150 * Tasks cannot be assigned the internet label.
151 * An internet subject can access any object.
153 if (object_label == smack_known_web.smk_known ||
154 subject_known == &smack_known_web)
157 * A star object can be accessed by any subject.
159 if (object_label == smack_known_star.smk_known)
162 * An object can be accessed in any way by a subject
163 * with the same label.
165 if (subject_known->smk_known == object_label)
168 * A hat subject can read any object.
169 * A floor object can be read by any subject.
171 if ((request & MAY_ANYREAD) == request) {
172 if (object_label == smack_known_floor.smk_known)
174 if (subject_known == &smack_known_hat)
178 * Beyond here an explicit relationship is required.
179 * If the requested access is contained in the available
180 * access (e.g. read is included in readwrite) it's
181 * good. A negative response from smk_access_entry()
182 * indicates there is no entry for this pair.
185 may = smk_access_entry(subject_known->smk_known, object_label,
186 &subject_known->smk_rules);
189 if (may > 0 && (request & may) == request)
196 smack_log(subject_known->smk_known, object_label, request,
199 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
200 if (permissive_mode == SMACK_PERMISSIVE_ALLOWED)
207 * smk_curacc - determine if current has a specific access to an object
208 * @obj_label: a pointer to the object's Smack label
209 * @mode: the access requested, in "MAY" format
210 * @a : common audit data
212 * This function checks the current subject label/object label pair
213 * in the access rule list and returns 0 if the access is permitted,
214 * non zero otherwise. It allows that current may have the capability
215 * to override the rules.
217 int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
219 struct task_smack *tsp = current_security();
220 struct smack_known *skp = smk_of_task(tsp);
225 * Check the global rule list
227 rc = smk_access(skp, obj_label, mode, NULL);
230 * If there is an entry in the task's rule list
231 * it can further restrict access.
233 may = smk_access_entry(skp->smk_known, obj_label,
237 if ((mode & may) == mode)
243 * Allow for priviliged to override policy.
245 if (rc != 0 && smack_privileged(CAP_MAC_OVERRIDE))
251 smack_log(skp->smk_known, obj_label, mode, rc, a);
258 * smack_str_from_perm : helper to transalate an int to a
260 * @string : the string to fill
264 static inline void smack_str_from_perm(char *string, int access)
268 if (access & MAY_READ)
270 if (access & MAY_WRITE)
272 if (access & MAY_EXEC)
274 if (access & MAY_APPEND)
276 if (access & MAY_TRANSMUTE)
278 if (access & MAY_LOCK)
283 * smack_log_callback - SMACK specific information
284 * will be called by generic audit code
285 * @ab : the audit_buffer
289 static void smack_log_callback(struct audit_buffer *ab, void *a)
291 struct common_audit_data *ad = a;
292 struct smack_audit_data *sad = ad->smack_audit_data;
293 audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
294 ad->smack_audit_data->function,
295 sad->result ? "denied" : "granted");
296 audit_log_format(ab, " subject=");
297 audit_log_untrustedstring(ab, sad->subject);
298 audit_log_format(ab, " object=");
299 audit_log_untrustedstring(ab, sad->object);
300 audit_log_format(ab, " requested=%s", sad->request);
304 * smack_log - Audit the granting or denial of permissions.
305 * @subject_label : smack label of the requester
306 * @object_label : smack label of the object being accessed
307 * @request: requested permissions
308 * @result: result from smk_access
309 * @a: auxiliary audit data
311 * Audit the granting or denial of permissions in accordance
314 void smack_log(char *subject_label, char *object_label, int request,
315 int result, struct smk_audit_info *ad)
317 char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
318 struct smack_audit_data *sad;
319 struct common_audit_data *a = &ad->a;
321 /* check if we have to log the current event */
322 if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
324 if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
327 sad = a->smack_audit_data;
329 if (sad->function == NULL)
330 sad->function = "unknown";
332 /* end preparing the audit data */
333 smack_str_from_perm(request_buffer, request);
334 sad->subject = subject_label;
335 sad->object = object_label;
336 sad->request = request_buffer;
337 sad->result = result;
339 common_lsm_audit(a, smack_log_callback, NULL);
341 #else /* #ifdef CONFIG_AUDIT */
342 void smack_log(char *subject_label, char *object_label, int request,
343 int result, struct smk_audit_info *ad)
348 DEFINE_MUTEX(smack_known_lock);
350 struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
353 * smk_insert_entry - insert a smack label into a hash map,
355 * this function must be called under smack_known_lock
357 void smk_insert_entry(struct smack_known *skp)
360 struct hlist_head *head;
362 hash = full_name_hash(skp->smk_known, strlen(skp->smk_known));
363 head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
365 hlist_add_head_rcu(&skp->smk_hashed, head);
366 list_add_rcu(&skp->list, &smack_known_list);
370 * smk_find_entry - find a label on the list, return the list entry
371 * @string: a text string that might be a Smack label
373 * Returns a pointer to the entry in the label list that
374 * matches the passed string.
376 struct smack_known *smk_find_entry(const char *string)
379 struct hlist_head *head;
380 struct smack_known *skp;
382 hash = full_name_hash(string, strlen(string));
383 head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
385 hlist_for_each_entry_rcu(skp, head, smk_hashed)
386 if (strcmp(skp->smk_known, string) == 0)
393 * smk_parse_smack - parse smack label from a text string
394 * @string: a text string that might contain a Smack label
395 * @len: the maximum size, or zero if it is NULL terminated.
397 * Returns a pointer to the clean label, or NULL
399 char *smk_parse_smack(const char *string, int len)
405 len = strlen(string) + 1;
408 * Reserve a leading '-' as an indicator that
409 * this isn't a label, but an option to interfaces
410 * including /smack/cipso and /smack/cipso2
412 if (string[0] == '-')
415 for (i = 0; i < len; i++)
416 if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
417 string[i] == '"' || string[i] == '\\' || string[i] == '\'')
420 if (i == 0 || i >= SMK_LONGLABEL)
423 smack = kzalloc(i + 1, GFP_KERNEL);
425 strncpy(smack, string, i + 1);
432 * smk_netlbl_mls - convert a catset to netlabel mls categories
433 * @catset: the Smack categories
434 * @sap: where to put the netlabel categories
436 * Allocates and fills attr.mls
437 * Returns 0 on success, error code on failure.
439 int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
448 sap->flags |= NETLBL_SECATTR_MLS_CAT;
449 sap->attr.mls.lvl = level;
450 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
451 if (!sap->attr.mls.cat)
453 sap->attr.mls.cat->startbit = 0;
455 for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
456 for (m = 0x80; m != 0; m >>= 1, cat++) {
459 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
462 netlbl_secattr_catmap_free(sap->attr.mls.cat);
471 * smk_import_entry - import a label, return the list entry
472 * @string: a text string that might be a Smack label
473 * @len: the maximum size, or zero if it is NULL terminated.
475 * Returns a pointer to the entry in the label list that
476 * matches the passed string, adding it if necessary.
478 struct smack_known *smk_import_entry(const char *string, int len)
480 struct smack_known *skp;
485 smack = smk_parse_smack(string, len);
489 mutex_lock(&smack_known_lock);
491 skp = smk_find_entry(smack);
495 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
499 skp->smk_known = smack;
500 skp->smk_secid = smack_next_secid++;
501 skp->smk_netlabel.domain = skp->smk_known;
502 skp->smk_netlabel.flags =
503 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
505 * If direct labeling works use it.
506 * Otherwise use mapped labeling.
508 slen = strlen(smack);
509 if (slen < SMK_CIPSOLEN)
510 rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
511 &skp->smk_netlabel, slen);
513 rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
514 &skp->smk_netlabel, sizeof(skp->smk_secid));
517 INIT_LIST_HEAD(&skp->smk_rules);
518 mutex_init(&skp->smk_rules_lock);
520 * Make sure that the entry is actually
521 * filled before putting it on the list.
523 smk_insert_entry(skp);
527 * smk_netlbl_mls failed.
534 mutex_unlock(&smack_known_lock);
540 * smk_import - import a smack label
541 * @string: a text string that might be a Smack label
542 * @len: the maximum size, or zero if it is NULL terminated.
544 * Returns a pointer to the label in the label list that
545 * matches the passed string, adding it if necessary.
547 char *smk_import(const char *string, int len)
549 struct smack_known *skp;
551 /* labels cannot begin with a '-' */
552 if (string[0] == '-')
554 skp = smk_import_entry(string, len);
557 return skp->smk_known;
561 * smack_from_secid - find the Smack label associated with a secid
562 * @secid: an integer that might be associated with a Smack label
564 * Returns a pointer to the appropriate Smack label entry if there is one,
565 * otherwise a pointer to the invalid Smack label.
567 struct smack_known *smack_from_secid(const u32 secid)
569 struct smack_known *skp;
572 list_for_each_entry_rcu(skp, &smack_known_list, list) {
573 if (skp->smk_secid == secid) {
580 * If we got this far someone asked for the translation
581 * of a secid that is not on the list.
584 return &smack_known_invalid;
588 * smack_to_secid - find the secid associated with a Smack label
589 * @smack: the Smack label
591 * Returns the appropriate secid if there is one,
594 u32 smack_to_secid(const char *smack)
596 struct smack_known *skp = smk_find_entry(smack);
600 return skp->smk_secid;