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>
26 #include "sys/smack.h"
32 #include <sys/socket.h>
34 #include <sys/types.h>
38 #define LOAD_LEN (2 * (LABEL_LEN + 1) + ACC_LEN)
47 #define KERNEL_FORMAT "%-23s %-23s %5s"
48 #define READ_BUF_SIZE 512
49 #define SMACKFS_MNT "/smack"
50 #define SELF_LABEL_FILE "/proc/self/attr/current"
53 char subject[LABEL_LEN + 1];
54 char object[LABEL_LEN + 1];
56 struct smack_rule *next;
59 struct smack_accesses {
60 struct smack_rule *first;
61 struct smack_rule *last;
64 static int accesses_apply(struct smack_accesses *handle, int clear);
65 static inline int access_type_to_int(const char *access_type);
66 static inline void int_to_access_type_c(unsigned ac, char *str);
67 static inline void int_to_access_type_k(unsigned ac, char *str);
69 int smack_accesses_new(struct smack_accesses **accesses)
71 struct smack_accesses *result;
73 result = calloc(sizeof(struct smack_accesses), 1);
81 int smack_accesses_new_from_file(int fd, struct smack_accesses **accesses)
83 struct smack_accesses *result = NULL;
85 char buf[READ_BUF_SIZE];
87 const char *subject, *object, *access;
90 if (smack_accesses_new(&result))
97 file = fdopen(newfd, "r");
103 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
104 subject = strtok_r(buf, " \t\n", &ptr);
105 object = strtok_r(NULL, " \t\n", &ptr);
106 access = strtok_r(NULL, " \t\n", &ptr);
108 if (subject == NULL || object == NULL || access == NULL ||
109 strtok_r(NULL, " \t\n", &ptr) != NULL) {
114 if (smack_accesses_add(result, subject, object, access))
126 smack_accesses_free(result);
130 void smack_accesses_free(struct smack_accesses *handle)
135 struct smack_rule *rule = handle->first;
136 struct smack_rule *next_rule = NULL;
138 while (rule != NULL) {
139 next_rule = rule->next;
147 int smack_accesses_save(struct smack_accesses *handle, int fd)
149 struct smack_rule *rule = handle->first;
150 char access_type[ACC_LEN + 1];
159 file = fdopen(newfd, "w");
166 int_to_access_type_c(rule->access_code, access_type);
168 ret = fprintf(file, "%s %s %s\n",
169 rule->subject, rule->object, access_type);
182 int smack_accesses_apply(struct smack_accesses *handle)
184 return accesses_apply(handle, 0);
187 int smack_accesses_clear(struct smack_accesses *handle)
189 return accesses_apply(handle, 1);
192 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
193 const char *object, const char *access_type)
195 struct smack_rule *rule = NULL;
197 rule = calloc(sizeof(struct smack_rule), 1);
201 strncpy(rule->subject, subject, LABEL_LEN + 1);
202 strncpy(rule->object, object, LABEL_LEN + 1);
203 rule->access_code = access_type_to_int(access_type);
205 if (handle->first == NULL) {
206 handle->first = handle->last = rule;
208 handle->last->next = rule;
215 int smack_have_access(const char *subject, const char *object,
216 const char *access_type)
218 char buf[LOAD_LEN + 1];
219 char access_type_k[ACC_LEN + 1];
220 unsigned access_code;
224 access_code = access_type_to_int(access_type);
225 int_to_access_type_k(access_code, access_type_k);
227 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
232 fd = open(SMACKFS_MNT "/access", O_RDWR);
236 ret = write(fd, buf, LOAD_LEN);
242 ret = read(fd, buf, 1);
247 return buf[0] == '1';
250 int smack_new_label_from_self(char **label)
256 result = calloc(LABEL_LEN + 1, 1);
260 fd = open(SELF_LABEL_FILE, O_RDONLY);
266 ret = read(fd, result, LABEL_LEN);
277 int smack_new_label_from_socket(int fd, char **label)
281 socklen_t length = 1;
284 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
285 if (ret < 0 && errno != ERANGE)
288 result = calloc(length, 1);
292 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
302 static int accesses_apply(struct smack_accesses *handle, int clear)
304 char buf[LOAD_LEN + 1];
305 char access_type[ACC_LEN + 1];
306 struct smack_rule *rule;
310 fd = open(SMACKFS_MNT "/load", O_WRONLY);
315 strcpy(access_type, "-----");
317 for (rule = handle->first; rule != NULL; rule = rule->next) {
319 int_to_access_type_k(rule->access_code, access_type);
321 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
327 ret = write(fd, buf, LOAD_LEN);
338 static inline int access_type_to_int(const char *access_type)
344 for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
345 switch (access_type[i]) {
373 static inline void int_to_access_type_c(unsigned access, char *str)
377 if ((access & ACC_R) != 0)
379 if ((access & ACC_W) != 0)
381 if ((access & ACC_X) != 0)
383 if ((access & ACC_A) != 0)
385 if ((access & ACC_T) != 0)
390 static inline void int_to_access_type_k(unsigned access, char *str)
392 str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
393 str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
394 str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
395 str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
396 str[4] = ((access & ACC_T) != 0) ? 't' : '-';