Cleaned up init code.
[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
23 #include "sys/smack.h"
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <sys/xattr.h>
35
36 #define ACC_LEN 5
37 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
38
39 #define LEVEL_MAX 255
40 #define NUM_LEN 4
41 #define BUF_SIZE 512
42 #define CAT_MAX_COUNT 240
43 #define CAT_MAX_VALUE 63
44 #define CIPSO_POS(i)   (SMACK_LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
45 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
46 #define CIPSO_NUM_LEN_STR "%-4d"
47
48 #define KERNEL_LONG_FORMAT "%s %s %s"
49 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5s"
50 #define KERNEL_MODIFY_FORMAT "%s %s %s %s"
51 #define READ_BUF_SIZE LOAD_LEN + 1
52 #define SELF_LABEL_FILE "/proc/self/attr/current"
53
54 extern char *smackfs_mnt;
55
56 struct smack_rule {
57         char subject[SMACK_LABEL_LEN + 1];
58         char object[SMACK_LABEL_LEN + 1];
59         int is_modify;
60         char access_set[ACC_LEN + 1];
61         char access_add[ACC_LEN + 1];
62         char access_del[ACC_LEN + 1];
63         struct smack_rule *next;
64 };
65
66 struct smack_accesses {
67         struct smack_rule *first;
68         struct smack_rule *last;
69 };
70
71 struct cipso_mapping {
72         char label[SMACK_LABEL_LEN + 1];
73         int cats[CAT_MAX_VALUE];
74         int ncats;
75         int level;
76         struct cipso_mapping *next;
77 };
78
79 struct smack_cipso {
80         struct cipso_mapping *first;
81         struct cipso_mapping *last;
82 };
83
84 static int accesses_apply(struct smack_accesses *handle, int clear);
85 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1]);
86
87 int smack_accesses_new(struct smack_accesses **accesses)
88 {
89         struct smack_accesses *result;
90
91         result = calloc(sizeof(struct smack_accesses), 1);
92         if (result == NULL)
93                 return -1;
94
95         *accesses = result;
96         return 0;
97 }
98
99 void smack_accesses_free(struct smack_accesses *handle)
100 {
101         if (handle == NULL)
102                 return;
103
104         struct smack_rule *rule = handle->first;
105         struct smack_rule *next_rule = NULL;
106
107         while (rule != NULL) {
108                 next_rule = rule->next;
109                 free(rule);
110                 rule = next_rule;
111         }
112
113         free(handle);
114 }
115
116 int smack_accesses_save(struct smack_accesses *handle, int fd)
117 {
118         struct smack_rule *rule = handle->first;
119         FILE *file;
120         int ret;
121         int newfd;
122
123         newfd = dup(fd);
124         if (newfd == -1)
125                 return -1;
126
127         file = fdopen(newfd, "w");
128         if (file == NULL) {
129                 close(newfd);
130                 return -1;
131         }
132
133         while (rule) {
134                 if (rule->is_modify) {
135                         ret = fprintf(file, "%s %s %s %s\n",
136                                       rule->subject, rule->object,
137                                       rule->access_add, rule->access_del);
138                 } else {
139                         ret = fprintf(file, "%s %s %s\n",
140                                       rule->subject, rule->object,
141                                       rule->access_set);
142                 }
143
144                 if (ret < 0) {
145                         fclose(file);
146                         return -1;
147                 }
148
149                 rule = rule->next;
150         }
151
152         fclose(file);
153         return 0;
154 }
155
156 int smack_accesses_apply(struct smack_accesses *handle)
157 {
158         return accesses_apply(handle, 0);
159 }
160
161 int smack_accesses_clear(struct smack_accesses *handle)
162 {
163         return accesses_apply(handle, 1);
164 }
165
166 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
167                        const char *object, const char *access_type)
168 {
169         struct smack_rule *rule = NULL;
170
171         if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
172             strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN) {
173                 errno = ERANGE;
174                 return -1;
175         }
176
177         rule = calloc(sizeof(struct smack_rule), 1);
178         if (rule == NULL)
179                 return -1;
180
181         strcpy(rule->subject, subject);
182         strcpy(rule->object, object);
183         parse_access_type(access_type, rule->access_set);
184
185         if (handle->first == NULL) {
186                 handle->first = handle->last = rule;
187         } else {
188                 handle->last->next = rule;
189                 handle->last = rule;
190         }
191
192         return 0;
193 }
194
195 int smack_accesses_add_modify(struct smack_accesses *handle, const char *subject,
196                        const char *object, const char *access_add, const char *access_del)
197 {
198         struct smack_rule *rule = NULL;
199
200         if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
201             strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN) {
202                 errno = ERANGE;
203                 return -1;
204         }
205
206         rule = calloc(sizeof(struct smack_rule), 1);
207         if (rule == NULL)
208                 return -1;
209
210         strcpy(rule->subject, subject);
211         strcpy(rule->object, object);
212         parse_access_type(access_add, rule->access_add);
213         parse_access_type(access_del, rule->access_del);
214         rule->is_modify = 1;
215
216         if (handle->first == NULL) {
217                 handle->first = handle->last = rule;
218         } else {
219                 handle->last->next = rule;
220                 handle->last = rule;
221         }
222
223         return 0;
224 }
225
226 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
227 {
228         FILE *file = NULL;
229         char buf[READ_BUF_SIZE];
230         char *ptr;
231         const char *subject, *object, *access, *access2;
232         int newfd;
233         int ret;
234
235         newfd = dup(fd);
236         if (newfd == -1)
237                 return -1;
238
239         file = fdopen(newfd, "r");
240         if (file == NULL) {
241                 close(newfd);
242                 return -1;
243         }
244
245         while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
246                 if (strcmp(buf, "\n") == 0)
247                         continue;
248                 subject = strtok_r(buf, " \t\n", &ptr);
249                 object = strtok_r(NULL, " \t\n", &ptr);
250                 access = strtok_r(NULL, " \t\n", &ptr);
251                 access2 = strtok_r(NULL, " \t\n", &ptr);
252
253                 if (subject == NULL || object == NULL || access == NULL ||
254                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
255                         errno = EINVAL;
256                         fclose(file);
257                         return -1;
258                 }
259
260                 if (access2 == NULL)
261                         ret = smack_accesses_add(accesses, subject, object, access);
262                 else
263                         ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
264
265                 if (ret) {
266                         fclose(file);
267                         return -1;
268                 }
269         }
270
271         if (ferror(file)) {
272                 fclose(file);
273                 return -1;
274         }
275
276         fclose(file);
277         return 0;
278 }
279
280 int smack_have_access(const char *subject, const char *object,
281                       const char *access_type)
282 {
283         char buf[LOAD_LEN + 1];
284         char access_type_k[ACC_LEN + 1];
285         int ret;
286         int fd;
287         int access2 = 1;
288         char path[PATH_MAX];
289
290         if (!smackfs_mnt) {
291                 errno = EFAULT;
292                 return -1; 
293         }
294         
295         snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
296         fd = open(path, O_RDWR);
297         if (fd < 0) {
298                 if (errno != ENOENT)
299                         return -1;
300                 
301                 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
302                 fd = open(path, O_RDWR);
303                 if (fd < 0)
304                         return -1;
305                 access2 = 0;
306         }
307
308         parse_access_type(access_type, access_type_k);
309
310         if (access2)
311                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
312                                subject, object, access_type_k);
313         else
314                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
315                                subject, object, access_type_k);
316
317         if (ret < 0) {
318                 close(fd);
319                 return -1;
320         }
321
322         ret = write(fd, buf, strlen(buf));
323         if (ret < 0) {
324                 close(fd);
325                 return -1;
326         }
327
328         ret = read(fd, buf, 1);
329         close(fd);
330         if (ret < 0)
331                 return -1;
332
333         return buf[0] == '1';
334 }
335
336 int smack_cipso_new(struct smack_cipso **cipso)
337 {
338         struct smack_cipso *result;
339
340         result = calloc(sizeof(struct smack_cipso), 1);
341         if (result == NULL)
342                 return -1;
343
344         *cipso = result;
345         return 0;
346 }
347
348 void smack_cipso_free(struct smack_cipso *cipso)
349 {
350         if (cipso == NULL)
351                 return;
352
353         struct cipso_mapping *mapping = cipso->first;
354         struct cipso_mapping *next_mapping = NULL;
355
356         while (mapping != NULL) {
357                 next_mapping = mapping->next;
358                 free(mapping);
359                 mapping = next_mapping;
360         }
361 }
362
363 int smack_cipso_apply(struct smack_cipso *cipso)
364 {
365         struct cipso_mapping *m = NULL;
366         char buf[CIPSO_MAX_SIZE];
367         int fd;
368         int i;
369         char path[PATH_MAX];
370         int offset=0;
371
372         if (!smackfs_mnt) {
373                 errno = EFAULT;
374                 return -1;
375         }
376
377         snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
378         fd = open(path, O_WRONLY);
379         if (fd < 0)
380                 return -1;
381
382         memset(buf,0,CIPSO_MAX_SIZE);
383         for (m = cipso->first; m != NULL; m = m->next) {
384                 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
385                 offset += strlen(buf) + 1;
386
387                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
388                 offset += NUM_LEN;
389
390                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
391                 offset += NUM_LEN;
392
393                 for (i = 0; i < m->ncats; i++){
394                         sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
395                         offset += NUM_LEN;
396                 }
397
398                 if (write(fd, buf, offset) < 0) {
399                         close(fd);
400                         return -1;
401                 }
402         }
403
404         close(fd);
405         return 0;
406 }
407
408 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
409 {
410         struct cipso_mapping *mapping = NULL;
411         FILE *file = NULL;
412         char buf[BUF_SIZE];
413         char *label, *level, *cat, *ptr;
414         long int val;
415         int i;
416         int newfd;
417
418         newfd = dup(fd);
419         if (newfd == -1)
420                 return -1;
421
422         file = fdopen(newfd, "r");
423         if (file == NULL) {
424                 close(newfd);
425                 return -1;
426         }
427
428         while (fgets(buf, BUF_SIZE, file) != NULL) {
429                 mapping = calloc(sizeof(struct cipso_mapping), 1);
430                 if (mapping == NULL)
431                         goto err_out;
432
433                 label = strtok_r(buf, " \t\n", &ptr);
434                 level = strtok_r(NULL, " \t\n", &ptr);
435                 cat = strtok_r(NULL, " \t\n", &ptr);
436                 if (label == NULL || cat == NULL || level == NULL ||
437                     strlen(label) > SMACK_LABEL_LEN) {
438                         errno = EINVAL;
439                         goto err_out;
440                 }
441
442                 strcpy(mapping->label, label);
443
444                 errno = 0;
445                 val = strtol(level, NULL, 10);
446                 if (errno)
447                         goto err_out;
448
449                 if (val < 0 || val > LEVEL_MAX) {
450                         errno = ERANGE;
451                         goto err_out;
452                 }
453
454                 mapping->level = val;
455
456                 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
457                         errno = 0;
458                         val = strtol(cat, NULL, 10);
459                         if (errno)
460                                 goto err_out;
461
462                         if (val < 0 || val > CAT_MAX_VALUE) {
463                                 errno = ERANGE;
464                                 goto err_out;
465                         }
466
467                         mapping->cats[i] = val;
468
469                         cat = strtok_r(NULL, " \t\n", &ptr);
470                 }
471
472                 mapping->ncats = i;
473
474                 if (cipso->first == NULL) {
475                         cipso->first = cipso->last = mapping;
476                 } else {
477                         cipso->last->next = mapping;
478                         cipso->last = mapping;
479                 }
480         }
481
482         if (ferror(file))
483                 goto err_out;
484
485         fclose(file);
486         return 0;
487 err_out:
488         fclose(file);
489         free(mapping);
490         return -1;
491 }
492
493 const char *smack_smackfs_path(void)
494 {
495         return smackfs_mnt;
496 }
497
498 ssize_t smack_new_label_from_self(char **label)
499 {
500         char *result;
501         int fd;
502         int ret;
503
504         result = calloc(SMACK_LABEL_LEN + 1, 1);
505         if (result == NULL)
506                 return -1;
507
508         fd = open(SELF_LABEL_FILE, O_RDONLY);
509         if (fd < 0) {
510                 free(result);
511                 return -1;
512         }
513
514         ret = read(fd, result, SMACK_LABEL_LEN);
515         close(fd);
516         if (ret < 0) {
517                 free(result);
518                 return -1;
519         }
520
521         *label = result;
522         return ret;
523 }
524
525 ssize_t smack_new_label_from_socket(int fd, char **label)
526 {
527         char dummy;
528         int ret;
529         socklen_t length = 1;
530         char *result;
531
532         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
533         if (ret < 0 && errno != ERANGE)
534                 return -1;
535
536         result = calloc(length + 1, 1);
537         if (result == NULL)
538                 return -1;
539
540         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
541         if (ret < 0) {
542                 free(result);
543                 return -1;
544         }
545
546         *label = result;
547         return length;
548 }
549
550 ssize_t smack_new_label_from_path(const char *path, const char *xattr, 
551                                   int follow, char **label)
552 {
553         char *result;
554         ssize_t ret = 0;
555
556         ret = follow ?
557                 getxattr(path, xattr, NULL, 0) :
558                 lgetxattr(path, xattr, NULL, 0);
559         if (ret < 0 && errno != ERANGE)
560                 return -1;
561
562         result = calloc(ret + 1, 1);
563         if (result == NULL)
564                 return -1;
565
566         ret = follow ?
567                 getxattr(path, xattr, result, ret) :
568                 lgetxattr(path, xattr, result, ret);
569         if (ret < 0) {
570                 free(result);
571                 return -1;
572         }
573
574         *label = result;
575         return 0;
576 }
577
578 int smack_set_label_for_self(const char *label)
579 {
580         int len;
581         int fd;
582         int ret;
583
584         len = strnlen(label, SMACK_LABEL_LEN + 1);
585         if (len > SMACK_LABEL_LEN)
586                 return -1;
587
588         fd = open(SELF_LABEL_FILE, O_WRONLY);
589         if (fd < 0)
590                 return -1;
591
592         ret = write(fd, label, len);
593         close(fd);
594
595         return (ret < 0) ? -1 : 0;
596 }
597
598 int smack_revoke_subject(const char *subject)
599 {
600         int ret;
601         int fd;
602         int len;
603         char path[PATH_MAX];
604
605         len = strnlen(subject, SMACK_LABEL_LEN + 1);
606         if (len > SMACK_LABEL_LEN)
607                 return -1;
608
609         snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
610         fd = open(path, O_WRONLY);
611         if (fd < 0)
612                 return -1;
613
614         ret = write(fd, subject, len);
615         close(fd);
616
617         return (ret < 0) ? -1 : 0;
618 }
619
620 static int accesses_apply(struct smack_accesses *handle, int clear)
621 {
622         char buf[LOAD_LEN + 1];
623         struct smack_rule *rule;
624         int ret;
625         int fd;
626         int load_fd;
627         int change_fd;
628         int load2 = 1;
629         char path[PATH_MAX];
630
631         if (!smackfs_mnt) {
632                 errno = EFAULT;
633                 return -1; 
634         }
635         
636         snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
637         load_fd = open(path, O_WRONLY);
638         if (load_fd < 0) {
639                 if (errno != ENOENT)
640                         return -1;
641                 /* fallback */
642                 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
643                 load_fd = open(path, O_WRONLY);
644                 /* Try to continue if the file doesn't exist, we might not need it. */
645                 if (load_fd < 0 && errno != ENOENT)
646                         return -1;
647                 load2 = 0;
648         }
649
650         snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
651         change_fd = open(path, O_WRONLY);
652         /* Try to continue if the file doesn't exist, we might not need it. */
653         if (change_fd < 0 && errno != ENOENT) {
654                 ret = -1;
655                 goto err_out;
656         }
657
658         for (rule = handle->first; rule != NULL; rule = rule->next) {
659                 if (clear) {
660                         strcpy(rule->access_set, "-----");
661                         rule->is_modify = 0;
662                 }
663
664                 if (rule->is_modify) {
665                         fd = change_fd;
666                         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
667                                                 rule->subject, rule->object,
668                                                 rule->access_add, rule->access_del);
669                 } else {
670                         fd = load_fd;
671                         if (load2)
672                                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
673                                                rule->subject, rule->object,
674                                                rule->access_set);
675                         else
676                                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
677                                                rule->subject, rule->object,
678                                                rule->access_set);
679                 }
680
681                 if (ret < 0 || fd < 0) {
682                         ret = -1;
683                         goto err_out;
684                 }
685
686                 ret = write(fd, buf, strlen(buf));
687                 if (ret < 0) {
688                         ret = -1;
689                         goto err_out;
690                 }
691         }
692         ret = 0;
693
694 err_out:
695         if (load_fd >= 0)
696                 close(load_fd);
697         if (change_fd >= 0)
698                 close(change_fd);
699         return ret;
700 }
701
702 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
703 {
704         int i;
705
706         for (i = 0; i < ACC_LEN; ++i)
707                 out[i] = '-';
708         out[ACC_LEN] = '\0';
709
710         for (i = 0; in[i] != '\0'; i++)
711                 switch (in[i]) {
712                 case 'r':
713                 case 'R':
714                         out[0] = 'r';
715                         break;
716                 case 'w':
717                 case 'W':
718                         out[1] = 'w';
719                         break;
720                 case 'x':
721                 case 'X':
722                         out[2] = 'x';
723                         break;
724                 case 'a':
725                 case 'A':
726                         out[3] = 'a';
727                         break;
728                 case 't':
729                 case 'T':
730                         out[4] = 't';
731                         break;
732                 default:
733                         break;
734                 }
735 }
736
737