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 void smack_accesses_free(struct smack_accesses *handle)
86 struct smack_rule *rule = handle->first;
87 struct smack_rule *next_rule = NULL;
89 while (rule != NULL) {
90 next_rule = rule->next;
98 int smack_accesses_save(struct smack_accesses *handle, int fd)
100 struct smack_rule *rule = handle->first;
101 char access_type[ACC_LEN + 1];
110 file = fdopen(newfd, "w");
117 int_to_access_type_c(rule->access_code, access_type);
119 ret = fprintf(file, "%s %s %s\n",
120 rule->subject, rule->object, access_type);
133 int smack_accesses_apply(struct smack_accesses *handle)
135 return accesses_apply(handle, 0);
138 int smack_accesses_clear(struct smack_accesses *handle)
140 return accesses_apply(handle, 1);
143 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
144 const char *object, const char *access_type)
146 struct smack_rule *rule = NULL;
148 rule = calloc(sizeof(struct smack_rule), 1);
152 strncpy(rule->subject, subject, LABEL_LEN + 1);
153 strncpy(rule->object, object, LABEL_LEN + 1);
154 rule->access_code = access_type_to_int(access_type);
156 if (handle->first == NULL) {
157 handle->first = handle->last = rule;
159 handle->last->next = rule;
166 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
169 char buf[READ_BUF_SIZE];
171 const char *subject, *object, *access;
178 file = fdopen(newfd, "r");
184 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
185 subject = strtok_r(buf, " \t\n", &ptr);
186 object = strtok_r(NULL, " \t\n", &ptr);
187 access = strtok_r(NULL, " \t\n", &ptr);
189 if (subject == NULL || object == NULL || access == NULL ||
190 strtok_r(NULL, " \t\n", &ptr) != NULL) {
196 if (smack_accesses_add(accesses, subject, object, access)) {
211 int smack_have_access(const char *subject, const char *object,
212 const char *access_type)
214 char buf[LOAD_LEN + 1];
215 char access_type_k[ACC_LEN + 1];
216 unsigned access_code;
220 access_code = access_type_to_int(access_type);
221 int_to_access_type_k(access_code, access_type_k);
223 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
228 fd = open(SMACKFS_MNT "/access", O_RDWR);
232 ret = write(fd, buf, LOAD_LEN);
238 ret = read(fd, buf, 1);
243 return buf[0] == '1';
246 int smack_new_label_from_self(char **label)
252 result = calloc(LABEL_LEN + 1, 1);
256 fd = open(SELF_LABEL_FILE, O_RDONLY);
262 ret = read(fd, result, LABEL_LEN);
273 int smack_new_label_from_socket(int fd, char **label)
277 socklen_t length = 1;
280 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
281 if (ret < 0 && errno != ERANGE)
284 result = calloc(length, 1);
288 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
298 static int accesses_apply(struct smack_accesses *handle, int clear)
300 char buf[LOAD_LEN + 1];
301 char access_type[ACC_LEN + 1];
302 struct smack_rule *rule;
306 fd = open(SMACKFS_MNT "/load", O_WRONLY);
311 strcpy(access_type, "-----");
313 for (rule = handle->first; rule != NULL; rule = rule->next) {
315 int_to_access_type_k(rule->access_code, access_type);
317 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
323 ret = write(fd, buf, LOAD_LEN);
334 static inline int access_type_to_int(const char *access_type)
340 for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
341 switch (access_type[i]) {
369 static inline void int_to_access_type_c(unsigned access, char *str)
373 if ((access & ACC_R) != 0)
375 if ((access & ACC_W) != 0)
377 if ((access & ACC_X) != 0)
379 if ((access & ACC_A) != 0)
381 if ((access & ACC_T) != 0)
386 static inline void int_to_access_type_k(unsigned access, char *str)
388 str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
389 str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
390 str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
391 str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
392 str[4] = ((access & ACC_T) != 0) ? 't' : '-';