Fix value returned by smack_label_length()
[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                 return -1;
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_type);
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,
196                               const char *subject,
197                               const char *object,
198                               const char *allow_access_type,
199                               const char *deny_access_type)
200 {
201         struct smack_rule *rule = NULL;
202
203         if (strnlen(subject, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN ||
204             strnlen(object, SMACK_LABEL_LEN + 1) > SMACK_LABEL_LEN)
205                 return -1;
206
207         rule = calloc(sizeof(struct smack_rule), 1);
208         if (rule == NULL)
209                 return -1;
210
211         strcpy(rule->subject, subject);
212         strcpy(rule->object, object);
213         parse_access_type(allow_access_type, rule->allow_access_type);
214         parse_access_type(deny_access_type, rule->deny_access_type);
215         rule->is_modify = 1;
216
217         if (handle->first == NULL) {
218                 handle->first = handle->last = rule;
219         } else {
220                 handle->last->next = rule;
221                 handle->last = rule;
222         }
223
224         return 0;
225 }
226
227 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
228 {
229         FILE *file = NULL;
230         char buf[READ_BUF_SIZE];
231         char *ptr;
232         const char *subject, *object, *access, *access2;
233         int newfd;
234         int ret;
235
236         newfd = dup(fd);
237         if (newfd == -1)
238                 return -1;
239
240         file = fdopen(newfd, "r");
241         if (file == NULL) {
242                 close(newfd);
243                 return -1;
244         }
245
246         while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
247                 if (strcmp(buf, "\n") == 0)
248                         continue;
249                 subject = strtok_r(buf, " \t\n", &ptr);
250                 object = strtok_r(NULL, " \t\n", &ptr);
251                 access = strtok_r(NULL, " \t\n", &ptr);
252                 access2 = strtok_r(NULL, " \t\n", &ptr);
253
254                 if (subject == NULL || object == NULL || access == NULL ||
255                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
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                 return -1; 
292         
293         snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
294         fd = open(path, O_RDWR);
295         if (fd < 0) {
296                 if (errno != ENOENT)
297                         return -1;
298                 
299                 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
300                 fd = open(path, O_RDWR);
301                 if (fd < 0)
302                         return -1;
303                 access2 = 0;
304         }
305
306         parse_access_type(access_type, access_type_k);
307
308         if (access2)
309                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
310                                subject, object, access_type_k);
311         else
312                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
313                                subject, object, access_type_k);
314
315         if (ret < 0) {
316                 close(fd);
317                 return -1;
318         }
319
320         ret = write(fd, buf, strlen(buf));
321         if (ret < 0) {
322                 close(fd);
323                 return -1;
324         }
325
326         ret = read(fd, buf, 1);
327         close(fd);
328         if (ret < 0)
329                 return -1;
330
331         return buf[0] == '1';
332 }
333
334 int smack_cipso_new(struct smack_cipso **cipso)
335 {
336         struct smack_cipso *result;
337
338         result = calloc(sizeof(struct smack_cipso), 1);
339         if (result == NULL)
340                 return -1;
341
342         *cipso = result;
343         return 0;
344 }
345
346 void smack_cipso_free(struct smack_cipso *cipso)
347 {
348         if (cipso == NULL)
349                 return;
350
351         struct cipso_mapping *mapping = cipso->first;
352         struct cipso_mapping *next_mapping = NULL;
353
354         while (mapping != NULL) {
355                 next_mapping = mapping->next;
356                 free(mapping);
357                 mapping = next_mapping;
358         }
359 }
360
361 int smack_cipso_apply(struct smack_cipso *cipso)
362 {
363         struct cipso_mapping *m = NULL;
364         char buf[CIPSO_MAX_SIZE];
365         int fd;
366         int i;
367         char path[PATH_MAX];
368         int offset=0;
369
370         if (!smackfs_mnt)
371                 return -1;
372
373         snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
374         fd = open(path, O_WRONLY);
375         if (fd < 0)
376                 return -1;
377
378         memset(buf,0,CIPSO_MAX_SIZE);
379         for (m = cipso->first; m != NULL; m = m->next) {
380                 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
381                 offset += strlen(buf) + 1;
382
383                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
384                 offset += NUM_LEN;
385
386                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
387                 offset += NUM_LEN;
388
389                 for (i = 0; i < m->ncats; i++){
390                         sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
391                         offset += NUM_LEN;
392                 }
393
394                 if (write(fd, buf, offset) < 0) {
395                         close(fd);
396                         return -1;
397                 }
398         }
399
400         close(fd);
401         return 0;
402 }
403
404 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
405 {
406         struct cipso_mapping *mapping = NULL;
407         FILE *file = NULL;
408         char buf[BUF_SIZE];
409         char *label, *level, *cat, *ptr;
410         long int val;
411         int i;
412         int newfd;
413
414         newfd = dup(fd);
415         if (newfd == -1)
416                 return -1;
417
418         file = fdopen(newfd, "r");
419         if (file == NULL) {
420                 close(newfd);
421                 return -1;
422         }
423
424         while (fgets(buf, BUF_SIZE, file) != NULL) {
425                 mapping = calloc(sizeof(struct cipso_mapping), 1);
426                 if (mapping == NULL)
427                         goto err_out;
428
429                 label = strtok_r(buf, " \t\n", &ptr);
430                 level = strtok_r(NULL, " \t\n", &ptr);
431                 cat = strtok_r(NULL, " \t\n", &ptr);
432                 if (label == NULL || level == NULL ||
433                     strlen(label) > SMACK_LABEL_LEN)
434                         goto err_out;
435
436                 strcpy(mapping->label, label);
437
438                 errno = 0;
439                 val = strtol(level, NULL, 10);
440                 if (errno)
441                         goto err_out;
442
443                 if (val < 0 || val > LEVEL_MAX)
444                         goto err_out;
445
446                 mapping->level = val;
447
448                 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
449                         errno = 0;
450                         val = strtol(cat, NULL, 10);
451                         if (errno)
452                                 goto err_out;
453
454                         if (val < 0 || val > CAT_MAX_VALUE)
455                                 goto err_out;
456
457                         mapping->cats[i] = val;
458
459                         cat = strtok_r(NULL, " \t\n", &ptr);
460                 }
461
462                 mapping->ncats = i;
463
464                 if (cipso->first == NULL) {
465                         cipso->first = cipso->last = mapping;
466                 } else {
467                         cipso->last->next = mapping;
468                         cipso->last = mapping;
469                 }
470         }
471
472         if (ferror(file))
473                 goto err_out;
474
475         fclose(file);
476         return 0;
477 err_out:
478         fclose(file);
479         free(mapping);
480         return -1;
481 }
482
483 const char *smack_smackfs_path(void)
484 {
485         return smackfs_mnt;
486 }
487
488 ssize_t smack_new_label_from_self(char **label)
489 {
490         char *result;
491         int fd;
492         int ret;
493
494         result = calloc(SMACK_LABEL_LEN + 1, 1);
495         if (result == NULL)
496                 return -1;
497
498         fd = open(SELF_LABEL_FILE, O_RDONLY);
499         if (fd < 0) {
500                 free(result);
501                 return -1;
502         }
503
504         ret = read(fd, result, SMACK_LABEL_LEN);
505         close(fd);
506         if (ret < 0) {
507                 free(result);
508                 return -1;
509         }
510
511         *label = result;
512         return ret;
513 }
514
515 ssize_t smack_new_label_from_socket(int fd, char **label)
516 {
517         char dummy;
518         int ret;
519         socklen_t length = 1;
520         char *result;
521
522         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
523         if (ret < 0 && errno != ERANGE)
524                 return -1;
525
526         result = calloc(length + 1, 1);
527         if (result == NULL)
528                 return -1;
529
530         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
531         if (ret < 0) {
532                 free(result);
533                 return -1;
534         }
535
536         *label = result;
537         return length;
538 }
539
540 ssize_t smack_new_label_from_path(const char *path, const char *xattr, 
541                                   int follow, char **label)
542 {
543         char *result;
544         ssize_t ret = 0;
545
546         ret = follow ?
547                 getxattr(path, xattr, NULL, 0) :
548                 lgetxattr(path, xattr, NULL, 0);
549         if (ret < 0 && errno != ERANGE)
550                 return -1;
551
552         result = calloc(ret + 1, 1);
553         if (result == NULL)
554                 return -1;
555
556         ret = follow ?
557                 getxattr(path, xattr, result, ret) :
558                 lgetxattr(path, xattr, result, ret);
559         if (ret < 0) {
560                 free(result);
561                 return -1;
562         }
563
564         *label = result;
565         return 0;
566 }
567
568 int smack_set_label_for_self(const char *label)
569 {
570         int len;
571         int fd;
572         int ret;
573
574         len = strnlen(label, SMACK_LABEL_LEN + 1);
575         if (len > SMACK_LABEL_LEN)
576                 return -1;
577
578         fd = open(SELF_LABEL_FILE, O_WRONLY);
579         if (fd < 0)
580                 return -1;
581
582         ret = write(fd, label, len);
583         close(fd);
584
585         return (ret < 0) ? -1 : 0;
586 }
587
588 int smack_revoke_subject(const char *subject)
589 {
590         int ret;
591         int fd;
592         int len;
593         char path[PATH_MAX];
594
595         len = strnlen(subject, SMACK_LABEL_LEN + 1);
596         if (len > SMACK_LABEL_LEN)
597                 return -1;
598
599         snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
600         fd = open(path, O_WRONLY);
601         if (fd < 0)
602                 return -1;
603
604         ret = write(fd, subject, len);
605         close(fd);
606
607         return (ret < 0) ? -1 : 0;
608 }
609
610 static int accesses_apply(struct smack_accesses *handle, int clear)
611 {
612         char buf[LOAD_LEN + 1];
613         struct smack_rule *rule;
614         int ret;
615         int fd;
616         int load_fd;
617         int change_fd;
618         int load2 = 1;
619         char path[PATH_MAX];
620
621         if (!smackfs_mnt)
622                 return -1; 
623         
624         snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
625         load_fd = open(path, O_WRONLY);
626         if (load_fd < 0) {
627                 if (errno != ENOENT)
628                         return -1;
629                 /* fallback */
630                 snprintf(path, sizeof path, "%s/load", smackfs_mnt);
631                 load_fd = open(path, O_WRONLY);
632                 /* Try to continue if the file doesn't exist, we might not need it. */
633                 if (load_fd < 0 && errno != ENOENT)
634                         return -1;
635                 load2 = 0;
636         }
637
638         snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
639         change_fd = open(path, O_WRONLY);
640         /* Try to continue if the file doesn't exist, we might not need it. */
641         if (change_fd < 0 && errno != ENOENT) {
642                 ret = -1;
643                 goto err_out;
644         }
645
646         for (rule = handle->first; rule != NULL; rule = rule->next) {
647                 if (clear) {
648                         strcpy(rule->access_type, "-----");
649                         rule->is_modify = 0;
650                 }
651
652                 if (rule->is_modify) {
653                         fd = change_fd;
654                         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
655                                        rule->subject, rule->object,
656                                        rule->allow_access_type,
657                                        rule->deny_access_type);
658                 } else {
659                         fd = load_fd;
660                         if (load2)
661                                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
662                                                rule->subject, rule->object,
663                                                rule->access_type);
664                         else
665                                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
666                                                rule->subject, rule->object,
667                                                rule->access_type);
668                 }
669
670                 if (ret < 0 || fd < 0) {
671                         ret = -1;
672                         goto err_out;
673                 }
674
675                 ret = write(fd, buf, strlen(buf));
676                 if (ret < 0) {
677                         ret = -1;
678                         goto err_out;
679                 }
680         }
681         ret = 0;
682
683 err_out:
684         if (load_fd >= 0)
685                 close(load_fd);
686         if (change_fd >= 0)
687                 close(change_fd);
688         return ret;
689 }
690
691 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
692 {
693         int i;
694
695         for (i = 0; i < ACC_LEN; ++i)
696                 out[i] = '-';
697         out[ACC_LEN] = '\0';
698
699         for (i = 0; in[i] != '\0'; i++)
700                 switch (in[i]) {
701                 case 'r':
702                 case 'R':
703                         out[0] = 'r';
704                         break;
705                 case 'w':
706                 case 'W':
707                         out[1] = 'w';
708                         break;
709                 case 'x':
710                 case 'X':
711                         out[2] = 'x';
712                         break;
713                 case 'a':
714                 case 'A':
715                         out[3] = 'a';
716                         break;
717                 case 't':
718                 case 'T':
719                         out[4] = 't';
720                         break;
721                 default:
722                         break;
723                 }
724 }
725
726 static int smack_label_length(const char *label)
727 {
728         int i;
729
730         if (!label || label[0] == '\0' || label[0] == '-')
731                 return -1;
732
733         for (i = 0; i < (SMACK_LABEL_LEN + 1) && label[i]; i++) {
734                 switch (label[i]) {
735                 case ' ':
736                 case '/':
737                 case '"':
738                 case '\\':
739                 case '\'':
740                         return -1;
741                 default:
742                         break;
743                 }
744         }
745
746         return i < (SMACK_LABEL_LEN + 1) ? i : -1;
747 }