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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "android_common.h"
 
// For 'system', 'product' (optional), 'vendor' (mandatory) and/or 'odm' (optional).
#define MAX_FILE_CONTEXT_SIZE 4
 
#ifdef __ANDROID_VNDK__
#ifndef LOG_EVENT_STRING
#define LOG_EVENT_STRING(...)
#endif  // LOG_EVENT_STRING
#endif  // __ANDROID_VNDK__
 
static const struct selinux_opt seopts_service_plat[] = {
    { SELABEL_OPT_PATH, "/system/etc/selinux/plat_service_contexts" },
    { SELABEL_OPT_PATH, "/plat_service_contexts" }
};
static const struct selinux_opt seopts_service_product[] = {
    { SELABEL_OPT_PATH, "/product/etc/selinux/product_service_contexts" },
    { SELABEL_OPT_PATH, "/product_service_contexts" }
};
static const struct selinux_opt seopts_service_vendor[] = {
    { SELABEL_OPT_PATH, "/vendor/etc/selinux/vendor_service_contexts" },
    { SELABEL_OPT_PATH, "/vendor_service_contexts" },
    // TODO: remove nonplat* when no need to retain backward compatibility.
    { SELABEL_OPT_PATH, "/vendor/etc/selinux/nonplat_service_contexts" },
    { SELABEL_OPT_PATH, "/nonplat_service_contexts" }
};
 
static const struct selinux_opt seopts_hwservice_plat[] = {
    { SELABEL_OPT_PATH, "/system/etc/selinux/plat_hwservice_contexts" },
    { SELABEL_OPT_PATH, "/plat_hwservice_contexts" }
};
static const struct selinux_opt seopts_hwservice_product[] = {
    { SELABEL_OPT_PATH, "/product/etc/selinux/product_hwservice_contexts" },
    { SELABEL_OPT_PATH, "/product_hwservice_contexts" }
};
static const struct selinux_opt seopts_hwservice_vendor[] = {
    { SELABEL_OPT_PATH, "/vendor/etc/selinux/vendor_hwservice_contexts" },
    { SELABEL_OPT_PATH, "/vendor_hwservice_contexts" },
    // TODO: remove nonplat* when no need to retain backward compatibility.
    { SELABEL_OPT_PATH, "/vendor/etc/selinux/nonplat_hwservice_contexts" },
    { SELABEL_OPT_PATH, "/nonplat_hwservice_contexts" }
};
static const struct selinux_opt seopts_hwservice_odm[] = {
    { SELABEL_OPT_PATH, "/odm/etc/selinux/odm_hwservice_contexts" },
    { SELABEL_OPT_PATH, "/odm_hwservice_contexts" }
};
 
static const struct selinux_opt seopts_vndservice =
    { SELABEL_OPT_PATH, "/vendor/etc/selinux/vndservice_contexts" };
 
static const struct selinux_opt seopts_vndservice_rootfs =
    { SELABEL_OPT_PATH, "/vndservice_contexts" };
 
struct selabel_handle* selinux_android_service_open_context_handle(const struct selinux_opt* seopts_service,
                                                                   unsigned nopts)
{
    struct selabel_handle* sehandle;
 
    sehandle = selabel_open(SELABEL_CTX_ANDROID_SERVICE,
            seopts_service, nopts);
 
    if (!sehandle) {
        selinux_log(SELINUX_ERROR, "%s: Error getting service context handle (%s)\n",
                __FUNCTION__, strerror(errno));
        return NULL;
    }
    selinux_log(SELINUX_INFO, "SELinux: Loaded service_contexts from:\n");
    for (unsigned i = 0; i < nopts; i++) {
        selinux_log(SELINUX_INFO, "    %s\n", seopts_service[i].value);
    }
    return sehandle;
}
 
struct selabel_handle* selinux_android_service_context_handle(void)
{
    struct selinux_opt seopts_service[MAX_FILE_CONTEXT_SIZE];
    int size = 0;
    unsigned int i;
    for (i = 0; i < ARRAY_SIZE(seopts_service_plat); i++) {
        if (access(seopts_service_plat[i].value, R_OK) != -1) {
            seopts_service[size++] = seopts_service_plat[i];
            break;
        }
    }
    for (i = 0; i < ARRAY_SIZE(seopts_service_product); i++) {
        if (access(seopts_service_product[i].value, R_OK) != -1) {
            seopts_service[size++] = seopts_service_product[i];
            break;
        }
    }
    for (i = 0; i < ARRAY_SIZE(seopts_service_vendor); i++) {
        if (access(seopts_service_vendor[i].value, R_OK) != -1) {
            seopts_service[size++] = seopts_service_vendor[i];
            break;
        }
    }
 
    return selinux_android_service_open_context_handle(seopts_service, size);
}
 
struct selabel_handle* selinux_android_hw_service_context_handle(void)
{
    struct selinux_opt seopts_service[MAX_FILE_CONTEXT_SIZE];
    int size = 0;
    unsigned int i;
    for (i = 0; i < ARRAY_SIZE(seopts_hwservice_plat); i++) {
        if (access(seopts_hwservice_plat[i].value, R_OK) != -1) {
            seopts_service[size++] = seopts_hwservice_plat[i];
            break;
        }
    }
    for (i = 0; i < ARRAY_SIZE(seopts_hwservice_product); i++) {
        if (access(seopts_hwservice_product[i].value, R_OK) != -1) {
            seopts_service[size++] = seopts_hwservice_product[i];
            break;
        }
    }
    for (i = 0; i < ARRAY_SIZE(seopts_hwservice_vendor); i++) {
        if (access(seopts_hwservice_vendor[i].value, R_OK) != -1) {
            seopts_service[size++] = seopts_hwservice_vendor[i];
            break;
        }
    }
    for (i = 0; i < ARRAY_SIZE(seopts_hwservice_odm); i++) {
        if (access(seopts_hwservice_odm[i].value, R_OK) != -1) {
            seopts_service[size++] = seopts_hwservice_odm[i];
            break;
        }
    }
    return selinux_android_service_open_context_handle(seopts_service, size);
}
 
struct selabel_handle* selinux_android_vendor_service_context_handle(void)
{
    const struct selinux_opt* seopts_service;
    if (access(seopts_vndservice.value, R_OK) != -1) {
        seopts_service = &seopts_vndservice;
    } else {
        seopts_service = &seopts_vndservice_rootfs;
    }
 
    return selinux_android_service_open_context_handle(seopts_service, 1);
}
 
int selinux_log_callback(int type, const char *fmt, ...)
{
    va_list ap;
    int priority;
    char *strp;
 
    switch(type) {
    case SELINUX_WARNING:
        priority = ANDROID_LOG_WARN;
        break;
    case SELINUX_INFO:
        priority = ANDROID_LOG_INFO;
        break;
    default:
        priority = ANDROID_LOG_ERROR;
        break;
    }
 
    va_start(ap, fmt);
    if (vasprintf(&strp, fmt, ap) != -1) {
        LOG_PRI(priority, "SELinux", "%s", strp);
        LOG_EVENT_STRING(AUDITD_LOG_TAG, strp);
        free(strp);
    }
    va_end(ap);
    return 0;
}
 
int selinux_vendor_log_callback(int type, const char *fmt, ...)
{
    va_list ap;
    int priority;
    char *strp;
 
    switch(type) {
    case SELINUX_WARNING:
        priority = ANDROID_LOG_WARN;
        break;
    case SELINUX_INFO:
        priority = ANDROID_LOG_INFO;
        break;
    default:
        priority = ANDROID_LOG_ERROR;
        break;
    }
 
    va_start(ap, fmt);
    if (vasprintf(&strp, fmt, ap) != -1) {
        LOG_PRI(priority, "SELinux", "%s", strp);
        free(strp);
    }
    va_end(ap);
    return 0;
}