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
24 #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;
78 struct smack_rule *next;
81 struct smack_accesses {
82 struct smack_rule *first;
83 struct smack_rule *last;
84 struct label_dict *dict;
88 struct cipso_mapping {
89 char label[SMACK_LABEL_LEN + 1];
90 int cats[CAT_MAX_VALUE];
93 struct cipso_mapping *next;
97 struct cipso_mapping *first;
98 struct cipso_mapping *last;
101 static int open_smackfs_file(const char *long_name, const char *short_name,
103 static int accesses_apply(struct smack_accesses *handle, int clear);
104 static int accesses_print(struct smack_accesses *handle, int clear,
105 int load_fd, int change_fd, int use_long, int add_lf);
106 static inline ssize_t get_label(char *dest, const char *src);
107 static inline int str_to_access_code(const char *str);
108 static inline void access_code_to_str(unsigned code, char *str);
109 static int dict_create(struct label_dict **dict);
110 static int dict_free(struct label_dict *dict);
111 static const char *dict_get_label(const struct label_dict *dict, int id);
112 static ssize_t dict_add_label(struct label_dict *dict, int *id, const char *src);
114 int smack_accesses_new(struct smack_accesses **accesses)
116 struct smack_accesses *result;
118 result = calloc(sizeof(struct smack_accesses), 1);
122 if (dict_create(&(result->dict)))
128 void smack_accesses_free(struct smack_accesses *handle)
133 struct smack_rule *rule = handle->first;
134 struct smack_rule *next_rule = NULL;
136 while (rule != NULL) {
137 next_rule = rule->next;
142 dict_free(handle->dict);
146 int smack_accesses_save(struct smack_accesses *handle, int fd)
148 return accesses_print(handle, 0, fd, fd, 1, 1);
151 int smack_accesses_apply(struct smack_accesses *handle)
153 return accesses_apply(handle, 0);
156 int smack_accesses_clear(struct smack_accesses *handle)
158 return accesses_apply(handle, 1);
161 int accesses_add(struct smack_accesses *handle, const char *subject,
162 const char *object, const char *allow_access_type,
163 const char *deny_access_type)
165 struct smack_rule *rule = NULL;
168 rule = calloc(sizeof(struct smack_rule), 1);
172 ret = dict_add_label(handle->dict, &(rule->subject_id), subject);
175 if (ret > SHORT_LABEL_LEN)
176 handle->long_labels = 1;
178 ret = dict_add_label(handle->dict, &(rule->object_id), object);
181 if (ret > SHORT_LABEL_LEN)
182 handle->long_labels = 1;
184 rule->allow_code = str_to_access_code(allow_access_type);
185 if (rule->allow_code == -1)
188 if (deny_access_type != NULL) {
189 rule->deny_code = str_to_access_code(deny_access_type);
190 if (rule->deny_code == -1)
193 rule->deny_code = -1; /* no modify */
195 if (handle->first == NULL) {
196 handle->first = handle->last = rule;
198 handle->last->next = rule;
208 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
209 const char *object, const char *access_type)
211 return accesses_add(handle, subject, object, access_type, NULL);
214 int smack_accesses_add_modify(struct smack_accesses *handle,
217 const char *allow_access_type,
218 const char *deny_access_type)
220 return accesses_add(handle, subject, object,
221 allow_access_type, deny_access_type);
224 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
227 char buf[LOAD_LEN + 1];
229 const char *subject, *object, *access, *access2;
237 file = fdopen(newfd, "r");
243 while (fgets(buf, LOAD_LEN + 1, file) != NULL) {
244 if (strcmp(buf, "\n") == 0)
246 subject = strtok_r(buf, " \t\n", &ptr);
247 object = strtok_r(NULL, " \t\n", &ptr);
248 access = strtok_r(NULL, " \t\n", &ptr);
249 access2 = strtok_r(NULL, " \t\n", &ptr);
251 if (subject == NULL || object == NULL || access == NULL ||
252 strtok_r(NULL, " \t\n", &ptr) != NULL) {
258 ret = smack_accesses_add(accesses, subject, object, access);
260 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
277 int smack_have_access(const char *subject, const char *object,
278 const char *access_type)
280 char buf[LOAD_LEN + 1];
281 char str[ACC_LEN + 1];
287 if (smackfs_mnt_dirfd < 0)
290 fd = open_smackfs_file("access2", "access", &use_long);
294 if ((code = str_to_access_code(access_type)) < 0)
296 access_code_to_str(code, str);
299 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
300 subject, object, str);
302 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
303 subject, object, str);
310 ret = write(fd, buf, strlen(buf));
316 ret = read(fd, buf, 1);
321 return buf[0] == '1';
324 int smack_cipso_new(struct smack_cipso **cipso)
326 struct smack_cipso *result;
328 result = calloc(sizeof(struct smack_cipso), 1);
336 void smack_cipso_free(struct smack_cipso *cipso)
341 struct cipso_mapping *mapping = cipso->first;
342 struct cipso_mapping *next_mapping = NULL;
344 while (mapping != NULL) {
345 next_mapping = mapping->next;
347 mapping = next_mapping;
353 int smack_cipso_apply(struct smack_cipso *cipso)
355 struct cipso_mapping *m = NULL;
356 char buf[CIPSO_MAX_SIZE];
361 if (smackfs_mnt_dirfd < 0)
364 fd = openat(smackfs_mnt_dirfd, "cipso2", O_WRONLY);
368 memset(buf,0,CIPSO_MAX_SIZE);
369 for (m = cipso->first; m != NULL; m = m->next) {
370 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
371 offset = strlen(buf) + 1;
373 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
376 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
379 for (i = 0; i < m->ncats; i++){
380 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
384 if (write(fd, buf, offset) < 0) {
394 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
396 struct cipso_mapping *mapping = NULL;
399 char *label, *level, *cat, *ptr;
408 file = fdopen(newfd, "r");
414 while (fgets(buf, BUF_SIZE, file) != NULL) {
415 mapping = calloc(sizeof(struct cipso_mapping), 1);
419 label = strtok_r(buf, " \t\n", &ptr);
420 level = strtok_r(NULL, " \t\n", &ptr);
421 cat = strtok_r(NULL, " \t\n", &ptr);
423 if (level == NULL || get_label(mapping->label, label) < 0)
427 val = strtol(level, NULL, 10);
431 if (val < 0 || val > LEVEL_MAX)
434 mapping->level = val;
436 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
438 val = strtol(cat, NULL, 10);
442 if (val < 0 || val > CAT_MAX_VALUE)
445 mapping->cats[i] = val;
447 cat = strtok_r(NULL, " \t\n", &ptr);
452 if (cipso->first == NULL) {
453 cipso->first = cipso->last = mapping;
455 cipso->last->next = mapping;
456 cipso->last = mapping;
471 const char *smack_smackfs_path(void)
476 ssize_t smack_new_label_from_self(char **label)
482 result = calloc(SMACK_LABEL_LEN + 1, 1);
486 fd = open(SELF_LABEL_FILE, O_RDONLY);
492 ret = read(fd, result, SMACK_LABEL_LEN);
503 ssize_t smack_new_label_from_socket(int fd, char **label)
507 socklen_t length = 1;
510 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
511 if (ret < 0 && errno != ERANGE)
514 result = calloc(length + 1, 1);
518 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
528 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
529 int follow, char **label)
531 char buf[SMACK_LABEL_LEN + 1];
536 getxattr(path, xattr, buf, SMACK_LABEL_LEN + 1) :
537 lgetxattr(path, xattr, buf, SMACK_LABEL_LEN + 1);
541 result = calloc(ret + 1, 1);
545 ret = get_label(result, buf);
555 int smack_set_label_for_self(const char *label)
561 len = get_label(NULL, label);
565 fd = open(SELF_LABEL_FILE, O_WRONLY);
569 ret = write(fd, label, len);
572 return (ret < 0) ? -1 : 0;
575 int smack_revoke_subject(const char *subject)
581 if (smackfs_mnt_dirfd < 0)
584 len = get_label(NULL, subject);
588 fd = openat(smackfs_mnt_dirfd, "revoke-subject", O_WRONLY);
592 ret = write(fd, subject, len);
595 return (ret < 0) ? -1 : 0;
598 static int open_smackfs_file(const char *long_name, const char *short_name,
603 fd = openat(smackfs_mnt_dirfd, long_name, O_WRONLY);
608 fd = openat(smackfs_mnt_dirfd, short_name, O_WRONLY);
620 static int accesses_apply(struct smack_accesses *handle, int clear)
627 if (smackfs_mnt_dirfd < 0)
630 load_fd = open_smackfs_file("load2", "load", &use_long);
634 change_fd = openat(smackfs_mnt_dirfd, "change-rule", O_WRONLY);
635 /* Try to continue if the file doesn't exist, we might not need it. */
636 if (change_fd < 0 && errno != ENOENT) {
641 ret = accesses_print(handle, clear, load_fd, change_fd, use_long, 0);
651 static int accesses_print(struct smack_accesses *handle, int clear,
652 int load_fd, int change_fd, int use_long, int add_lf)
654 char buf[LOAD_LEN + 1];
655 char allow_str[ACC_LEN + 1];
656 char deny_str[ACC_LEN + 1];
657 struct smack_rule *rule;
663 if (!use_long && handle->long_labels)
666 for (rule = handle->first; rule != NULL; rule = rule->next) {
667 /* Fail immediately without doing any further processing
668 if modify rules are not supported. */
669 if (rule->deny_code >= 0 && change_fd < 0)
672 access_code_to_str(clear ? 0 : rule->allow_code, allow_str);
674 if (rule->deny_code != -1 && !clear) {
675 access_code_to_str(rule->deny_code, deny_str);
678 cnt = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
679 dict_get_label(handle->dict, rule->subject_id),
680 dict_get_label(handle->dict, rule->object_id),
686 cnt = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
687 dict_get_label(handle->dict, rule->subject_id),
688 dict_get_label(handle->dict, rule->object_id),
691 cnt = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
692 dict_get_label(handle->dict, rule->subject_id),
693 dict_get_label(handle->dict, rule->object_id),
703 for (i = 0; i < cnt; ) {
704 ret = write(fd, buf + i, cnt - i);
717 static inline ssize_t get_label(char *dest, const char *src)
721 if (!src || src[0] == '\0' || src[0] == '-')
724 for (i = 0; i < (SMACK_LABEL_LEN + 1) && src[i]; i++) {
725 if (src[i] <= ' ' || src[i] > '~')
741 if (dest && i < (SMACK_LABEL_LEN + 1))
744 return i < (SMACK_LABEL_LEN + 1) ? i : -1;
748 static inline int str_to_access_code(const char *str)
751 unsigned int code = 0;
753 for (i = 0; str[i] != '\0'; i++) {
757 code |= ACCESS_TYPE_R;
761 code |= ACCESS_TYPE_W;
765 code |= ACCESS_TYPE_X;
769 code |= ACCESS_TYPE_A;
773 code |= ACCESS_TYPE_T;
777 code |= ACCESS_TYPE_L;
789 static inline void access_code_to_str(unsigned int code, char *str)
791 str[0] = ((code & ACCESS_TYPE_R) != 0) ? 'r' : '-';
792 str[1] = ((code & ACCESS_TYPE_W) != 0) ? 'w' : '-';
793 str[2] = ((code & ACCESS_TYPE_X) != 0) ? 'x' : '-';
794 str[3] = ((code & ACCESS_TYPE_A) != 0) ? 'a' : '-';
795 str[4] = ((code & ACCESS_TYPE_T) != 0) ? 't' : '-';
796 str[5] = ((code & ACCESS_TYPE_L) != 0) ? 'l' : '-';
800 static int dict_create(struct label_dict **dict)
802 *dict = calloc(1, sizeof(struct label_dict));
805 (*dict)->htab = calloc(1, sizeof(struct hsearch_data));
808 (*dict)->labels = calloc(DICT_HASH_SIZE, sizeof(char *));
809 if (!(*dict)->labels)
811 if (hcreate_r(DICT_HASH_SIZE, (*dict)->htab) == 0)
816 free((*dict)->labels);
825 static int dict_free(struct label_dict *dict)
828 for (i = 0; i < (dict->nof_labels); i++)
829 free((dict->labels)[i]);
831 hdestroy_r(dict->htab);
837 static ssize_t dict_add_label(struct label_dict *dict, int *id, const char *label)
842 ret = get_label(NULL, label);
844 if (dict->nof_labels == DICT_HASH_SIZE)
849 e.key = (char *)label;
850 e.data = (void *)(&(dict->labels[dict->nof_labels]));
852 search = hsearch_r(e, ENTER, &ep, dict->htab);
855 if (e.data != ep->data) {/*found an existing entry*/
856 *id = (int)((char **)(ep->data) - dict->labels);
857 } else {/*new entry added*/
858 ep->key = malloc(ret + 1);
862 memcpy(ep->key, label, ret);
863 dict->labels[dict->nof_labels] = ep->key;
864 *id = dict->nof_labels++;
869 static const char *dict_get_label(const struct label_dict *dict, int id)
871 if (id < dict->nof_labels)
872 return dict->labels[id];