Added smack_get_self_label().
[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                 subject = strtok_r(buf, " \t\n", &ptr);
186                 object = strtok_r(NULL, " \t\n", &ptr);
187                 access = strtok_r(NULL, " \t\n", &ptr);
188
189                 if (subject == NULL || object == NULL || access == NULL ||
190                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
191                         errno = EINVAL;
192                         fclose(file);
193                         return -1;
194                 }
195
196                 if (smack_accesses_add(accesses, subject, object, access)) {
197                         fclose(file);
198                         return -1;
199                 }
200         }
201
202         if (ferror(file)) {
203                 fclose(file);
204                 return -1;
205         }
206
207         fclose(file);
208         return 0;
209 }
210
211 int smack_have_access(const char *subject, const char *object,
212                       const char *access_type)
213 {
214         char buf[LOAD_LEN + 1];
215         char access_type_k[ACC_LEN + 1];
216         unsigned access_code;
217         int ret;
218         int fd;
219
220         access_code = access_type_to_int(access_type);
221         int_to_access_type_k(access_code, access_type_k);
222
223         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
224                        access_type_k);
225         if (ret < 0)
226                 return -1;
227
228         fd = open(SMACKFS_MNT "/access", O_RDWR);
229         if (fd < 0)
230                 return -1;
231
232         ret = write(fd, buf, LOAD_LEN);
233         if (ret < 0) {
234                 close(fd);
235                 return -1;
236         }
237
238         ret = read(fd, buf, 1);
239         close(fd);
240         if (ret < 0)
241                 return -1;
242
243         return buf[0] == '1';
244 }
245
246 ssize_t smack_get_self_label(char *buf, size_t count)
247 {
248         int fd;
249         int ret;
250
251         fd = open(SELF_LABEL_FILE, O_RDONLY);
252         if (fd < 0)
253                 return -1;
254
255         ret = read(fd, buf, count);
256         close(fd);
257         if (ret < 0)
258                 return -1;
259
260         return count;
261 }
262
263 int smack_set_self_label(char *label)
264 {
265         int fd;
266
267         fd = open(SELF_LABEL_FILE, O_WRONLY);
268         if (fd < 0)
269                 return -1;
270
271         if (write(fd, label, strlen(label)) < 0) {
272                 close(fd);
273                 return -1;
274         }
275
276         close(fd);
277         return 0;
278 }
279
280 int smack_new_label_from_socket(int fd, char **label)
281 {
282         char dummy;
283         int ret;
284         socklen_t length = 1;
285         char *result;
286
287         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
288         if (ret < 0 && errno != ERANGE)
289                 return -1;
290
291         result = calloc(length, 1);
292         if (result == NULL)
293                 return -1;
294
295         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
296         if (ret < 0) {
297                 free(result);
298                 return -1;
299         }
300
301         *label = result;
302         return 0;
303 }
304
305 static int accesses_apply(struct smack_accesses *handle, int clear)
306 {
307         char buf[LOAD_LEN + 1];
308         char access_type[ACC_LEN + 1];
309         struct smack_rule *rule;
310         int ret;
311         int fd;
312
313         fd = open(SMACKFS_MNT "/load", O_WRONLY);
314         if (fd < 0)
315                 return -1;
316
317         if (clear)
318                 strcpy(access_type, "-----");
319
320         for (rule = handle->first; rule != NULL; rule = rule->next) {
321                 if (!clear)
322                         int_to_access_type_k(rule->access_code, access_type);
323
324                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
325                 if (ret < 0) {
326                         close(fd);
327                         return -1;
328                 }
329
330                 ret = write(fd, buf, LOAD_LEN);
331                 if (ret < 0) {
332                         close(fd);
333                         return -1;
334                 }
335         }
336
337         close(fd);
338         return 0;
339 }
340
341 static inline int access_type_to_int(const char *access_type)
342 {
343         int i;
344         unsigned access;
345
346         access = 0;
347         for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
348                 switch (access_type[i]) {
349                 case 'r':
350                 case 'R':
351                         access |= ACC_R;
352                         break;
353                 case 'w':
354                 case 'W':
355                         access |= ACC_W;
356                         break;
357                 case 'x':
358                 case 'X':
359                         access |= ACC_X;
360                         break;
361                 case 'a':
362                 case 'A':
363                         access |= ACC_A;
364                         break;
365                 case 't':
366                 case 'T':
367                         access |= ACC_T;
368                         break;
369                 default:
370                         break;
371                 }
372
373         return access;
374 }
375
376 static inline void int_to_access_type_c(unsigned access, char *str)
377 {
378         int i;
379         i = 0;
380         if ((access & ACC_R) != 0)
381                 str[i++] = 'r';
382         if ((access & ACC_W) != 0)
383                 str[i++] = 'w';
384         if ((access & ACC_X) != 0)
385                 str[i++] = 'x';
386         if ((access & ACC_A) != 0)
387                 str[i++] = 'a';
388         if ((access & ACC_T) != 0)
389                 str[i++] = 't';
390         str[i] = '\0';
391 }
392
393 static inline void int_to_access_type_k(unsigned access, char *str)
394 {
395         str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
396         str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
397         str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
398         str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
399         str[4] = ((access & ACC_T) != 0) ? 't' : '-';
400         str[5] = '\0';
401 }
402