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>
37 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
42 #define CAT_MAX_COUNT 240
43 #define CAT_MAX_VALUE 63
44 #define CIPSO_POS(i) (SMACK_LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
45 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
46 #define CIPSO_NUM_LEN_STR "%-4d"
48 #define KERNEL_LONG_FORMAT "%s %s %s"
49 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5s"
50 #define KERNEL_MODIFY_FORMAT "%s %s %s %s"
51 #define READ_BUF_SIZE LOAD_LEN + 1
52 #define SELF_LABEL_FILE "/proc/self/attr/current"
53 #define ACC_CLEAR "-----"
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
61 extern char *smackfs_mnt;
64 char subject[SMACK_LABEL_LEN + 1];
65 char object[SMACK_LABEL_LEN + 1];
68 struct smack_rule *next;
71 struct smack_accesses {
72 struct smack_rule *first;
73 struct smack_rule *last;
76 struct cipso_mapping {
77 char label[SMACK_LABEL_LEN + 1];
78 int cats[CAT_MAX_VALUE];
81 struct cipso_mapping *next;
85 struct cipso_mapping *first;
86 struct cipso_mapping *last;
89 static int accesses_apply(struct smack_accesses *handle, int clear);
90 static ssize_t smack_label_length(const char *label) __attribute__((unused));
91 static inline ssize_t get_label(char *dest, const char *src);
92 static inline int str_to_access_code(const char *str);
93 static inline void access_code_to_str(unsigned code, char *str);
95 int smack_accesses_new(struct smack_accesses **accesses)
97 struct smack_accesses *result;
99 result = calloc(sizeof(struct smack_accesses), 1);
107 void smack_accesses_free(struct smack_accesses *handle)
112 struct smack_rule *rule = handle->first;
113 struct smack_rule *next_rule = NULL;
115 while (rule != NULL) {
116 next_rule = rule->next;
124 int smack_accesses_save(struct smack_accesses *handle, int fd)
126 struct smack_rule *rule = handle->first;
127 char allow_str[ACC_LEN + 1];
128 char deny_str[ACC_LEN + 1];
137 file = fdopen(newfd, "w");
144 access_code_to_str(rule->allow_code, allow_str);
146 if (rule->deny_code != -1) /* modify? */ {
147 access_code_to_str(rule->deny_code, deny_str);
149 ret = fprintf(file, "%s %s %s %s\n",
150 rule->subject, rule->object,
151 allow_str, deny_str);
153 ret = fprintf(file, "%s %s %s\n",
154 rule->subject, rule->object,
170 int smack_accesses_apply(struct smack_accesses *handle)
172 return accesses_apply(handle, 0);
175 int smack_accesses_clear(struct smack_accesses *handle)
177 return accesses_apply(handle, 1);
180 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
181 const char *object, const char *access_type)
183 struct smack_rule *rule = NULL;
185 rule = calloc(sizeof(struct smack_rule), 1);
189 if (get_label(rule->subject, subject) < 0 ||
190 get_label(rule->object, object) < 0) {
195 rule->allow_code = str_to_access_code(access_type);
196 rule->deny_code = -1; /* no modify */
197 if (rule->allow_code == -1) {
202 if (handle->first == NULL) {
203 handle->first = handle->last = rule;
205 handle->last->next = rule;
212 int smack_accesses_add_modify(struct smack_accesses *handle,
215 const char *allow_access_type,
216 const char *deny_access_type)
218 struct smack_rule *rule = NULL;
220 rule = calloc(sizeof(struct smack_rule), 1);
224 if (get_label(rule->subject, subject) < 0 ||
225 get_label(rule->object, object) < 0) {
230 rule->allow_code = str_to_access_code(allow_access_type);
231 rule->deny_code = str_to_access_code(deny_access_type);
232 if (rule->allow_code == -1 || rule->deny_code == -1) {
237 if (handle->first == NULL) {
238 handle->first = handle->last = rule;
240 handle->last->next = rule;
247 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
250 char buf[READ_BUF_SIZE];
252 const char *subject, *object, *access, *access2;
260 file = fdopen(newfd, "r");
266 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
267 if (strcmp(buf, "\n") == 0)
269 subject = strtok_r(buf, " \t\n", &ptr);
270 object = strtok_r(NULL, " \t\n", &ptr);
271 access = strtok_r(NULL, " \t\n", &ptr);
272 access2 = strtok_r(NULL, " \t\n", &ptr);
274 if (subject == NULL || object == NULL || access == NULL ||
275 strtok_r(NULL, " \t\n", &ptr) != NULL) {
281 ret = smack_accesses_add(accesses, subject, object, access);
283 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
300 int smack_have_access(const char *subject, const char *object,
301 const char *access_type)
303 char buf[LOAD_LEN + 1];
304 char str[ACC_LEN + 1];
314 snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
315 fd = open(path, O_RDWR);
320 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
321 fd = open(path, O_RDWR);
327 if ((code = str_to_access_code(access_type)) < 0)
329 access_code_to_str(code, str);
332 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
333 subject, object, str);
335 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
336 subject, object, str);
343 ret = write(fd, buf, strlen(buf));
349 ret = read(fd, buf, 1);
354 return buf[0] == '1';
357 int smack_cipso_new(struct smack_cipso **cipso)
359 struct smack_cipso *result;
361 result = calloc(sizeof(struct smack_cipso), 1);
369 void smack_cipso_free(struct smack_cipso *cipso)
374 struct cipso_mapping *mapping = cipso->first;
375 struct cipso_mapping *next_mapping = NULL;
377 while (mapping != NULL) {
378 next_mapping = mapping->next;
380 mapping = next_mapping;
386 int smack_cipso_apply(struct smack_cipso *cipso)
388 struct cipso_mapping *m = NULL;
389 char buf[CIPSO_MAX_SIZE];
398 snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
399 fd = open(path, O_WRONLY);
403 memset(buf,0,CIPSO_MAX_SIZE);
404 for (m = cipso->first; m != NULL; m = m->next) {
405 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
406 offset = strlen(buf) + 1;
408 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
411 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
414 for (i = 0; i < m->ncats; i++){
415 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
419 if (write(fd, buf, offset) < 0) {
429 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
431 struct cipso_mapping *mapping = NULL;
434 char *label, *level, *cat, *ptr;
443 file = fdopen(newfd, "r");
449 while (fgets(buf, BUF_SIZE, file) != NULL) {
450 mapping = calloc(sizeof(struct cipso_mapping), 1);
454 label = strtok_r(buf, " \t\n", &ptr);
455 level = strtok_r(NULL, " \t\n", &ptr);
456 cat = strtok_r(NULL, " \t\n", &ptr);
458 if (level == NULL || get_label(mapping->label, label) < 0)
462 val = strtol(level, NULL, 10);
466 if (val < 0 || val > LEVEL_MAX)
469 mapping->level = val;
471 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
473 val = strtol(cat, NULL, 10);
477 if (val < 0 || val > CAT_MAX_VALUE)
480 mapping->cats[i] = val;
482 cat = strtok_r(NULL, " \t\n", &ptr);
487 if (cipso->first == NULL) {
488 cipso->first = cipso->last = mapping;
490 cipso->last->next = mapping;
491 cipso->last = mapping;
506 const char *smack_smackfs_path(void)
511 ssize_t smack_new_label_from_self(char **label)
517 result = calloc(SMACK_LABEL_LEN + 1, 1);
521 fd = open(SELF_LABEL_FILE, O_RDONLY);
527 ret = read(fd, result, SMACK_LABEL_LEN);
538 ssize_t smack_new_label_from_socket(int fd, char **label)
542 socklen_t length = 1;
545 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
546 if (ret < 0 && errno != ERANGE)
549 result = calloc(length + 1, 1);
553 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
563 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
564 int follow, char **label)
570 getxattr(path, xattr, NULL, 0) :
571 lgetxattr(path, xattr, NULL, 0);
572 if (ret < 0 && errno != ERANGE)
575 result = calloc(ret + 1, 1);
580 getxattr(path, xattr, result, ret) :
581 lgetxattr(path, xattr, result, ret);
591 int smack_set_label_for_self(const char *label)
597 len = get_label(NULL, label);
601 fd = open(SELF_LABEL_FILE, O_WRONLY);
605 ret = write(fd, label, len);
608 return (ret < 0) ? -1 : 0;
611 int smack_revoke_subject(const char *subject)
618 len = get_label(NULL, subject);
622 snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
623 fd = open(path, O_WRONLY);
627 ret = write(fd, subject, len);
630 return (ret < 0) ? -1 : 0;
633 ssize_t smack_label_length(const char *label)
635 return get_label(NULL, label);
638 static int accesses_apply(struct smack_accesses *handle, int clear)
640 char buf[LOAD_LEN + 1];
641 char allow_str[ACC_LEN + 1];
642 char deny_str[ACC_LEN + 1];
643 struct smack_rule *rule;
654 snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
655 load_fd = open(path, O_WRONLY);
660 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
661 load_fd = open(path, O_WRONLY);
662 /* Try to continue if the file doesn't exist, we might not need it. */
663 if (load_fd < 0 && errno != ENOENT)
668 snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
669 change_fd = open(path, O_WRONLY);
670 /* Try to continue if the file doesn't exist, we might not need it. */
671 if (change_fd < 0 && errno != ENOENT) {
676 for (rule = handle->first; rule != NULL; rule = rule->next) {
677 access_code_to_str(clear ? 0 : rule->allow_code, allow_str);
679 if (rule->deny_code != -1 && !clear) {
680 access_code_to_str(clear ? 0 : rule->deny_code, deny_str);
683 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
684 rule->subject, rule->object,
690 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
691 rule->subject, rule->object,
694 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
695 rule->subject, rule->object,
699 if (ret < 0 || fd < 0) {
704 ret = write(fd, buf, strlen(buf));
720 static inline ssize_t get_label(char *dest, const char *src)
724 if (!src || src[0] == '\0' || src[0] == '-')
727 for (i = 0; i < (SMACK_LABEL_LEN + 1) && src[i]; i++) {
743 if (dest && i < (SMACK_LABEL_LEN + 1))
746 return i < (SMACK_LABEL_LEN + 1) ? i : -1;
750 static inline int str_to_access_code(const char *str)
753 unsigned int code = 0;
755 for (i = 0; str[i] != '\0'; i++) {
759 code |= ACCESS_TYPE_R;
763 code |= ACCESS_TYPE_W;
767 code |= ACCESS_TYPE_X;
771 code |= ACCESS_TYPE_A;
775 code |= ACCESS_TYPE_T;
787 static inline void access_code_to_str(unsigned int code, char *str)
789 str[0] = ((code & ACCESS_TYPE_R) != 0) ? 'r' : '-';
790 str[1] = ((code & ACCESS_TYPE_W) != 0) ? 'w' : '-';
791 str[2] = ((code & ACCESS_TYPE_X) != 0) ? 'x' : '-';
792 str[3] = ((code & ACCESS_TYPE_A) != 0) ? 'a' : '-';
793 str[4] = ((code & ACCESS_TYPE_T) != 0) ? 't' : '-';