source code open - smack
[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  * Copyright (C) 2012 Samsung Electronics Co.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  * Authors:
23  * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
24  * Brian McGillion <brian.mcgillion@intel.com>
25  * Rafal Krypa <r.krypa@samsung.com>
26  */
27
28 #include "sys/smack.h"
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/xattr.h>
38 #include <unistd.h>
39
40 #define ACC_LEN 5
41 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
42
43 #define KERNEL_FORMAT "%s %s %s"
44 #define KERNEL_FORMAT_MODIFY "%s %s %s %s"
45 #define READ_BUF_SIZE LOAD_LEN + 10
46 #define SMACKFS_MNT "/smack"
47 #define SELF_LABEL_FILE "/proc/self/attr/current"
48
49 typedef int (*getxattr_func)(void*, const char*, void*, size_t);
50 typedef int (*setxattr_func)(const void*, const char*, const void*, size_t, int);
51 typedef int (*removexattr_func)(void*, const char*);
52
53 struct smack_rule {
54         char subject[SMACK_LABEL_LEN + 1];
55         char object[SMACK_LABEL_LEN + 1];
56         int is_modify;
57         char access_set[ACC_LEN + 1];
58         char access_add[ACC_LEN + 1];
59         char access_del[ACC_LEN + 1];
60         struct smack_rule *next;
61 };
62
63 struct smack_accesses {
64         struct smack_rule *first;
65         struct smack_rule *last;
66 };
67
68 static int accesses_apply(struct smack_accesses *handle, int clear);
69 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1]);
70 static inline char* get_xattr_name(enum smack_label_type type);
71
72 int smack_accesses_new(struct smack_accesses **accesses)
73 {
74         struct smack_accesses *result;
75
76         result = calloc(sizeof(struct smack_accesses), 1);
77         if (result == NULL)
78                 return -1;
79
80         *accesses = result;
81         return 0;
82 }
83
84 void smack_accesses_free(struct smack_accesses *handle)
85 {
86         if (handle == NULL)
87                 return;
88
89         struct smack_rule *rule = handle->first;
90         struct smack_rule *next_rule = NULL;
91
92         while (rule != NULL) {
93                 next_rule = rule->next;
94                 free(rule);
95                 rule = next_rule;
96         }
97
98         free(handle);
99 }
100
101 int smack_accesses_save(struct smack_accesses *handle, int fd)
102 {
103         struct smack_rule *rule = handle->first;
104         FILE *file;
105         int ret;
106         int newfd;
107
108         newfd = dup(fd);
109         if (newfd == -1)
110                 return -1;
111
112         file = fdopen(newfd, "w");
113         if (file == NULL) {
114                 close(newfd);
115                 return -1;
116         }
117
118         while (rule) {
119                 if (rule->is_modify) {
120                         ret = fprintf(file, "%s %s %s %s\n",
121                                       rule->subject, rule->object,
122                                       rule->access_add, rule->access_del);
123                 } else {
124                         ret = fprintf(file, "%s %s %s\n",
125                                       rule->subject, rule->object,
126                                       rule->access_set);
127                 }
128
129                 if (ret < 0) {
130                         fclose(file);
131                         return -1;
132                 }
133
134                 rule = rule->next;
135         }
136
137         fclose(file);
138         return 0;
139 }
140
141 int smack_accesses_apply(struct smack_accesses *handle)
142 {
143         return accesses_apply(handle, 0);
144 }
145
146 int smack_accesses_clear(struct smack_accesses *handle)
147 {
148         return accesses_apply(handle, 1);
149 }
150
151 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
152                        const char *object, const char *access_type)
153 {
154         struct smack_rule *rule = NULL;
155
156         rule = calloc(sizeof(struct smack_rule), 1);
157         if (rule == NULL)
158                 return -1;
159
160         strncpy(rule->subject, subject, SMACK_LABEL_LEN + 1);
161         strncpy(rule->object, object, SMACK_LABEL_LEN + 1);
162         parse_access_type(access_type, rule->access_set);
163
164         if (handle->first == NULL) {
165                 handle->first = handle->last = rule;
166         } else {
167                 handle->last->next = rule;
168                 handle->last = rule;
169         }
170
171         return 0;
172 }
173
174 int smack_accesses_add_modify(struct smack_accesses *handle, const char *subject,
175                        const char *object, const char *access_add, const char *access_del)
176 {
177         struct smack_rule *rule = NULL;
178
179         rule = calloc(sizeof(struct smack_rule), 1);
180         if (rule == NULL)
181                 return -1;
182
183         strncpy(rule->subject, subject, SMACK_LABEL_LEN + 1);
184         strncpy(rule->object, object, SMACK_LABEL_LEN + 1);
185         parse_access_type(access_add, rule->access_add);
186         parse_access_type(access_del, rule->access_del);
187         rule->is_modify = 1;
188
189         if (handle->first == NULL) {
190                 handle->first = handle->last = rule;
191         } else {
192                 handle->last->next = rule;
193                 handle->last = rule;
194         }
195
196         return 0;
197 }
198
199 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
200 {
201         FILE *file = NULL;
202         char buf[READ_BUF_SIZE];
203         char *ptr;
204         const char *subject, *object, *access, *access2;
205         int newfd;
206         int ret;
207
208         newfd = dup(fd);
209         if (newfd == -1)
210                 return -1;
211
212         file = fdopen(newfd, "r");
213         if (file == NULL) {
214                 close(newfd);
215                 return -1;
216         }
217
218         while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
219                 if (strcmp(buf, "\n") == 0)
220                         continue;
221                 subject = strtok_r(buf, " \t\n", &ptr);
222                 object = strtok_r(NULL, " \t\n", &ptr);
223                 access = strtok_r(NULL, " \t\n", &ptr);
224                 access2 = strtok_r(NULL, " \t\n", &ptr);
225
226                 if (subject == NULL || object == NULL || access == NULL ||
227                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
228                         errno = EINVAL;
229                         fclose(file);
230                         return -1;
231                 }
232
233                 if (access2 == NULL)
234                         ret = smack_accesses_add(accesses, subject, object, access);
235                 else
236                         ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
237
238                 if (ret) {
239                         fclose(file);
240                         return -1;
241                 }
242         }
243
244         if (ferror(file)) {
245                 fclose(file);
246                 return -1;
247         }
248
249         fclose(file);
250         return 0;
251 }
252
253 int smack_have_access(const char *subject, const char *object,
254                       const char *access_type)
255 {
256         char buf[LOAD_LEN + 1];
257         char access_type_k[ACC_LEN + 1];
258         int ret;
259         int fd;
260
261         parse_access_type(access_type, access_type_k);
262
263         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, subject, object,
264                        access_type_k);
265         if (ret < 0)
266                 return -1;
267
268         fd = open(SMACKFS_MNT "/access2", O_RDWR);
269         if (fd < 0)
270                 return -1;
271
272         ret = write(fd, buf, LOAD_LEN);
273         if (ret < 0) {
274                 close(fd);
275                 return -1;
276         }
277
278         ret = read(fd, buf, 1);
279         close(fd);
280         if (ret < 0)
281                 return -1;
282
283         return buf[0] == '1';
284 }
285
286 int smack_new_label_from_self(char **label)
287 {
288         char *result;
289         int fd;
290         int ret;
291
292         result = calloc(SMACK_LABEL_LEN + 1, 1);
293         if (result == NULL)
294                 return -1;
295
296         fd = open(SELF_LABEL_FILE, O_RDONLY);
297         if (fd < 0) {
298                 free(result);
299                 return -1;
300         }
301
302         ret = read(fd, result, SMACK_LABEL_LEN);
303         close(fd);
304         if (ret < 0) {
305                 free(result);
306                 return -1;
307         }
308
309         *label = result;
310         return 0;
311 }
312
313 int smack_new_label_from_socket(int fd, char **label)
314 {
315         char dummy;
316         int ret;
317         socklen_t length = 1;
318         char *result;
319
320         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
321         if (ret < 0 && errno != ERANGE)
322                 return -1;
323
324         result = calloc(length, 1);
325         if (result == NULL)
326                 return -1;
327
328         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
329         if (ret < 0) {
330                 free(result);
331                 return -1;
332         }
333
334         *label = result;
335         return 0;
336 }
337
338 int smack_set_label_for_self(const char *label)
339 {
340         int len;
341         int fd;
342         int ret;
343
344         len = strnlen(label, SMACK_LABEL_LEN + 1);
345         if (len > SMACK_LABEL_LEN)
346                 return -1;
347
348         fd = open(SELF_LABEL_FILE, O_WRONLY);
349         if (fd < 0)
350                 return -1;
351
352         ret = write(fd, label, len);
353         close(fd);
354
355         return (ret < 0) ? -1 : 0;
356 }
357
358 int smack_revoke_subject(const char *subject)
359 {
360         int ret;
361         int fd;
362
363         fd = open(SMACKFS_MNT "/revoke-subject", O_RDWR);
364         if (fd < 0)
365                 return -1;
366
367         ret = write(fd, subject, strnlen(subject, SMACK_LABEL_LEN));
368         close(fd);
369
370         return (ret < 0) ? -1 : 0;
371 }
372
373 static int internal_getlabel(void* file, char** label,
374                 enum smack_label_type type,
375                 getxattr_func getfunc)
376 {
377         char* xattr_name = get_xattr_name(type);
378         char value[SMACK_LABEL_LEN + 1];
379         int ret;
380
381         ret = getfunc(file, xattr_name, value, SMACK_LABEL_LEN + 1);
382         if (ret == -1) {
383                 if (errno == ENODATA) {
384                         *label = NULL;
385                         return 0;
386                 }
387                 return -1;
388         }
389
390         value[ret] = '\0';
391         *label = calloc(ret + 1, 1);
392         if (*label == NULL)
393                 return -1;
394         strncpy(*label, value, ret);
395         return 0;
396 }
397
398 static int internal_setlabel(void* file, const char* label,
399                 enum smack_label_type type,
400                 setxattr_func setfunc, removexattr_func removefunc)
401 {
402         char* xattr_name = get_xattr_name(type);
403
404         /* Check validity of labels for LABEL_TRANSMUTE */
405         if (type == SMACK_LABEL_TRANSMUTE && label != NULL) {
406                 if (!strcmp(label, "0"))
407                         label = NULL;
408                 else if (!strcmp(label, "1"))
409                         label = "TRUE";
410                 else
411                         return -1;
412         }
413
414         if (label == NULL || label[0] == '\0') {
415                 return removefunc(file, label);
416         } else {
417                 int len = strnlen(label, SMACK_LABEL_LEN + 1);
418                 if (len > SMACK_LABEL_LEN)
419                         return -1;
420                 return setfunc(file, xattr_name, label, len, 0);
421         }
422 }
423
424 int smack_getlabel(const char *path, char** label,
425                 enum smack_label_type type)
426 {
427         return internal_getlabel((void*) path, label, type,
428                         (getxattr_func) getxattr);
429 }
430
431 int smack_lgetlabel(const char *path, char** label,
432                 enum smack_label_type type)
433 {
434         return internal_getlabel((void*) path, label, type,
435                         (getxattr_func) lgetxattr);
436 }
437
438 int smack_fgetlabel(int fd, char** label,
439                 enum smack_label_type type)
440 {
441         return internal_getlabel((void*) fd, label, type,
442                         (getxattr_func) fgetxattr);
443 }
444
445 int smack_setlabel(const char *path, const char* label,
446                 enum smack_label_type type)
447 {
448         return internal_setlabel((void*) path, label, type,
449                         (setxattr_func) setxattr, (removexattr_func) removexattr);
450 }
451
452 int smack_lsetlabel(const char *path, const char* label,
453                 enum smack_label_type type)
454 {
455         return internal_setlabel((void*) path, label, type,
456                         (setxattr_func) lsetxattr, (removexattr_func) lremovexattr);
457 }
458
459 int smack_fsetlabel(int fd, const char* label,
460                 enum smack_label_type type)
461 {
462         return internal_setlabel((void*) fd, label, type,
463                         (setxattr_func) fsetxattr, (removexattr_func) fremovexattr);
464 }
465
466 static int accesses_apply(struct smack_accesses *handle, int clear)
467 {
468         char buf[LOAD_LEN + 1];
469         struct smack_rule *rule;
470         int ret;
471         int fd;
472         int load_fd;
473         int change_fd;
474         int size;
475
476         load_fd = open(SMACKFS_MNT "/load2", O_WRONLY);
477         if (load_fd < 0)
478                 return -1;
479
480         change_fd = open(SMACKFS_MNT "/change-rule", O_WRONLY);
481         if (change_fd < 0) {
482                 close(load_fd);
483                 return -1;
484         }
485
486         for (rule = handle->first; rule != NULL; rule = rule->next) {
487                 fd = load_fd;
488                 if (clear) {
489                         size = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT, rule->subject, rule->object, "-----");
490                 } else {
491
492                         if (rule->is_modify) {
493                                 fd = change_fd;
494                                 size = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT_MODIFY,
495                                                 rule->subject, rule->object, rule->access_add, rule->access_del);
496
497                         } else {
498                                 size = snprintf(buf, LOAD_LEN + 1, KERNEL_FORMAT,
499                                                 rule->subject, rule->object, rule->access_set);
500                         }
501                 }
502
503                 if (size == -1 || size > LOAD_LEN) {
504                         close(load_fd);
505                         close(change_fd);
506                         return -1;
507                 }
508
509                 ret = write(fd, buf, size);
510                 if (ret < 0) {
511                         close(load_fd);
512                         close(change_fd);
513                         return -1;
514                 }
515         }
516
517         close(load_fd);
518         close(change_fd);
519         return 0;
520 }
521
522 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
523 {
524         int i;
525
526         for (i = 0; i < ACC_LEN; ++i)
527                 out[i] = '-';
528         out[ACC_LEN] = '\0';
529
530         for (i = 0; i < ACC_LEN && in[i] != '\0'; i++)
531                 switch (in[i]) {
532                 case 'r':
533                 case 'R':
534                         out[0] = 'r';
535                         break;
536                 case 'w':
537                 case 'W':
538                         out[1] = 'w';
539                         break;
540                 case 'x':
541                 case 'X':
542                         out[2] = 'x';
543                         break;
544                 case 'a':
545                 case 'A':
546                         out[3] = 'a';
547                         break;
548                 case 't':
549                 case 'T':
550                         out[4] = 't';
551                         break;
552                 default:
553                         break;
554                 }
555 }
556
557 static inline char* get_xattr_name(enum smack_label_type type)
558 {
559         switch (type) {
560         case SMACK_LABEL_ACCESS:
561                 return "security.SMACK64";
562         case SMACK_LABEL_EXEC:
563                 return "security.SMACK64EXEC";
564         case SMACK_LABEL_MMAP:
565                 return "security.SMACK64MMAP";
566         case SMACK_LABEL_TRANSMUTE:
567                 return "security.SMACK64TRANSMUTE";
568         case SMACK_LABEL_IPIN:
569                 return "security.SMACK64IPIN";
570         case SMACK_LABEL_IPOUT:
571                 return "security.SMACK64IPOUT";
572         default:
573                 /* Should not reach this point */
574                 return NULL;
575         }
576
577 }