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)
177 rule = calloc(sizeof(struct smack_rule), 1);
181 strcpy(rule->subject, subject);
182 strcpy(rule->object, object);
183 parse_access_type(access_type, rule->access_type);
185 if (handle->first == NULL) {
186 handle->first = handle->last = rule;
188 handle->last->next = rule;
195 int smack_accesses_add_modify(struct smack_accesses *handle,
198 const char *allow_access_type,
199 const char *deny_access_type)
201 struct smack_rule *rule = NULL;
203 if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
204 strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN)
207 rule = calloc(sizeof(struct smack_rule), 1);
211 strcpy(rule->subject, subject);
212 strcpy(rule->object, object);
213 parse_access_type(allow_access_type, rule->allow_access_type);
214 parse_access_type(deny_access_type, rule->deny_access_type);
217 if (handle->first == NULL) {
218 handle->first = handle->last = rule;
220 handle->last->next = rule;
227 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
230 char buf[READ_BUF_SIZE];
232 const char *subject, *object, *access, *access2;
240 file = fdopen(newfd, "r");
246 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
247 if (strcmp(buf, "\n") == 0)
249 subject = strtok_r(buf, " \t\n", &ptr);
250 object = strtok_r(NULL, " \t\n", &ptr);
251 access = strtok_r(NULL, " \t\n", &ptr);
252 access2 = strtok_r(NULL, " \t\n", &ptr);
254 if (subject == NULL || object == NULL || access == NULL ||
255 strtok_r(NULL, " \t\n", &ptr) != NULL) {
261 ret = smack_accesses_add(accesses, subject, object, access);
263 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
280 int smack_have_access(const char *subject, const char *object,
281 const char *access_type)
283 char buf[LOAD_LEN + 1];
284 char access_type_k[ACC_LEN + 1];
293 snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
294 fd = open(path, O_RDWR);
299 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
300 fd = open(path, O_RDWR);
306 parse_access_type(access_type, access_type_k);
309 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
310 subject, object, access_type_k);
312 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
313 subject, object, access_type_k);
320 ret = write(fd, buf, strlen(buf));
326 ret = read(fd, buf, 1);
331 return buf[0] == '1';
334 int smack_cipso_new(struct smack_cipso **cipso)
336 struct smack_cipso *result;
338 result = calloc(sizeof(struct smack_cipso), 1);
346 void smack_cipso_free(struct smack_cipso *cipso)
351 struct cipso_mapping *mapping = cipso->first;
352 struct cipso_mapping *next_mapping = NULL;
354 while (mapping != NULL) {
355 next_mapping = mapping->next;
357 mapping = next_mapping;
361 int smack_cipso_apply(struct smack_cipso *cipso)
363 struct cipso_mapping *m = NULL;
364 char buf[CIPSO_MAX_SIZE];
373 snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
374 fd = open(path, O_WRONLY);
378 memset(buf,0,CIPSO_MAX_SIZE);
379 for (m = cipso->first; m != NULL; m = m->next) {
380 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
381 offset += strlen(buf) + 1;
383 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
386 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
389 for (i = 0; i < m->ncats; i++){
390 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
394 if (write(fd, buf, offset) < 0) {
404 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
406 struct cipso_mapping *mapping = NULL;
409 char *label, *level, *cat, *ptr;
418 file = fdopen(newfd, "r");
424 while (fgets(buf, BUF_SIZE, file) != NULL) {
425 mapping = calloc(sizeof(struct cipso_mapping), 1);
429 label = strtok_r(buf, " \t\n", &ptr);
430 level = strtok_r(NULL, " \t\n", &ptr);
431 cat = strtok_r(NULL, " \t\n", &ptr);
432 if (label == NULL || level == NULL ||
433 strlen(label) > SMACK_LABEL_LEN)
436 strcpy(mapping->label, label);
439 val = strtol(level, NULL, 10);
443 if (val < 0 || val > LEVEL_MAX)
446 mapping->level = val;
448 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
450 val = strtol(cat, NULL, 10);
454 if (val < 0 || val > CAT_MAX_VALUE)
457 mapping->cats[i] = val;
459 cat = strtok_r(NULL, " \t\n", &ptr);
464 if (cipso->first == NULL) {
465 cipso->first = cipso->last = mapping;
467 cipso->last->next = mapping;
468 cipso->last = mapping;
483 const char *smack_smackfs_path(void)
488 ssize_t smack_new_label_from_self(char **label)
494 result = calloc(SMACK_LABEL_LEN + 1, 1);
498 fd = open(SELF_LABEL_FILE, O_RDONLY);
504 ret = read(fd, result, SMACK_LABEL_LEN);
515 ssize_t smack_new_label_from_socket(int fd, char **label)
519 socklen_t length = 1;
522 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
523 if (ret < 0 && errno != ERANGE)
526 result = calloc(length + 1, 1);
530 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
540 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
541 int follow, char **label)
547 getxattr(path, xattr, NULL, 0) :
548 lgetxattr(path, xattr, NULL, 0);
549 if (ret < 0 && errno != ERANGE)
552 result = calloc(ret + 1, 1);
557 getxattr(path, xattr, result, ret) :
558 lgetxattr(path, xattr, result, ret);
568 int smack_set_label_for_self(const char *label)
574 len = strnlen(label, SMACK_LABEL_LEN + 1);
575 if (len > SMACK_LABEL_LEN)
578 fd = open(SELF_LABEL_FILE, O_WRONLY);
582 ret = write(fd, label, len);
585 return (ret < 0) ? -1 : 0;
588 int smack_revoke_subject(const char *subject)
595 len = strnlen(subject, SMACK_LABEL_LEN + 1);
596 if (len > SMACK_LABEL_LEN)
599 snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
600 fd = open(path, O_WRONLY);
604 ret = write(fd, subject, len);
607 return (ret < 0) ? -1 : 0;
610 static int accesses_apply(struct smack_accesses *handle, int clear)
612 char buf[LOAD_LEN + 1];
613 struct smack_rule *rule;
624 snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
625 load_fd = open(path, O_WRONLY);
630 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
631 load_fd = open(path, O_WRONLY);
632 /* Try to continue if the file doesn't exist, we might not need it. */
633 if (load_fd < 0 && errno != ENOENT)
638 snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
639 change_fd = open(path, O_WRONLY);
640 /* Try to continue if the file doesn't exist, we might not need it. */
641 if (change_fd < 0 && errno != ENOENT) {
646 for (rule = handle->first; rule != NULL; rule = rule->next) {
648 strcpy(rule->access_type, "-----");
652 if (rule->is_modify) {
654 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
655 rule->subject, rule->object,
656 rule->allow_access_type,
657 rule->deny_access_type);
661 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
662 rule->subject, rule->object,
665 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
666 rule->subject, rule->object,
670 if (ret < 0 || fd < 0) {
675 ret = write(fd, buf, strlen(buf));
691 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
695 for (i = 0; i < ACC_LEN; ++i)
699 for (i = 0; in[i] != '\0'; i++)
726 static int smack_label_length(const char *label)
730 if (!label || label[0] == '\0' || label[0] == '-')
733 for (i = 0; i < (SMACK_LABEL_LEN + 1) && label[i]; i++) {
746 return i < (SMACK_LABEL_LEN + 1) ? i : -1;