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_set[ACC_LEN + 1];
61 char access_add[ACC_LEN + 1];
62 char access_del[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->access_add, rule->access_del);
139 ret = fprintf(file, "%s %s %s\n",
140 rule->subject, rule->object,
156 int smack_accesses_apply(struct smack_accesses *handle)
158 return accesses_apply(handle, 0);
161 int smack_accesses_clear(struct smack_accesses *handle)
163 return accesses_apply(handle, 1);
166 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
167 const char *object, const char *access_type)
169 struct smack_rule *rule = NULL;
171 if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
172 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_set);
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, const char *subject,
196 const char *object, const char *access_add, const char *access_del)
198 struct smack_rule *rule = NULL;
200 if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
201 strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN) {
206 rule = calloc(sizeof(struct smack_rule), 1);
210 strcpy(rule->subject, subject);
211 strcpy(rule->object, object);
212 parse_access_type(access_add, rule->access_add);
213 parse_access_type(access_del, rule->access_del);
216 if (handle->first == NULL) {
217 handle->first = handle->last = rule;
219 handle->last->next = rule;
226 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
229 char buf[READ_BUF_SIZE];
231 const char *subject, *object, *access, *access2;
239 file = fdopen(newfd, "r");
245 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
246 if (strcmp(buf, "\n") == 0)
248 subject = strtok_r(buf, " \t\n", &ptr);
249 object = strtok_r(NULL, " \t\n", &ptr);
250 access = strtok_r(NULL, " \t\n", &ptr);
251 access2 = strtok_r(NULL, " \t\n", &ptr);
253 if (subject == NULL || object == NULL || access == NULL ||
254 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];
295 snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
296 fd = open(path, O_RDWR);
301 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
302 fd = open(path, O_RDWR);
308 parse_access_type(access_type, access_type_k);
311 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
312 subject, object, access_type_k);
314 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
315 subject, object, access_type_k);
322 ret = write(fd, buf, strlen(buf));
328 ret = read(fd, buf, 1);
333 return buf[0] == '1';
336 int smack_cipso_new(struct smack_cipso **cipso)
338 struct smack_cipso *result;
340 result = calloc(sizeof(struct smack_cipso), 1);
348 void smack_cipso_free(struct smack_cipso *cipso)
353 struct cipso_mapping *mapping = cipso->first;
354 struct cipso_mapping *next_mapping = NULL;
356 while (mapping != NULL) {
357 next_mapping = mapping->next;
359 mapping = next_mapping;
363 int smack_cipso_apply(struct smack_cipso *cipso)
365 struct cipso_mapping *m = NULL;
366 char buf[CIPSO_MAX_SIZE];
377 snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
378 fd = open(path, O_WRONLY);
382 memset(buf,0,CIPSO_MAX_SIZE);
383 for (m = cipso->first; m != NULL; m = m->next) {
384 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
385 offset += strlen(buf) + 1;
387 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
390 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
393 for (i = 0; i < m->ncats; i++){
394 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
398 if (write(fd, buf, offset) < 0) {
408 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
410 struct cipso_mapping *mapping = NULL;
413 char *label, *level, *cat, *ptr;
422 file = fdopen(newfd, "r");
428 while (fgets(buf, BUF_SIZE, file) != NULL) {
429 mapping = calloc(sizeof(struct cipso_mapping), 1);
433 label = strtok_r(buf, " \t\n", &ptr);
434 level = strtok_r(NULL, " \t\n", &ptr);
435 cat = strtok_r(NULL, " \t\n", &ptr);
436 if (label == NULL || cat == NULL || level == NULL ||
437 strlen(label) > SMACK_LABEL_LEN) {
442 strcpy(mapping->label, label);
445 val = strtol(level, NULL, 10);
449 if (val < 0 || val > LEVEL_MAX) {
454 mapping->level = val;
456 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
458 val = strtol(cat, NULL, 10);
462 if (val < 0 || val > CAT_MAX_VALUE) {
467 mapping->cats[i] = val;
469 cat = strtok_r(NULL, " \t\n", &ptr);
474 if (cipso->first == NULL) {
475 cipso->first = cipso->last = mapping;
477 cipso->last->next = mapping;
478 cipso->last = mapping;
493 const char *smack_smackfs_path(void)
498 ssize_t smack_new_label_from_self(char **label)
504 result = calloc(SMACK_LABEL_LEN + 1, 1);
508 fd = open(SELF_LABEL_FILE, O_RDONLY);
514 ret = read(fd, result, SMACK_LABEL_LEN);
525 ssize_t smack_new_label_from_socket(int fd, char **label)
529 socklen_t length = 1;
532 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
533 if (ret < 0 && errno != ERANGE)
536 result = calloc(length + 1, 1);
540 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
550 ssize_t smack_new_label_from_path(const char *path, const char *xattr,
551 int follow, char **label)
557 getxattr(path, xattr, NULL, 0) :
558 lgetxattr(path, xattr, NULL, 0);
559 if (ret < 0 && errno != ERANGE)
562 result = calloc(ret + 1, 1);
567 getxattr(path, xattr, result, ret) :
568 lgetxattr(path, xattr, result, ret);
578 int smack_set_label_for_self(const char *label)
584 len = strnlen(label, SMACK_LABEL_LEN + 1);
585 if (len > SMACK_LABEL_LEN)
588 fd = open(SELF_LABEL_FILE, O_WRONLY);
592 ret = write(fd, label, len);
595 return (ret < 0) ? -1 : 0;
598 int smack_revoke_subject(const char *subject)
605 len = strnlen(subject, SMACK_LABEL_LEN + 1);
606 if (len > SMACK_LABEL_LEN)
609 snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
610 fd = open(path, O_WRONLY);
614 ret = write(fd, subject, len);
617 return (ret < 0) ? -1 : 0;
620 static int accesses_apply(struct smack_accesses *handle, int clear)
622 char buf[LOAD_LEN + 1];
623 struct smack_rule *rule;
636 snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
637 load_fd = open(path, O_WRONLY);
642 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
643 load_fd = open(path, O_WRONLY);
644 /* Try to continue if the file doesn't exist, we might not need it. */
645 if (load_fd < 0 && errno != ENOENT)
650 snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
651 change_fd = open(path, O_WRONLY);
652 /* Try to continue if the file doesn't exist, we might not need it. */
653 if (change_fd < 0 && errno != ENOENT) {
658 for (rule = handle->first; rule != NULL; rule = rule->next) {
660 strcpy(rule->access_set, "-----");
664 if (rule->is_modify) {
666 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
667 rule->subject, rule->object,
668 rule->access_add, rule->access_del);
672 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
673 rule->subject, rule->object,
676 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
677 rule->subject, rule->object,
681 if (ret < 0 || fd < 0) {
686 ret = write(fd, buf, strlen(buf));
702 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
706 for (i = 0; i < ACC_LEN; ++i)
710 for (i = 0; in[i] != '\0'; i++)