File: selinux.c

package info (click to toggle)
cppcheck 2.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 26,412 kB
  • sloc: cpp: 272,462; python: 22,408; ansic: 8,088; sh: 1,059; makefile: 1,041; xml: 987; cs: 291
file content (306 lines) | stat: -rw-r--r-- 7,018 bytes parent folder | download | duplicates (6)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

// Test library configuration for selinux.cfg
//
// Usage:
// $ cppcheck --check-library --library=selinux --enable=style,information --inconclusive --error-exitcode=1 --inline-suppr test/cfg/selinux.c
// =>
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
//

#include <stdio.h>

#include <selinux/restorecon.h>
#include <selinux/get_default_type.h>
#include <selinux/label.h>
#include <selinux/context.h>
#include <selinux/get_context_list.h>
#include <selinux/avc.h>

void restorecon(void)
{
    // cppcheck-suppress [ignoredReturnValue, nullPointer, invalidFunctionArgBool]
    selinux_restorecon(NULL, true);

    selinux_restorecon_set_sehandle(NULL);

    // cppcheck-suppress ignoredReturnValue
    selinux_restorecon_default_handle();

    // cppcheck-suppress [ignoredReturnValue, nullPointer]
    selinux_restorecon_set_alt_rootpath(NULL);

    // cppcheck-suppress nullPointer
    selinux_restorecon_set_exclude_list(NULL);

    // cppcheck-suppress ignoredReturnValue
    selinux_restorecon_get_skipped_errors();

    struct dir_xattr **arg3;
    // cppcheck-suppress [ignoredReturnValue, nullPointer, invalidFunctionArgBool, uninitvar]
    selinux_restorecon_xattr(NULL, true, &arg3);
}

void get_default_type_fail(void)
{
    // cppcheck-suppress ignoredReturnValue
    selinux_default_type_path();

    char *type1;
    // FIXME: report ignoredReturnValue
    // cppcheck-suppress [nullPointer]
    get_default_type(NULL, &type1);

    char **type2;
    // FIXME: report ignoredReturnValue
    // cppcheck-suppress [uninitvar]
    get_default_type("object_r", type2);

    // cppcheck-suppress memleak
}

void get_default_type_success(void)
{
    char *type = NULL;
    int err = get_default_type("object_r", &type);
    if (err != 0)
        return;
    free(type);
}

void selabel_fail1(void)
{
    // cppcheck-suppress [unreadVariable, constVariablePointer]
    struct selabel_handle *hnd = selabel_open(SELABEL_CTX_FILE, NULL, 1);

    // cppcheck-suppress resourceLeak
}

void selabel_fail2(void)
{
    struct selabel_handle *hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);

    char *ctx;
    // cppcheck-suppress nullPointerOutOfResources
    selabel_lookup(hnd, &ctx, "/", 0);

    // cppcheck-suppress nullPointerOutOfResources
    selabel_close(hnd);

    // cppcheck-suppress memleak
}

void selabel_success(void)
{
    struct selabel_handle *hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);

    char *ctx;
    // cppcheck-suppress nullPointerOutOfResources
    selabel_lookup(hnd, &ctx, "/", 0);

    freecon(ctx);

    // cppcheck-suppress nullPointerOutOfResources
    (void)selabel_cmp(hnd, hnd);

    // cppcheck-suppress nullPointerOutOfResources
    selabel_stats(hnd);

    // cppcheck-suppress nullPointerOutOfResources
    selabel_close(hnd);
}

void context_fail1(void)
{
    // cppcheck-suppress [unreadVariable, nullPointer]
    context_t con = context_new(NULL);

    // cppcheck-suppress memleak
}

void context_fail2(void)
{
    // cppcheck-suppress unreadVariable
    context_t con = context_new("kernel");

    // cppcheck-suppress memleak
}

void context_success(void)
{
    context_t con = context_new("system_u:system_r:kernel_t:s0");

    printf("%s: %s %s %s %s\n", context_str(con),
           context_type_get(con), context_range_get(con),
           context_role_get(con), context_user_get(con));

    (void)context_type_set(con, "init_t");

    context_free(con);
}

void get_ordered_context_list_fail1(void)
{
    char **ret;
    // cppcheck-suppress nullPointer
    get_ordered_context_list(NULL, NULL, &ret);

    // cppcheck-suppress memleak
}

void get_ordered_context_list_fail2(void)
{
    char **ret;
    get_ordered_context_list("root", NULL, &ret);

    // cppcheck-suppress mismatchAllocDealloc
    freecon((void*)ret);
}

void get_ordered_context_list_success1(void)
{
    char **ret;
    get_ordered_context_list("root", NULL, &ret);
    freeconary(ret);
}

void get_default_context_with_rolelevel_fail1(void)
{
    char *ctx;
    // cppcheck-suppress nullPointer
    get_default_context_with_rolelevel("root", NULL, "s0", "system_u:system_r:init_t:s0", &ctx);

    // cppcheck-suppress memleak
}

void get_default_context_with_rolelevel_fail2(void)
{
    char *ctx;
    get_default_context_with_rolelevel("root", "sysadm_r", NULL, NULL, &ctx);

    // cppcheck-suppress mismatchAllocDealloc
    freeconary((void*)ctx);
}

void get_default_context_with_rolelevel_success1(void)
{
    char *ctx;
    get_default_context_with_rolelevel("root", "sysadm_r", NULL, NULL, &ctx);
    freecon(ctx);
}

void selinux_status_fail1(void)
{
    // cppcheck-suppress [invalidFunctionArg, ignoredReturnValue]
    selinux_status_open(-1);
    // TODO: report leak
}

void selinux_status_success1(void)
{
    (void)selinux_status_open(0);
    (void)selinux_status_updated();
    selinux_status_close();
}

void realpath_not_final_fail1(void)
{
    char buf[64];
    // cppcheck-suppress bufferAccessOutOfBounds
    (void)realpath_not_final("/root", buf);
}

void realpath_not_final_success1(void)
{
#define PATH_MAX 4096
    char buf[PATH_MAX + 1];
    // cppcheck-suppress ignoredReturnValue
    realpath_not_final("/root", buf);
}

void selinux_getpolicytype_fail1(void)
{
    // cppcheck-suppress nullPointer
    selinux_getpolicytype(NULL);
}

void selinux_getpolicytype_fail2(void)
{
    char *type;
    (void)selinux_getpolicytype(&type);

    // cppcheck-suppress memleak
}

void selinux_check_access_fail1(void)
{
    const char *msg = "Hello World!";
    // cppcheck-suppress [ignoredReturnValue, nullPointer]
    selinux_check_access("foo", "bar", NULL, "baz", msg);
}

void selinux_check_access_success1(void)
{
    (void)selinux_check_access("kernel", "init", "file", "write", NULL);
}

void selinux_trans_to_raw_context_fail1(void)
{
    // FIXME: report ignoredReturnValue
    // cppcheck-suppress nullPointer
    selinux_trans_to_raw_context("kernel", NULL);
}

void selinux_trans_to_raw_context_fail2(void)
{
    char *ctx;
    // FIXME: report ignoredReturnValue
    selinux_trans_to_raw_context("kernel", &ctx);

    // cppcheck-suppress memleak
}

void selinux_trans_to_raw_context_success1(void)
{
    char *ctx;
    (void)selinux_trans_to_raw_context("kernel", &ctx);
    free(ctx);
}

void getseuserbyname_fail1(void)
{
    char *seuser, *level;
    // cppcheck-suppress nullPointer
    getseuserbyname(NULL, &seuser, &level);
    free(seuser);

    // cppcheck-suppress memleak
}

void getseuserbyname_fail2(void)
{
    char *seuser, *level;
    getseuserbyname("root", &seuser, &level);
    free(level);

    // FIXME: report memleak
}

void getseuserbyname_success1(void)
{
    char *seuser, *level;
    getseuserbyname("root", &seuser, &level);
    free(seuser);
    free(level);
}

void danger1(void)
{
    // cppcheck-suppress selinux_reset_configCalled
    selinux_reset_config();
}

void danger2(void)
{
    // cppcheck-suppress [security_disableCalled, ignoredReturnValue]
    security_disable();
}