liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pwd.h>
#include <limits.h>
#include "selinux_internal.h"
#include "context_internal.h"
 
static int get_customizable_type_list(char *** retlist)
{
   FILE *fp;
   char *buf;
   unsigned int ctr = 0, i;
   char **list = NULL;
 
   fp = fopen(selinux_customizable_types_path(), "re");
   if (!fp)
       return -1;
 
   buf = malloc(selinux_page_size);
   if (!buf) {
       fclose(fp);
       return -1;
   }
   while (fgets_unlocked(buf, selinux_page_size, fp) && ctr < UINT_MAX) {
       ctr++;
   }
   rewind(fp);
   if (ctr) {
       list =
           (char **) calloc(sizeof(char *),
                         ctr + 1);
       if (list) {
           i = 0;
           while (fgets_unlocked(buf, selinux_page_size, fp)
                  && i < ctr) {
               buf[strlen(buf) - 1] = 0;
               list[i] = (char *) strdup(buf);
               if (!list[i]) {
                   unsigned int j;
                   for (j = 0; j < i; j++)
                       free(list[j]);
                   free(list);
                   list = NULL;
                   break;
               }
               i++;
           }
       }
   }
   fclose(fp);
   free(buf);
   if (!list)
       return -1;
   *retlist = list;
   return 0;
}
 
static char **customizable_list = NULL;
 
int is_context_customizable(const char * scontext)
{
   int i;
   const char *type;
   context_t c;
 
   if (!customizable_list) {
       if (get_customizable_type_list(&customizable_list) != 0)
           return -1;
   }
 
   c = context_new(scontext);
   if (!c)
       return -1;
 
   type = context_type_get(c);
   if (!type) {
       context_free(c);
       return -1;
   }
 
   for (i = 0; customizable_list[i]; i++) {
       if (strcmp(customizable_list[i], type) == 0) {
           context_free(c);
           return 1;
       }
   }
   context_free(c);
   return 0;
}