upload tizen1.0 source
[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 int smack_new_label_from_self(char **label)
247 {
248         char *result;
249         int fd;
250         int ret;
251
252         result = calloc(LABEL_LEN + 1, 1);
253         if (result == NULL)
254                 return -1;
255
256         fd = open(SELF_LABEL_FILE, O_RDONLY);
257         if (fd < 0) {
258                 free(result);
259                 return -1;
260         }
261
262         ret = read(fd, result, LABEL_LEN);
263         close(fd);
264         if (ret < 0) {
265                 free(result);
266                 return -1;
267         }
268
269         *label = result;
270         return 0;
271 }
272
273 int smack_new_label_from_socket(int fd, char **label)
274 {
275         char dummy;
276         int ret;
277         socklen_t length = 1;
278         char *result;
279
280         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
281         if (ret < 0 && errno != ERANGE)
282                 return -1;
283
284         result = calloc(length, 1);
285         if (result == NULL)
286                 return -1;
287
288         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
289         if (ret < 0) {
290                 free(result);
291                 return -1;
292         }
293
294         *label = result;
295         return 0;
296 }
297
298 static int accesses_apply(struct smack_accesses *handle, int clear)
299 {
300         char buf[LOAD_LEN + 1];
301         char access_type[ACC_LEN + 1];
302         struct smack_rule *rule;
303         int ret;
304         int fd;
305
306         fd = open(SMACKFS_MNT "/load", O_WRONLY);
307         if (fd < 0)
308                 return -1;
309
310         if (clear)
311                 strcpy(access_type, "-----");
312
313         for (rule = handle->first; rule != NULL; rule = rule->next) {
314                 if (!clear)
315                         int_to_access_type_k(rule->access_code, access_type);
316
317                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, access_type);
318                 if (ret < 0) {
319                         close(fd);
320                         return -1;
321                 }
322
323                 ret = write(fd, buf, LOAD_LEN);
324                 if (ret < 0) {
325                         close(fd);
326                         return -1;
327                 }
328         }
329
330         close(fd);
331         return 0;
332 }
333
334 static inline int access_type_to_int(const char *access_type)
335 {
336         int i;
337         unsigned access;
338
339         access = 0;
340         for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
341                 switch (access_type[i]) {
342                 case 'r':
343                 case 'R':
344                         access |= ACC_R;
345                         break;
346                 case 'w':
347                 case 'W':
348                         access |= ACC_W;
349                         break;
350                 case 'x':
351                 case 'X':
352                         access |= ACC_X;
353                         break;
354                 case 'a':
355                 case 'A':
356                         access |= ACC_A;
357                         break;
358                 case 't':
359                 case 'T':
360                         access |= ACC_T;
361                         break;
362                 default:
363                         break;
364                 }
365
366         return access;
367 }
368
369 static inline void int_to_access_type_c(unsigned access, char *str)
370 {
371         int i;
372         i = 0;
373         if ((access & ACC_R) != 0)
374                 str[i++] = 'r';
375         if ((access & ACC_W) != 0)
376                 str[i++] = 'w';
377         if ((access & ACC_X) != 0)
378                 str[i++] = 'x';
379         if ((access & ACC_A) != 0)
380                 str[i++] = 'a';
381         if ((access & ACC_T) != 0)
382                 str[i++] = 't';
383         str[i] = '\0';
384 }
385
386 static inline void int_to_access_type_k(unsigned access, char *str)
387 {
388         str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
389         str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
390         str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
391         str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
392         str[4] = ((access & ACC_T) != 0) ? 't' : '-';
393         str[5] = '\0';
394 }
395