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