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