Declare smack_mnt as non-static in init.c.
[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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  * Authors:
22  * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
23  * Brian McGillion <brian.mcgillion@intel.com>
24  * Passion Zhao <passion.zhao@intel.com>
25  */
26
27 #include "sys/smack.h"
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <limits.h>
38
39 #define LABEL_LEN 255
40 #define ACC_LEN 5
41 #define LOAD_LEN (2 * (LABEL_LEN + 1) + ACC_LEN)
42
43 #define LEVEL_MAX 255
44 #define NUM_LEN 4
45 #define BUF_SIZE 512
46 #define CAT_MAX_COUNT 240
47 #define CAT_MAX_VALUE 63
48 #define CIPSO_POS(i)   (LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
49 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
50 #define CIPSO_NUM_LEN_STR "%-4d"
51
52 #define ACC_R 0x01
53 #define ACC_W 0x02
54 #define ACC_X 0x04
55 #define ACC_A 0x08
56 #define ACC_T 0x10
57
58 #define KERNEL_LONG_FORMAT "%s %s %s"
59 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5s"
60 #define READ_BUF_SIZE LOAD_LEN + 1
61 #define SELF_LABEL_FILE "/proc/self/attr/current"
62
63 extern char *smack_mnt;
64
65 struct smack_rule {
66         char subject[LABEL_LEN + 1];
67         char object[LABEL_LEN + 1];
68         int access_code;
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[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 int access_type_to_int(const char *access_type);
92 static inline void int_to_access_type_c(unsigned ac, char *str);
93 static inline void int_to_access_type_k(unsigned ac, char *str);
94
95 int smack_accesses_new(struct smack_accesses **accesses)
96 {
97         struct smack_accesses *result;
98
99         result = calloc(sizeof(struct smack_accesses), 1);
100         if (result == NULL)
101                 return -1;
102
103         *accesses = result;
104         return 0;
105 }
106
107 void smack_accesses_free(struct smack_accesses *handle)
108 {
109         if (handle == NULL)
110                 return;
111
112         struct smack_rule *rule = handle->first;
113         struct smack_rule *next_rule = NULL;
114
115         while (rule != NULL) {
116                 next_rule = rule->next;
117                 free(rule);
118                 rule = next_rule;
119         }
120
121         free(handle);
122 }
123
124 int smack_accesses_save(struct smack_accesses *handle, int fd)
125 {
126         struct smack_rule *rule = handle->first;
127         char access_type[ACC_LEN + 1];
128         FILE *file;
129         int ret;
130         int newfd;
131
132         newfd = dup(fd);
133         if (newfd == -1)
134                 return -1;
135
136         file = fdopen(newfd, "w");
137         if (file == NULL) {
138                 close(newfd);
139                 return -1;
140         }
141
142         while (rule) {
143                 int_to_access_type_c(rule->access_code, access_type);
144
145                 ret = fprintf(file, "%s %s %s\n",
146                               rule->subject, rule->object, access_type);
147                 if (ret < 0) {
148                         fclose(file);
149                         return -1;
150                 }
151
152                 rule = rule->next;
153         }
154
155         fclose(file);
156         return 0;
157 }
158
159 int smack_accesses_apply(struct smack_accesses *handle)
160 {
161         return accesses_apply(handle, 0);
162 }
163
164 int smack_accesses_clear(struct smack_accesses *handle)
165 {
166         return accesses_apply(handle, 1);
167 }
168
169 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
170                        const char *object, const char *access_type)
171 {
172         struct smack_rule *rule = NULL;
173
174         rule = calloc(sizeof(struct smack_rule), 1);
175         if (rule == NULL)
176                 return -1;
177
178         strncpy(rule->subject, subject, LABEL_LEN + 1);
179         strncpy(rule->object, object, LABEL_LEN + 1);
180         rule->access_code = access_type_to_int(access_type);
181
182         if (handle->first == NULL) {
183                 handle->first = handle->last = rule;
184         } else {
185                 handle->last->next = rule;
186                 handle->last = rule;
187         }
188
189         return 0;
190 }
191
192 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
193 {
194         FILE *file = NULL;
195         char buf[READ_BUF_SIZE];
196         char *ptr;
197         const char *subject, *object, *access;
198         int newfd;
199
200         newfd = dup(fd);
201         if (newfd == -1)
202                 return -1;
203
204         file = fdopen(newfd, "r");
205         if (file == NULL) {
206                 close(newfd);
207                 return -1;
208         }
209
210         while (fgets(buf, READ_BUF_SIZE, file) != NULL) {
211                 if (strcmp(buf, "\n") == 0)
212                         continue;
213                 subject = strtok_r(buf, " \t\n", &ptr);
214                 object = strtok_r(NULL, " \t\n", &ptr);
215                 access = strtok_r(NULL, " \t\n", &ptr);
216
217                 if (subject == NULL || object == NULL || access == NULL ||
218                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
219                         errno = EINVAL;
220                         fclose(file);
221                         return -1;
222                 }
223
224                 if (smack_accesses_add(accesses, subject, object, access)) {
225                         fclose(file);
226                         return -1;
227                 }
228         }
229
230         if (ferror(file)) {
231                 fclose(file);
232                 return -1;
233         }
234
235         fclose(file);
236         return 0;
237 }
238
239 int smack_have_access(const char *subject, const char *object,
240                       const char *access_type)
241 {
242         char buf[LOAD_LEN + 1];
243         char access_type_k[ACC_LEN + 1];
244         unsigned access_code;
245         int ret;
246         int fd;
247         int access2 = 1;
248         char path[PATH_MAX];
249
250         if (!smack_mnt) {
251                 errno = EFAULT;
252                 return -1; 
253         }
254         
255         snprintf(path, sizeof path, "%s/access2", smack_mnt);
256         fd = open(path, O_RDWR);
257         if (fd < 0) {
258                 if (errno != ENOENT)
259                         return -1;
260                 
261                 snprintf(path, sizeof path, "%s/access", smack_mnt);
262                 fd = open(path, O_RDWR);
263                 if (fd < 0)
264                         return -1;
265                 access2 = 0;
266         }
267
268         access_code = access_type_to_int(access_type);
269         int_to_access_type_k(access_code, access_type_k);
270
271         if (access2)
272                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
273                                subject, object, access_type_k);
274         else
275                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
276                                subject, object, access_type_k);
277
278         if (ret < 0) {
279                 close(fd);
280                 return -1;
281         }
282
283         ret = write(fd, buf, strlen(buf));
284         if (ret < 0) {
285                 close(fd);
286                 return -1;
287         }
288
289         ret = read(fd, buf, 1);
290         close(fd);
291         if (ret < 0)
292                 return -1;
293
294         return buf[0] == '1';
295 }
296 void smack_cipso_free(struct smack_cipso *cipso)
297 {
298         if (cipso == NULL)
299                 return;
300
301         struct cipso_mapping *mapping = cipso->first;
302         struct cipso_mapping *next_mapping = NULL;
303
304         while (mapping != NULL) {
305                 next_mapping = mapping->next;
306                 free(mapping);
307                 mapping = next_mapping;
308         }
309 }
310
311 struct smack_cipso *smack_cipso_new(int fd)
312 {
313         struct smack_cipso *cipso = NULL;
314         struct cipso_mapping *mapping = NULL;
315         FILE *file = NULL;
316         char buf[BUF_SIZE];
317         char *label, *level, *cat, *ptr;
318         long int val;
319         int i;
320         int newfd;
321
322         newfd = dup(fd);
323         if (newfd == -1)
324                 return NULL;
325
326         file = fdopen(newfd, "r");
327         if (file == NULL) {
328                 close(newfd);
329                 return NULL;
330         }
331
332         cipso = calloc(sizeof(struct smack_cipso ), 1);
333         if (cipso == NULL) {
334                 fclose(file);
335                 return NULL;
336         }
337
338         while (fgets(buf, BUF_SIZE, file) != NULL) {
339                 mapping = calloc(sizeof(struct cipso_mapping), 1);
340                 if (mapping == NULL)
341                         goto err_out;
342
343                 label = strtok_r(buf, " \t\n", &ptr);
344                 level = strtok_r(NULL, " \t\n", &ptr);
345                 cat = strtok_r(NULL, " \t\n", &ptr);
346                 if (label == NULL || cat == NULL || level == NULL ||
347                     strlen(label) > LABEL_LEN) {
348                         errno = EINVAL;
349                         goto err_out;
350                 }
351
352                 strcpy(mapping->label, label);
353
354                 errno = 0;
355                 val = strtol(level, NULL, 10);
356                 if (errno)
357                         goto err_out;
358
359                 if (val < 0 || val > LEVEL_MAX) {
360                         errno = ERANGE;
361                         goto err_out;
362                 }
363
364                 mapping->level = val;
365
366                 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
367                         errno = 0;
368                         val = strtol(cat, NULL, 10);
369                         if (errno)
370                                 goto err_out;
371
372                         if (val < 0 || val > CAT_MAX_VALUE) {
373                                 errno = ERANGE;
374                                 goto err_out;
375                         }
376
377                         mapping->cats[i] = val;
378
379                         cat = strtok_r(NULL, " \t\n", &ptr);
380                 }
381
382                 mapping->ncats = i;
383
384                 if (cipso->first == NULL) {
385                         cipso->first = cipso->last = mapping;
386                 } else {
387                         cipso->last->next = mapping;
388                         cipso->last = mapping;
389                 }
390         }
391
392         if (ferror(file))
393                 goto err_out;
394
395         fclose(file);
396         return cipso;
397 err_out:
398         fclose(file);
399         smack_cipso_free(cipso);
400         free(mapping);
401         return NULL;
402 }
403
404 const char *smack_smackfs_path(void)
405 {
406         return smack_mnt;
407 }
408
409 int smack_cipso_apply(struct smack_cipso *cipso)
410 {
411         struct cipso_mapping *m = NULL;
412         char buf[CIPSO_MAX_SIZE];
413         int fd;
414         int i;
415         char path[PATH_MAX];
416
417         if (!smack_mnt) {
418                 errno = EFAULT;
419                 return -1; 
420         }
421         
422         snprintf(path, sizeof path, "%s/cipso2", smack_mnt);
423         fd = open(path, O_WRONLY);
424         if (fd < 0)
425                 return -1;
426
427         for (m = cipso->first; m != NULL; m = m->next) {
428                 sprintf(buf, "%s ", m->label);
429                 sprintf(&buf[LABEL_LEN + 1], CIPSO_NUM_LEN_STR, m->level);
430                 sprintf(&buf[LABEL_LEN + 1 + NUM_LEN], CIPSO_NUM_LEN_STR, m->ncats);
431
432                 for (i = 0; i < m->ncats; i++)
433                         sprintf(&buf[CIPSO_POS(i)], CIPSO_NUM_LEN_STR, m->cats[i]);
434
435                 if (write(fd, buf, strlen(buf)) < 0) {
436                         close(fd);
437                         return -1;
438                 }
439         }
440
441         close(fd);
442         return 0;
443 }
444
445 int smack_new_label_from_self(char **label)
446 {
447         char *result;
448         int fd;
449         int ret;
450
451         result = calloc(LABEL_LEN + 1, 1);
452         if (result == NULL)
453                 return -1;
454
455         fd = open(SELF_LABEL_FILE, O_RDONLY);
456         if (fd < 0) {
457                 free(result);
458                 return -1;
459         }
460
461         ret = read(fd, result, LABEL_LEN);
462         close(fd);
463         if (ret < 0) {
464                 free(result);
465                 return -1;
466         }
467
468         *label = result;
469         return 0;
470 }
471
472 int smack_new_label_from_socket(int fd, char **label)
473 {
474         char dummy;
475         int ret;
476         socklen_t length = 1;
477         char *result;
478
479         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
480         if (ret < 0 && errno != ERANGE)
481                 return -1;
482
483         result = calloc(length + 1, 1);
484         if (result == NULL)
485                 return -1;
486
487         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
488         if (ret < 0) {
489                 free(result);
490                 return -1;
491         }
492
493         *label = result;
494         return 0;
495 }
496
497 static int accesses_apply(struct smack_accesses *handle, int clear)
498 {
499         char buf[LOAD_LEN + 1];
500         char access_type[ACC_LEN + 1];
501         struct smack_rule *rule;
502         int ret;
503         int fd;
504         int load2 = 1;
505         char path[PATH_MAX];
506
507         if (!smack_mnt) {
508                 errno = EFAULT;
509                 return -1; 
510         }
511         
512         snprintf(path, sizeof path, "%s/load2", smack_mnt);
513         fd = open(path, O_WRONLY);
514         if (fd < 0) {
515                 if (errno != ENOENT)
516                         return -1;
517                 /* fallback */
518                 snprintf(path, sizeof path, "%s/load", smack_mnt);
519                 fd = open(path, O_WRONLY);
520                 if (fd < 0)
521                         return -1;
522                 load2 = 0;
523         }
524
525         if (clear)
526                 strcpy(access_type, "-----");
527
528         for (rule = handle->first; rule != NULL; rule = rule->next) {
529                 if (!clear)
530                         int_to_access_type_k(rule->access_code, access_type);
531
532                 if (load2)
533                         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
534                                        rule->subject, rule->object,
535                                        access_type);
536                 else
537                         ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
538                                        rule->subject, rule->object,
539                                        access_type);
540                 if (ret < 0) {
541                         close(fd);
542                         return -1;
543                 }
544
545                 ret = write(fd, buf, strlen(buf));
546                 if (ret < 0) {
547                         close(fd);
548                         return -1;
549                 }
550         }
551
552         close(fd);
553         return 0;
554 }
555
556 static inline int access_type_to_int(const char *access_type)
557 {
558         int i;
559         unsigned access;
560
561         access = 0;
562         for (i = 0; i < ACC_LEN && access_type[i] != '\0'; i++)
563                 switch (access_type[i]) {
564                 case 'r':
565                 case 'R':
566                         access |= ACC_R;
567                         break;
568                 case 'w':
569                 case 'W':
570                         access |= ACC_W;
571                         break;
572                 case 'x':
573                 case 'X':
574                         access |= ACC_X;
575                         break;
576                 case 'a':
577                 case 'A':
578                         access |= ACC_A;
579                         break;
580                 case 't':
581                 case 'T':
582                         access |= ACC_T;
583                         break;
584                 default:
585                         break;
586                 }
587
588         return access;
589 }
590
591 static inline void int_to_access_type_c(unsigned access, char *str)
592 {
593         int i;
594         i = 0;
595         if ((access & ACC_R) != 0)
596                 str[i++] = 'r';
597         if ((access & ACC_W) != 0)
598                 str[i++] = 'w';
599         if ((access & ACC_X) != 0)
600                 str[i++] = 'x';
601         if ((access & ACC_A) != 0)
602                 str[i++] = 'a';
603         if ((access & ACC_T) != 0)
604                 str[i++] = 't';
605         str[i] = '\0';
606 }
607
608 static inline void int_to_access_type_k(unsigned access, char *str)
609 {
610         str[0] = ((access & ACC_R) != 0) ? 'r' : '-';
611         str[1] = ((access & ACC_W) != 0) ? 'w' : '-';
612         str[2] = ((access & ACC_X) != 0) ? 'x' : '-';
613         str[3] = ((access & ACC_A) != 0) ? 'a' : '-';
614         str[4] = ((access & ACC_T) != 0) ? 't' : '-';
615         str[5] = '\0';
616 }
617
618