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]);
87 int smack_accesses_new(struct smack_accesses **accesses)
89 struct smack_accesses *result;
91 result = calloc(sizeof(struct smack_accesses), 1);
99 void smack_accesses_free(struct smack_accesses *handle)
104 struct smack_rule *rule = handle->first;
105 struct smack_rule *next_rule = NULL;
107 while (rule != NULL) {
108 next_rule = rule->next;
116 int smack_accesses_save(struct smack_accesses *handle, int fd)
118 struct smack_rule *rule = handle->first;
127 file = fdopen(newfd, "w");
134 if (rule->is_modify) {
135 ret = fprintf(file, "%s %s %s %s\n",
136 rule->subject, rule->object,
137 rule->allow_access_type,
138 rule->deny_access_type);
140 ret = fprintf(file, "%s %s %s\n",
141 rule->subject, rule->object,
157 int smack_accesses_apply(struct smack_accesses *handle)
159 return accesses_apply(handle, 0);
162 int smack_accesses_clear(struct smack_accesses *handle)
164 return accesses_apply(handle, 1);
167 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
168 const char *object, const char *access_type)
170 struct smack_rule *rule = NULL;
172 if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
173 strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN) {
178 rule = calloc(sizeof(struct smack_rule), 1);
182 strcpy(rule->subject, subject);
183 strcpy(rule->object, object);
184 parse_access_type(access_type, rule->access_type);
186 if (handle->first == NULL) {
187 handle->first = handle->last = rule;
189 handle->last->next = rule;
196 int smack_accesses_add_modify(struct smack_accesses *handle,
199 const char *allow_access_type,
200 const char *deny_access_type)
202 struct smack_rule *rule = NULL;
204 if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
205 strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN) {
210 rule = calloc(sizeof(struct smack_rule), 1);
214 strcpy(rule->subject, subject);
215 strcpy(rule->object, object);
216 parse_access_type(allow_access_type, rule->allow_access_type);
217 parse_access_type(deny_access_type, rule->deny_access_type);
220 if (handle->first == NULL) {
221 handle->first = handle->last = rule;
223 handle->last->next = rule;
230 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
233 char buf[READ_BUF_SIZE];
235 const char *subject, *object, *access, *access2;
243 file = fdopen(newfd, "r");
249 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
250 if (strcmp(buf, "\n") == 0)
252 subject = strtok_r(buf, " \t\n", &ptr);
253 object = strtok_r(NULL, " \t\n", &ptr);
254 access = strtok_r(NULL, " \t\n", &ptr);
255 access2 = strtok_r(NULL, " \t\n", &ptr);
257 if (subject == NULL || object == NULL || access == NULL ||
258 strtok_r(NULL, " \t\n", &ptr) != NULL) {
265 ret = smack_accesses_add(accesses, subject, object, access);
267 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
284 int smack_have_access(const char *subject, const char *object,
285 const char *access_type)
287 char buf[LOAD_LEN + 1];
288 char access_type_k[ACC_LEN + 1];
299 snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
300 fd = open(path, O_RDWR);
305 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
306 fd = open(path, O_RDWR);
312 parse_access_type(access_type, access_type_k);
315 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
316 subject, object, access_type_k);
318 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
319 subject, object, access_type_k);
326 ret = write(fd, buf, strlen(buf));
332 ret = read(fd, buf, 1);
337 return buf[0] == '1';
340 int smack_cipso_new(struct smack_cipso **cipso)
342 struct smack_cipso *result;
344 result = calloc(sizeof(struct smack_cipso), 1);
352 void smack_cipso_free(struct smack_cipso *cipso)
357 struct cipso_mapping *mapping = cipso->first;
358 struct cipso_mapping *next_mapping = NULL;
360 while (mapping != NULL) {
361 next_mapping = mapping->next;
363 mapping = next_mapping;
367 int smack_cipso_apply(struct smack_cipso *cipso)
369 struct cipso_mapping *m = NULL;
370 char buf[CIPSO_MAX_SIZE];
381 snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
382 fd = open(path, O_WRONLY);
386 memset(buf,0,CIPSO_MAX_SIZE);
387 for (m = cipso->first; m != NULL; m = m->next) {
388 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
389 offset += strlen(buf) + 1;
391 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
394 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
397 for (i = 0; i < m->ncats; i++){
398 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
402 if (write(fd, buf, offset) < 0) {
412 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
414 struct cipso_mapping *mapping = NULL;
417 char *label, *level, *cat, *ptr;
426 file = fdopen(newfd, "r");
432 while (fgets(buf, BUF_SIZE, file) != NULL) {
433 mapping = calloc(sizeof(struct cipso_mapping), 1);
437 label = strtok_r(buf, " \t\n", &ptr);
438 level = strtok_r(NULL, " \t\n", &ptr);
439 cat = strtok_r(NULL, " \t\n", &ptr);
440 if (label == NULL || cat == NULL || level == NULL ||
441 strlen(label) > SMACK_LABEL_LEN) {
446 strcpy(mapping->label, label);
449 val = strtol(level, NULL, 10);
453 if (val < 0 || val > LEVEL_MAX) {
458 mapping->level = val;
460 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
462 val = strtol(cat, NULL, 10);
466 if (val < 0 || val > CAT_MAX_VALUE) {
471 mapping->cats[i] = val;
473 cat = strtok_r(NULL, " \t\n", &ptr);
478 if (cipso->first == NULL) {
479 cipso->first = cipso->last = mapping;
481 cipso->last->next = mapping;
482 cipso->last = mapping;
497 const char *smack_smackfs_path(void)
502 ssize_t smack_new_label_from_self(char **label)
508 result = calloc(SMACK_LABEL_LEN + 1, 1);
512 fd = open(SELF_LABEL_FILE, O_RDONLY);
518 ret = read(fd, result, SMACK_LABEL_LEN);
529 ssize_t smack_new_label_from_socket(int fd, char **label)
533 socklen_t length = 1;
536 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
537 if (ret < 0 && errno != ERANGE)
540 result = calloc(length + 1, 1);
544 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
554 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
555 int follow, char **label)
561 getxattr(path, xattr, NULL, 0) :
562 lgetxattr(path, xattr, NULL, 0);
563 if (ret < 0 && errno != ERANGE)
566 result = calloc(ret + 1, 1);
571 getxattr(path, xattr, result, ret) :
572 lgetxattr(path, xattr, result, ret);
582 int smack_set_label_for_self(const char *label)
588 len = strnlen(label, SMACK_LABEL_LEN + 1);
589 if (len > SMACK_LABEL_LEN)
592 fd = open(SELF_LABEL_FILE, O_WRONLY);
596 ret = write(fd, label, len);
599 return (ret < 0) ? -1 : 0;
602 int smack_revoke_subject(const char *subject)
609 len = strnlen(subject, SMACK_LABEL_LEN + 1);
610 if (len > SMACK_LABEL_LEN)
613 snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
614 fd = open(path, O_WRONLY);
618 ret = write(fd, subject, len);
621 return (ret < 0) ? -1 : 0;
624 static int accesses_apply(struct smack_accesses *handle, int clear)
626 char buf[LOAD_LEN + 1];
627 struct smack_rule *rule;
640 snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
641 load_fd = open(path, O_WRONLY);
646 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
647 load_fd = open(path, O_WRONLY);
648 /* Try to continue if the file doesn't exist, we might not need it. */
649 if (load_fd < 0 && errno != ENOENT)
654 snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
655 change_fd = open(path, O_WRONLY);
656 /* Try to continue if the file doesn't exist, we might not need it. */
657 if (change_fd < 0 && errno != ENOENT) {
662 for (rule = handle->first; rule != NULL; rule = rule->next) {
664 strcpy(rule->access_type, "-----");
668 if (rule->is_modify) {
670 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
671 rule->subject, rule->object,
672 rule->allow_access_type,
673 rule->deny_access_type);
677 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
678 rule->subject, rule->object,
681 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
682 rule->subject, rule->object,
686 if (ret < 0 || fd < 0) {
691 ret = write(fd, buf, strlen(buf));
707 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
711 for (i = 0; i < ACC_LEN; ++i)
715 for (i = 0; in[i] != '\0'; i++)