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