2 * This file is part of libsmack
4 * Copyright (C) 2010 Nokia Corporation
5 * Copyright (C) 2011 Intel Corporation
6 * Copyright (C) 2012 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"
54 extern char *smackfs_mnt;
57 char subject[SMACK_LABEL_LEN + 1];
58 char object[SMACK_LABEL_LEN + 1];
60 char access_type[ACC_LEN + 1];
61 char allow_access_type[ACC_LEN + 1];
62 char deny_access_type[ACC_LEN + 1];
63 struct smack_rule *next;
66 struct smack_accesses {
67 struct smack_rule *first;
68 struct smack_rule *last;
71 struct cipso_mapping {
72 char label[SMACK_LABEL_LEN + 1];
73 int cats[CAT_MAX_VALUE];
76 struct cipso_mapping *next;
80 struct cipso_mapping *first;
81 struct cipso_mapping *last;
84 static int accesses_apply(struct smack_accesses *handle, int clear);
85 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1]);
86 static int smack_label_length(const char *label) __attribute__((unused));
88 int smack_accesses_new(struct smack_accesses **accesses)
90 struct smack_accesses *result;
92 result = calloc(sizeof(struct smack_accesses), 1);
100 void smack_accesses_free(struct smack_accesses *handle)
105 struct smack_rule *rule = handle->first;
106 struct smack_rule *next_rule = NULL;
108 while (rule != NULL) {
109 next_rule = rule->next;
117 int smack_accesses_save(struct smack_accesses *handle, int fd)
119 struct smack_rule *rule = handle->first;
128 file = fdopen(newfd, "w");
135 if (rule->is_modify) {
136 ret = fprintf(file, "%s %s %s %s\n",
137 rule->subject, rule->object,
138 rule->allow_access_type,
139 rule->deny_access_type);
141 ret = fprintf(file, "%s %s %s\n",
142 rule->subject, rule->object,
158 int smack_accesses_apply(struct smack_accesses *handle)
160 return accesses_apply(handle, 0);
163 int smack_accesses_clear(struct smack_accesses *handle)
165 return accesses_apply(handle, 1);
168 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
169 const char *object, const char *access_type)
171 struct smack_rule *rule = NULL;
173 if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
174 strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN) {
179 rule = calloc(sizeof(struct smack_rule), 1);
183 strcpy(rule->subject, subject);
184 strcpy(rule->object, object);
185 parse_access_type(access_type, rule->access_type);
187 if (handle->first == NULL) {
188 handle->first = handle->last = rule;
190 handle->last->next = rule;
197 int smack_accesses_add_modify(struct smack_accesses *handle,
200 const char *allow_access_type,
201 const char *deny_access_type)
203 struct smack_rule *rule = NULL;
205 if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
206 strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN) {
211 rule = calloc(sizeof(struct smack_rule), 1);
215 strcpy(rule->subject, subject);
216 strcpy(rule->object, object);
217 parse_access_type(allow_access_type, rule->allow_access_type);
218 parse_access_type(deny_access_type, rule->deny_access_type);
221 if (handle->first == NULL) {
222 handle->first = handle->last = rule;
224 handle->last->next = rule;
231 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
234 char buf[READ_BUF_SIZE];
236 const char *subject, *object, *access, *access2;
244 file = fdopen(newfd, "r");
250 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
251 if (strcmp(buf, "\n") == 0)
253 subject = strtok_r(buf, " \t\n", &ptr);
254 object = strtok_r(NULL, " \t\n", &ptr);
255 access = strtok_r(NULL, " \t\n", &ptr);
256 access2 = strtok_r(NULL, " \t\n", &ptr);
258 if (subject == NULL || object == NULL || access == NULL ||
259 strtok_r(NULL, " \t\n", &ptr) != NULL) {
266 ret = smack_accesses_add(accesses, subject, object, access);
268 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
285 int smack_have_access(const char *subject, const char *object,
286 const char *access_type)
288 char buf[LOAD_LEN + 1];
289 char access_type_k[ACC_LEN + 1];
300 snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
301 fd = open(path, O_RDWR);
306 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
307 fd = open(path, O_RDWR);
313 parse_access_type(access_type, access_type_k);
316 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
317 subject, object, access_type_k);
319 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
320 subject, object, access_type_k);
327 ret = write(fd, buf, strlen(buf));
333 ret = read(fd, buf, 1);
338 return buf[0] == '1';
341 int smack_cipso_new(struct smack_cipso **cipso)
343 struct smack_cipso *result;
345 result = calloc(sizeof(struct smack_cipso), 1);
353 void smack_cipso_free(struct smack_cipso *cipso)
358 struct cipso_mapping *mapping = cipso->first;
359 struct cipso_mapping *next_mapping = NULL;
361 while (mapping != NULL) {
362 next_mapping = mapping->next;
364 mapping = next_mapping;
368 int smack_cipso_apply(struct smack_cipso *cipso)
370 struct cipso_mapping *m = NULL;
371 char buf[CIPSO_MAX_SIZE];
382 snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
383 fd = open(path, O_WRONLY);
387 memset(buf,0,CIPSO_MAX_SIZE);
388 for (m = cipso->first; m != NULL; m = m->next) {
389 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
390 offset += strlen(buf) + 1;
392 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
395 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
398 for (i = 0; i < m->ncats; i++){
399 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
403 if (write(fd, buf, offset) < 0) {
413 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
415 struct cipso_mapping *mapping = NULL;
418 char *label, *level, *cat, *ptr;
427 file = fdopen(newfd, "r");
433 while (fgets(buf, BUF_SIZE, file) != NULL) {
434 mapping = calloc(sizeof(struct cipso_mapping), 1);
438 label = strtok_r(buf, " \t\n", &ptr);
439 level = strtok_r(NULL, " \t\n", &ptr);
440 cat = strtok_r(NULL, " \t\n", &ptr);
441 if (label == NULL || level == NULL ||
442 strlen(label) > SMACK_LABEL_LEN) {
447 strcpy(mapping->label, label);
450 val = strtol(level, NULL, 10);
454 if (val < 0 || val > LEVEL_MAX) {
459 mapping->level = val;
461 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
463 val = strtol(cat, NULL, 10);
467 if (val < 0 || val > CAT_MAX_VALUE) {
472 mapping->cats[i] = val;
474 cat = strtok_r(NULL, " \t\n", &ptr);
479 if (cipso->first == NULL) {
480 cipso->first = cipso->last = mapping;
482 cipso->last->next = mapping;
483 cipso->last = mapping;
498 const char *smack_smackfs_path(void)
503 ssize_t smack_new_label_from_self(char **label)
509 result = calloc(SMACK_LABEL_LEN + 1, 1);
513 fd = open(SELF_LABEL_FILE, O_RDONLY);
519 ret = read(fd, result, SMACK_LABEL_LEN);
530 ssize_t smack_new_label_from_socket(int fd, char **label)
534 socklen_t length = 1;
537 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
538 if (ret < 0 && errno != ERANGE)
541 result = calloc(length + 1, 1);
545 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
555 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
556 int follow, char **label)
562 getxattr(path, xattr, NULL, 0) :
563 lgetxattr(path, xattr, NULL, 0);
564 if (ret < 0 && errno != ERANGE)
567 result = calloc(ret + 1, 1);
572 getxattr(path, xattr, result, ret) :
573 lgetxattr(path, xattr, result, ret);
583 int smack_set_label_for_self(const char *label)
589 len = strnlen(label, SMACK_LABEL_LEN + 1);
590 if (len > SMACK_LABEL_LEN)
593 fd = open(SELF_LABEL_FILE, O_WRONLY);
597 ret = write(fd, label, len);
600 return (ret < 0) ? -1 : 0;
603 int smack_revoke_subject(const char *subject)
610 len = strnlen(subject, SMACK_LABEL_LEN + 1);
611 if (len > SMACK_LABEL_LEN)
614 snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
615 fd = open(path, O_WRONLY);
619 ret = write(fd, subject, len);
622 return (ret < 0) ? -1 : 0;
625 static int accesses_apply(struct smack_accesses *handle, int clear)
627 char buf[LOAD_LEN + 1];
628 struct smack_rule *rule;
641 snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
642 load_fd = open(path, O_WRONLY);
647 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
648 load_fd = open(path, O_WRONLY);
649 /* Try to continue if the file doesn't exist, we might not need it. */
650 if (load_fd < 0 && errno != ENOENT)
655 snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
656 change_fd = open(path, O_WRONLY);
657 /* Try to continue if the file doesn't exist, we might not need it. */
658 if (change_fd < 0 && errno != ENOENT) {
663 for (rule = handle->first; rule != NULL; rule = rule->next) {
665 strcpy(rule->access_type, "-----");
669 if (rule->is_modify) {
671 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
672 rule->subject, rule->object,
673 rule->allow_access_type,
674 rule->deny_access_type);
678 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
679 rule->subject, rule->object,
682 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
683 rule->subject, rule->object,
687 if (ret < 0 || fd < 0) {
692 ret = write(fd, buf, strlen(buf));
708 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
712 for (i = 0; i < ACC_LEN; ++i)
716 for (i = 0; in[i] != '\0'; i++)
743 static int smack_label_length(const char *label)
747 if (!label || label[0] == '\0' || label[0] == '-')
750 for (i = 0; i < (SMACK_LABEL_LEN + 1) && label[i]; i++) {
763 return i < (SMACK_LABEL_LEN + 1) ? i : -1;