2 * This file is part of libsmack
4 * Copyright (C) 2010, 2011 Nokia Corporation
5 * Copyright (C) 2011, 2012, 2013 Intel Corporation
6 * Copyright (C) 2012, 2013 Samsung Electronics Co.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1 as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include "sys/smack.h"
31 #include <sys/socket.h>
33 #include <sys/types.h>
35 #include <sys/xattr.h>
37 #define SELF_LABEL_FILE "/proc/self/attr/current"
39 #define SHORT_LABEL_LEN 23
41 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
42 #define KERNEL_LONG_FORMAT "%s %s %s"
43 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5.5s"
44 #define KERNEL_MODIFY_FORMAT "%s %s %s %s"
49 #define CAT_MAX_COUNT 240
50 #define CAT_MAX_VALUE 63
51 #define CIPSO_POS(i) (SMACK_LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
52 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
53 #define CIPSO_NUM_LEN_STR "%-4d"
55 #define ACCESS_TYPE_R 0x01
56 #define ACCESS_TYPE_W 0x02
57 #define ACCESS_TYPE_X 0x04
58 #define ACCESS_TYPE_A 0x08
59 #define ACCESS_TYPE_T 0x10
60 #define ACCESS_TYPE_L 0x20
62 #define DICT_HASH_SIZE 4096
64 extern char *smackfs_mnt;
65 extern int smackfs_mnt_dirfd;
70 struct hsearch_data *htab;
80 struct smack_rule *next;
83 struct smack_accesses {
84 struct smack_rule *first;
85 struct smack_rule *last;
86 struct label_dict *dict;
89 struct cipso_mapping {
90 char label[SMACK_LABEL_LEN + 1];
91 int cats[CAT_MAX_VALUE];
94 struct cipso_mapping *next;
98 struct cipso_mapping *first;
99 struct cipso_mapping *last;
102 static int accesses_apply(struct smack_accesses *handle, int clear);
103 static inline ssize_t get_label(char *dest, const char *src);
104 static inline int str_to_access_code(const char *str);
105 static inline void access_code_to_str(unsigned code, char *str);
106 static int dict_create(struct label_dict **dict);
107 static int dict_free(struct label_dict *dict);
108 static const char *dict_get_label(const struct label_dict *dict, int id);
109 static ssize_t dict_add_label(struct label_dict *dict, int *id, const char *src);
111 int smack_accesses_new(struct smack_accesses **accesses)
113 struct smack_accesses *result;
115 result = calloc(sizeof(struct smack_accesses), 1);
119 if (dict_create(&(result->dict)))
125 void smack_accesses_free(struct smack_accesses *handle)
130 struct smack_rule *rule = handle->first;
131 struct smack_rule *next_rule = NULL;
133 while (rule != NULL) {
134 next_rule = rule->next;
139 dict_free(handle->dict);
143 int smack_accesses_save(struct smack_accesses *handle, int fd)
145 struct smack_rule *rule = handle->first;
146 char allow_str[ACC_LEN + 1];
147 char deny_str[ACC_LEN + 1];
156 file = fdopen(newfd, "w");
163 access_code_to_str(rule->allow_code, allow_str);
165 if (rule->deny_code != -1) /* modify? */ {
166 access_code_to_str(rule->deny_code, deny_str);
168 ret = fprintf(file, "%s %s %s %s\n",
169 dict_get_label(handle->dict, rule->subject_id),
170 dict_get_label(handle->dict, rule->object_id),
171 allow_str, deny_str);
173 ret = fprintf(file, "%s %s %s\n",
174 dict_get_label(handle->dict, rule->subject_id),
175 dict_get_label(handle->dict, rule->object_id),
191 int smack_accesses_apply(struct smack_accesses *handle)
193 return accesses_apply(handle, 0);
196 int smack_accesses_clear(struct smack_accesses *handle)
198 return accesses_apply(handle, 1);
201 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
202 const char *object, const char *access_type)
204 struct smack_rule *rule = NULL;
206 rule = calloc(sizeof(struct smack_rule), 1);
210 rule->subject_len = dict_add_label(handle->dict, &(rule->subject_id), subject);
211 rule->object_len = dict_add_label(handle->dict, &(rule->object_id), object);
212 if (rule->subject_len < 0 || rule->object_len < 0) {
217 rule->allow_code = str_to_access_code(access_type);
218 rule->deny_code = -1; /* no modify */
219 if (rule->allow_code == -1) {
224 if (handle->first == NULL) {
225 handle->first = handle->last = rule;
227 handle->last->next = rule;
234 int smack_accesses_add_modify(struct smack_accesses *handle,
237 const char *allow_access_type,
238 const char *deny_access_type)
240 struct smack_rule *rule = NULL;
242 rule = calloc(sizeof(struct smack_rule), 1);
246 rule->subject_len = dict_add_label(handle->dict, &(rule->subject_id), subject);
247 rule->object_len = dict_add_label(handle->dict, &(rule->object_id), object);
248 if (rule->subject_len < 0 || rule->object_len < 0) {
253 rule->allow_code = str_to_access_code(allow_access_type);
254 rule->deny_code = str_to_access_code(deny_access_type);
255 if (rule->allow_code == -1 || rule->deny_code == -1) {
260 if (handle->first == NULL) {
261 handle->first = handle->last = rule;
263 handle->last->next = rule;
270 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
273 char buf[LOAD_LEN + 1];
275 const char *subject, *object, *access, *access2;
283 file = fdopen(newfd, "r");
289 while (fgets(buf, LOAD_LEN + 1, file) != NULL) {
290 if (strcmp(buf, "\n") == 0)
292 subject = strtok_r(buf, " \t\n", &ptr);
293 object = strtok_r(NULL, " \t\n", &ptr);
294 access = strtok_r(NULL, " \t\n", &ptr);
295 access2 = strtok_r(NULL, " \t\n", &ptr);
297 if (subject == NULL || object == NULL || access == NULL ||
298 strtok_r(NULL, " \t\n", &ptr) != NULL) {
304 ret = smack_accesses_add(accesses, subject, object, access);
306 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
323 int smack_have_access(const char *subject, const char *object,
324 const char *access_type)
326 char buf[LOAD_LEN + 1];
327 char str[ACC_LEN + 1];
333 if (smackfs_mnt_dirfd < 0)
336 fd = openat(smackfs_mnt_dirfd, "access2", O_RDWR);
341 fd = openat(smackfs_mnt_dirfd, "access", O_RDWR);
347 if ((code = str_to_access_code(access_type)) < 0)
349 access_code_to_str(code, str);
352 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
353 subject, object, str);
355 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
356 subject, object, str);
363 ret = write(fd, buf, strlen(buf));
369 ret = read(fd, buf, 1);
374 return buf[0] == '1';
377 int smack_cipso_new(struct smack_cipso **cipso)
379 struct smack_cipso *result;
381 result = calloc(sizeof(struct smack_cipso), 1);
389 void smack_cipso_free(struct smack_cipso *cipso)
394 struct cipso_mapping *mapping = cipso->first;
395 struct cipso_mapping *next_mapping = NULL;
397 while (mapping != NULL) {
398 next_mapping = mapping->next;
400 mapping = next_mapping;
406 int smack_cipso_apply(struct smack_cipso *cipso)
408 struct cipso_mapping *m = NULL;
409 char buf[CIPSO_MAX_SIZE];
414 if (smackfs_mnt_dirfd < 0)
417 fd = openat(smackfs_mnt_dirfd, "cipso2", O_WRONLY);
421 memset(buf,0,CIPSO_MAX_SIZE);
422 for (m = cipso->first; m != NULL; m = m->next) {
423 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
424 offset = strlen(buf) + 1;
426 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
429 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
432 for (i = 0; i < m->ncats; i++){
433 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
437 if (write(fd, buf, offset) < 0) {
447 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
449 struct cipso_mapping *mapping = NULL;
452 char *label, *level, *cat, *ptr;
461 file = fdopen(newfd, "r");
467 while (fgets(buf, BUF_SIZE, file) != NULL) {
468 mapping = calloc(sizeof(struct cipso_mapping), 1);
472 label = strtok_r(buf, " \t\n", &ptr);
473 level = strtok_r(NULL, " \t\n", &ptr);
474 cat = strtok_r(NULL, " \t\n", &ptr);
476 if (level == NULL || get_label(mapping->label, label) < 0)
480 val = strtol(level, NULL, 10);
484 if (val < 0 || val > LEVEL_MAX)
487 mapping->level = val;
489 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
491 val = strtol(cat, NULL, 10);
495 if (val < 0 || val > CAT_MAX_VALUE)
498 mapping->cats[i] = val;
500 cat = strtok_r(NULL, " \t\n", &ptr);
505 if (cipso->first == NULL) {
506 cipso->first = cipso->last = mapping;
508 cipso->last->next = mapping;
509 cipso->last = mapping;
524 const char *smack_smackfs_path(void)
529 ssize_t smack_new_label_from_self(char **label)
535 result = calloc(SMACK_LABEL_LEN + 1, 1);
539 fd = open(SELF_LABEL_FILE, O_RDONLY);
545 ret = read(fd, result, SMACK_LABEL_LEN);
556 ssize_t smack_new_label_from_socket(int fd, char **label)
560 socklen_t length = 1;
563 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
564 if (ret < 0 && errno != ERANGE)
567 result = calloc(length + 1, 1);
571 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
581 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
582 int follow, char **label)
584 char buf[SMACK_LABEL_LEN + 1];
589 getxattr(path, xattr, buf, SMACK_LABEL_LEN + 1) :
590 lgetxattr(path, xattr, buf, SMACK_LABEL_LEN + 1);
594 result = calloc(ret + 1, 1);
598 ret = get_label(result, buf);
608 int smack_set_label_for_self(const char *label)
614 len = get_label(NULL, label);
618 fd = open(SELF_LABEL_FILE, O_WRONLY);
622 ret = write(fd, label, len);
625 return (ret < 0) ? -1 : 0;
628 int smack_revoke_subject(const char *subject)
634 if (smackfs_mnt_dirfd < 0)
637 len = get_label(NULL, subject);
641 fd = openat(smackfs_mnt_dirfd, "revoke-subject", O_WRONLY);
645 ret = write(fd, subject, len);
648 return (ret < 0) ? -1 : 0;
651 static int accesses_apply(struct smack_accesses *handle, int clear)
653 char buf[LOAD_LEN + 1];
654 char allow_str[ACC_LEN + 1];
655 char deny_str[ACC_LEN + 1];
656 struct smack_rule *rule;
663 if (smackfs_mnt_dirfd < 0)
666 load_fd = openat(smackfs_mnt_dirfd, "load2", O_WRONLY);
671 load_fd = openat(smackfs_mnt_dirfd, "load", O_WRONLY);
672 /* Try to continue if the file doesn't exist, we might not need it. */
673 if (load_fd < 0 && errno != ENOENT)
678 change_fd = openat(smackfs_mnt_dirfd, "change-rule", O_WRONLY);
679 /* Try to continue if the file doesn't exist, we might not need it. */
680 if (change_fd < 0 && errno != ENOENT) {
685 for (rule = handle->first; rule != NULL; rule = rule->next) {
686 /* Fail immediately without doing any further processing
687 if modify rules are not supported. */
688 if (rule->deny_code >= 0 && change_fd < 0) {
693 access_code_to_str(clear ? 0 : rule->allow_code, allow_str);
695 if (rule->deny_code != -1 && !clear) {
696 access_code_to_str(rule->deny_code, deny_str);
699 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
700 dict_get_label(handle->dict, rule->subject_id),
701 dict_get_label(handle->dict, rule->object_id),
707 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
708 dict_get_label(handle->dict, rule->subject_id),
709 dict_get_label(handle->dict, rule->object_id),
712 if (rule->subject_len > SHORT_LABEL_LEN ||
713 rule->object_len > SHORT_LABEL_LEN) {
718 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
719 dict_get_label(handle->dict, rule->subject_id),
720 dict_get_label(handle->dict, rule->object_id),
730 ret = write(fd, buf, ret);
746 static inline ssize_t get_label(char *dest, const char *src)
750 if (!src || src[0] == '\0' || src[0] == '-')
753 for (i = 0; i < (SMACK_LABEL_LEN + 1) && src[i]; i++) {
754 if (src[i] <= ' ' || src[i] > '~')
770 if (dest && i < (SMACK_LABEL_LEN + 1))
773 return i < (SMACK_LABEL_LEN + 1) ? i : -1;
777 static inline int str_to_access_code(const char *str)
780 unsigned int code = 0;
782 for (i = 0; str[i] != '\0'; i++) {
786 code |= ACCESS_TYPE_R;
790 code |= ACCESS_TYPE_W;
794 code |= ACCESS_TYPE_X;
798 code |= ACCESS_TYPE_A;
802 code |= ACCESS_TYPE_T;
806 code |= ACCESS_TYPE_L;
818 static inline void access_code_to_str(unsigned int code, char *str)
820 str[0] = ((code & ACCESS_TYPE_R) != 0) ? 'r' : '-';
821 str[1] = ((code & ACCESS_TYPE_W) != 0) ? 'w' : '-';
822 str[2] = ((code & ACCESS_TYPE_X) != 0) ? 'x' : '-';
823 str[3] = ((code & ACCESS_TYPE_A) != 0) ? 'a' : '-';
824 str[4] = ((code & ACCESS_TYPE_T) != 0) ? 't' : '-';
825 str[5] = ((code & ACCESS_TYPE_L) != 0) ? 'l' : '-';
829 static int dict_create(struct label_dict **dict)
831 *dict = calloc(1, sizeof(struct label_dict));
834 (*dict)->htab = calloc(1, sizeof(struct hsearch_data));
837 (*dict)->labels = calloc(DICT_HASH_SIZE, sizeof(char *));
838 if (!(*dict)->labels)
840 if (hcreate_r(DICT_HASH_SIZE, (*dict)->htab) == 0)
845 free((*dict)->labels);
854 static int dict_free(struct label_dict *dict)
857 for (i = 0; i < (dict->nof_labels); i++)
858 free((dict->labels)[i]);
860 hdestroy_r(dict->htab);
866 static ssize_t dict_add_label(struct label_dict *dict, int *id, const char *label)
871 ret = get_label(NULL, label);
873 if (dict->nof_labels == DICT_HASH_SIZE)
878 e.key = (char *)label;
879 e.data = (void *)(&(dict->labels[dict->nof_labels]));
881 search = hsearch_r(e, ENTER, &ep, dict->htab);
884 if (e.data != ep->data) {/*found an existing entry*/
885 *id = (int)((char **)(ep->data) - dict->labels);
886 } else {/*new entry added*/
887 ep->key = malloc(ret + 1);
891 memcpy(ep->key, label, ret);
892 dict->labels[dict->nof_labels] = ep->key;
893 *id = dict->nof_labels++;
898 static const char *dict_get_label(const struct label_dict *dict, int id)
900 if (id < dict->nof_labels)
901 return dict->labels[id];