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