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 struct smack_accesses *smack_accesses_new(int fd)
69 struct smack_accesses *rules;
71 char buf[READ_BUF_SIZE];
73 const char *subject, *object, *access;
76 rules = calloc(sizeof(struct smack_accesses), 1);
89 file = fdopen(newfd, "r");
96 while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
97 subject = strtok_r(buf, " \t\n", &ptr);
98 object = strtok_r(NULL, " \t\n", &ptr);
99 access = strtok_r(NULL, " \t\n", &ptr);
101 if (subject == NULL || object == NULL || access == NULL ||
102 strtok_r(NULL, " \t\n", &ptr) != NULL) {
107 if (smack_accesses_add(rules, subject, object, access))
118 smack_accesses_free(rules);
122 void smack_accesses_free(struct smack_accesses *handle)
127 struct smack_rule *rule = handle->first;
128 struct smack_rule *next_rule = NULL;
130 while (rule != NULL) {
131 next_rule = rule->next;
139 int smack_accesses_save(struct smack_accesses *handle, int fd)
141 struct smack_rule *rule = handle->first;
142 char access_type[ACC_LEN + 1];
151 file = fdopen(newfd, "w");
158 int_to_access_type_c(rule->access_code, access_type);
160 ret = fprintf(file, "%s %s %s\n",
161 rule->subject, rule->object, access_type);
174 int smack_accesses_apply(struct smack_accesses *handle, int flags)
176 char buf[LOAD_LEN + 1];
177 char access_type[ACC_LEN + 1];
178 struct smack_rule *rule;
182 fd = open(SMACKFS_MNT "/load", O_WRONLY);
186 if (flags & SMACK_RULE_SET_APPLY_CLEAR)
187 strcpy(access_type, "-----");
189 for (rule = handle->first; rule != NULL; rule = rule->next) {
190 if (!(flags & SMACK_RULE_SET_APPLY_CLEAR))
191 int_to_access_type_k(rule->access_code, access_type);
193 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
199 ret = write(fd, buf, LOAD_LEN);
210 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
211 const char *object, const char *access_type)
213 struct smack_rule *rule = NULL;
215 rule = calloc(sizeof(struct smack_rule), 1);
219 strncpy(rule->subject, subject, LABEL_LEN + 1);
220 strncpy(rule->object, object, LABEL_LEN + 1);
221 rule->access_code = access_type_to_int(access_type);
223 if (handle->first == NULL) {
224 handle->first = handle->last = rule;
226 handle->last->next = rule;
233 int smack_have_access(const char *subject, const char *object,
234 const char *access_type)
236 char buf[LOAD_LEN + 1];
237 char access_type_k[ACC_LEN + 1];
238 unsigned access_code;
242 access_code = access_type_to_int(access_type);
243 int_to_access_type_k(access_code, access_type_k);
245 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
250 fd = open(SMACKFS_MNT "/access", O_RDWR);
254 ret = write(fd, buf, LOAD_LEN);
260 ret = read(fd, buf, 1);
265 return buf[0] == '1';
268 char *smack_get_self_label()
274 label = calloc(LABEL_LEN + 1, 1);
278 fd = open(SELF_LABEL_FILE, O_RDONLY);
284 ret = read(fd, label, LABEL_LEN);
294 char *smack_get_peer_label(int fd)
298 socklen_t length = 1;
301 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
302 if (ret < 0 && errno != ERANGE)
305 label = calloc(length, 1);
309 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, label, &length);
318 inline int access_type_to_int(const char *access_type)
324 for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
325 switch (access_type[i]) {
353 inline void int_to_access_type_c(unsigned access, char *str)
357 if ((access & ACC_R) != 0)
359 if ((access & ACC_W) != 0)
361 if ((access & ACC_X) != 0)
363 if ((access & ACC_A) != 0)
365 if ((access & ACC_T) != 0)
370 inline void int_to_access_type_k(unsigned access, char *str)
372 str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
373 str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
374 str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
375 str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
376 str[4] = ((access & ACC_T) != 0) ? 't' : '-';