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