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>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #define LOAD_LEN (2 * (LABEL_LEN + 1) + ACC_LEN)
46 #define KERNEL_FORMAT "%-23s %-23s %5s"
47 #define READ_BUF_SIZE 512
48 #define SMACKFS_MNT "/smack"
49 #define SELF_LABEL_FILE "/proc/self/attr/current"
52 char subject[LABEL_LEN + 1];
53 char object[LABEL_LEN + 1];
55 struct smack_rule *next;
58 struct smack_accesses {
59 struct smack_rule *first;
60 struct smack_rule *last;
63 inline int access_type_to_int(const char *access_type);
64 inline void int_to_access_type_c(unsigned ac, char *str);
65 inline void int_to_access_type_k(unsigned ac, char *str);
67 int smack_accesses_new(struct smack_accesses **accesses)
69 struct smack_accesses *result;
71 result = calloc(sizeof(struct smack_accesses), 1);
79 int smack_accesses_new_from_file(int fd, struct smack_accesses **accesses)
81 struct smack_accesses *result = NULL;
83 char buf[READ_BUF_SIZE];
85 const char *subject, *object, *access;
88 if (smack_accesses_new(&result))
95 file = fdopen(newfd, "r");
101 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
102 subject = strtok_r(buf, " \t\n", &ptr);
103 object = strtok_r(NULL, " \t\n", &ptr);
104 access = strtok_r(NULL, " \t\n", &ptr);
106 if (subject == NULL || object == NULL || access == NULL ||
107 strtok_r(NULL, " \t\n", &ptr) != NULL) {
112 if (smack_accesses_add(result, subject, object, access))
124 smack_accesses_free(result);
128 void smack_accesses_free(struct smack_accesses *handle)
133 struct smack_rule *rule = handle->first;
134 struct smack_rule *next_rule = NULL;
136 while (rule != NULL) {
137 next_rule = rule->next;
145 int smack_accesses_save(struct smack_accesses *handle, int fd)
147 struct smack_rule *rule = handle->first;
148 char access_type[ACC_LEN + 1];
157 file = fdopen(newfd, "w");
164 int_to_access_type_c(rule->access_code, access_type);
166 ret = fprintf(file, "%s %s %s\n",
167 rule->subject, rule->object, access_type);
180 int smack_accesses_apply(struct smack_accesses *handle, int flags)
182 char buf[LOAD_LEN + 1];
183 char access_type[ACC_LEN + 1];
184 struct smack_rule *rule;
188 fd = open(SMACKFS_MNT "/load", O_WRONLY);
192 if (flags & SMACK_RULE_SET_APPLY_CLEAR)
193 strcpy(access_type, "-----");
195 for (rule = handle->first; rule != NULL; rule = rule->next) {
196 if (!(flags & SMACK_RULE_SET_APPLY_CLEAR))
197 int_to_access_type_k(rule->access_code, access_type);
199 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
205 ret = write(fd, buf, LOAD_LEN);
216 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
217 const char *object, const char *access_type)
219 struct smack_rule *rule = NULL;
221 rule = calloc(sizeof(struct smack_rule), 1);
225 strncpy(rule->subject, subject, LABEL_LEN + 1);
226 strncpy(rule->object, object, LABEL_LEN + 1);
227 rule->access_code = access_type_to_int(access_type);
229 if (handle->first == NULL) {
230 handle->first = handle->last = rule;
232 handle->last->next = rule;
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;
248 access_code = access_type_to_int(access_type);
249 int_to_access_type_k(access_code, access_type_k);
251 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
256 fd = open(SMACKFS_MNT "/access", O_RDWR);
260 ret = write(fd, buf, LOAD_LEN);
266 ret = read(fd, buf, 1);
271 return buf[0] == '1';
274 char *smack_get_self_label()
280 label = calloc(LABEL_LEN + 1, 1);
284 fd = open(SELF_LABEL_FILE, O_RDONLY);
290 ret = read(fd, label, LABEL_LEN);
300 char *smack_get_peer_label(int fd)
304 socklen_t length = 1;
307 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
308 if (ret < 0 && errno != ERANGE)
311 label = calloc(length, 1);
315 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, label, &length);
324 inline int access_type_to_int(const char *access_type)
330 for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
331 switch (access_type[i]) {
359 inline void int_to_access_type_c(unsigned access, char *str)
363 if ((access & ACC_R) != 0)
365 if ((access & ACC_W) != 0)
367 if ((access & ACC_X) != 0)
369 if ((access & ACC_A) != 0)
371 if ((access & ACC_T) != 0)
376 inline void int_to_access_type_k(unsigned access, char *str)
378 str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
379 str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
380 str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
381 str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
382 str[4] = ((access & ACC_T) != 0) ? 't' : '-';