adjusted apply/clear API
[framework/security/smack.git] / libsmack / libsmack.c
1 /*
2  * This file is part of libsmack
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  * Copyright (C) 2011 Intel Corporation
6  *
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.
10  *
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.
15  *
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
19  * 02110-1301 USA
20  *
21  * Authors:
22  * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
23  * Brian McGillion <brian.mcgillion@intel.com>
24  */
25
26 #include "libsmack.h"
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35
36 #define LABEL_LEN 23
37 #define LOAD_LEN (2 * (LABEL_LEN + 1) + ACC_LEN)
38 #define ACC_LEN 5
39
40 #define ACC_R 0x01
41 #define ACC_W 0x02
42 #define ACC_X 0x04
43 #define ACC_A 0x08
44 #define ACC_T 0x10
45
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"
50
51 struct smack_rule {
52         char subject[LABEL_LEN + 1];
53         char object[LABEL_LEN + 1];
54         int access_code;
55         struct smack_rule *next;
56 };
57
58 struct smack_accesses {
59         struct smack_rule *first;
60         struct smack_rule *last;
61 };
62
63 static int accesses_apply(struct smack_accesses *handle, int clear);
64 static inline int access_type_to_int(const char *access_type);
65 static inline void int_to_access_type_c(unsigned ac, char *str);
66 static inline void int_to_access_type_k(unsigned ac, char *str);
67
68 int smack_accesses_new(struct smack_accesses **accesses)
69 {
70         struct smack_accesses *result;
71
72         result = calloc(sizeof(struct smack_accesses), 1);
73         if (result == NULL)
74                 return -1;
75
76         *accesses = result;
77         return 0;
78 }
79
80 int smack_accesses_new_from_file(int fd, struct smack_accesses **accesses)
81 {
82         struct smack_accesses *result = NULL;
83         FILE *file = NULL;
84         char buf[READ_BUF_SIZE];
85         char *ptr;
86         const char *subject, *object, *access;
87         int newfd;
88
89         if (smack_accesses_new(&result))
90                 goto err_out;
91
92         newfd = dup(fd);
93         if (newfd == -1)
94                 goto err_out;
95
96         file = fdopen(newfd, "r");
97         if (file == NULL) {
98                 close(newfd);
99                 goto err_out;
100         }
101
102         while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
103                 subject = strtok_r(buf, " \t\n", &ptr);
104                 object = strtok_r(NULL, " \t\n", &ptr);
105                 access = strtok_r(NULL, " \t\n", &ptr);
106
107                 if (subject == NULL || object == NULL || access == NULL ||
108                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
109                         errno = EINVAL;
110                         goto err_out;
111                 }
112
113                 if (smack_accesses_add(result, subject, object, access))
114                         goto err_out;
115         }
116
117         if (ferror(file))
118                 goto err_out;
119
120         fclose(file);
121         *accesses = result;
122         return 0;
123 err_out:
124         fclose(file);
125         smack_accesses_free(result);
126         return -1;
127 }
128
129 void smack_accesses_free(struct smack_accesses *handle)
130 {
131         if (handle == NULL)
132                 return;
133
134         struct smack_rule *rule = handle->first;
135         struct smack_rule *next_rule = NULL;
136
137         while (rule != NULL) {
138                 next_rule = rule->next;
139                 free(rule);
140                 rule = next_rule;
141         }
142
143         free(handle);
144 }
145
146 int smack_accesses_save(struct smack_accesses *handle, int fd)
147 {
148         struct smack_rule *rule = handle->first;
149         char access_type[ACC_LEN + 1];
150         FILE *file;
151         int ret;
152         int newfd;
153
154         newfd = dup(fd);
155         if (newfd == -1)
156                 return -1;
157
158         file = fdopen(newfd, "w");
159         if (file == NULL) {
160                 close(newfd);
161                 return -1;
162         }
163
164         while (rule) {
165                 int_to_access_type_c(rule->access_code, access_type);
166
167                 ret = fprintf(file, "%s %s %s\n",
168                               rule->subject, rule->object, access_type);
169                 if (ret < 0) {
170                         fclose(file);
171                         return -1;
172                 }
173
174                 rule = rule->next;
175         }
176
177         fclose(file);
178         return 0;
179 }
180
181 int smack_accesses_apply(struct smack_accesses *handle)
182 {
183         accesses_apply(handle, 0);
184 }
185
186 int smack_accesses_clear(struct smack_accesses *handle)
187 {
188         accesses_apply(handle, 1);
189 }
190
191 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
192                        const char *object, const char *access_type)
193 {
194         struct smack_rule *rule = NULL;
195
196         rule = calloc(sizeof(struct smack_rule), 1);
197         if (rule == NULL)
198                 return -1;
199
200         strncpy(rule->subject, subject, LABEL_LEN + 1);
201         strncpy(rule->object, object, LABEL_LEN + 1);
202         rule->access_code = access_type_to_int(access_type);
203
204         if (handle->first == NULL) {
205                 handle->first = handle->last = rule;
206         } else {
207                 handle->last->next = rule;
208                 handle->last = rule;
209         }
210
211         return 0;
212 }
213
214 int smack_have_access(const char *subject, const char *object,
215                       const char *access_type)
216 {
217         char buf[LOAD_LEN + 1];
218         char access_type_k[ACC_LEN + 1];
219         unsigned access_code;
220         int ret;
221         int fd;
222
223         access_code = access_type_to_int(access_type);
224         int_to_access_type_k(access_code, access_type_k);
225
226         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
227                        access_type_k);
228         if (ret < 0)
229                 return -1;
230
231         fd = open(SMACKFS_MNT "/access", O_RDWR);
232         if (fd < 0)
233                 return -1;
234
235         ret = write(fd, buf, LOAD_LEN);
236         if (ret < 0) {
237                 close(fd);
238                 return -1;
239         }
240
241         ret = read(fd, buf, 1);
242         close(fd);
243         if (ret < 0)
244                 return -1;
245
246         return buf[0] == '1';
247 }
248
249 int smack_new_label_from_self(char **label)
250 {
251         char *result;
252         int fd;
253         int ret;
254
255         result = calloc(LABEL_LEN + 1, 1);
256         if (result == NULL)
257                 return -1;
258
259         fd = open(SELF_LABEL_FILE, O_RDONLY);
260         if (fd < 0) {
261                 free(result);
262                 return -1;
263         }
264
265         ret = read(fd, result, LABEL_LEN);
266         close(fd);
267         if (ret < 0) {
268                 free(result);
269                 return -1;
270         }
271
272         *label = result;
273         return 0;
274 }
275
276 int smack_new_label_from_socket(int fd, char **label)
277 {
278         char dummy;
279         int ret;
280         socklen_t length = 1;
281         char *result;
282
283         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
284         if (ret < 0 && errno != ERANGE)
285                 return -1;
286
287         result = calloc(length, 1);
288         if (result == NULL)
289                 return -1;
290
291         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
292         if (ret < 0) {
293                 free(result);
294                 return -1;
295         }
296
297         *label = result;
298         return 0;
299 }
300
301 static int accesses_apply(struct smack_accesses *handle, int clear)
302 {
303         char buf[LOAD_LEN + 1];
304         char access_type[ACC_LEN + 1];
305         struct smack_rule *rule;
306         int ret;
307         int fd;
308
309         fd = open(SMACKFS_MNT "/load", O_WRONLY);
310         if (fd < 0)
311                 return -1;
312
313         if (clear)
314                 strcpy(access_type, "-----");
315
316         for (rule = handle->first; rule != NULL; rule = rule->next) {
317                 if (!clear)
318                         int_to_access_type_k(rule->access_code, access_type);
319
320                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
321                 if (ret < 0) {
322                         close(fd);
323                         return -1;
324                 }
325
326                 ret = write(fd, buf, LOAD_LEN);
327                 if (ret < 0) {
328                         close(fd);
329                         return -1;
330                 }
331         }
332
333         close(fd);
334         return 0;
335 }
336
337 static inline int access_type_to_int(const char *access_type)
338 {
339         int i, count;
340         unsigned access;
341
342         access = 0;
343         for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
344                 switch (access_type[i]) {
345                 case 'r':
346                 case 'R':
347                         access |= ACC_R;
348                         break;
349                 case 'w':
350                 case 'W':
351                         access |= ACC_W;
352                         break;
353                 case 'x':
354                 case 'X':
355                         access |= ACC_X;
356                         break;
357                 case 'a':
358                 case 'A':
359                         access |= ACC_A;
360                         break;
361                 case 't':
362                 case 'T':
363                         access |= ACC_T;
364                         break;
365                 default:
366                         break;
367                 }
368
369         return access;
370 }
371
372 static inline void int_to_access_type_c(unsigned access, char *str)
373 {
374         int i;
375         i = 0;
376         if ((access & ACC_R) != 0)
377                 str[i++] = 'r';
378         if ((access & ACC_W) != 0)
379                 str[i++] = 'w';
380         if ((access & ACC_X) != 0)
381                 str[i++] = 'x';
382         if ((access & ACC_A) != 0)
383                 str[i++] = 'a';
384         if ((access & ACC_T) != 0)
385                 str[i++] = 't';
386         str[i] = '\0';
387 }
388
389 static inline void int_to_access_type_k(unsigned access, char *str)
390 {
391         str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
392         str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
393         str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
394         str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
395         str[4] = ((access & ACC_T) != 0) ? 't' : '-';
396         str[5] = '\0';
397 }
398