2 * This file is part of libsmack
4 * Copyright (C) 2010 Nokia Corporation
5 * Copyright (C) 2011 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
23 * Brian McGillion <brian.mcgillion@intel.com>
24 * Passion Zhao <passion.zhao@intel.com>
27 #include "sys/smack.h"
33 #include <sys/socket.h>
35 #include <sys/types.h>
41 #define LOAD_LEN (2 * (LABEL_LEN + 1) + ACC_LEN)
46 #define CAT_MAX_COUNT 240
47 #define CAT_MAX_VALUE 63
48 #define CIPSO_POS(i) (LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
49 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
50 #define CIPSO_NUM_LEN_STR "%-4d"
58 #define KERNEL_LONG_FORMAT "%s %s %s"
59 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5s"
60 #define READ_BUF_SIZE LOAD_LEN + 1
61 #define SELF_LABEL_FILE "/proc/self/attr/current"
63 extern char *smack_mnt;
66 char subject[LABEL_LEN + 1];
67 char object[LABEL_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[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 int access_type_to_int(const char *access_type);
92 static inline void int_to_access_type_c(unsigned ac, char *str);
93 static inline void int_to_access_type_k(unsigned ac, char *str);
95 int smack_accesses_new(struct smack_accesses **accesses)
97 struct smack_accesses *result;
99 result = calloc(sizeof(struct smack_accesses), 1);
107 void smack_accesses_free(struct smack_accesses *handle)
112 struct smack_rule *rule = handle->first;
113 struct smack_rule *next_rule = NULL;
115 while (rule != NULL) {
116 next_rule = rule->next;
124 int smack_accesses_save(struct smack_accesses *handle, int fd)
126 struct smack_rule *rule = handle->first;
127 char access_type[ACC_LEN + 1];
136 file = fdopen(newfd, "w");
143 int_to_access_type_c(rule->access_code, access_type);
145 ret = fprintf(file, "%s %s %s\n",
146 rule->subject, rule->object, access_type);
159 int smack_accesses_apply(struct smack_accesses *handle)
161 return accesses_apply(handle, 0);
164 int smack_accesses_clear(struct smack_accesses *handle)
166 return accesses_apply(handle, 1);
169 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
170 const char *object, const char *access_type)
172 struct smack_rule *rule = NULL;
174 rule = calloc(sizeof(struct smack_rule), 1);
178 strncpy(rule->subject, subject, LABEL_LEN + 1);
179 strncpy(rule->object, object, LABEL_LEN + 1);
180 rule->access_code = access_type_to_int(access_type);
182 if (handle->first == NULL) {
183 handle->first = handle->last = rule;
185 handle->last->next = rule;
192 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
195 char buf[READ_BUF_SIZE];
197 const char *subject, *object, *access;
204 file = fdopen(newfd, "r");
210 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
211 if (strcmp(buf, "\n") == 0)
213 subject = strtok_r(buf, " \t\n", &ptr);
214 object = strtok_r(NULL, " \t\n", &ptr);
215 access = strtok_r(NULL, " \t\n", &ptr);
217 if (subject == NULL || object == NULL || access == NULL ||
218 strtok_r(NULL, " \t\n", &ptr) != NULL) {
224 if (smack_accesses_add(accesses, subject, object, access)) {
239 int smack_have_access(const char *subject, const char *object,
240 const char *access_type)
242 char buf[LOAD_LEN + 1];
243 char access_type_k[ACC_LEN + 1];
244 unsigned access_code;
255 snprintf(path, sizeof path, "%s/access2", smack_mnt);
256 fd = open(path, O_RDWR);
261 snprintf(path, sizeof path, "%s/access", smack_mnt);
262 fd = open(path, O_RDWR);
268 access_code = access_type_to_int(access_type);
269 int_to_access_type_k(access_code, access_type_k);
272 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
273 subject, object, access_type_k);
275 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
276 subject, object, access_type_k);
283 ret = write(fd, buf, strlen(buf));
289 ret = read(fd, buf, 1);
294 return buf[0] == '1';
296 void smack_cipso_free(struct smack_cipso *cipso)
301 struct cipso_mapping *mapping = cipso->first;
302 struct cipso_mapping *next_mapping = NULL;
304 while (mapping != NULL) {
305 next_mapping = mapping->next;
307 mapping = next_mapping;
311 struct smack_cipso *smack_cipso_new(int fd)
313 struct smack_cipso *cipso = NULL;
314 struct cipso_mapping *mapping = NULL;
317 char *label, *level, *cat, *ptr;
326 file = fdopen(newfd, "r");
332 cipso = calloc(sizeof(struct smack_cipso ), 1);
338 while (fgets(buf, BUF_SIZE, file) != NULL) {
339 mapping = calloc(sizeof(struct cipso_mapping), 1);
343 label = strtok_r(buf, " \t\n", &ptr);
344 level = strtok_r(NULL, " \t\n", &ptr);
345 cat = strtok_r(NULL, " \t\n", &ptr);
346 if (label == NULL || cat == NULL || level == NULL ||
347 strlen(label) > LABEL_LEN) {
352 strcpy(mapping->label, label);
355 val = strtol(level, NULL, 10);
359 if (val < 0 || val > LEVEL_MAX) {
364 mapping->level = val;
366 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
368 val = strtol(cat, NULL, 10);
372 if (val < 0 || val > CAT_MAX_VALUE) {
377 mapping->cats[i] = val;
379 cat = strtok_r(NULL, " \t\n", &ptr);
384 if (cipso->first == NULL) {
385 cipso->first = cipso->last = mapping;
387 cipso->last->next = mapping;
388 cipso->last = mapping;
399 smack_cipso_free(cipso);
404 const char *smack_smackfs_path(void)
409 int smack_cipso_apply(struct smack_cipso *cipso)
411 struct cipso_mapping *m = NULL;
412 char buf[CIPSO_MAX_SIZE];
422 snprintf(path, sizeof path, "%s/cipso2", smack_mnt);
423 fd = open(path, O_WRONLY);
427 for (m = cipso->first; m != NULL; m = m->next) {
428 sprintf(buf, "%s ", m->label);
429 sprintf(&buf[LABEL_LEN + 1], CIPSO_NUM_LEN_STR, m->level);
430 sprintf(&buf[LABEL_LEN + 1 + NUM_LEN], CIPSO_NUM_LEN_STR, m->ncats);
432 for (i = 0; i < m->ncats; i++)
433 sprintf(&buf[CIPSO_POS(i)], CIPSO_NUM_LEN_STR, m->cats[i]);
435 if (write(fd, buf, strlen(buf)) < 0) {
445 int smack_new_label_from_self(char **label)
451 result = calloc(LABEL_LEN + 1, 1);
455 fd = open(SELF_LABEL_FILE, O_RDONLY);
461 ret = read(fd, result, LABEL_LEN);
472 int smack_new_label_from_socket(int fd, char **label)
476 socklen_t length = 1;
479 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
480 if (ret < 0 && errno != ERANGE)
483 result = calloc(length + 1, 1);
487 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
497 static int accesses_apply(struct smack_accesses *handle, int clear)
499 char buf[LOAD_LEN + 1];
500 char access_type[ACC_LEN + 1];
501 struct smack_rule *rule;
512 snprintf(path, sizeof path, "%s/load2", smack_mnt);
513 fd = open(path, O_WRONLY);
518 snprintf(path, sizeof path, "%s/load", smack_mnt);
519 fd = open(path, O_WRONLY);
526 strcpy(access_type, "-----");
528 for (rule = handle->first; rule != NULL; rule = rule->next) {
530 int_to_access_type_k(rule->access_code, access_type);
533 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
534 rule->subject, rule->object,
537 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
538 rule->subject, rule->object,
545 ret = write(fd, buf, strlen(buf));
556 static inline int access_type_to_int(const char *access_type)
562 for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
563 switch (access_type[i]) {
591 static inline void int_to_access_type_c(unsigned access, char *str)
595 if ((access & ACC_R) != 0)
597 if ((access & ACC_W) != 0)
599 if ((access & ACC_X) != 0)
601 if ((access & ACC_A) != 0)
603 if ((access & ACC_T) != 0)
608 static inline void int_to_access_type_k(unsigned access, char *str)
610 str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
611 str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
612 str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
613 str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
614 str[4] = ((access & ACC_T) != 0) ? 't' : '-';