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 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
24 * Brian McGillion <brian.mcgillion@intel.com>
25 * Passion Zhao <passion.zhao@intel.com>
26 * Rafal Krypa <r.krypa@samsung.com>
29 #include "sys/smack.h"
35 #include <sys/socket.h>
37 #include <sys/types.h>
40 #include <attr/xattr.h>
43 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
48 #define CAT_MAX_COUNT 240
49 #define CAT_MAX_VALUE 63
50 #define CIPSO_POS(i) (SMACK_LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
51 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
52 #define CIPSO_NUM_LEN_STR "%-4d"
54 #define KERNEL_LONG_FORMAT "%s %s %s"
55 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5s"
56 #define KERNEL_MODIFY_FORMAT "%s %s %s %s"
57 #define READ_BUF_SIZE LOAD_LEN + 1
58 #define SELF_LABEL_FILE "/proc/self/attr/current"
60 extern char *smack_mnt;
63 char subject[SMACK_LABEL_LEN + 1];
64 char object[SMACK_LABEL_LEN + 1];
66 char access_set[ACC_LEN + 1];
67 char access_add[ACC_LEN + 1];
68 char access_del[ACC_LEN + 1];
69 struct smack_rule *next;
72 struct smack_accesses {
73 struct smack_rule *first;
74 struct smack_rule *last;
77 struct cipso_mapping {
78 char label[SMACK_LABEL_LEN + 1];
79 int cats[CAT_MAX_VALUE];
82 struct cipso_mapping *next;
86 struct cipso_mapping *first;
87 struct cipso_mapping *last;
90 static int accesses_apply(struct smack_accesses *handle, int clear);
91 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1]);
93 int smack_accesses_new(struct smack_accesses **accesses)
95 struct smack_accesses *result;
97 result = calloc(sizeof(struct smack_accesses), 1);
105 void smack_accesses_free(struct smack_accesses *handle)
110 struct smack_rule *rule = handle->first;
111 struct smack_rule *next_rule = NULL;
113 while (rule != NULL) {
114 next_rule = rule->next;
122 int smack_accesses_save(struct smack_accesses *handle, int fd)
124 struct smack_rule *rule = handle->first;
133 file = fdopen(newfd, "w");
140 if (rule->is_modify) {
141 ret = fprintf(file, "%s %s %s %s\n",
142 rule->subject, rule->object,
143 rule->access_add, rule->access_del);
145 ret = fprintf(file, "%s %s %s\n",
146 rule->subject, rule->object,
162 int smack_accesses_apply(struct smack_accesses *handle)
164 return accesses_apply(handle, 0);
167 int smack_accesses_clear(struct smack_accesses *handle)
169 return accesses_apply(handle, 1);
172 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
173 const char *object, const char *access_type)
175 struct smack_rule *rule = NULL;
177 rule = calloc(sizeof(struct smack_rule), 1);
181 strncpy(rule->subject, subject, SMACK_LABEL_LEN + 1);
182 strncpy(rule->object, object, SMACK_LABEL_LEN + 1);
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 rule = calloc(sizeof(struct smack_rule), 1);
204 strncpy(rule->subject, subject, SMACK_LABEL_LEN + 1);
205 strncpy(rule->object, object, SMACK_LABEL_LEN + 1);
206 parse_access_type(access_add, rule->access_add);
207 parse_access_type(access_del, rule->access_del);
210 if (handle->first == NULL) {
211 handle->first = handle->last = rule;
213 handle->last->next = rule;
220 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
223 char buf[READ_BUF_SIZE];
225 const char *subject, *object, *access, *access2;
233 file = fdopen(newfd, "r");
239 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
240 if (strcmp(buf, "\n") == 0)
242 subject = strtok_r(buf, " \t\n", &ptr);
243 object = strtok_r(NULL, " \t\n", &ptr);
244 access = strtok_r(NULL, " \t\n", &ptr);
245 access2 = strtok_r(NULL, " \t\n", &ptr);
247 if (subject == NULL || object == NULL || access == NULL ||
248 strtok_r(NULL, " \t\n", &ptr) != NULL) {
255 ret = smack_accesses_add(accesses, subject, object, access);
257 ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
274 int smack_have_access(const char *subject, const char *object,
275 const char *access_type)
277 char buf[LOAD_LEN + 1];
278 char access_type_k[ACC_LEN + 1];
289 snprintf(path, sizeof path, "%s/access2", smack_mnt);
290 fd = open(path, O_RDWR);
295 snprintf(path, sizeof path, "%s/access", smack_mnt);
296 fd = open(path, O_RDWR);
302 parse_access_type(access_type, access_type_k);
305 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
306 subject, object, access_type_k);
308 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
309 subject, object, access_type_k);
316 ret = write(fd, buf, strlen(buf));
322 ret = read(fd, buf, 1);
327 return buf[0] == '1';
330 int smack_cipso_new(struct smack_cipso **cipso)
332 struct smack_cipso *result;
334 result = calloc(sizeof(struct smack_cipso), 1);
342 void smack_cipso_free(struct smack_cipso *cipso)
347 struct cipso_mapping *mapping = cipso->first;
348 struct cipso_mapping *next_mapping = NULL;
350 while (mapping != NULL) {
351 next_mapping = mapping->next;
353 mapping = next_mapping;
357 int smack_cipso_apply(struct smack_cipso *cipso)
359 struct cipso_mapping *m = NULL;
360 char buf[CIPSO_MAX_SIZE];
371 snprintf(path, sizeof path, "%s/cipso2", smack_mnt);
372 fd = open(path, O_WRONLY);
376 memset(buf,0,CIPSO_MAX_SIZE);
377 for (m = cipso->first; m != NULL; m = m->next) {
378 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
379 offset += strlen(buf) + 1;
381 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
384 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
387 for (i = 0; i < m->ncats; i++){
388 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
392 if (write(fd, buf, offset) < 0) {
402 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
404 struct cipso_mapping *mapping = NULL;
407 char *label, *level, *cat, *ptr;
416 file = fdopen(newfd, "r");
422 while (fgets(buf, BUF_SIZE, file) != NULL) {
423 mapping = calloc(sizeof(struct cipso_mapping), 1);
427 label = strtok_r(buf, " \t\n", &ptr);
428 level = strtok_r(NULL, " \t\n", &ptr);
429 cat = strtok_r(NULL, " \t\n", &ptr);
430 if (label == NULL || cat == NULL || level == NULL ||
431 strlen(label) > SMACK_LABEL_LEN) {
436 strcpy(mapping->label, label);
439 val = strtol(level, NULL, 10);
443 if (val < 0 || val > LEVEL_MAX) {
448 mapping->level = val;
450 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
452 val = strtol(cat, NULL, 10);
456 if (val < 0 || val > CAT_MAX_VALUE) {
461 mapping->cats[i] = val;
463 cat = strtok_r(NULL, " \t\n", &ptr);
468 if (cipso->first == NULL) {
469 cipso->first = cipso->last = mapping;
471 cipso->last->next = mapping;
472 cipso->last = mapping;
487 const char *smack_smackfs_path(void)
492 int smack_new_label_from_self(char **label)
498 result = calloc(SMACK_LABEL_LEN + 1, 1);
502 fd = open(SELF_LABEL_FILE, O_RDONLY);
508 ret = read(fd, result, SMACK_LABEL_LEN);
519 int smack_new_label_from_socket(int fd, char **label)
523 socklen_t length = 1;
526 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
527 if (ret < 0 && errno != ERANGE)
530 result = calloc(length + 1, 1);
534 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
544 int smack_new_label_from_path(const char *path, const char *xattr, int follow,
551 getxattr(path, xattr, NULL, 0) :
552 lgetxattr(path, xattr, NULL, 0);
553 if (ret < 0 && errno != ERANGE)
556 result = calloc(ret + 1, 1);
561 getxattr(path, xattr, result, ret) :
562 lgetxattr(path, xattr, result, ret);
573 int smack_set_label_for_self(const char *label)
579 len = strnlen(label, SMACK_LABEL_LEN + 1);
580 if (len > SMACK_LABEL_LEN)
583 fd = open(SELF_LABEL_FILE, O_WRONLY);
587 ret = write(fd, label, len);
590 return (ret < 0) ? -1 : 0;
593 int smack_revoke_subject(const char *subject)
600 len = strnlen(subject, SMACK_LABEL_LEN + 1);
601 if (len > SMACK_LABEL_LEN)
604 snprintf(path, sizeof path, "%s/revoke-subject", smack_mnt);
605 fd = open(path, O_WRONLY);
609 ret = write(fd, subject, len);
612 return (ret < 0) ? -1 : 0;
615 static int accesses_apply(struct smack_accesses *handle, int clear)
617 char buf[LOAD_LEN + 1];
618 struct smack_rule *rule;
631 snprintf(path, sizeof path, "%s/load2", smack_mnt);
632 load_fd = open(path, O_WRONLY);
637 snprintf(path, sizeof path, "%s/load", smack_mnt);
638 load_fd = open(path, O_WRONLY);
639 /* Try to continue if the file doesn't exist, we might not need it. */
640 if (load_fd < 0 && errno != ENOENT)
645 snprintf(path, sizeof path, "%s/change-rule", smack_mnt);
646 change_fd = open(path, O_WRONLY);
647 /* Try to continue if the file doesn't exist, we might not need it. */
648 if (change_fd < 0 && errno != ENOENT) {
653 for (rule = handle->first; rule != NULL; rule = rule->next) {
655 strcpy(rule->access_set, "-----");
659 if (rule->is_modify) {
661 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
662 rule->subject, rule->object,
663 rule->access_add, rule->access_del);
667 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
668 rule->subject, rule->object,
671 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
672 rule->subject, rule->object,
676 if (ret < 0 || fd < 0) {
681 ret = write(fd, buf, strlen(buf));
697 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
701 for (i = 0; i < ACC_LEN; ++i)
705 for (i = 0; in[i] != '\0'; i++)