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 or lock any object.
169 * A floor object can be read or locked by any subject.
171 if ((request & MAY_ANYREAD) == request ||
172 (request & MAY_LOCK) == request) {
173 if (object_label == smack_known_floor.smk_known)
175 if (subject_known == &smack_known_hat)
179 * Beyond here an explicit relationship is required.
180 * If the requested access is contained in the available
181 * access (e.g. read is included in readwrite) it's
182 * good. A negative response from smk_access_entry()
183 * indicates there is no entry for this pair.
186 may = smk_access_entry(subject_known->smk_known, object_label,
187 &subject_known->smk_rules);
190 if (may > 0 && (request & may) == request)
197 smack_log(subject_known->smk_known, object_label, request,
200 #ifdef CONFIG_SECURITY_SMACK_PERMISSIVE_MODE
201 if (permissive_mode == SMACK_PERMISSIVE_ALLOWED)
208 * smk_curacc - determine if current has a specific access to an object
209 * @obj_label: a pointer to the object's Smack label
210 * @mode: the access requested, in "MAY" format
211 * @a : common audit data
213 * This function checks the current subject label/object label pair
214 * in the access rule list and returns 0 if the access is permitted,
215 * non zero otherwise. It allows that current may have the capability
216 * to override the rules.
218 int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
220 struct task_smack *tsp = current_security();
221 struct smack_known *skp = smk_of_task(tsp);
226 * Check the global rule list
228 rc = smk_access(skp, obj_label, mode, NULL);
231 * If there is an entry in the task's rule list
232 * it can further restrict access.
234 may = smk_access_entry(skp->smk_known, obj_label,
238 if ((mode & may) == mode)
244 * Allow for priviliged to override policy.
246 if (rc != 0 && smack_privileged(CAP_MAC_OVERRIDE))
252 smack_log(skp->smk_known, obj_label, mode, rc, a);
259 * smack_str_from_perm : helper to transalate an int to a
261 * @string : the string to fill
265 static inline void smack_str_from_perm(char *string, int access)
269 if (access & MAY_READ)
271 if (access & MAY_WRITE)
273 if (access & MAY_EXEC)
275 if (access & MAY_APPEND)
277 if (access & MAY_TRANSMUTE)
279 if (access & MAY_LOCK)
284 * smack_log_callback - SMACK specific information
285 * will be called by generic audit code
286 * @ab : the audit_buffer
290 static void smack_log_callback(struct audit_buffer *ab, void *a)
292 struct common_audit_data *ad = a;
293 struct smack_audit_data *sad = &ad->smack_audit_data;
294 audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
295 ad->smack_audit_data.function,
296 sad->result ? "denied" : "granted");
297 audit_log_format(ab, " subject=");
298 audit_log_untrustedstring(ab, sad->subject);
299 audit_log_format(ab, " object=");
300 audit_log_untrustedstring(ab, sad->object);
301 audit_log_format(ab, " requested=%s", sad->request);
305 * smack_log - Audit the granting or denial of permissions.
306 * @subject_label : smack label of the requester
307 * @object_label : smack label of the object being accessed
308 * @request: requested permissions
309 * @result: result from smk_access
310 * @a: auxiliary audit data
312 * Audit the granting or denial of permissions in accordance
315 void smack_log(char *subject_label, char *object_label, int request,
316 int result, struct smk_audit_info *ad)
318 char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
319 struct smack_audit_data *sad;
320 struct common_audit_data *a = &ad->a;
322 /* check if we have to log the current event */
323 if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
325 if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
328 if (a->smack_audit_data.function == NULL)
329 a->smack_audit_data.function = "unknown";
331 /* end preparing the audit data */
332 sad = &a->smack_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;
338 a->lsm_pre_audit = smack_log_callback;
342 #else /* #ifdef CONFIG_AUDIT */
343 void smack_log(char *subject_label, char *object_label, int request,
344 int result, struct smk_audit_info *ad)
349 DEFINE_MUTEX(smack_known_lock);
351 struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
354 * smk_insert_entry - insert a smack label into a hash map,
356 * this function must be called under smack_known_lock
358 void smk_insert_entry(struct smack_known *skp)
361 struct hlist_head *head;
363 hash = full_name_hash(skp->smk_known, strlen(skp->smk_known));
364 head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
366 hlist_add_head_rcu(&skp->smk_hashed, head);
367 list_add_rcu(&skp->list, &smack_known_list);
371 * smk_find_entry - find a label on the list, return the list entry
372 * @string: a text string that might be a Smack label
374 * Returns a pointer to the entry in the label list that
375 * matches the passed string.
377 struct smack_known *smk_find_entry(const char *string)
380 struct hlist_head *head;
381 struct smack_known *skp;
382 struct hlist_node *n;
384 hash = full_name_hash(string, strlen(string));
385 head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
387 hlist_for_each_entry_rcu(skp, n, head, smk_hashed)
388 if (strcmp(skp->smk_known, string) == 0)
395 * smk_parse_smack - parse smack label from a text string
396 * @string: a text string that might contain a Smack label
397 * @len: the maximum size, or zero if it is NULL terminated.
399 * Returns a pointer to the clean label, or NULL
401 char *smk_parse_smack(const char *string, int len)
407 len = strlen(string) + 1;
410 * Reserve a leading '-' as an indicator that
411 * this isn't a label, but an option to interfaces
412 * including /smack/cipso and /smack/cipso2
414 if (string[0] == '-')
417 for (i = 0; i < len; i++)
418 if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
419 string[i] == '"' || string[i] == '\\' || string[i] == '\'')
422 if (i == 0 || i >= SMK_LONGLABEL)
425 smack = kzalloc(i + 1, GFP_KERNEL);
427 strncpy(smack, string, i + 1);
434 * smk_netlbl_mls - convert a catset to netlabel mls categories
435 * @catset: the Smack categories
436 * @sap: where to put the netlabel categories
438 * Allocates and fills attr.mls
439 * Returns 0 on success, error code on failure.
441 int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
450 sap->flags |= NETLBL_SECATTR_MLS_CAT;
451 sap->attr.mls.lvl = level;
452 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
453 if (!sap->attr.mls.cat)
455 sap->attr.mls.cat->startbit = 0;
457 for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
458 for (m = 0x80; m != 0; m >>= 1, cat++) {
461 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
464 netlbl_secattr_catmap_free(sap->attr.mls.cat);
473 * smk_import_entry - import a label, return the list entry
474 * @string: a text string that might be a Smack label
475 * @len: the maximum size, or zero if it is NULL terminated.
477 * Returns a pointer to the entry in the label list that
478 * matches the passed string, adding it if necessary.
480 struct smack_known *smk_import_entry(const char *string, int len)
482 struct smack_known *skp;
487 smack = smk_parse_smack(string, len);
491 mutex_lock(&smack_known_lock);
493 skp = smk_find_entry(smack);
497 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
501 skp->smk_known = smack;
502 skp->smk_secid = smack_next_secid++;
503 skp->smk_netlabel.domain = skp->smk_known;
504 skp->smk_netlabel.flags =
505 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
507 * If direct labeling works use it.
508 * Otherwise use mapped labeling.
510 slen = strlen(smack);
511 if (slen < SMK_CIPSOLEN)
512 rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
513 &skp->smk_netlabel, slen);
515 rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
516 &skp->smk_netlabel, sizeof(skp->smk_secid));
519 INIT_LIST_HEAD(&skp->smk_rules);
520 mutex_init(&skp->smk_rules_lock);
522 * Make sure that the entry is actually
523 * filled before putting it on the list.
525 smk_insert_entry(skp);
529 * smk_netlbl_mls failed.
536 mutex_unlock(&smack_known_lock);
542 * smk_import - import a smack label
543 * @string: a text string that might be a Smack label
544 * @len: the maximum size, or zero if it is NULL terminated.
546 * Returns a pointer to the label in the label list that
547 * matches the passed string, adding it if necessary.
549 char *smk_import(const char *string, int len)
551 struct smack_known *skp;
553 /* labels cannot begin with a '-' */
554 if (string[0] == '-')
556 skp = smk_import_entry(string, len);
559 return skp->smk_known;
563 * smack_from_secid - find the Smack label associated with a secid
564 * @secid: an integer that might be associated with a Smack label
566 * Returns a pointer to the appropriate Smack label entry if there is one,
567 * otherwise a pointer to the invalid Smack label.
569 struct smack_known *smack_from_secid(const u32 secid)
571 struct smack_known *skp;
574 list_for_each_entry_rcu(skp, &smack_known_list, list) {
575 if (skp->smk_secid == secid) {
582 * If we got this far someone asked for the translation
583 * of a secid that is not on the list.
586 return &smack_known_invalid;
590 * smack_to_secid - find the secid associated with a Smack label
591 * @smack: the Smack label
593 * Returns the appropriate secid if there is one,
596 u32 smack_to_secid(const char *smack)
598 struct smack_known *skp = smk_find_entry(smack);
602 return skp->smk_secid;