security_selinux.c 11.8 KB
Newer Older
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
/*
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Authors:
 *     James Morris <jmorris@namei.org>
 *
 * SELinux security driver.
 */
#include <config.h>
#include <selinux/selinux.h>
#include <selinux/context.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "security.h"
#include "security_selinux.h"
#include "virterror_internal.h"
#include "util.h"
#include "memory.h"

D
Daniel P. Berrange 已提交
27 28 29

#define VIR_FROM_THIS VIR_FROM_SECURITY

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static char default_domain_context[1024];
static char default_image_context[1024];
#define SECURITY_SELINUX_VOID_DOI       "0"
#define SECURITY_SELINUX_NAME "selinux"

/* TODO
   The data struct of used mcs should be replaced with a better data structure in the future
*/

struct MCS {
    char *mcs;
    struct MCS *next;
};
static struct MCS *mcsList = NULL;

static int
mcsAdd(const char *mcs)
{
    struct MCS *ptr;

    for (ptr = mcsList; ptr; ptr = ptr->next) {
D
Daniel P. Berrange 已提交
51
        if (STREQ(ptr->mcs, mcs))
52 53
            return -1;
    }
D
Daniel P. Berrange 已提交
54 55
    if (VIR_ALLOC(ptr) < 0)
        return -1;
56 57 58 59 60 61 62 63 64 65 66 67 68
    ptr->mcs = strdup(mcs);
    ptr->next = mcsList;
    mcsList = ptr;
    return 0;
}

static int
mcsRemove(const char *mcs)
{
    struct MCS *prevptr = NULL;
    struct MCS *ptr = NULL;

    for (ptr = mcsList; ptr; ptr = ptr->next) {
D
Daniel P. Berrange 已提交
69
        if (STREQ(ptr->mcs, mcs)) {
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
            if (prevptr)
                prevptr->next = ptr->next;
            else {
                mcsList = ptr->next;
            }
            free(ptr->mcs);
            free(ptr);
            return 0;
        }
        prevptr = ptr;
    }
    return -1;
}

static char *
SELinuxGenNewContext(const char *oldcontext, const char *mcs)
{
    char *newcontext = NULL;
    char *scontext = strdup(oldcontext);
    if (!scontext) goto err;
    context_t con = context_new(scontext);
    if (!con) goto err;
    context_range_set(con, mcs);
    newcontext = strdup(context_str(con));
    context_free(con);
err:
    freecon(scontext);
    return (newcontext);
}

static int
SELinuxInitialize(virConnectPtr conn)
{
    char *ptr = NULL;
    int fd = 0;
    char ebuf[1024];

    virRandomInitialize(time(NULL) ^ getpid());

    fd = open(selinux_virtual_domain_context_path(), O_RDONLY);
    if (fd < 0) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("%s: cannot open SELinux virtual domain context file %s: %s"),
                               __func__,selinux_virtual_domain_context_path(),
                               virStrerror(errno, ebuf, sizeof ebuf));
        return -1;
    }

    if (saferead(fd, default_domain_context, sizeof(default_domain_context)) < 0) {
D
Daniel P. Berrange 已提交
119
       virSecurityReportError(conn, VIR_ERR_ERROR,
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
                               _("%s: cannot read SELinux virtual domain context file %s: %s"),
                               __func__,selinux_virtual_domain_context_path(),
                               virStrerror(errno, ebuf, sizeof ebuf));
        close(fd);
        return -1;
    }
    close(fd);

    ptr = strchrnul(default_domain_context, '\n');
    *ptr = '\0';

    if ((fd = open(selinux_virtual_image_context_path(), O_RDONLY)) < 0) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("%s: cannot open SELinux virtual image context file %s: %s"),
                               __func__,selinux_virtual_image_context_path(),
                               virStrerror(errno, ebuf, sizeof ebuf));
        return -1;
    }

    if (saferead(fd, default_image_context, sizeof(default_image_context)) < 0) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("%s: cannot read SELinux virtual image context file %s: %s"),
                               __func__,selinux_virtual_image_context_path(),
                               virStrerror(errno, ebuf, sizeof ebuf));
        close(fd);
        return -1;
    }
    close(fd);

    ptr = strchrnul(default_image_context, '\n');
    *ptr = '\0';

    return 0;
}

static int
D
Daniel P. Berrange 已提交
156 157
SELinuxGenSecurityLabel(virConnectPtr conn,
                        virDomainObjPtr vm)
158 159 160 161 162 163 164 165
{
    int rc = -1;
    char mcs[1024];
    char *scontext = NULL;
    int c1 = 0;
    int c2 = 0;
    if ( ( vm->def->seclabel.label ) ||
         ( vm->def->seclabel.model ) ||
D
Daniel P. Berrange 已提交
166 167 168
         ( vm->def->seclabel.imagelabel )) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               "%s", _("security labellin already defined for VM"));
169
        return rc;
D
Daniel P. Berrange 已提交
170
    }
171 172 173 174 175 176 177 178

    do {
        c1 = virRandom(1024);
        c2 = virRandom(1024);

        if ( c1 == c2 ) {
            sprintf(mcs, "s0:c%d", c1);
        } else {
D
Daniel P. Berrange 已提交
179
            if ( c1 < c2 )
180 181 182 183 184 185 186
                sprintf(mcs, "s0:c%d,c%d", c1, c2);
            else
                sprintf(mcs, "s0:c%d,c%d", c2, c1);
        }
    } while(mcsAdd(mcs) == -1);

    vm->def->seclabel.label = SELinuxGenNewContext(default_domain_context, mcs);
D
Daniel P. Berrange 已提交
187 188 189 190 191
    if (! vm->def->seclabel.label)  {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("cannot generate selinux context for %s"), mcs);
        goto err;
    }
192
    vm->def->seclabel.imagelabel = SELinuxGenNewContext(default_image_context, mcs);
D
Daniel P. Berrange 已提交
193 194 195 196 197
    if (! vm->def->seclabel.imagelabel)  {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("cannot generate selinux context for %s"), mcs);
        goto err;
    }
198
    vm->def->seclabel.model = strdup(SECURITY_SELINUX_NAME);
D
Daniel P. Berrange 已提交
199 200 201 202 203
    if (! vm->def->seclabel.model) {
        virReportOOMError(conn);
        goto err;
    }

204 205 206 207

    rc = 0;
    goto done;
err:
D
Daniel P. Berrange 已提交
208 209 210
    VIR_FREE(vm->def->seclabel.label);
    VIR_FREE(vm->def->seclabel.imagelabel);
    VIR_FREE(vm->def->seclabel.model);
211
done:
D
Daniel P. Berrange 已提交
212
    VIR_FREE(scontext);
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    return rc;
}

static int
SELinuxSecurityDriverProbe(void)
{
    return is_selinux_enabled() ? SECURITY_DRIVER_ENABLE : SECURITY_DRIVER_DISABLE;
}

static int
SELinuxSecurityDriverOpen(virConnectPtr conn, virSecurityDriverPtr drv)
{
    /*
     * Where will the DOI come from?  SELinux configuration, or qemu
     * configuration? For the moment, we'll just set it to "0".
     */
    virSecurityDriverSetDOI(conn, drv, SECURITY_SELINUX_VOID_DOI);
    return SELinuxInitialize(conn);
}

static int
SELinuxGetSecurityLabel(virConnectPtr conn,
                        virDomainObjPtr vm,
                        virSecurityLabelPtr sec)
{
    security_context_t ctx;

    if (getpidcon(vm->pid, &ctx) == -1) {
        char ebuf[1024];
        virSecurityReportError(conn, VIR_ERR_ERROR, _("%s: error calling "
                               "getpidcon(): %s"), __func__,
                               virStrerror(errno, ebuf, sizeof ebuf));
        return -1;
    }

    if (strlen((char *) ctx) >= VIR_SECURITY_LABEL_BUFLEN) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("%s: security label exceeds "
                               "maximum lenth: %d"), __func__,
                               VIR_SECURITY_LABEL_BUFLEN - 1);
        return -1;
    }

    strcpy(sec->label, (char *) ctx);
    free(ctx);

    sec->enforcing = security_getenforce();
    if (sec->enforcing == -1) {
        char ebuf[1024];
        virSecurityReportError(conn, VIR_ERR_ERROR, _("%s: error calling "
                               "security_getenforce(): %s"), __func__,
                               virStrerror(errno, ebuf, sizeof ebuf));
        return -1;
    }

    return 0;
}

static int
SELinuxSetFilecon(virConnectPtr conn, char *path, char *tcon)
{
    char ebuf[1024];

    if(setfilecon(path, tcon) < 0) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("%s: unable to set security context "
                                 "'\%s\' on %s: %s."), __func__,
                               tcon,
                               path,
                               virStrerror(errno, ebuf, sizeof ebuf));
        if (security_getenforce() == 1)
            return -1;
    }
    return 0;
}

static int
SELinuxRestoreSecurityImageLabel(virConnectPtr conn,
                                 virDomainObjPtr vm,
                                 virDomainDeviceDefPtr dev)
{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

    if (secdef->imagelabel) {
        return SELinuxSetFilecon(conn, dev->data.disk->src, default_image_context);
    }
    return 0;
}

static int
SELinuxSetSecurityImageLabel(virConnectPtr conn,
                             virDomainObjPtr vm,
                             virDomainDeviceDefPtr dev)

{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

    if (secdef->imagelabel) {
        return SELinuxSetFilecon(conn, dev->data.disk->src, secdef->imagelabel);
    }
    return 0;
}

static int
SELinuxRestoreSecurityLabel(virConnectPtr conn,
                            virDomainObjPtr vm)
{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
    int i;
    int rc = 0;
    if (secdef->imagelabel) {
        for (i = 0 ; i < vm->def->ndisks ; i++) {
            if (SELinuxSetFilecon(conn, vm->def->disks[i]->src, default_image_context) < 0)
                rc = -1;
        }
        VIR_FREE(secdef->model);
        VIR_FREE(secdef->label);
        context_t con = context_new(secdef->imagelabel);
        if (con) {
            mcsRemove(context_range_get(con));
            context_free(con);
        }
        VIR_FREE(secdef->imagelabel);
    }
    return rc;
}

static int
SELinuxSetSecurityLabel(virConnectPtr conn,
                        virSecurityDriverPtr drv,
                        virDomainObjPtr vm)
{
    /* TODO: verify DOI */
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
    int i;
    char ebuf[1024];

    if (!STREQ(drv->name, secdef->model)) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("%s: security label driver mismatch: "
                                 "\'%s\' model configured for domain, but "
                                 "hypervisor driver is \'%s\'."),
                                 __func__, secdef->model, drv->name);
        if (security_getenforce() == 1)
                return -1;
    }

    if (setexeccon(secdef->label) == -1) {
        virSecurityReportError(conn, VIR_ERR_ERROR,
                               _("%s: unable to set security context "
                               "'\%s\': %s."), __func__, secdef->label,
                               virStrerror(errno, ebuf, sizeof ebuf));
        if (security_getenforce() == 1)
                return -1;
    }

    if (secdef->imagelabel) {
        for (i = 0 ; i < vm->def->ndisks ; i++) {
            if(setfilecon(vm->def->disks[i]->src, secdef->imagelabel) < 0) {
                virSecurityReportError(conn, VIR_ERR_ERROR,
                                       _("%s: unable to set security context "
                                         "'\%s\' on %s: %s."), __func__,
                                       secdef->imagelabel,
                                       vm->def->disks[i]->src,
                                       virStrerror(errno, ebuf, sizeof ebuf));
                if (security_getenforce() == 1)
                    return -1;
            }
        }
    }

    return 0;
}

virSecurityDriver virSELinuxSecurityDriver = {
    .name                       = SECURITY_SELINUX_NAME,
    .probe                      = SELinuxSecurityDriverProbe,
    .open                       = SELinuxSecurityDriverOpen,
    .domainSetSecurityImageLabel = SELinuxSetSecurityImageLabel,
    .domainRestoreSecurityImageLabel = SELinuxRestoreSecurityImageLabel,
    .domainGenSecurityLabel     = SELinuxGenSecurityLabel,
    .domainGetSecurityLabel     = SELinuxGetSecurityLabel,
    .domainRestoreSecurityLabel = SELinuxRestoreSecurityLabel,
    .domainSetSecurityLabel     = SELinuxSetSecurityLabel,
};