Adjusted self label and peer label 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 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);
66
67 int smack_accesses_new(struct smack_accesses **accesses)
68 {
69         struct smack_accesses *result;
70
71         result = calloc(sizeof(struct smack_accesses), 1);
72         if (result == NULL)
73                 return -1;
74
75         *accesses = result;
76         return 0;
77 }
78
79 int smack_accesses_new_from_file(int fd, struct smack_accesses **accesses)
80 {
81         struct smack_accesses *result = NULL;
82         FILE *file = NULL;
83         char buf[READ_BUF_SIZE];
84         char *ptr;
85         const char *subject, *object, *access;
86         int newfd;
87
88         if (smack_accesses_new(&result))
89                 goto err_out;
90
91         newfd = dup(fd);
92         if (newfd == -1)
93                 goto err_out;
94
95         file = fdopen(newfd, "r");
96         if (file == NULL) {
97                 close(newfd);
98                 goto err_out;
99         }
100
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);
105
106                 if (subject == NULL || object == NULL || access == NULL ||
107                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
108                         errno = EINVAL;
109                         goto err_out;
110                 }
111
112                 if (smack_accesses_add(result, subject, object, access))
113                         goto err_out;
114         }
115
116         if (ferror(file))
117                 goto err_out;
118
119         fclose(file);
120         *accesses = result;
121         return 0;
122 err_out:
123         fclose(file);
124         smack_accesses_free(result);
125         return -1;
126 }
127
128 void smack_accesses_free(struct smack_accesses *handle)
129 {
130         if (handle == NULL)
131                 return;
132
133         struct smack_rule *rule = handle->first;
134         struct smack_rule *next_rule = NULL;
135
136         while (rule != NULL) {
137                 next_rule = rule->next;
138                 free(rule);
139                 rule = next_rule;
140         }
141
142         free(handle);
143 }
144
145 int smack_accesses_save(struct smack_accesses *handle, int fd)
146 {
147         struct smack_rule *rule = handle->first;
148         char access_type[ACC_LEN + 1];
149         FILE *file;
150         int ret;
151         int newfd;
152
153         newfd = dup(fd);
154         if (newfd == -1)
155                 return -1;
156
157         file = fdopen(newfd, "w");
158         if (file == NULL) {
159                 close(newfd);
160                 return -1;
161         }
162
163         while (rule) {
164                 int_to_access_type_c(rule->access_code, access_type);
165
166                 ret = fprintf(file, "%s %s %s\n",
167                               rule->subject, rule->object, access_type);
168                 if (ret < 0) {
169                         fclose(file);
170                         return -1;
171                 }
172
173                 rule = rule->next;
174         }
175
176         fclose(file);
177         return 0;
178 }
179
180 int smack_accesses_apply(struct smack_accesses *handle, int flags)
181 {
182         char buf[LOAD_LEN + 1];
183         char access_type[ACC_LEN + 1];
184         struct smack_rule *rule;
185         int ret;
186         int fd;
187
188         fd = open(SMACKFS_MNT "/load", O_WRONLY);
189         if (fd < 0)
190                 return -1;
191
192         if (flags & SMACK_RULE_SET_APPLY_CLEAR)
193                 strcpy(access_type, "-----");
194
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);
198
199                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
200                 if (ret < 0) {
201                         close(fd);
202                         return -1;
203                 }
204
205                 ret = write(fd, buf, LOAD_LEN);
206                 if (ret < 0) {
207                         close(fd);
208                         return -1;
209                 }
210         }
211
212         close(fd);
213         return 0;
214 }
215
216 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
217                        const char *object, const char *access_type)
218 {
219         struct smack_rule *rule = NULL;
220
221         rule = calloc(sizeof(struct smack_rule), 1);
222         if (rule == NULL)
223                 return -1;
224
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);
228
229         if (handle->first == NULL) {
230                 handle->first = handle->last = rule;
231         } else {
232                 handle->last->next = rule;
233                 handle->last = rule;
234         }
235
236         return 0;
237 }
238
239 int smack_have_access(const char *subject, const char *object,
240                       const char *access_type)
241 {
242         char buf[LOAD_LEN + 1];
243         char access_type_k[ACC_LEN + 1];
244         unsigned access_code;
245         int ret;
246         int fd;
247
248         access_code = access_type_to_int(access_type);
249         int_to_access_type_k(access_code, access_type_k);
250
251         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
252                        access_type_k);
253         if (ret < 0)
254                 return -1;
255
256         fd = open(SMACKFS_MNT "/access", O_RDWR);
257         if (fd < 0)
258                 return -1;
259
260         ret = write(fd, buf, LOAD_LEN);
261         if (ret < 0) {
262                 close(fd);
263                 return -1;
264         }
265
266         ret = read(fd, buf, 1);
267         close(fd);
268         if (ret < 0)
269                 return -1;
270
271         return buf[0] == '1';
272 }
273
274 int smack_new_label_from_self(char **label)
275 {
276         char *result;
277         int fd;
278         int ret;
279
280         result = calloc(LABEL_LEN + 1, 1);
281         if (result == NULL)
282                 return -1;
283
284         fd = open(SELF_LABEL_FILE, O_RDONLY);
285         if (fd < 0) {
286                 free(result);
287                 return -1;
288         }
289
290         ret = read(fd, result, LABEL_LEN);
291         close(fd);
292         if (ret < 0) {
293                 free(result);
294                 return -1;
295         }
296
297         *label = result;
298         return 0;
299 }
300
301 int smack_new_label_from_socket(int fd, char **label)
302 {
303         char dummy;
304         int ret;
305         socklen_t length = 1;
306         char *result;
307
308         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
309         if (ret < 0 && errno != ERANGE)
310                 return -1;
311
312         result = calloc(length, 1);
313         if (result == NULL)
314                 return -1;
315
316         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
317         if (ret < 0) {
318                 free(result);
319                 return -1;
320         }
321
322         *label = result;
323         return 0;
324 }
325
326 inline int access_type_to_int(const char *access_type)
327 {
328         int i, count;
329         unsigned access;
330
331         access = 0;
332         for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
333                 switch (access_type[i]) {
334                 case 'r':
335                 case 'R':
336                         access |= ACC_R;
337                         break;
338                 case 'w':
339                 case 'W':
340                         access |= ACC_W;
341                         break;
342                 case 'x':
343                 case 'X':
344                         access |= ACC_X;
345                         break;
346                 case 'a':
347                 case 'A':
348                         access |= ACC_A;
349                         break;
350                 case 't':
351                 case 'T':
352                         access |= ACC_T;
353                         break;
354                 default:
355                         break;
356                 }
357
358         return access;
359 }
360
361 inline void int_to_access_type_c(unsigned access, char *str)
362 {
363         int i;
364         i = 0;
365         if ((access & ACC_R) != 0)
366                 str[i++] = 'r';
367         if ((access & ACC_W) != 0)
368                 str[i++] = 'w';
369         if ((access & ACC_X) != 0)
370                 str[i++] = 'x';
371         if ((access & ACC_A) != 0)
372                 str[i++] = 'a';
373         if ((access & ACC_T) != 0)
374                 str[i++] = 't';
375         str[i] = '\0';
376 }
377
378 inline void int_to_access_type_k(unsigned access, char *str)
379 {
380         str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
381         str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
382         str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
383         str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
384         str[4] = ((access & ACC_T) != 0) ? 't' : '-';
385         str[5] = '\0';
386 }
387