~ubuntu-branches/ubuntu/raring/libvirt/raring

« back to all changes in this revision

Viewing changes to tests/securityselinuxtest.c

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-11-19 10:41:02 UTC
  • mfrom: (1.2.15) (223.1.2 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121119104102-l6ewdppikysbzztu
Tags: 1.0.0-0ubuntu2
debian/patches/add-armhf-sysinfo-infomration.patch: Disable
to fix FTBFS on arm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011-2012 Red Hat, Inc.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library.  If not, see
 
16
 * <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 */
 
19
 
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <time.h>
 
27
 
 
28
#include <selinux/selinux.h>
 
29
#include <selinux/context.h>
 
30
 
 
31
#include "internal.h"
 
32
#include "testutils.h"
 
33
#include "memory.h"
 
34
#include "util.h"
 
35
#include "logging.h"
 
36
#include "virterror_internal.h"
 
37
#include "security/security_manager.h"
 
38
 
 
39
 
 
40
#define VIR_FROM_THIS VIR_FROM_NONE
 
41
 
 
42
struct testSELinuxGenLabelData {
 
43
    virSecurityManagerPtr mgr;
 
44
 
 
45
    const char *pidcon;
 
46
 
 
47
    bool dynamic;
 
48
    const char *label;
 
49
    const char *baselabel;
 
50
 
 
51
    const char *user;
 
52
    const char *role;
 
53
    const char *imagerole;
 
54
    const char *type;
 
55
    const char *imagetype;
 
56
 
 
57
    int sensMin;
 
58
    int sensMax;
 
59
    int catMin;
 
60
    int catMax;
 
61
};
 
62
 
 
63
static virDomainDefPtr
 
64
testBuildDomainDef(bool dynamic,
 
65
                   const char *label,
 
66
                   const char *baselabel)
 
67
{
 
68
    virDomainDefPtr def;
 
69
    virSecurityLabelDefPtr secdef;
 
70
 
 
71
    if (VIR_ALLOC(def) < 0)
 
72
        goto no_memory;
 
73
 
 
74
    if (VIR_ALLOC_N(def->seclabels, 1) < 0)
 
75
        goto no_memory;
 
76
 
 
77
    if (VIR_ALLOC(secdef) < 0)
 
78
        goto no_memory;
 
79
 
 
80
    def->seclabels[0] = secdef;
 
81
    def->seclabels[0]->type = dynamic ? VIR_DOMAIN_SECLABEL_DYNAMIC : VIR_DOMAIN_SECLABEL_STATIC;
 
82
 
 
83
    if (label &&
 
84
        !(def->seclabels[0]->label = strdup(label)))
 
85
        goto no_memory;
 
86
 
 
87
    if (baselabel &&
 
88
        !(def->seclabels[0]->baselabel = strdup(baselabel)))
 
89
        goto no_memory;
 
90
 
 
91
    return def;
 
92
 
 
93
no_memory:
 
94
    virReportOOMError();
 
95
    virDomainDefFree(def);
 
96
    return NULL;
 
97
}
 
98
 
 
99
 
 
100
static bool
 
101
testSELinuxCheckCon(context_t con,
 
102
                    const char *user,
 
103
                    const char *role,
 
104
                    const char *type,
 
105
                    int sensMin,
 
106
                    int sensMax ATTRIBUTE_UNUSED,
 
107
                    int catMin,
 
108
                    int catMax)
 
109
{
 
110
    const char *range;
 
111
    char *tmp;
 
112
    int gotSens;
 
113
    int gotCatOne;
 
114
    int gotCatTwo;
 
115
 
 
116
    if (STRNEQ(context_user_get(con), user)) {
 
117
        fprintf(stderr, "Expect user %s got %s\n",
 
118
                user, context_user_get(con));
 
119
        return false;
 
120
    }
 
121
    if (STRNEQ(context_role_get(con), role)) {
 
122
        fprintf(stderr, "Expect role %s got %s\n",
 
123
                role, context_role_get(con));
 
124
        return false;
 
125
    }
 
126
    if (STRNEQ(context_type_get(con), type)) {
 
127
        fprintf(stderr, "Expect type %s got %s\n",
 
128
                type, context_type_get(con));
 
129
        return false;
 
130
    }
 
131
 
 
132
    range = context_range_get(con);
 
133
    if (range[0] != 's') {
 
134
        fprintf(stderr, "Malformed range %s, cannot find sensitivity\n",
 
135
                range);
 
136
        return false;
 
137
    }
 
138
    if (virStrToLong_i(range + 1, &tmp, 10, &gotSens) < 0 ||
 
139
        !tmp) {
 
140
        fprintf(stderr, "Malformed range %s, cannot parse sensitivity\n",
 
141
                range + 1);
 
142
        return false;
 
143
    }
 
144
    if (*tmp != ':') {
 
145
        fprintf(stderr, "Malformed range %s, too many sensitivity values\n",
 
146
                tmp);
 
147
        return false;
 
148
    }
 
149
    tmp++;
 
150
    if (*tmp != 'c') {
 
151
        fprintf(stderr, "Malformed range %s, cannot find first category\n",
 
152
                tmp);
 
153
        return false;
 
154
    }
 
155
    tmp++;
 
156
    if (virStrToLong_i(tmp, &tmp, 10, &gotCatOne) < 0) {
 
157
        fprintf(stderr, "Malformed range %s, cannot parse category one\n",
 
158
                tmp);
 
159
        return false;
 
160
    }
 
161
    if (tmp && *tmp == ',')
 
162
        tmp++;
 
163
    if (tmp && *tmp == 'c') {
 
164
        tmp++;
 
165
        if (virStrToLong_i(tmp, &tmp, 10, &gotCatTwo) < 0) {
 
166
            fprintf(stderr, "Malformed range %s, cannot parse category two\n",
 
167
                    tmp);
 
168
            return false;
 
169
        }
 
170
        if (*tmp != '\0') {
 
171
            fprintf(stderr, "Malformed range %s, junk after second category\n",
 
172
                    tmp);
 
173
            return false;
 
174
        }
 
175
        if (gotCatOne == gotCatTwo) {
 
176
            fprintf(stderr, "Saw category pair %d,%d where cats were equal\n",
 
177
                    gotCatOne, gotCatTwo);
 
178
            return false;
 
179
        }
 
180
    } else {
 
181
        gotCatTwo = gotCatOne;
 
182
    }
 
183
 
 
184
    if (gotSens != sensMin) {
 
185
        fprintf(stderr, "Sensitivity %d is not equal to min %d\n",
 
186
                gotSens, sensMin);
 
187
        return false;
 
188
    }
 
189
    if (gotCatOne < catMin ||
 
190
        gotCatOne > catMax) {
 
191
        fprintf(stderr, "Category one %d is out of range %d-%d\n",
 
192
                gotCatTwo, catMin, catMax);
 
193
        return false;
 
194
    }
 
195
    if (gotCatTwo < catMin ||
 
196
        gotCatTwo > catMax) {
 
197
        fprintf(stderr, "Category two %d is out of range %d-%d\n",
 
198
                gotCatTwo, catMin, catMax);
 
199
        return false;
 
200
    }
 
201
 
 
202
    if (gotCatOne > gotCatTwo) {
 
203
        fprintf(stderr, "Category one %d is greater than category two %d\n",
 
204
                gotCatOne, gotCatTwo);
 
205
        return false;
 
206
    }
 
207
 
 
208
    return true;
 
209
}
 
210
 
 
211
static int
 
212
testSELinuxGenLabel(const void *opaque)
 
213
{
 
214
    const struct testSELinuxGenLabelData *data = opaque;
 
215
    int ret = -1;
 
216
    virDomainDefPtr def;
 
217
    context_t con = NULL;
 
218
    context_t imgcon = NULL;
 
219
 
 
220
    if (setcon_raw((security_context_t)data->pidcon) < 0) {
 
221
        perror("Cannot set process security context");
 
222
        return -1;
 
223
    }
 
224
 
 
225
    if (!(def = testBuildDomainDef(data->dynamic,
 
226
                                   data->label,
 
227
                                   data->baselabel)))
 
228
        goto cleanup;
 
229
 
 
230
    if (virSecurityManagerGenLabel(data->mgr, def) < 0) {
 
231
        virErrorPtr err = virGetLastError();
 
232
        fprintf(stderr, "Cannot generated label %s\n", err->message);
 
233
        goto cleanup;
 
234
    }
 
235
 
 
236
    VIR_DEBUG("label=%s imagelabel=%s",
 
237
              def->seclabels[0]->label, def->seclabels[0]->imagelabel);
 
238
 
 
239
    if (!(con = context_new(def->seclabels[0]->label)))
 
240
        goto cleanup;
 
241
    if (!(imgcon = context_new(def->seclabels[0]->imagelabel)))
 
242
        goto cleanup;
 
243
 
 
244
    if (!testSELinuxCheckCon(con,
 
245
                             data->user, data->role, data->type,
 
246
                             data->sensMin, data->sensMax,
 
247
                             data->catMin, data->catMax))
 
248
        goto cleanup;
 
249
 
 
250
    if (!testSELinuxCheckCon(imgcon,
 
251
                             data->user, data->imagerole, data->imagetype,
 
252
                             data->sensMin, data->sensMax,
 
253
                             data->catMin, data->catMax))
 
254
        goto cleanup;
 
255
 
 
256
    ret = 0;
 
257
 
 
258
cleanup:
 
259
    context_free(con);
 
260
    context_free(imgcon);
 
261
    virDomainDefFree(def);
 
262
    return ret;
 
263
}
 
264
 
 
265
 
 
266
 
 
267
static int
 
268
mymain(void)
 
269
{
 
270
    int ret = 0;
 
271
    virSecurityManagerPtr mgr;
 
272
 
 
273
    if (!(mgr = virSecurityManagerNew("selinux", "QEMU", false, true, false))) {
 
274
        virErrorPtr err = virGetLastError();
 
275
        if (err->code == VIR_ERR_CONFIG_UNSUPPORTED)
 
276
            exit(EXIT_AM_SKIP);
 
277
 
 
278
        fprintf(stderr, "Unable to initialize security driver: %s\n",
 
279
                err->message);
 
280
        exit(EXIT_FAILURE);
 
281
    }
 
282
 
 
283
#define DO_TEST_GEN_LABEL(desc, pidcon,                                 \
 
284
                          dynamic, label, baselabel,                    \
 
285
                          user, role, imageRole,                        \
 
286
                          type, imageType,                              \
 
287
                          sensMin, sensMax, catMin, catMax)             \
 
288
    do {                                                                \
 
289
        struct testSELinuxGenLabelData data = {                         \
 
290
            mgr, pidcon, dynamic, label, baselabel,                     \
 
291
            user, role, imageRole, type, imageType,                     \
 
292
            sensMin, sensMax, catMin, catMax                            \
 
293
        };                                                              \
 
294
        if (virtTestRun("GenLabel " # desc, 1, testSELinuxGenLabel, &data) < 0) \
 
295
            ret = -1;                                                   \
 
296
    } while (0)
 
297
 
 
298
    DO_TEST_GEN_LABEL("dynamic unconfined, s0, c0.c1023",
 
299
                      "unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023",
 
300
                      true, NULL, NULL,
 
301
                      "unconfined_u", "unconfined_r", "object_r",
 
302
                      "svirt_t", "svirt_image_t",
 
303
                      0, 0, 0, 1023);
 
304
    DO_TEST_GEN_LABEL("dynamic virtd, s0, c0.c1023",
 
305
                      "system_u:system_r:virtd_t:s0-s0:c0.c1023",
 
306
                      true, NULL, NULL,
 
307
                      "system_u", "system_r", "object_r",
 
308
                      "svirt_t", "svirt_image_t",
 
309
                      0, 0, 0, 1023);
 
310
    DO_TEST_GEN_LABEL("dynamic virtd, s0, c0.c10",
 
311
                      "system_u:system_r:virtd_t:s0-s0:c0.c10",
 
312
                      true, NULL, NULL,
 
313
                      "system_u", "system_r", "object_r",
 
314
                      "svirt_t", "svirt_image_t",
 
315
                      0, 0, 0, 10);
 
316
    DO_TEST_GEN_LABEL("dynamic virtd, s2-s3, c0.c1023",
 
317
                      "system_u:system_r:virtd_t:s2-s3:c0.c1023",
 
318
                      true, NULL, NULL,
 
319
                      "system_u", "system_r", "object_r",
 
320
                      "svirt_t", "svirt_image_t",
 
321
                      2, 3, 0, 1023);
 
322
 
 
323
    return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 
324
}
 
325
 
 
326
VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/libsecurityselinuxhelper.so")