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