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