2016-11-23 10:52:57 +00:00
|
|
|
/*
|
|
|
|
* qemu_security.c: QEMU security management
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "qemu_domain.h"
|
2020-07-20 14:18:57 +00:00
|
|
|
#include "qemu_namespace.h"
|
2016-11-23 10:52:57 +00:00
|
|
|
#include "qemu_security.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_QEMU
|
|
|
|
|
2020-06-23 07:58:12 +00:00
|
|
|
VIR_LOG_INIT("qemu.qemu_security");
|
2016-11-23 10:52:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecuritySetAllLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
2020-06-30 20:02:19 +00:00
|
|
|
const char *incomingPath,
|
2019-09-11 05:53:09 +00:00
|
|
|
bool migrated)
|
2016-11-23 10:52:57 +00:00
|
|
|
{
|
2016-12-15 15:47:15 +00:00
|
|
|
int ret = -1;
|
2017-05-29 12:27:51 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2016-12-15 15:47:15 +00:00
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2016-12-15 15:47:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerSetAllLabel(driver->securityManager,
|
|
|
|
vm->def,
|
2020-06-30 20:02:19 +00:00
|
|
|
incomingPath,
|
2019-09-11 05:53:09 +00:00
|
|
|
priv->chardevStdioLogd,
|
|
|
|
migrated) < 0)
|
2016-12-15 15:47:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2016-12-15 15:47:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
2016-11-23 10:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
qemuSecurityRestoreAllLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
bool migrated)
|
|
|
|
{
|
2017-05-29 12:27:51 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
bool transactionStarted = false;
|
|
|
|
|
|
|
|
/* In contrast to qemuSecuritySetAllLabel, do not use vm->pid
|
|
|
|
* here. This function is called from qemuProcessStop() which
|
|
|
|
* is meant to do cleanup after qemu process died. The
|
|
|
|
* domain's namespace is gone as qemu was the only process
|
|
|
|
* running there. We would not succeed in entering the
|
|
|
|
* namespace then. */
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) >= 0)
|
|
|
|
transactionStarted = true;
|
2017-05-29 12:27:51 +00:00
|
|
|
|
2017-01-20 09:06:39 +00:00
|
|
|
virSecurityManagerRestoreAllLabel(driver->securityManager,
|
|
|
|
vm->def,
|
2017-05-29 12:27:51 +00:00
|
|
|
migrated,
|
|
|
|
priv->chardevStdioLogd);
|
2018-09-05 12:00:20 +00:00
|
|
|
|
|
|
|
if (transactionStarted &&
|
2018-11-13 09:57:25 +00:00
|
|
|
virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
-1, priv->rememberOwner) < 0)
|
2018-09-05 12:00:20 +00:00
|
|
|
VIR_WARN("Unable to run security manager transaction");
|
|
|
|
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
2016-11-23 10:52:57 +00:00
|
|
|
}
|
2016-11-15 15:53:04 +00:00
|
|
|
|
|
|
|
|
2017-02-03 16:09:33 +00:00
|
|
|
int
|
|
|
|
qemuSecuritySetImageLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
2019-01-23 12:37:00 +00:00
|
|
|
virStorageSourcePtr src,
|
2020-02-27 10:20:51 +00:00
|
|
|
bool backingChain,
|
|
|
|
bool chainTop)
|
2017-02-03 16:09:33 +00:00
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2017-02-03 16:09:33 +00:00
|
|
|
int ret = -1;
|
2019-01-23 12:37:00 +00:00
|
|
|
virSecurityDomainImageLabelFlags labelFlags = 0;
|
|
|
|
|
|
|
|
if (backingChain)
|
|
|
|
labelFlags |= VIR_SECURITY_DOMAIN_IMAGE_LABEL_BACKING_CHAIN;
|
2017-02-03 16:09:33 +00:00
|
|
|
|
2020-02-27 10:20:51 +00:00
|
|
|
if (chainTop)
|
|
|
|
labelFlags |= VIR_SECURITY_DOMAIN_IMAGE_PARENT_CHAIN_TOP;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-02-03 16:09:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerSetImageLabel(driver->securityManager,
|
2019-01-23 12:37:00 +00:00
|
|
|
vm->def, src, labelFlags) < 0)
|
2017-02-03 16:09:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-02-03 16:09:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecurityRestoreImageLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
2019-01-23 12:37:00 +00:00
|
|
|
virStorageSourcePtr src,
|
|
|
|
bool backingChain)
|
2017-02-03 16:09:33 +00:00
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2017-02-03 16:09:33 +00:00
|
|
|
int ret = -1;
|
2019-01-23 12:37:00 +00:00
|
|
|
virSecurityDomainImageLabelFlags labelFlags = 0;
|
|
|
|
|
|
|
|
if (backingChain)
|
|
|
|
labelFlags |= VIR_SECURITY_DOMAIN_IMAGE_LABEL_BACKING_CHAIN;
|
2017-02-03 16:09:33 +00:00
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-02-03 16:09:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerRestoreImageLabel(driver->securityManager,
|
2019-01-23 12:37:00 +00:00
|
|
|
vm->def, src, labelFlags) < 0)
|
2017-02-03 16:09:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-01-18 08:50:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
2016-11-15 15:53:04 +00:00
|
|
|
}
|
2016-11-16 14:27:47 +00:00
|
|
|
|
|
|
|
|
2019-03-21 15:36:38 +00:00
|
|
|
int
|
|
|
|
qemuSecurityMoveImageMetadata(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virStorageSourcePtr src,
|
|
|
|
virStorageSourcePtr dst)
|
|
|
|
{
|
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
|
|
|
pid_t pid = -1;
|
|
|
|
|
|
|
|
if (!priv->rememberOwner)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
return virSecurityManagerMoveImageMetadata(driver->securityManager, pid, src, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-16 14:27:47 +00:00
|
|
|
int
|
|
|
|
qemuSecuritySetHostdevLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virDomainHostdevDefPtr hostdev)
|
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2017-01-18 08:50:14 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-01-18 08:50:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerSetHostdevLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
hostdev,
|
|
|
|
NULL) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-01-18 08:50:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
2016-11-16 14:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecurityRestoreHostdevLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virDomainHostdevDefPtr hostdev)
|
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2017-01-18 08:50:14 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-01-18 08:50:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerRestoreHostdevLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
hostdev,
|
|
|
|
NULL) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-01-18 08:50:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
2016-11-16 14:27:47 +00:00
|
|
|
}
|
2016-08-04 13:26:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecuritySetMemoryLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virDomainMemoryDefPtr mem)
|
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2016-08-04 13:26:09 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2016-08-04 13:26:09 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerSetMemoryLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
mem) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2016-08-04 13:26:09 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecurityRestoreMemoryLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virDomainMemoryDefPtr mem)
|
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2016-08-04 13:26:09 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2016-08-04 13:26:09 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerRestoreMemoryLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
mem) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2016-08-04 13:26:09 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-11-21 12:55:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecuritySetInputLabel(virDomainObjPtr vm,
|
|
|
|
virDomainInputDefPtr input)
|
|
|
|
{
|
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
|
|
|
virQEMUDriverPtr driver = priv->driver;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2017-11-21 12:55:12 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-11-21 12:55:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerSetInputLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
input) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-11-21 12:55:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecurityRestoreInputLabel(virDomainObjPtr vm,
|
|
|
|
virDomainInputDefPtr input)
|
|
|
|
{
|
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
|
|
|
virQEMUDriverPtr driver = priv->driver;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2017-11-21 12:55:12 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-11-21 12:55:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerRestoreInputLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
input) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-11-21 12:55:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-01 12:10:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecuritySetChardevLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virDomainChrDefPtr chr)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2017-12-01 12:10:35 +00:00
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-12-01 12:10:35 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerSetChardevLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
chr->source,
|
|
|
|
priv->chardevStdioLogd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-12-01 12:10:35 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecurityRestoreChardevLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virDomainChrDefPtr chr)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
|
|
|
|
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
2017-12-01 12:10:35 +00:00
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2017-12-01 12:10:35 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerRestoreChardevLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
chr->source,
|
|
|
|
priv->chardevStdioLogd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2017-12-01 12:10:35 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-04-04 16:40:32 +00:00
|
|
|
|
|
|
|
|
2019-09-23 10:44:34 +00:00
|
|
|
/*
|
|
|
|
* qemuSecurityStartVhostUserGPU:
|
|
|
|
*
|
|
|
|
* @driver: the QEMU driver
|
|
|
|
* @vm: the domain object
|
|
|
|
* @cmd: the command to run
|
|
|
|
* @existstatus: pointer to int returning exit status of process
|
|
|
|
* @cmdret: pointer to int returning result of virCommandRun
|
|
|
|
*
|
2020-07-09 04:42:21 +00:00
|
|
|
* Start the vhost-user-gpu process with appropriate labels.
|
2019-09-23 10:44:34 +00:00
|
|
|
* This function returns -1 on security setup error, 0 if all the
|
|
|
|
* setup was done properly. In case the virCommand failed to run
|
|
|
|
* 0 is returned but cmdret is set appropriately with the process
|
|
|
|
* exitstatus also set.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
qemuSecurityStartVhostUserGPU(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virCommandPtr cmd,
|
|
|
|
int *exitstatus,
|
|
|
|
int *cmdret)
|
|
|
|
{
|
|
|
|
if (virSecurityManagerSetChildProcessLabel(driver->securityManager,
|
|
|
|
vm->def, cmd) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virSecurityManagerPreFork(driver->securityManager) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*cmdret = virCommandRun(cmd, exitstatus);
|
|
|
|
|
|
|
|
virSecurityManagerPostFork(driver->securityManager);
|
|
|
|
|
|
|
|
if (*cmdret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-04 16:40:32 +00:00
|
|
|
/*
|
|
|
|
* qemuSecurityStartTPMEmulator:
|
|
|
|
*
|
|
|
|
* @driver: the QEMU driver
|
2018-11-13 14:14:43 +00:00
|
|
|
* @vm: the domain object
|
2018-04-04 16:40:32 +00:00
|
|
|
* @cmd: the command to run
|
|
|
|
* @uid: the uid to run the emulator
|
|
|
|
* @gid: the gid to run the emulator
|
|
|
|
* @existstatus: pointer to int returning exit status of process
|
|
|
|
* @cmdret: pointer to int returning result of virCommandRun
|
|
|
|
*
|
2018-12-04 17:08:14 +00:00
|
|
|
* Start the TPM emulator with appropriate labels. Apply security
|
2018-04-04 16:40:32 +00:00
|
|
|
* labels to files first.
|
|
|
|
* This function returns -1 on security setup error, 0 if all the
|
|
|
|
* setup was done properly. In case the virCommand failed to run
|
|
|
|
* 0 is returned but cmdret is set appropriately with the process
|
|
|
|
* exitstatus also set.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
qemuSecurityStartTPMEmulator(virQEMUDriverPtr driver,
|
2018-11-13 14:14:43 +00:00
|
|
|
virDomainObjPtr vm,
|
2018-04-04 16:40:32 +00:00
|
|
|
virCommandPtr cmd,
|
|
|
|
uid_t uid,
|
|
|
|
gid_t gid,
|
|
|
|
int *exitstatus,
|
|
|
|
int *cmdret)
|
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-04-04 16:40:32 +00:00
|
|
|
int ret = -1;
|
2018-09-05 12:00:20 +00:00
|
|
|
bool transactionStarted = false;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
|
|
|
return -1;
|
|
|
|
transactionStarted = true;
|
2018-04-04 16:40:32 +00:00
|
|
|
|
|
|
|
if (virSecurityManagerSetTPMLabels(driver->securityManager,
|
2018-11-13 14:14:43 +00:00
|
|
|
vm->def) < 0) {
|
2018-09-05 12:00:20 +00:00
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
-1, priv->rememberOwner) < 0)
|
2019-07-26 21:07:05 +00:00
|
|
|
goto cleanup_abort;
|
2018-09-05 12:00:20 +00:00
|
|
|
transactionStarted = false;
|
2018-04-04 16:40:32 +00:00
|
|
|
|
2019-08-08 14:54:59 +00:00
|
|
|
if (qemuSecurityCommandRun(driver, vm, cmd, uid, gid, exitstatus, cmdret) < 0)
|
2018-04-04 16:40:32 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if (*cmdret < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup:
|
2018-09-05 12:00:20 +00:00
|
|
|
if (!transactionStarted &&
|
|
|
|
virSecurityManagerTransactionStart(driver->securityManager) >= 0)
|
|
|
|
transactionStarted = true;
|
|
|
|
|
2018-11-13 14:14:43 +00:00
|
|
|
virSecurityManagerRestoreTPMLabels(driver->securityManager, vm->def);
|
2018-04-04 16:40:32 +00:00
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (transactionStarted &&
|
2018-11-13 09:57:25 +00:00
|
|
|
virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
-1, priv->rememberOwner) < 0)
|
2018-09-05 12:00:20 +00:00
|
|
|
VIR_WARN("Unable to run security manager transaction");
|
|
|
|
|
2019-07-26 21:07:05 +00:00
|
|
|
cleanup_abort:
|
2018-09-05 12:00:20 +00:00
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
2018-04-04 16:40:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
qemuSecurityCleanupTPMEmulator(virQEMUDriverPtr driver,
|
2018-11-13 14:14:43 +00:00
|
|
|
virDomainObjPtr vm)
|
2018-04-04 16:40:32 +00:00
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
bool transactionStarted = false;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) >= 0)
|
|
|
|
transactionStarted = true;
|
|
|
|
|
2018-11-13 14:14:43 +00:00
|
|
|
virSecurityManagerRestoreTPMLabels(driver->securityManager, vm->def);
|
2018-09-05 12:00:20 +00:00
|
|
|
|
|
|
|
if (transactionStarted &&
|
2018-11-13 09:57:25 +00:00
|
|
|
virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
-1, priv->rememberOwner) < 0)
|
2018-09-05 12:00:20 +00:00
|
|
|
VIR_WARN("Unable to run security manager transaction");
|
|
|
|
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
2018-04-04 16:40:32 +00:00
|
|
|
}
|
2018-09-05 09:19:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuSecurityDomainSetPathLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
const char *path,
|
|
|
|
bool allowSubtree)
|
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2018-09-05 09:19:14 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-09-05 12:00:20 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2018-09-05 09:19:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSecurityManagerDomainSetPathLabel(driver->securityManager,
|
|
|
|
vm->def,
|
|
|
|
path,
|
|
|
|
allowSubtree) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2018-09-05 09:19:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-09-05 09:29:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2020-06-17 10:40:28 +00:00
|
|
|
qemuSecurityDomainRestorePathLabel(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
2020-06-30 17:49:12 +00:00
|
|
|
const char *path)
|
2018-09-05 09:29:46 +00:00
|
|
|
{
|
2018-11-13 09:57:25 +00:00
|
|
|
qemuDomainObjPrivatePtr priv = vm->privateData;
|
2018-09-05 12:00:20 +00:00
|
|
|
pid_t pid = -1;
|
2018-09-05 09:29:46 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2020-06-30 17:49:12 +00:00
|
|
|
if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT))
|
2018-09-05 12:00:20 +00:00
|
|
|
pid = vm->pid;
|
|
|
|
|
|
|
|
if (virSecurityManagerTransactionStart(driver->securityManager) < 0)
|
2018-09-05 09:29:46 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2020-06-17 09:32:53 +00:00
|
|
|
if (virSecurityManagerDomainRestorePathLabel(driver->securityManager,
|
2018-09-05 09:29:46 +00:00
|
|
|
vm->def,
|
2020-06-17 10:40:28 +00:00
|
|
|
path) < 0)
|
2018-09-05 09:29:46 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-11-13 09:57:25 +00:00
|
|
|
if (virSecurityManagerTransactionCommit(driver->securityManager,
|
|
|
|
pid, priv->rememberOwner) < 0)
|
2018-09-05 09:29:46 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virSecurityManagerTransactionAbort(driver->securityManager);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-08-08 14:54:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemuSecurityCommandRun:
|
|
|
|
* @driver: the QEMU driver
|
|
|
|
* @vm: the domain object
|
|
|
|
* @cmd: the command to run
|
|
|
|
* @uid: the uid to force
|
|
|
|
* @gid: the gid to force
|
|
|
|
* @existstatus: pointer to int returning exit status of process
|
|
|
|
* @cmdret: pointer to int returning result of virCommandRun
|
|
|
|
*
|
|
|
|
* Run @cmd with seclabels set on it. If @uid and/or @gid are not
|
|
|
|
* -1 then their value is enforced.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
|
|
|
* -1 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
qemuSecurityCommandRun(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
virCommandPtr cmd,
|
|
|
|
uid_t uid,
|
|
|
|
gid_t gid,
|
|
|
|
int *exitstatus,
|
|
|
|
int *cmdret)
|
|
|
|
{
|
|
|
|
if (virSecurityManagerSetChildProcessLabel(driver->securityManager,
|
|
|
|
vm->def, cmd) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (uid != (uid_t) -1)
|
|
|
|
virCommandSetUID(cmd, uid);
|
|
|
|
if (gid != (gid_t) -1)
|
|
|
|
virCommandSetGID(cmd, gid);
|
|
|
|
|
|
|
|
if (virSecurityManagerPreFork(driver->securityManager) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*cmdret = virCommandRun(cmd, exitstatus);
|
|
|
|
|
|
|
|
virSecurityManagerPostFork(driver->securityManager);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|