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>
15 #include <linux/sched.h>
18 struct smack_known smack_known_huh = {
25 struct smack_known smack_known_hat = {
26 .smk_next = &smack_known_huh,
32 struct smack_known smack_known_star = {
33 .smk_next = &smack_known_hat,
39 struct smack_known smack_known_floor = {
40 .smk_next = &smack_known_star,
46 struct smack_known smack_known_invalid = {
47 .smk_next = &smack_known_floor,
53 struct smack_known smack_known_web = {
54 .smk_next = &smack_known_invalid,
60 struct smack_known *smack_known = &smack_known_web;
63 * The initial value needs to be bigger than any of the
66 static u32 smack_next_secid = 10;
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
74 * This function looks up the subject/object pair in the
75 * access rule list and returns 0 if the access is permitted,
78 * Even though Smack labels are usually shared on smack_list
79 * labels that come in off the network can't be imported
80 * and added to the list for locking reasons.
82 * Therefore, it is necessary to check the contents of the labels,
83 * not just the pointer values. Of course, in most cases the labels
84 * will be on the list, so checking the pointers may be a worthwhile
87 int smk_access(char *subject_label, char *object_label, int request)
90 struct smk_list_entry *sp;
91 struct smack_rule *srp;
94 * Hardcoded comparisons.
96 * A star subject can't access any object.
98 if (subject_label == smack_known_star.smk_known ||
99 strcmp(subject_label, smack_known_star.smk_known) == 0)
102 * An internet object can be accessed by any subject.
103 * Tasks cannot be assigned the internet label.
104 * An internet subject can access any object.
106 if (object_label == smack_known_web.smk_known ||
107 subject_label == smack_known_web.smk_known ||
108 strcmp(object_label, smack_known_web.smk_known) == 0 ||
109 strcmp(subject_label, smack_known_web.smk_known) == 0)
112 * A star object can be accessed by any subject.
114 if (object_label == smack_known_star.smk_known ||
115 strcmp(object_label, smack_known_star.smk_known) == 0)
118 * An object can be accessed in any way by a subject
119 * with the same label.
121 if (subject_label == object_label ||
122 strcmp(subject_label, object_label) == 0)
125 * A hat subject can read any object.
126 * A floor object can be read by any subject.
128 if ((request & MAY_ANYREAD) == request) {
129 if (object_label == smack_known_floor.smk_known ||
130 strcmp(object_label, smack_known_floor.smk_known) == 0)
132 if (subject_label == smack_known_hat.smk_known ||
133 strcmp(subject_label, smack_known_hat.smk_known) == 0)
137 * Beyond here an explicit relationship is required.
138 * If the requested access is contained in the available
139 * access (e.g. read is included in readwrite) it's
142 for (sp = smack_list; sp != NULL; sp = sp->smk_next) {
145 if (srp->smk_subject == subject_label ||
146 strcmp(srp->smk_subject, subject_label) == 0) {
147 if (srp->smk_object == object_label ||
148 strcmp(srp->smk_object, object_label) == 0) {
149 may = srp->smk_access;
155 * This is a bit map operation.
157 if ((request & may) == request)
164 * smk_curacc - determine if current has a specific access to an object
165 * @object_label: a pointer to the object's Smack label
166 * @request: the access requested, in "MAY" format
168 * This function checks the current subject label/object label pair
169 * in the access rule list and returns 0 if the access is permitted,
170 * non zero otherwise. It allows that current may have the capability
171 * to override the rules.
173 int smk_curacc(char *obj_label, u32 mode)
177 rc = smk_access(current_security(), obj_label, mode);
182 * Return if a specific label has been designated as the
183 * only one that gets privilege and current does not
186 if (smack_onlycap != NULL && smack_onlycap != current->cred->security)
189 if (capable(CAP_MAC_OVERRIDE))
195 static DEFINE_MUTEX(smack_known_lock);
198 * smk_import_entry - import a label, return the list entry
199 * @string: a text string that might be a Smack label
200 * @len: the maximum size, or zero if it is NULL terminated.
202 * Returns a pointer to the entry in the label list that
203 * matches the passed string, adding it if necessary.
205 struct smack_known *smk_import_entry(const char *string, int len)
207 struct smack_known *skp;
208 char smack[SMK_LABELLEN];
212 if (len <= 0 || len > SMK_MAXLEN)
215 for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
218 else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
223 smack[i] = string[i];
226 if (smack[0] == '\0')
229 mutex_lock(&smack_known_lock);
231 for (skp = smack_known; skp != NULL; skp = skp->smk_next)
232 if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0)
236 skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
238 skp->smk_next = smack_known;
239 strncpy(skp->smk_known, smack, SMK_MAXLEN);
240 skp->smk_secid = smack_next_secid++;
241 skp->smk_cipso = NULL;
242 spin_lock_init(&skp->smk_cipsolock);
244 * Make sure that the entry is actually
245 * filled before putting it on the list.
252 mutex_unlock(&smack_known_lock);
258 * smk_import - import a smack label
259 * @string: a text string that might be a Smack label
260 * @len: the maximum size, or zero if it is NULL terminated.
262 * Returns a pointer to the label in the label list that
263 * matches the passed string, adding it if necessary.
265 char *smk_import(const char *string, int len)
267 struct smack_known *skp;
269 skp = smk_import_entry(string, len);
272 return skp->smk_known;
276 * smack_from_secid - find the Smack label associated with a secid
277 * @secid: an integer that might be associated with a Smack label
279 * Returns a pointer to the appropraite Smack label if there is one,
280 * otherwise a pointer to the invalid Smack label.
282 char *smack_from_secid(const u32 secid)
284 struct smack_known *skp;
286 for (skp = smack_known; skp != NULL; skp = skp->smk_next)
287 if (skp->smk_secid == secid)
288 return skp->smk_known;
291 * If we got this far someone asked for the translation
292 * of a secid that is not on the list.
294 return smack_known_invalid.smk_known;
298 * smack_to_secid - find the secid associated with a Smack label
299 * @smack: the Smack label
301 * Returns the appropriate secid if there is one,
304 u32 smack_to_secid(const char *smack)
306 struct smack_known *skp;
308 for (skp = smack_known; skp != NULL; skp = skp->smk_next)
309 if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0)
310 return skp->smk_secid;
315 * smack_from_cipso - find the Smack label associated with a CIPSO option
316 * @level: Bell & LaPadula level from the network
317 * @cp: Bell & LaPadula categories from the network
318 * @result: where to put the Smack value
320 * This is a simple lookup in the label table.
322 * This is an odd duck as far as smack handling goes in that
323 * it sends back a copy of the smack label rather than a pointer
324 * to the master list. This is done because it is possible for
325 * a foreign host to send a smack label that is new to this
326 * machine and hence not on the list. That would not be an
327 * issue except that adding an entry to the master list can't
328 * be done at that point.
330 void smack_from_cipso(u32 level, char *cp, char *result)
332 struct smack_known *kp;
335 for (kp = smack_known; final == NULL && kp != NULL; kp = kp->smk_next) {
336 if (kp->smk_cipso == NULL)
339 spin_lock_bh(&kp->smk_cipsolock);
341 if (kp->smk_cipso->smk_level == level &&
342 memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
343 final = kp->smk_known;
345 spin_unlock_bh(&kp->smk_cipsolock);
348 final = smack_known_huh.smk_known;
349 strncpy(result, final, SMK_MAXLEN);
354 * smack_to_cipso - find the CIPSO option to go with a Smack label
355 * @smack: a pointer to the smack label in question
356 * @cp: where to put the result
358 * Returns zero if a value is available, non-zero otherwise.
360 int smack_to_cipso(const char *smack, struct smack_cipso *cp)
362 struct smack_known *kp;
364 for (kp = smack_known; kp != NULL; kp = kp->smk_next)
365 if (kp->smk_known == smack ||
366 strcmp(kp->smk_known, smack) == 0)
369 if (kp == NULL || kp->smk_cipso == NULL)
372 memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));