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