Regression fix: don't modify smack_accesses while applying the rules.
[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 #define ACC_CLEAR "-----"
54
55 extern char *smackfs_mnt;
56
57 struct smack_rule {
58         char subject[SMACK_LABEL_LEN + 1];
59         char object[SMACK_LABEL_LEN + 1];
60         int is_modify;
61         char access_type[ACC_LEN + 1];
62         char allow_access_type[ACC_LEN + 1];
63         char deny_access_type[ACC_LEN + 1];
64         struct smack_rule *next;
65 };
66
67 struct smack_accesses {
68         struct smack_rule *first;
69         struct smack_rule *last;
70 };
71
72 struct cipso_mapping {
73         char label[SMACK_LABEL_LEN + 1];
74         int cats[CAT_MAX_VALUE];
75         int ncats;
76         int level;
77         struct cipso_mapping *next;
78 };
79
80 struct smack_cipso {
81         struct cipso_mapping *first;
82         struct cipso_mapping *last;
83 };
84
85 static int accesses_apply(struct smack_accesses *handle, int clear);
86 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1]);
87 static int smack_label_length(const char *label) __attribute__((unused));
88
89 int smack_accesses_new(struct smack_accesses **accesses)
90 {
91         struct smack_accesses *result;
92
93         result = calloc(sizeof(struct smack_accesses), 1);
94         if (result == NULL)
95                 return -1;
96
97         *accesses = result;
98         return 0;
99 }
100
101 void smack_accesses_free(struct smack_accesses *handle)
102 {
103         if (handle == NULL)
104                 return;
105
106         struct smack_rule *rule = handle->first;
107         struct smack_rule *next_rule = NULL;
108
109         while (rule != NULL) {
110                 next_rule = rule->next;
111                 free(rule);
112                 rule = next_rule;
113         }
114
115         free(handle);
116 }
117
118 int smack_accesses_save(struct smack_accesses *handle, int fd)
119 {
120         struct smack_rule *rule = handle->first;
121         FILE *file;
122         int ret;
123         int newfd;
124
125         newfd = dup(fd);
126         if (newfd == -1)
127                 return -1;
128
129         file = fdopen(newfd, "w");
130         if (file == NULL) {
131                 close(newfd);
132                 return -1;
133         }
134
135         while (rule) {
136                 if (rule->is_modify) {
137                         ret = fprintf(file, "%s %s %s %s\n",
138                                       rule->subject, rule->object,
139                                       rule->allow_access_type,
140                                       rule->deny_access_type);
141                 } else {
142                         ret = fprintf(file, "%s %s %s\n",
143                                       rule->subject, rule->object,
144                                       rule->access_type);
145                 }
146
147                 if (ret < 0) {
148                         fclose(file);
149                         return -1;
150                 }
151
152                 rule = rule->next;
153         }
154
155         fclose(file);
156         return 0;
157 }
158
159 int smack_accesses_apply(struct smack_accesses *handle)
160 {
161         return accesses_apply(handle, 0);
162 }
163
164 int smack_accesses_clear(struct smack_accesses *handle)
165 {
166         return accesses_apply(handle, 1);
167 }
168
169 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
170                        const char *object, const char *access_type)
171 {
172         struct smack_rule *rule = NULL;
173
174         if (smack_label_length(subject) < 0 ||
175             smack_label_length(object) < 0)
176                 return -1;
177
178         rule = calloc(sizeof(struct smack_rule), 1);
179         if (rule == NULL)
180                 return -1;
181
182         strcpy(rule->subject, subject);
183         strcpy(rule->object, object);
184         parse_access_type(access_type, rule->access_type);
185
186         if (handle->first == NULL) {
187                 handle->first = handle->last = rule;
188         } else {
189                 handle->last->next = rule;
190                 handle->last = rule;
191         }
192
193         return 0;
194 }
195
196 int smack_accesses_add_modify(struct smack_accesses *handle,
197                               const char *subject,
198                               const char *object,
199                               const char *allow_access_type,
200                               const char *deny_access_type)
201 {
202         struct smack_rule *rule = NULL;
203
204         if (smack_label_length(subject) < 0 ||
205             smack_label_length(object) < 0)
206                 return -1;
207
208         rule = calloc(sizeof(struct smack_rule), 1);
209         if (rule == NULL)
210                 return -1;
211
212         strcpy(rule->subject, subject);
213         strcpy(rule->object, object);
214         parse_access_type(allow_access_type, rule->allow_access_type);
215         parse_access_type(deny_access_type, rule->deny_access_type);
216         rule->is_modify = 1;
217
218         if (handle->first == NULL) {
219                 handle->first = handle->last = rule;
220         } else {
221                 handle->last->next = rule;
222                 handle->last = rule;
223         }
224
225         return 0;
226 }
227
228 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
229 {
230         FILE *file = NULL;
231         char buf[READ_BUF_SIZE];
232         char *ptr;
233         const char *subject, *object, *access, *access2;
234         int newfd;
235         int ret;
236
237         newfd = dup(fd);
238         if (newfd == -1)
239                 return -1;
240
241         file = fdopen(newfd, "r");
242         if (file == NULL) {
243                 close(newfd);
244                 return -1;
245         }
246
247         while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
248                 if (strcmp(buf, "\n") == 0)
249                         continue;
250                 subject = strtok_r(buf, " \t\n", &ptr);
251                 object = strtok_r(NULL, " \t\n", &ptr);
252                 access = strtok_r(NULL, " \t\n", &ptr);
253                 access2 = strtok_r(NULL, " \t\n", &ptr);
254
255                 if (subject == NULL || object == NULL || access == NULL ||
256                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
257                         fclose(file);
258                         return -1;
259                 }
260
261                 if (access2 == NULL)
262                         ret = smack_accesses_add(accesses, subject, object, access);
263                 else
264                         ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
265
266                 if (ret) {
267                         fclose(file);
268                         return -1;
269                 }
270         }
271
272         if (ferror(file)) {
273                 fclose(file);
274                 return -1;
275         }
276
277         fclose(file);
278         return 0;
279 }
280
281 int smack_have_access(const char *subject, const char *object,
282                       const char *access_type)
283 {
284         char buf[LOAD_LEN + 1];
285         char access_type_k[ACC_LEN + 1];
286         int ret;
287         int fd;
288         int access2 = 1;
289         char path[PATH_MAX];
290
291         if (!smackfs_mnt)
292                 return -1; 
293         
294         snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
295         fd = open(path, O_RDWR);
296         if (fd < 0) {
297                 if (errno != ENOENT)
298                         return -1;
299                 
300                 snprintf(path, sizeof path, "%s/access", smackfs_mnt);
301                 fd = open(path, O_RDWR);
302                 if (fd < 0)
303                         return -1;
304                 access2 = 0;
305         }
306
307         parse_access_type(access_type, access_type_k);
308
309         if (access2)
310                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
311                                subject, object, access_type_k);
312         else
313                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
314                                subject, object, access_type_k);
315
316         if (ret < 0) {
317                 close(fd);
318                 return -1;
319         }
320
321         ret = write(fd, buf, strlen(buf));
322         if (ret < 0) {
323                 close(fd);
324                 return -1;
325         }
326
327         ret = read(fd, buf, 1);
328         close(fd);
329         if (ret < 0)
330                 return -1;
331
332         return buf[0] == '1';
333 }
334
335 int smack_cipso_new(struct smack_cipso **cipso)
336 {
337         struct smack_cipso *result;
338
339         result = calloc(sizeof(struct smack_cipso), 1);
340         if (result == NULL)
341                 return -1;
342
343         *cipso = result;
344         return 0;
345 }
346
347 void smack_cipso_free(struct smack_cipso *cipso)
348 {
349         if (cipso == NULL)
350                 return;
351
352         struct cipso_mapping *mapping = cipso->first;
353         struct cipso_mapping *next_mapping = NULL;
354
355         while (mapping != NULL) {
356                 next_mapping = mapping->next;
357                 free(mapping);
358                 mapping = next_mapping;
359         }
360 }
361
362 int smack_cipso_apply(struct smack_cipso *cipso)
363 {
364         struct cipso_mapping *m = NULL;
365         char buf[CIPSO_MAX_SIZE];
366         int fd;
367         int i;
368         char path[PATH_MAX];
369         int offset=0;
370
371         if (!smackfs_mnt)
372                 return -1;
373
374         snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
375         fd = open(path, O_WRONLY);
376         if (fd < 0)
377                 return -1;
378
379         memset(buf,0,CIPSO_MAX_SIZE);
380         for (m = cipso->first; m != NULL; m = m->next) {
381                 snprintf(buf, SMACK_LABEL_LEN + 1, "%s", m->label);
382                 offset += strlen(buf) + 1;
383
384                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
385                 offset += NUM_LEN;
386
387                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
388                 offset += NUM_LEN;
389
390                 for (i = 0; i < m->ncats; i++){
391                         sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
392                         offset += NUM_LEN;
393                 }
394
395                 if (write(fd, buf, offset) < 0) {
396                         close(fd);
397                         return -1;
398                 }
399         }
400
401         close(fd);
402         return 0;
403 }
404
405 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
406 {
407         struct cipso_mapping *mapping = NULL;
408         FILE *file = NULL;
409         char buf[BUF_SIZE];
410         char *label, *level, *cat, *ptr;
411         long int val;
412         int i;
413         int newfd;
414
415         newfd = dup(fd);
416         if (newfd == -1)
417                 return -1;
418
419         file = fdopen(newfd, "r");
420         if (file == NULL) {
421                 close(newfd);
422                 return -1;
423         }
424
425         while (fgets(buf, BUF_SIZE, file) != NULL) {
426                 mapping = calloc(sizeof(struct cipso_mapping), 1);
427                 if (mapping == NULL)
428                         goto err_out;
429
430                 label = strtok_r(buf, " \t\n", &ptr);
431                 level = strtok_r(NULL, " \t\n", &ptr);
432                 cat = strtok_r(NULL, " \t\n", &ptr);
433                 if (smack_label_length(label) < 0 || level == NULL)
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 = smack_label_length(label);
575         if (len < 0)
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 = smack_label_length(subject);
596         if (len < 0)
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 (rule->is_modify && !clear) {
648                         fd = change_fd;
649                         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
650                                        rule->subject, rule->object,
651                                        rule->allow_access_type,
652                                        rule->deny_access_type);
653                 } else {
654                         fd = load_fd;
655                         if (load2)
656                                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
657                                                rule->subject, rule->object,
658                                                clear ? ACC_CLEAR : rule->access_type);
659                         else
660                                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
661                                                rule->subject, rule->object,
662                                                clear ? ACC_CLEAR : rule->access_type);
663                 }
664
665                 if (ret < 0 || fd < 0) {
666                         ret = -1;
667                         goto err_out;
668                 }
669
670                 ret = write(fd, buf, strlen(buf));
671                 if (ret < 0) {
672                         ret = -1;
673                         goto err_out;
674                 }
675         }
676         ret = 0;
677
678 err_out:
679         if (load_fd >= 0)
680                 close(load_fd);
681         if (change_fd >= 0)
682                 close(change_fd);
683         return ret;
684 }
685
686 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
687 {
688         int i;
689
690         for (i = 0; i < ACC_LEN; ++i)
691                 out[i] = '-';
692         out[ACC_LEN] = '\0';
693
694         for (i = 0; in[i] != '\0'; i++)
695                 switch (in[i]) {
696                 case 'r':
697                 case 'R':
698                         out[0] = 'r';
699                         break;
700                 case 'w':
701                 case 'W':
702                         out[1] = 'w';
703                         break;
704                 case 'x':
705                 case 'X':
706                         out[2] = 'x';
707                         break;
708                 case 'a':
709                 case 'A':
710                         out[3] = 'a';
711                         break;
712                 case 't':
713                 case 'T':
714                         out[4] = 't';
715                         break;
716                 default:
717                         break;
718                 }
719 }
720
721 static int smack_label_length(const char *label)
722 {
723         int i;
724
725         if (!label || label[0] == '\0' || label[0] == '-')
726                 return -1;
727
728         for (i = 0; i < (SMACK_LABEL_LEN + 1) && label[i]; i++) {
729                 switch (label[i]) {
730                 case ' ':
731                 case '/':
732                 case '"':
733                 case '\\':
734                 case '\'':
735                         return -1;
736                 default:
737                         break;
738                 }
739         }
740
741         return i < (SMACK_LABEL_LEN + 1) ? i : -1;
742 }