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
23 #include "sys/smack.h"
29 #include <sys/socket.h>
31 #include <sys/types.h>
34 #include <sys/xattr.h>
36 #define SELF_LABEL_FILE "/proc/self/attr/current"
38 #define SHORT_LABEL_LEN 23
40 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
41 #define KERNEL_LONG_FORMAT "%s %s %s"
42 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5.5s"
43 #define KERNEL_MODIFY_FORMAT "%s %s %s %s"
48 #define CAT_MAX_COUNT 240
49 #define CAT_MAX_VALUE 63
50 #define CIPSO_POS(i) (SMACK_LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
51 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
52 #define CIPSO_NUM_LEN_STR "%-4d"
54 #define ACCESS_TYPE_R 0x01
55 #define ACCESS_TYPE_W 0x02
56 #define ACCESS_TYPE_X 0x04
57 #define ACCESS_TYPE_A 0x08
58 #define ACCESS_TYPE_T 0x10
59 #define ACCESS_TYPE_L 0x20
61 extern char *smackfs_mnt;
64 char subject[SMACK_LABEL_LEN + 1];
65 char object[SMACK_LABEL_LEN + 1];
70 struct smack_rule *next;
73 struct smack_accesses {
74 struct smack_rule *first;
75 struct smack_rule *last;
78 struct cipso_mapping {
79 char label[SMACK_LABEL_LEN + 1];
80 int cats[CAT_MAX_VALUE];
83 struct cipso_mapping *next;
87 struct cipso_mapping *first;
88 struct cipso_mapping *last;
91 static int accesses_apply(struct smack_accesses *handle, int clear);
92 static inline ssize_t get_label(char *dest, const char *src);
93 static inline int str_to_access_code(const char *str);
94 static inline void access_code_to_str(unsigned code, char *str);
96 int smack_accesses_new(struct smack_accesses **accesses)
98 struct smack_accesses *result;
100 result = calloc(sizeof(struct smack_accesses), 1);
108 void smack_accesses_free(struct smack_accesses *handle)
113 struct smack_rule *rule = handle->first;
114 struct smack_rule *next_rule = NULL;
116 while (rule != NULL) {
117 next_rule = rule->next;
125 int smack_accesses_save(struct smack_accesses *handle, int fd)
127 struct smack_rule *rule = handle->first;
128 char allow_str[ACC_LEN + 1];
129 char deny_str[ACC_LEN + 1];
138 file = fdopen(newfd, "w");
145 access_code_to_str(rule->allow_code, allow_str);
147 if (rule->deny_code != -1) /* modify? */ {
148 access_code_to_str(rule->deny_code, deny_str);
150 ret = fprintf(file, "%s %s %s %s\n",
151 rule->subject, rule->object,
152 allow_str, deny_str);
154 ret = fprintf(file, "%s %s %s\n",
155 rule->subject, rule->object,
171 int smack_accesses_apply(struct smack_accesses *handle)
173 return accesses_apply(handle, 0);
176 int smack_accesses_clear(struct smack_accesses *handle)
178 return accesses_apply(handle, 1);
181 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
182 const char *object, const char *access_type)
184 struct smack_rule *rule = NULL;
186 rule = calloc(sizeof(struct smack_rule), 1);
190 rule->subject_len = get_label(rule->subject, subject);
191 rule->object_len = get_label(rule->object, object);
192 if (rule->subject_len < 0 || rule->object_len < 0) {
197 rule->allow_code = str_to_access_code(access_type);
198 rule->deny_code = -1; /* no modify */
199 if (rule->allow_code == -1) {
204 if (handle->first == NULL) {
205 handle->first = handle->last = rule;
207 handle->last->next = rule;
214 int smack_accesses_add_modify(struct smack_accesses *handle,
217 const char *allow_access_type,
218 const char *deny_access_type)
220 struct smack_rule *rule = NULL;
222 rule = calloc(sizeof(struct smack_rule), 1);
226 rule->subject_len = get_label(rule->subject, subject);
227 rule->object_len = get_label(rule->object, object);
228 if (rule->subject_len < 0 || rule->object_len < 0) {
233 rule->allow_code = str_to_access_code(allow_access_type);
234 rule->deny_code = str_to_access_code(deny_access_type);
235 if (rule->allow_code == -1 || rule->deny_code == -1) {
240 if (handle->first == NULL) {
241 handle->first = handle->last = rule;
243 handle->last->next = rule;
250 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
253 char buf[LOAD_LEN + 1];
255 const char *subject, *object, *access, *access2;
263 file = fdopen(newfd, "r");
269 while (fgets(buf, LOAD_LEN + 1, file) != NULL) {
270 if (strcmp(buf, "\n") == 0)
272 subject = strtok_r(buf, " \t\n", &ptr);
273 object = strtok_r(NULL, " \t\n", &ptr);
274 access = strtok_r(NULL, " \t\n", &ptr);
275 access2 = strtok_r(NULL, " \t\n", &ptr);
277 if (subject == NULL || object == NULL || access == NULL ||
278 strtok_r(NULL, " \t\n", &ptr) != NULL) {
284 ret = smack_accesses_add(accesses, subject, object, access);
286 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
303 int smack_have_access(const char *subject, const char *object,
304 const char *access_type)
306 char buf[LOAD_LEN + 1];
307 char str[ACC_LEN + 1];
317 snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
318 fd = open(path, O_RDWR);
323 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
324 fd = open(path, O_RDWR);
330 if ((code = str_to_access_code(access_type)) < 0)
332 access_code_to_str(code, str);
335 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
336 subject, object, str);
338 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
339 subject, object, str);
346 ret = write(fd, buf, strlen(buf));
352 ret = read(fd, buf, 1);
357 return buf[0] == '1';
360 int smack_cipso_new(struct smack_cipso **cipso)
362 struct smack_cipso *result;
364 result = calloc(sizeof(struct smack_cipso), 1);
372 void smack_cipso_free(struct smack_cipso *cipso)
377 struct cipso_mapping *mapping = cipso->first;
378 struct cipso_mapping *next_mapping = NULL;
380 while (mapping != NULL) {
381 next_mapping = mapping->next;
383 mapping = next_mapping;
389 int smack_cipso_apply(struct smack_cipso *cipso)
391 struct cipso_mapping *m = NULL;
392 char buf[CIPSO_MAX_SIZE];
401 snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
402 fd = open(path, O_WRONLY);
406 memset(buf,0,CIPSO_MAX_SIZE);
407 for (m = cipso->first; m != NULL; m = m->next) {
408 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
409 offset = strlen(buf) + 1;
411 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
414 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
417 for (i = 0; i < m->ncats; i++){
418 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
422 if (write(fd, buf, offset) < 0) {
432 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
434 struct cipso_mapping *mapping = NULL;
437 char *label, *level, *cat, *ptr;
446 file = fdopen(newfd, "r");
452 while (fgets(buf, BUF_SIZE, file) != NULL) {
453 mapping = calloc(sizeof(struct cipso_mapping), 1);
457 label = strtok_r(buf, " \t\n", &ptr);
458 level = strtok_r(NULL, " \t\n", &ptr);
459 cat = strtok_r(NULL, " \t\n", &ptr);
461 if (level == NULL || get_label(mapping->label, label) < 0)
465 val = strtol(level, NULL, 10);
469 if (val < 0 || val > LEVEL_MAX)
472 mapping->level = val;
474 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
476 val = strtol(cat, NULL, 10);
480 if (val < 0 || val > CAT_MAX_VALUE)
483 mapping->cats[i] = val;
485 cat = strtok_r(NULL, " \t\n", &ptr);
490 if (cipso->first == NULL) {
491 cipso->first = cipso->last = mapping;
493 cipso->last->next = mapping;
494 cipso->last = mapping;
509 const char *smack_smackfs_path(void)
514 ssize_t smack_new_label_from_self(char **label)
520 result = calloc(SMACK_LABEL_LEN + 1, 1);
524 fd = open(SELF_LABEL_FILE, O_RDONLY);
530 ret = read(fd, result, SMACK_LABEL_LEN);
541 ssize_t smack_new_label_from_socket(int fd, char **label)
545 socklen_t length = 1;
548 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
549 if (ret < 0 && errno != ERANGE)
552 result = calloc(length + 1, 1);
556 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
566 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
567 int follow, char **label)
573 getxattr(path, xattr, NULL, 0) :
574 lgetxattr(path, xattr, NULL, 0);
575 if (ret < 0 && errno != ERANGE)
578 result = calloc(ret + 1, 1);
583 getxattr(path, xattr, result, ret) :
584 lgetxattr(path, xattr, result, ret);
594 int smack_set_label_for_self(const char *label)
600 len = get_label(NULL, label);
604 fd = open(SELF_LABEL_FILE, O_WRONLY);
608 ret = write(fd, label, len);
611 return (ret < 0) ? -1 : 0;
614 int smack_revoke_subject(const char *subject)
621 len = get_label(NULL, subject);
625 snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
626 fd = open(path, O_WRONLY);
630 ret = write(fd, subject, len);
633 return (ret < 0) ? -1 : 0;
636 static int accesses_apply(struct smack_accesses *handle, int clear)
638 char buf[LOAD_LEN + 1];
639 char allow_str[ACC_LEN + 1];
640 char deny_str[ACC_LEN + 1];
641 struct smack_rule *rule;
652 snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
653 load_fd = open(path, O_WRONLY);
658 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
659 load_fd = open(path, O_WRONLY);
660 /* Try to continue if the file doesn't exist, we might not need it. */
661 if (load_fd < 0 && errno != ENOENT)
666 snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
667 change_fd = open(path, O_WRONLY);
668 /* Try to continue if the file doesn't exist, we might not need it. */
669 if (change_fd < 0 && errno != ENOENT) {
674 for (rule = handle->first; rule != NULL; rule = rule->next) {
675 /* Fail immediately without doing any further processing
676 if modify rules are not supported. */
677 if (rule->deny_code >= 0 && change_fd < 0) {
682 access_code_to_str(clear ? 0 : rule->allow_code, allow_str);
684 if (rule->deny_code != -1 && !clear) {
685 access_code_to_str(rule->deny_code, deny_str);
688 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
689 rule->subject, rule->object,
695 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
696 rule->subject, rule->object,
699 if (rule->subject_len > SHORT_LABEL_LEN ||
700 rule->object_len > SHORT_LABEL_LEN) {
705 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
706 rule->subject, rule->object,
716 ret = write(fd, buf, ret);
732 static inline ssize_t get_label(char *dest, const char *src)
736 if (!src || src[0] == '\0' || src[0] == '-')
739 for (i = 0; i < (SMACK_LABEL_LEN + 1) && src[i]; i++) {
755 if (dest && i < (SMACK_LABEL_LEN + 1))
758 return i < (SMACK_LABEL_LEN + 1) ? i : -1;
762 static inline int str_to_access_code(const char *str)
765 unsigned int code = 0;
767 for (i = 0; str[i] != '\0'; i++) {
771 code |= ACCESS_TYPE_R;
775 code |= ACCESS_TYPE_W;
779 code |= ACCESS_TYPE_X;
783 code |= ACCESS_TYPE_A;
787 code |= ACCESS_TYPE_T;
791 code |= ACCESS_TYPE_L;
803 static inline void access_code_to_str(unsigned int code, char *str)
805 str[0] = ((code & ACCESS_TYPE_R) != 0) ? 'r' : '-';
806 str[1] = ((code & ACCESS_TYPE_W) != 0) ? 'w' : '-';
807 str[2] = ((code & ACCESS_TYPE_X) != 0) ? 'x' : '-';
808 str[3] = ((code & ACCESS_TYPE_A) != 0) ? 'a' : '-';
809 str[4] = ((code & ACCESS_TYPE_T) != 0) ? 't' : '-';
810 str[5] = ((code & ACCESS_TYPE_L) != 0) ? 'l' : '-';