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 SHORT_LABEL_LEN 23
38 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
43 #define CAT_MAX_COUNT 240
44 #define CAT_MAX_VALUE 63
45 #define CIPSO_POS(i) (SMACK_LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
46 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
47 #define CIPSO_NUM_LEN_STR "%-4d"
49 #define KERNEL_LONG_FORMAT "%s %s %s"
50 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5s"
51 #define KERNEL_MODIFY_FORMAT "%s %s %s %s"
52 #define READ_BUF_SIZE LOAD_LEN + 1
53 #define SELF_LABEL_FILE "/proc/self/attr/current"
54 #define ACC_CLEAR "-----"
56 #define ACCESS_TYPE_R 0x01
57 #define ACCESS_TYPE_W 0x02
58 #define ACCESS_TYPE_X 0x04
59 #define ACCESS_TYPE_A 0x08
60 #define ACCESS_TYPE_T 0x10
62 extern char *smackfs_mnt;
65 char subject[SMACK_LABEL_LEN + 1];
66 char object[SMACK_LABEL_LEN + 1];
71 struct smack_rule *next;
74 struct smack_accesses {
75 struct smack_rule *first;
76 struct smack_rule *last;
79 struct cipso_mapping {
80 char label[SMACK_LABEL_LEN + 1];
81 int cats[CAT_MAX_VALUE];
84 struct cipso_mapping *next;
88 struct cipso_mapping *first;
89 struct cipso_mapping *last;
92 static int accesses_apply(struct smack_accesses *handle, int clear);
93 static ssize_t smack_label_length(const char *label) __attribute__((unused));
94 static inline ssize_t get_label(char *dest, const char *src);
95 static inline int str_to_access_code(const char *str);
96 static inline void access_code_to_str(unsigned code, char *str);
98 int smack_accesses_new(struct smack_accesses **accesses)
100 struct smack_accesses *result;
102 result = calloc(sizeof(struct smack_accesses), 1);
110 void smack_accesses_free(struct smack_accesses *handle)
115 struct smack_rule *rule = handle->first;
116 struct smack_rule *next_rule = NULL;
118 while (rule != NULL) {
119 next_rule = rule->next;
127 int smack_accesses_save(struct smack_accesses *handle, int fd)
129 struct smack_rule *rule = handle->first;
130 char allow_str[ACC_LEN + 1];
131 char deny_str[ACC_LEN + 1];
140 file = fdopen(newfd, "w");
147 access_code_to_str(rule->allow_code, allow_str);
149 if (rule->deny_code != -1) /* modify? */ {
150 access_code_to_str(rule->deny_code, deny_str);
152 ret = fprintf(file, "%s %s %s %s\n",
153 rule->subject, rule->object,
154 allow_str, deny_str);
156 ret = fprintf(file, "%s %s %s\n",
157 rule->subject, rule->object,
173 int smack_accesses_apply(struct smack_accesses *handle)
175 return accesses_apply(handle, 0);
178 int smack_accesses_clear(struct smack_accesses *handle)
180 return accesses_apply(handle, 1);
183 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
184 const char *object, const char *access_type)
186 struct smack_rule *rule = NULL;
188 rule = calloc(sizeof(struct smack_rule), 1);
192 rule->subject_len = get_label(rule->subject, subject);
193 rule->object_len = get_label(rule->object, object);
194 if (rule->subject_len < 0 || rule->object_len < 0) {
199 rule->allow_code = str_to_access_code(access_type);
200 rule->deny_code = -1; /* no modify */
201 if (rule->allow_code == -1) {
206 if (handle->first == NULL) {
207 handle->first = handle->last = rule;
209 handle->last->next = rule;
216 int smack_accesses_add_modify(struct smack_accesses *handle,
219 const char *allow_access_type,
220 const char *deny_access_type)
222 struct smack_rule *rule = NULL;
224 rule = calloc(sizeof(struct smack_rule), 1);
228 rule->subject_len = get_label(rule->subject, subject);
229 rule->object_len = get_label(rule->object, object);
230 if (rule->subject_len < 0 || rule->object_len < 0) {
235 rule->allow_code = str_to_access_code(allow_access_type);
236 rule->deny_code = str_to_access_code(deny_access_type);
237 if (rule->allow_code == -1 || rule->deny_code == -1) {
242 if (handle->first == NULL) {
243 handle->first = handle->last = rule;
245 handle->last->next = rule;
252 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
255 char buf[READ_BUF_SIZE];
257 const char *subject, *object, *access, *access2;
265 file = fdopen(newfd, "r");
271 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
272 if (strcmp(buf, "\n") == 0)
274 subject = strtok_r(buf, " \t\n", &ptr);
275 object = strtok_r(NULL, " \t\n", &ptr);
276 access = strtok_r(NULL, " \t\n", &ptr);
277 access2 = strtok_r(NULL, " \t\n", &ptr);
279 if (subject == NULL || object == NULL || access == NULL ||
280 strtok_r(NULL, " \t\n", &ptr) != NULL) {
286 ret = smack_accesses_add(accesses, subject, object, access);
288 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
305 int smack_have_access(const char *subject, const char *object,
306 const char *access_type)
308 char buf[LOAD_LEN + 1];
309 char str[ACC_LEN + 1];
319 snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
320 fd = open(path, O_RDWR);
325 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
326 fd = open(path, O_RDWR);
332 if ((code = str_to_access_code(access_type)) < 0)
334 access_code_to_str(code, str);
337 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
338 subject, object, str);
340 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
341 subject, object, str);
348 ret = write(fd, buf, strlen(buf));
354 ret = read(fd, buf, 1);
359 return buf[0] == '1';
362 int smack_cipso_new(struct smack_cipso **cipso)
364 struct smack_cipso *result;
366 result = calloc(sizeof(struct smack_cipso), 1);
374 void smack_cipso_free(struct smack_cipso *cipso)
379 struct cipso_mapping *mapping = cipso->first;
380 struct cipso_mapping *next_mapping = NULL;
382 while (mapping != NULL) {
383 next_mapping = mapping->next;
385 mapping = next_mapping;
391 int smack_cipso_apply(struct smack_cipso *cipso)
393 struct cipso_mapping *m = NULL;
394 char buf[CIPSO_MAX_SIZE];
403 snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
404 fd = open(path, O_WRONLY);
408 memset(buf,0,CIPSO_MAX_SIZE);
409 for (m = cipso->first; m != NULL; m = m->next) {
410 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
411 offset += strlen(buf) + 1;
413 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
416 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
419 for (i = 0; i < m->ncats; i++){
420 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
424 if (write(fd, buf, offset) < 0) {
434 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
436 struct cipso_mapping *mapping = NULL;
439 char *label, *level, *cat, *ptr;
448 file = fdopen(newfd, "r");
454 while (fgets(buf, BUF_SIZE, file) != NULL) {
455 mapping = calloc(sizeof(struct cipso_mapping), 1);
459 label = strtok_r(buf, " \t\n", &ptr);
460 level = strtok_r(NULL, " \t\n", &ptr);
461 cat = strtok_r(NULL, " \t\n", &ptr);
463 if (level == NULL || get_label(mapping->label, label) < 0)
467 val = strtol(level, NULL, 10);
471 if (val < 0 || val > LEVEL_MAX)
474 mapping->level = val;
476 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
478 val = strtol(cat, NULL, 10);
482 if (val < 0 || val > CAT_MAX_VALUE)
485 mapping->cats[i] = val;
487 cat = strtok_r(NULL, " \t\n", &ptr);
492 if (cipso->first == NULL) {
493 cipso->first = cipso->last = mapping;
495 cipso->last->next = mapping;
496 cipso->last = mapping;
511 const char *smack_smackfs_path(void)
516 ssize_t smack_new_label_from_self(char **label)
522 result = calloc(SMACK_LABEL_LEN + 1, 1);
526 fd = open(SELF_LABEL_FILE, O_RDONLY);
532 ret = read(fd, result, SMACK_LABEL_LEN);
543 ssize_t smack_new_label_from_socket(int fd, char **label)
547 socklen_t length = 1;
550 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
551 if (ret < 0 && errno != ERANGE)
554 result = calloc(length + 1, 1);
558 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
568 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
569 int follow, char **label)
575 getxattr(path, xattr, NULL, 0) :
576 lgetxattr(path, xattr, NULL, 0);
577 if (ret < 0 && errno != ERANGE)
580 result = calloc(ret + 1, 1);
585 getxattr(path, xattr, result, ret) :
586 lgetxattr(path, xattr, result, ret);
596 int smack_set_label_for_self(const char *label)
602 len = get_label(NULL, label);
606 fd = open(SELF_LABEL_FILE, O_WRONLY);
610 ret = write(fd, label, len);
613 return (ret < 0) ? -1 : 0;
616 int smack_revoke_subject(const char *subject)
623 len = get_label(NULL, subject);
627 snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
628 fd = open(path, O_WRONLY);
632 ret = write(fd, subject, len);
635 return (ret < 0) ? -1 : 0;
638 ssize_t smack_label_length(const char *label)
640 return get_label(NULL, label);
643 static int accesses_apply(struct smack_accesses *handle, int clear)
645 char buf[LOAD_LEN + 1];
646 char allow_str[ACC_LEN + 1];
647 char deny_str[ACC_LEN + 1];
648 struct smack_rule *rule;
659 snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
660 load_fd = open(path, O_WRONLY);
665 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
666 load_fd = open(path, O_WRONLY);
667 /* Try to continue if the file doesn't exist, we might not need it. */
668 if (load_fd < 0 && errno != ENOENT)
673 snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
674 change_fd = open(path, O_WRONLY);
675 /* Try to continue if the file doesn't exist, we might not need it. */
676 if (change_fd < 0 && errno != ENOENT) {
681 for (rule = handle->first; rule != NULL; rule = rule->next) {
682 /* Fail immediately without doing any further processing
683 if modify rules are not supported. */
684 if (rule->deny_code >= 0 && change_fd < 0) {
689 access_code_to_str(clear ? 0 : rule->allow_code, allow_str);
691 if (rule->deny_code != -1 && !clear) {
692 access_code_to_str(rule->deny_code, deny_str);
695 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
696 rule->subject, rule->object,
702 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
703 rule->subject, rule->object,
706 if (rule->subject_len > SHORT_LABEL_LEN ||
707 rule->object_len > SHORT_LABEL_LEN) {
712 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
713 rule->subject, rule->object,
723 ret = write(fd, buf, ret);
739 static inline ssize_t get_label(char *dest, const char *src)
743 if (!src || src[0] == '\0' || src[0] == '-')
746 for (i = 0; i < (SMACK_LABEL_LEN + 1) && src[i]; i++) {
762 if (i < (SMACK_LABEL_LEN + 1))
765 return i < (SMACK_LABEL_LEN + 1) ? i : -1;
769 static inline int str_to_access_code(const char *str)
772 unsigned int code = 0;
774 for (i = 0; i < ACC_LEN && str[i] != '\0'; i++) {
778 code |= ACCESS_TYPE_R;
782 code |= ACCESS_TYPE_W;
786 code |= ACCESS_TYPE_X;
790 code |= ACCESS_TYPE_A;
794 code |= ACCESS_TYPE_T;
806 static inline void access_code_to_str(unsigned int code, char *str)
808 str[0] = ((code & ACCESS_TYPE_R) != 0) ? 'r' : '-';
809 str[1] = ((code & ACCESS_TYPE_W) != 0) ? 'w' : '-';
810 str[2] = ((code & ACCESS_TYPE_X) != 0) ? 'x' : '-';
811 str[3] = ((code & ACCESS_TYPE_A) != 0) ? 'a' : '-';
812 str[4] = ((code & ACCESS_TYPE_T) != 0) ? 't' : '-';