From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826456; cv=none; d=zohomail.com; s=zohoarc; b=YEBYhReauOkeOx0nGE00v+we50arvrVdf4r7T75rqM8YfDBiyHPJq4njSLG1Dj70N9BU+sNbIPL1hqiET7uQHcJDawtEKUSbtBHaTO7hQZXRtwWpE5/FPa5Jj0WafL3oej/7Stg+cfynYEynxpOE2vsFofuc976dYuxitBi8XyQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826456; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=YiKb+y371mNU6ngLCWNId09CEultisUOluzBj6IGhy0=; b=l7kYqSSxs9hbvIhRhGr4ueQ8c8MXonUKMBtf30lUeH1CuL+bxH7Pe9ksg9YFH6E2yapkKrLTekmC3grBNx9Of6C0ljy3cUYKJzC9YcV6muvSaVtXput92hX6pkW6wWt9hUlsvtYQHRlwG6w41lGWgCb9ZVNweJ7WzaWXZnNoRmw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 162082645654583.41860273760881; Wed, 12 May 2021 06:34:16 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-467-oulDb90dOvqbRnZOarL3JA-1; Wed, 12 May 2021 09:34:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D42D66D5CC; Wed, 12 May 2021 13:34:05 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B0F3B5D6AC; Wed, 12 May 2021 13:34:05 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0BA2C1800BB0; Wed, 12 May 2021 13:34:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDY3Md031293 for ; Wed, 12 May 2021 09:34:03 -0400 Received: by smtp.corp.redhat.com (Postfix) id 320E963BA7; Wed, 12 May 2021 13:34:03 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E0C260657; Wed, 12 May 2021 13:34:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826454; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=YiKb+y371mNU6ngLCWNId09CEultisUOluzBj6IGhy0=; b=YQxEhmcjJGfQpqeanXBZH9HBlcDk169Xpj9Mj8cV0dEfIOAAHUEbNf9PU8dMa6Qxm0VXyH 0+SCaiSFyQKClQl0NNNYLO7tQGxYOi+apAn8GIJgSvID1/jx18sswqJ/kKLKF51UXRCuob /NNF1fZW21bgGrj7AfuxnLAmvu8PEFA= X-MC-Unique: oulDb90dOvqbRnZOarL3JA-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 01/10] util: add virRandomToken API Date: Wed, 12 May 2021 14:33:47 +0100 Message-Id: <20210512133356.1162418-2-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) A random token is simply a string of random bytes formatted in hexidecimal. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/libvirt_private.syms | 1 + src/util/virrandom.c | 18 ++++++++++++++++++ src/util/virrandom.h | 1 + 3 files changed, 20 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1b12c49018..23621fcfd0 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3081,6 +3081,7 @@ virRandomBits; virRandomBytes; virRandomGenerateWWN; virRandomInt; +virRandomToken; =20 =20 # util/virresctrl.h diff --git a/src/util/virrandom.c b/src/util/virrandom.c index 3ae1297e6b..c3f3aa1fa6 100644 --- a/src/util/virrandom.c +++ b/src/util/virrandom.c @@ -161,3 +161,21 @@ virRandomGenerateWWN(char **wwn, (unsigned long long)virRandomBits(36)); return 0; } + +char *virRandomToken(size_t len) +{ + g_autofree unsigned char *data =3D g_new0(unsigned char, len); + g_autofree char *token =3D g_new0(char, (len * 2) + 1); + static const char hex[] =3D "0123456789abcdef"; + size_t i; + + if (virRandomBytes(data, len) < 0) + return NULL; + + for (i =3D 0; i < len; i++) { + token[(i*2)] =3D hex[data[i] & 0xf]; + token[(i*2)+1] =3D hex[(data[i] >> 4) & 0xf]; + } + + return g_steal_pointer(&token); +} diff --git a/src/util/virrandom.h b/src/util/virrandom.h index 297721f912..aac684ada9 100644 --- a/src/util/virrandom.h +++ b/src/util/virrandom.h @@ -26,3 +26,4 @@ uint32_t virRandomInt(uint32_t max); int virRandomBytes(unsigned char *buf, size_t buflen) ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE; int virRandomGenerateWWN(char **wwn, const char *virt_type) G_GNUC_NO_INLI= NE; +char *virRandomToken(size_t len); --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826468; cv=none; d=zohomail.com; s=zohoarc; b=VHa2fge5sFUSj0MKfHkfUSYuwQ4oF+xAL1LG5O1RnKE82IhrANd2fW1DFX7Z8jK/YC54ftwrgA/7xl3AawyT8LyGXaZ2RCLfIiOBCXbxhJzqkghO/+56ejK7zNzEQBf5iREv2D+OvOtTI+cACRngivLCKVmtJhbt6tXedRE7a5U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826468; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=v11OXXCZq1ir1SAqDt+0XOocSDlqwiRiwa7DFrUtWSY=; b=jxcG6nyf1CcpdWkgyuLEpMOf9dIbl0ZnZ6glpAFTTdqGSY/q6VSMTJTX8ix3/HswTdqRIosmsopXkopmfqXL3GUfLvfy/l0cBPGaRio8CVo0EJaz5IxX+RbZSaodSs8OJGDIt/TMF+XzZeOmm8MlqRBp5YTNTcBWhJxxQrEq0HA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1620826468492715.0045178659616; Wed, 12 May 2021 06:34:28 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-99-BjEeRGOIM_O303Tqrl2dww-1; Wed, 12 May 2021 09:34:22 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 75010107ACE3; Wed, 12 May 2021 13:34:13 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4983E5D9D7; Wed, 12 May 2021 13:34:13 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 06C7355344; Wed, 12 May 2021 13:34:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDY4VX031301 for ; Wed, 12 May 2021 09:34:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6A79E63BA7; Wed, 12 May 2021 13:34:04 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9400E60657; Wed, 12 May 2021 13:34:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826467; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=v11OXXCZq1ir1SAqDt+0XOocSDlqwiRiwa7DFrUtWSY=; b=NFvMsDhaFmoMbvqZdZT3DWm4RcMG7yGaMro3pVZjw4Plyhe6sduMQR1EVRpz30K3v2quf/ H2PGb25j0x+EVL1XEJfIcgofgYhR4oWGok327Qbuu4W5dpeg/XZnJeyAKovKgnC/Ku9+1t szvoOMtAJNHQ1PqfDcJIFAqXBmfa7Go= X-MC-Unique: BjEeRGOIM_O303Tqrl2dww-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 02/10] util: introduce concept of a system token into identities Date: Wed, 12 May 2021 14:33:48 +0100 Message-Id: <20210512133356.1162418-3-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) We want a way to distinguish between calls from a libvirt daemon, and a regular client application when both are running as the same user account. This is not possible with the current set of attributes recorded against an identity, as there is nothing that is common to all of the modular libvirt daemons, while distinct to all other processes. We thus introduce the idea of a system token, which is simply a random hex string that is only known by the libvirt daemons, to be recorded against the system identity. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/libvirt_private.syms | 2 ++ src/util/viridentity.c | 34 ++++++++++++++++++++++++++++++++++ src/util/viridentity.h | 4 ++++ 3 files changed, 40 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 23621fcfd0..aaae1c8002 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2404,6 +2404,7 @@ virIdentityGetProcessTime; virIdentityGetSASLUserName; virIdentityGetSELinuxContext; virIdentityGetSystem; +virIdentityGetSystemToken; virIdentityGetUNIXGroupID; virIdentityGetUNIXUserID; virIdentityGetUserName; @@ -2416,6 +2417,7 @@ virIdentitySetProcessID; virIdentitySetProcessTime; virIdentitySetSASLUserName; virIdentitySetSELinuxContext; +virIdentitySetSystemToken; virIdentitySetUNIXGroupID; virIdentitySetUNIXUserID; virIdentitySetUserName; diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 7edb6a171a..7da4ea12f5 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -40,6 +40,8 @@ =20 #define VIR_FROM_THIS VIR_FROM_IDENTITY =20 +#define VIR_CONNECT_IDENTITY_SYSTEM_TOKEN "system.token" + VIR_LOG_INIT("util.identity"); =20 struct _virIdentity { @@ -382,6 +384,17 @@ int virIdentityGetSELinuxContext(virIdentity *ident, } =20 =20 +int virIdentityGetSystemToken(virIdentity *ident, + const char **token) +{ + *token =3D NULL; + return virTypedParamsGetString(ident->params, + ident->nparams, + VIR_CONNECT_IDENTITY_SYSTEM_TOKEN, + token); +} + + int virIdentitySetUserName(virIdentity *ident, const char *username) { @@ -554,6 +567,25 @@ int virIdentitySetSELinuxContext(virIdentity *ident, } =20 =20 +int virIdentitySetSystemToken(virIdentity *ident, + const char *token) +{ + if (virTypedParamsGet(ident->params, + ident->nparams, + VIR_CONNECT_IDENTITY_SYSTEM_TOKEN)) { + virReportError(VIR_ERR_OPERATION_DENIED, "%s", + _("Identity attribute is already set")); + return -1; + } + + return virTypedParamsAddString(&ident->params, + &ident->nparams, + &ident->maxparams, + VIR_CONNECT_IDENTITY_SYSTEM_TOKEN, + token); +} + + int virIdentitySetParameters(virIdentity *ident, virTypedParameterPtr params, int nparams) @@ -577,6 +609,8 @@ int virIdentitySetParameters(virIdentity *ident, VIR_TYPED_PARAM_STRING, VIR_CONNECT_IDENTITY_SELINUX_CONTEXT, VIR_TYPED_PARAM_STRING, + VIR_CONNECT_IDENTITY_SYSTEM_TOKEN, + VIR_TYPED_PARAM_STRING, NULL) < 0) return -1; =20 diff --git a/src/util/viridentity.h b/src/util/viridentity.h index fa3f46788c..640a7ba2e4 100644 --- a/src/util/viridentity.h +++ b/src/util/viridentity.h @@ -52,6 +52,8 @@ int virIdentityGetX509DName(virIdentity *ident, const char **dname); int virIdentityGetSELinuxContext(virIdentity *ident, const char **context); +int virIdentityGetSystemToken(virIdentity *ident, + const char **token); =20 =20 int virIdentitySetUserName(virIdentity *ident, @@ -72,6 +74,8 @@ int virIdentitySetX509DName(virIdentity *ident, const char *dname); int virIdentitySetSELinuxContext(virIdentity *ident, const char *context); +int virIdentitySetSystemToken(virIdentity *ident, + const char *token); =20 int virIdentitySetParameters(virIdentity *ident, virTypedParameterPtr params, --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826473; cv=none; d=zohomail.com; s=zohoarc; b=Ze+jD1j6OXTb0DmtA53c+E34+7cBkxiEWYxCBc8r4KQ9NFlO3RLycZWao3r/dFJA7I+7mkQhvRb8GU2dXMPdGdGeBvKYTzkbksfvAbMxYGgukCCi4YuchCpFDO63cbRiBTJf2jy+bc9yOwaANYgTMnEGmwHYkkXF8yymFWdLgL0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826473; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=i/86EE3g598rOpkfNt39k12afV7gEySvgt175NYDpt8=; b=i7+374+R//VApMlmQYaqyy3oZxh3UA8NulJwLZmsFstTDQoFQ33bjzIoodFQH8qejWuS8z3RDlQfLbxtyo3hoT7cPvfa8DXBGhVVUedd35aBRUmTExZE5kOcQLTTmfTFWDs9W9/qJ5C81alq4ETZ37+9p8jaR7yKQqr6PZygoVQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 16208264738951020.580272937107; Wed, 12 May 2021 06:34:33 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-541-M86HtN-SPaecJVroj-4J3Q-1; Wed, 12 May 2021 09:34:27 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B5C36107ACED; Wed, 12 May 2021 13:34:16 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 97A5A6091A; Wed, 12 May 2021 13:34:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5C3D95535C; Wed, 12 May 2021 13:34:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDY5qO031319 for ; Wed, 12 May 2021 09:34:05 -0400 Received: by smtp.corp.redhat.com (Postfix) id BC53160657; Wed, 12 May 2021 13:34:05 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id C031B2BFC7; Wed, 12 May 2021 13:34:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826472; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=i/86EE3g598rOpkfNt39k12afV7gEySvgt175NYDpt8=; b=Vd2eUbrM6fG0/Jo5IzWSUXa8fctEqp9Brnv8GhYHFcdF0c4dsE58SIc6RzQDubQVbxaDXB S4knc2MgHqRaYjGOJ4ruINmrTomFUi1y7qwYYdDrohKsThMitHZdHbBTZqiDQ+lRw7OMeU qxe+QGBxJhJpo8yYB8ZmrDS0PuaJSh4= X-MC-Unique: M86HtN-SPaecJVroj-4J3Q-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 03/10] util: generate a persistent system token Date: Wed, 12 May 2021 14:33:49 +0100 Message-Id: <20210512133356.1162418-4-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) When creating the system identity set the system token. The system token is currently stored in a local path /var/run/libvirt/common/system.token Obviously with only traditional UNIX DAC in effect, this is largely security through obscurity, if the client is running at the same privilege level as the daemon. It does, however, reliably distinguish an unprivileged client from the system daemons. With a MAC system like SELinux though, or possible use of containers, access can be further restricted. A possible future improvement for Linux would be to populate the kernel keyring with a secret for libvirt daemons to share. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/viridentity.c | 107 ++++++++++++++++++++++++++++++++++++- src/util/viridentitypriv.h | 30 +++++++++++ tests/viridentitytest.c | 11 +++- 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/util/viridentitypriv.h diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index aaae1c8002..9c3c473c1c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2396,6 +2396,7 @@ virHostGetBootTime; =20 =20 # util/viridentity.h +virIdentityEnsureSystemToken; virIdentityGetCurrent; virIdentityGetGroupName; virIdentityGetParameters; diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 7da4ea12f5..5174f5a2d3 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -22,21 +22,27 @@ #include =20 #include +#include #if WITH_SELINUX # include #endif =20 +#define LIBVIRT_VIRIDENTITYPRIV_H_ALLOW + #include "internal.h" #include "viralloc.h" #include "virerror.h" -#include "viridentity.h" +#include "viridentitypriv.h" #include "virlog.h" #include "virobject.h" +#include "virrandom.h" #include "virthread.h" #include "virutil.h" #include "virstring.h" #include "virprocess.h" #include "virtypedparam.h" +#include "virfile.h" +#include "configmake.h" =20 #define VIR_FROM_THIS VIR_FROM_IDENTITY =20 @@ -55,6 +61,7 @@ struct _virIdentity { G_DEFINE_TYPE(virIdentity, vir_identity, G_TYPE_OBJECT) =20 static virThreadLocal virIdentityCurrent; +static char *systemToken; =20 static void virIdentityFinalize(GObject *obj); =20 @@ -73,6 +80,9 @@ static int virIdentityOnceInit(void) return -1; } =20 + if (!(systemToken =3D virIdentityEnsureSystemToken())) + return -1; + return 0; } =20 @@ -144,6 +154,101 @@ int virIdentitySetCurrent(virIdentity *ident) } =20 =20 +#define TOKEN_BYTES 16 +#define TOKEN_STRLEN (TOKEN_BYTES * 2) + +static char * +virIdentityConstructSystemTokenPath(void) +{ + g_autofree char *commondir =3D NULL; + if (geteuid() =3D=3D 0) { + commondir =3D g_strdup(RUNSTATEDIR "/libvirt/common"); + } else { + g_autofree char *rundir =3D virGetUserRuntimeDirectory(); + commondir =3D g_strdup_printf("%s/common", rundir); + } + + if (g_mkdir_with_parents(commondir, 0700) < 0) { + virReportSystemError(errno, + _("Cannot create daemon common directory '%s'= "), + commondir); + return NULL; + } + + return g_strdup_printf("%s/system.token", commondir); +} + + +char * +virIdentityEnsureSystemToken(void) +{ + g_autofree char *tokenfile =3D virIdentityConstructSystemTokenPath(); + g_autofree char *token =3D NULL; + VIR_AUTOCLOSE fd =3D -1; + struct stat st; + + if (!tokenfile) + return NULL; + + fd =3D open(tokenfile, O_RDWR|O_APPEND|O_CREAT, 0600); + if (fd < 0) { + virReportSystemError(errno, + _("Unable to open system token %s"), + tokenfile); + return NULL; + } + + if (virSetCloseExec(fd) < 0) { + virReportSystemError(errno, + _("Failed to set close-on-exec flag '%s'"), + tokenfile); + return NULL; + } + + if (virFileLock(fd, false, 0, 1, true) < 0) { + virReportSystemError(errno, + _("Failed to lock system token '%s'"), + tokenfile); + return NULL; + } + + if (fstat(fd, &st) < 0) { + virReportSystemError(errno, + _("Failed to check system token '%s'"), + tokenfile); + return NULL; + } + + /* Ok, we're the first one here, so we must populate it */ + if (st.st_size =3D=3D 0) { + if (!(token =3D virRandomToken(TOKEN_BYTES))) { + return NULL; + } + if (safewrite(fd, token, TOKEN_STRLEN) !=3D TOKEN_STRLEN) { + virReportSystemError(errno, + _("Failed to write system token '%s'"), + tokenfile); + return NULL; + } + } else { + if (virFileReadLimFD(fd, TOKEN_STRLEN, &token) < 0) { + virReportSystemError(errno, + _("Failed to write system token '%s'"), + tokenfile); + return NULL; + } + if (strlen(token) !=3D TOKEN_STRLEN) { + virReportSystemError(errno, + _("System token in %s was corrupt"), + tokenfile); + return NULL; + } + } + + return g_steal_pointer(&token); +} + + /** * virIdentityGetSystem: * diff --git a/src/util/viridentitypriv.h b/src/util/viridentitypriv.h new file mode 100644 index 0000000000..e5ca8430f8 --- /dev/null +++ b/src/util/viridentitypriv.h @@ -0,0 +1,30 @@ +/* + * viridentitypriv.h: helper APIs for managing user identities + * + * Copyright (C) 2012-2013 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 + * . + */ + +#ifndef LIBVIRT_VIRIDENTITYPRIV_H_ALLOW +# error "viridentitypriv.h may only be included by viridentity.c or test s= uites" +#endif /* LIBVIRT_VIRIDENTITYPRIV_H_ALLOW */ + +#pragma once + +#include "viridentity.h" + +char * +virIdentityEnsureSystemToken(void) G_GNUC_NO_INLINE; diff --git a/tests/viridentitytest.c b/tests/viridentitytest.c index afb9fdaec4..99c7899ed7 100644 --- a/tests/viridentitytest.c +++ b/tests/viridentitytest.c @@ -25,7 +25,9 @@ =20 #include "testutils.h" =20 -#include "viridentity.h" +#define LIBVIRT_VIRIDENTITYPRIV_H_ALLOW + +#include "viridentitypriv.h" #include "virerror.h" #include "viralloc.h" #include "virlog.h" @@ -36,6 +38,13 @@ =20 VIR_LOG_INIT("tests.identitytest"); =20 +char * +virIdentityEnsureSystemToken(void) +{ + return g_strdup("3de80bcbf22d4833897f1638e01be9b2"); +} + + static int testIdentityAttrs(const void *data G_GNUC_UNUSED) { g_autoptr(virIdentity) ident =3D virIdentityNew(); --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826487; cv=none; d=zohomail.com; s=zohoarc; b=gmTnVbh4mdLcVeITBCwPASf7HYRYVLG+SxKm0yU/2K5MqI0G400D5dFR3qo2mTKn3i0wjR+ih/XWxEZ+hEP+EJd2tQPkWp7R2fv/Azf8sefeZEt47ygN5Rekuaf0zI4dZLFNXM4Us9BO8j4Oa6G8P7xSgmlTs6+b6vaJvg+hllk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826487; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Aa7zaEpz0n1LA3Q3rU0fvn6aGnXpZNjri1MK60k6nvs=; b=hf4NDhUStPYKdKrn12yb9OCB61xqGJhb1zEVOxse2FBP9ZfpdIf5gXWT1NGjfv7JOI4UXGztqi+y5wqTZg5NBHu+XdAXd40XnXb4VZZjf5cWW5r2dWZD9enncHazQVtH3ZPbBLqWu0R4owbF2RNj0yLEGtm4uaPdZprhbjO52D8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1620826487651247.39198055163865; Wed, 12 May 2021 06:34:47 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-465-0upJpzlNOuKwD08caC065w-1; Wed, 12 May 2021 09:34:30 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 16A1C106BB41; Wed, 12 May 2021 13:34:20 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E7BBB2CFB2; Wed, 12 May 2021 13:34:19 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A82561801264; Wed, 12 May 2021 13:34:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDY7V6031333 for ; Wed, 12 May 2021 09:34:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 311D063BA7; Wed, 12 May 2021 13:34:07 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A3F41981F; Wed, 12 May 2021 13:34:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826486; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Aa7zaEpz0n1LA3Q3rU0fvn6aGnXpZNjri1MK60k6nvs=; b=Bu6kaMaq83s4oTpub5qVfs3IpD8oxJ5hmGDQf0UGAenLZbhtn0tkoRVkfXIfq26zhcSQjz l1MwXJjD5Hpc2vzfohbamhm9ymRgq9/FrLpckN0uYI7tech2OQUX8UHCwQw12c206IBaiR qRvMmal5lz/VQBmzcfh7tIri3JZzxf8= X-MC-Unique: 0upJpzlNOuKwD08caC065w-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 04/10] util: set system token for system identity Date: Wed, 12 May 2021 14:33:50 +0100 Message-Id: <20210512133356.1162418-5-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/util/viridentity.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 5174f5a2d3..e822f0bd74 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -266,6 +266,7 @@ virIdentity *virIdentityGetSystem(void) #if WITH_SELINUX char *con; #endif + g_autofree char *token =3D NULL; =20 if (!(ret =3D virIdentityNew())) return NULL; @@ -308,6 +309,12 @@ virIdentity *virIdentityGetSystem(void) } #endif =20 + if (!(token =3D virIdentityEnsureSystemToken())) + return NULL; + + if (virIdentitySetSystemToken(ret, token) < 0) + return NULL; + return g_steal_pointer(&ret); } =20 --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826504; cv=none; d=zohomail.com; s=zohoarc; b=Kpk2yPzn7hN9uW/7bEODGGHlhBLa6yo7VfT2/eBrr62vXZSvQxK6mR3Nz+j9Mek4ovnbOomhpduWpL4/mjSZ/GWTkMCHNothNfWz7sUgIapWbZHPSSBOOFpUoE3dAumvA2PPO7Guuxr9wU+f4im3NaDoeK1iHMKx33FB/K/hwJw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826504; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0wZr1DYzn+VWp2jLN1Hv6EN5VLPL5+4WcYom/XmsQeM=; b=VWqeEd/k53Q5JOI7SzjqtCANGVBv9ZAbK+roSY2J52SXPzKjcKEPlb0bMUR7sLUaGLvDxYhjooswiABVg4cjNfdI/RuuQyd6QMZQDJGQbnGfsWc/XCRDjYRVOj83uTXDRTcUj9Zf4+szeK7PFcltd6tdifgt9VxRqsRaR2LjXDY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1620826504377572.0199316366; Wed, 12 May 2021 06:35:04 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-521-c78LSY8wOQWntEOVi7-gqQ-1; Wed, 12 May 2021 09:34:16 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5F4BC106BB24; Wed, 12 May 2021 13:34:10 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3C5882BFC7; Wed, 12 May 2021 13:34:10 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DE63D180B463; Wed, 12 May 2021 13:34:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDY8v0031345 for ; Wed, 12 May 2021 09:34:08 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5148F19CB6; Wed, 12 May 2021 13:34:08 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 854D163BA7; Wed, 12 May 2021 13:34:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826502; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=0wZr1DYzn+VWp2jLN1Hv6EN5VLPL5+4WcYom/XmsQeM=; b=K4NSnx0FWpPTtXWLzDIwGIpFHvifdrclUPdaJImdLXhF2ko5ooHXbhVBPb86i63HLi6to2 GI//aJEtN1yJzWRGlnKFoLuHfW4pgRGO3szKob7Iwagdx/qJFsiAoe8WvcbsP2XfU+0LMc 6kkYb0gEpOcUYVnRDEXnB4+OLuAuCj0= X-MC-Unique: c78LSY8wOQWntEOVi7-gqQ-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 05/10] util: add API for copying identity objects Date: Wed, 12 May 2021 14:33:51 +0100 Message-Id: <20210512133356.1162418-6-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/libvirt_private.syms | 1 + src/util/viridentity.c | 21 +++++++++++++++++++++ src/util/viridentity.h | 1 + 3 files changed, 23 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 9c3c473c1c..443a78e698 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2411,6 +2411,7 @@ virIdentityGetUNIXUserID; virIdentityGetUserName; virIdentityGetX509DName; virIdentityNew; +virIdentityNewCopy; virIdentitySetCurrent; virIdentitySetGroupName; virIdentitySetParameters; diff --git a/src/util/viridentity.c b/src/util/viridentity.c index e822f0bd74..01edabf2d7 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -333,6 +333,27 @@ virIdentity *virIdentityNew(void) } =20 =20 +/** + * virIdentityNewCopy: + * + * Creates a new identity object that is a deep copy of an + * existing identity. + * + * Returns: a copy of the source identity + */ +virIdentity *virIdentityNewCopy(virIdentity *src) +{ + g_autoptr(virIdentity) ident =3D virIdentityNew(); + + if (virTypedParamsCopy(&ident->params, src->params, src->nparams) < 0) + return NULL; + ident->nparams =3D src->nparams; + ident->maxparams =3D src->nparams; + + return g_steal_pointer(&ident); +} + + static void virIdentityFinalize(GObject *object) { virIdentity *ident =3D VIR_IDENTITY(object); diff --git a/src/util/viridentity.h b/src/util/viridentity.h index 640a7ba2e4..512bca286d 100644 --- a/src/util/viridentity.h +++ b/src/util/viridentity.h @@ -33,6 +33,7 @@ int virIdentitySetCurrent(virIdentity *ident); virIdentity *virIdentityGetSystem(void); =20 virIdentity *virIdentityNew(void); +virIdentity *virIdentityNewCopy(virIdentity *src); =20 int virIdentityGetUserName(virIdentity *ident, const char **username); --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826468; cv=none; d=zohomail.com; s=zohoarc; b=mcA/oCPSdREq7tZM6UmmGvRdCG7LUeGfHEJ6nFyZu0XNXd8aCYksKOS5gLUljYJpRwlRuc30ts/eOk2LSPHmkcW3CvxkOw38Uz6qt5HJ+/C3GQv7sMLMLStwz9EzmQ65IuFExMn0SZAmDycZxzzgAq0HV5LZR7qmQ1iVNN7egwg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826468; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=scjhKulXD+z3YHsWx6y7U6nEK7bksAjsuRP68jlNRtk=; b=aPsfq1COM0MtOfYf9AVKeXEaLhX5iLsA4/badcLIb7Xa6KcAqurvnQF20lGNUIP/sya/Ipasq6iXgFCEVJbhjFTFdAm9FWe+M0MdrZkzj4+vqmD+rR2YWfB6fmZGpax3xbq48N5BuTRpwpU8spkpqRLfyGBCeaQ57r3OMcvSndc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1620826468893141.06815571098969; Wed, 12 May 2021 06:34:28 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-160-8JCpw1SMNAujgqw4EgofAQ-1; Wed, 12 May 2021 09:34:22 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C4A54801FAF; Wed, 12 May 2021 13:34:14 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A8C485D6AC; Wed, 12 May 2021 13:34:14 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 69B3055345; Wed, 12 May 2021 13:34:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDY93J031355 for ; Wed, 12 May 2021 09:34:09 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6E88B60657; Wed, 12 May 2021 13:34:09 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id A830B19CB6; Wed, 12 May 2021 13:34:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826466; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=scjhKulXD+z3YHsWx6y7U6nEK7bksAjsuRP68jlNRtk=; b=WMr4QnLcWqPYaJ3MdkW1V2PpoE1leyHV93zHR7yywRT6of1ytYUTI3TAWXkVR42zN9yi3X L/z91UW+nNbrs51NbkE8TkPtk4HOa5tXhe7qJHfnZwHBAtHuVCocRQjW+mBoWyCp43+Xn3 wr5eg3IvcepG+ftqUr6ot6oXjhRMj2Q= X-MC-Unique: 8JCpw1SMNAujgqw4EgofAQ-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 06/10] util: helper to temporary elevate privileges of the current identity Date: Wed, 12 May 2021 14:33:52 +0100 Message-Id: <20210512133356.1162418-7-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) When talking to the secret driver, the callers inside libvirt daemons need to be able to run with an elevated privileges that prove the API calls are made by a libvirt daemon, not an end user application. The virIdentityElevateCurrent method will take the current identity and, if not already present, add the system token. The old current identity is returned to the caller. With the VIR_IDENTITY_AUTORESTORE annotation, the old current identity will be restored upon leaving the codeblock scope. ... early work with regular privileges ... if (something needing elevated privs) { VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D virIdentityElevateCurrent(); if (!oldident) return -1; ... do something with elevated privileges ... } ... later work with regular privileges ... Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/libvirt_private.syms | 2 ++ src/util/viridentity.c | 47 ++++++++++++++++++++++++++++++++++++++++ src/util/viridentity.h | 5 +++++ 3 files changed, 54 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 443a78e698..2ea950c5cd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2396,6 +2396,7 @@ virHostGetBootTime; =20 =20 # util/viridentity.h +virIdentityElevateCurrent; virIdentityEnsureSystemToken; virIdentityGetCurrent; virIdentityGetGroupName; @@ -2412,6 +2413,7 @@ virIdentityGetUserName; virIdentityGetX509DName; virIdentityNew; virIdentityNewCopy; +virIdentityRestoreHelper; virIdentitySetCurrent; virIdentitySetGroupName; virIdentitySetParameters; diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 01edabf2d7..2e3fcc5add 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -154,6 +154,53 @@ int virIdentitySetCurrent(virIdentity *ident) } =20 =20 +/** + * virIdentityElevateCurrent: + * + * Set the new identity to be associated with this thread, + * to an elevated copy of the current identity. The old + * current identity is returned and should be released by + * the caller when no longer required. + * + * Returns the previous identity, or NULL on error + */ +virIdentity *virIdentityElevateCurrent(void) +{ + g_autoptr(virIdentity) ident =3D virIdentityGetCurrent(); + const char *token; + int rc; + + if (!ident) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No current identity to elevate")); + return NULL; + } + + if ((rc =3D virIdentityGetSystemToken(ident, &token)) < 0) + return NULL; + + if (rc =3D=3D 0) { + g_autoptr(virIdentity) identel =3D virIdentityNewCopy(ident); + + if (virIdentitySetSystemToken(identel, systemToken) < 0) + return NULL; + + if (virIdentitySetCurrent(identel) < 0) + return NULL; + } + + return g_steal_pointer(&ident); +} + + +void virIdentityRestoreHelper(virIdentity **identptr) +{ + virIdentity *ident =3D *identptr; + + if (ident !=3D NULL) + virIdentitySetCurrent(ident); +} + #define TOKEN_BYTES 16 #define TOKEN_STRLEN (TOKEN_BYTES * 2) =20 diff --git a/src/util/viridentity.h b/src/util/viridentity.h index 512bca286d..848e5b2056 100644 --- a/src/util/viridentity.h +++ b/src/util/viridentity.h @@ -27,8 +27,13 @@ #define VIR_TYPE_IDENTITY vir_identity_get_type() G_DECLARE_FINAL_TYPE(virIdentity, vir_identity, VIR, IDENTITY, GObject); =20 +#define VIR_IDENTITY_AUTORESTORE __attribute__((cleanup(virIdentityRestore= Helper))) + virIdentity *virIdentityGetCurrent(void); int virIdentitySetCurrent(virIdentity *ident); +virIdentity *virIdentityElevateCurrent(void); + +void virIdentityRestoreHelper(virIdentity **identptr); =20 virIdentity *virIdentityGetSystem(void); =20 --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826464; cv=none; d=zohomail.com; s=zohoarc; b=Xas0uMUMbCYD6zQRnk1slhFdbXbltkVMiip4Wcm8jxvTSnZjEfZVk4pIqC/STv34+dvpBLNwmxfRUMhG/n/Rej9vXWJRl2b42Qolz2J2tRHnL/XH38Pf2D0KNReUSn4kOVw4stlYeXCouvFjgWZ+gAMeBGiyKFyj+/GZCUhK28A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826464; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QQB1apvFE5grSJk1OfoWNdtFeH0CZEQcdVExX7cLzhc=; b=X2574g2cpgrrFFzlGb0+5quCr4AW2uOJ0W0gTZWgfxAX+B+5IlSQTP5l3guMDOgnOHjUG1uBgoudbBS9fBGlTEwOsBa6QrJnZjEo4hlTx8MHr7U/WuigN6fbjp/cLx6LmRqTibXIEzCkuHw5BGmCjgICPeCZeWr3b7YG2l3mibE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1620826464942979.2649483825564; Wed, 12 May 2021 06:34:24 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-504-6Rq9lCaEOR-UGWKAFngviw-1; Wed, 12 May 2021 09:34:19 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4C5A9107ACCD; Wed, 12 May 2021 13:34:13 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2C1EC6268F; Wed, 12 May 2021 13:34:13 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E5E08180B651; Wed, 12 May 2021 13:34:12 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDYAp4031361 for ; Wed, 12 May 2021 09:34:10 -0400 Received: by smtp.corp.redhat.com (Postfix) id CACD01981F; Wed, 12 May 2021 13:34:10 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id C3FFA60657; Wed, 12 May 2021 13:34:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826463; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=QQB1apvFE5grSJk1OfoWNdtFeH0CZEQcdVExX7cLzhc=; b=af4/jn3dMnefgWgYaDNFOXFp7L+uthax0j+0SwGp35bjbNZ9cdQtFbQ8k0esCfBpAsQD+8 Ev27qXWL95y9n0ZSbbFuf/4rQQpIIDdZkmA+E0llqlH6z63P/38r3X1k46wifbWXSpiEpv seIg01TTiVGyfa77IlPBmoYqPDYFO2k= X-MC-Unique: 6Rq9lCaEOR-UGWKAFngviw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 07/10] src: add API to determine if current identity is a system identity Date: Wed, 12 May 2021 14:33:53 +0100 Message-Id: <20210512133356.1162418-8-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) This is essentially a way to determine if the current identity is that of another libvirt daemon. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/libvirt_private.syms | 1 + src/util/viridentity.c | 28 ++++++++++++++++++++++++++++ src/util/viridentity.h | 1 + 3 files changed, 30 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2ea950c5cd..1df4b8cfe8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2411,6 +2411,7 @@ virIdentityGetUNIXGroupID; virIdentityGetUNIXUserID; virIdentityGetUserName; virIdentityGetX509DName; +virIdentityIsCurrentElevated; virIdentityNew; virIdentityNewCopy; virIdentityRestoreHelper; diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 2e3fcc5add..e7e5c31241 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -366,6 +366,34 @@ virIdentity *virIdentityGetSystem(void) } =20 =20 +/** + * virIdentityIsCurrentElevated: + * + * Determine if the current identity has elevated privileges. + * This indicates that it was invoked on behalf of the + * user by a libvirt daemon. + * + * Returns: true if elevated + */ +int virIdentityIsCurrentElevated(void) +{ + g_autoptr(virIdentity) current =3D virIdentityGetCurrent(); + const char *currentToken =3D NULL; + int rv; + + if (!current) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No current identity")); + return -1; + } + + rv =3D virIdentityGetSystemToken(current, ¤tToken); + if (rv <=3D 0) + return rv; + + return STREQ_NULLABLE(currentToken, systemToken); +} + /** * virIdentityNew: * diff --git a/src/util/viridentity.h b/src/util/viridentity.h index 848e5b2056..6da6d0c557 100644 --- a/src/util/viridentity.h +++ b/src/util/viridentity.h @@ -35,6 +35,7 @@ virIdentity *virIdentityElevateCurrent(void); =20 void virIdentityRestoreHelper(virIdentity **identptr); =20 +int virIdentityIsCurrentElevated(void); virIdentity *virIdentityGetSystem(void); =20 virIdentity *virIdentityNew(void); --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826474; cv=none; d=zohomail.com; s=zohoarc; b=ksPqn8hvVzO3pwj2+dhtmJZmOQmPHxrkJx2cIQ2hAXmscZ2YSXDhtlygQ1wl4ETPhjnHcOMsDp1B83OriG/NzQ2FhlJBamsbepb4jmAUpS1XzwPCuNAFVPxdadQ/2q9XjEL5hZz2ozWqEekyZaWzwnQvTox+aR8oXbPGHVqJzDY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826474; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=alj+4zcf3dbREy5J+cFawK6Z9p8hdQJhz/G4h8bAD1k=; b=i9wF1t/zxhNpBCM/xALPMoqsgja8YKMtWq/fLTKARPSPoAkzsv4M4nmBI6JdYOYGi8nMexfFTEROCZBRJeQMnCq4jmyVXVLFpI4DZ1LF9m64fH78LCzguORk0oFCm5JIVJTosqtghXQJDkkss7LvjygYSO3GeWRSRdAQrfGbvoc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1620826474951445.0753508333556; Wed, 12 May 2021 06:34:34 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-354-x-KPm3M1OSuH-2JY_m3jGQ-1; Wed, 12 May 2021 09:34:28 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 70F5E107ACF8; Wed, 12 May 2021 13:34:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5152A2BFC7; Wed, 12 May 2021 13:34:23 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 113425535F; Wed, 12 May 2021 13:34:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDYBXJ031372 for ; Wed, 12 May 2021 09:34:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id E42C163BA7; Wed, 12 May 2021 13:34:11 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 21A9B60657; Wed, 12 May 2021 13:34:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826472; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=alj+4zcf3dbREy5J+cFawK6Z9p8hdQJhz/G4h8bAD1k=; b=CrceCx7d1GYLPM5liGlNWX9mgHXSCfQi5e4G1+sqRjByaqmd8/0XXQH2CFskN12/ZNFvth tTg+3VsHhYgwG50fGLvdAl1vLdku13Lv8W37mqBPL6QXY/ua3PZPJ+Uli7elfM8hurS4ox y/Hqfwmh8IVcRMNVmB1g5BntBhpz8RE= X-MC-Unique: x-KPm3M1OSuH-2JY_m3jGQ-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 08/10] src: set identity when opening secondary drivers Date: Wed, 12 May 2021 14:33:54 +0100 Message-Id: <20210512133356.1162418-9-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The drivers can all call virGetConnectXXX to open a connection to a secondary driver. For example, when creating a encrypted storage volume, the storage driver has to open a secret driver connection, or when starting a guest, the QEMU driver has to open the network driver to lookup a virtual network. When using monolithic libvirtd, the connection has the same effective identity as the client, since everything is still in the same process. When using the modular daemons, however, the remote daemon sees the identity of the calling daemon. This is a mistake as it results in the modular daemons seeing the client with elevated privileges. We need to pass on the current identity explicitly when opening the secondary drivers. This is the same thing that is done by daemon RPC dispatcher code when it is directly forwarding top level API calls from virtproxyd and other daemons. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/driver.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/driver.c b/src/driver.c index f8022d2522..227bb56e48 100644 --- a/src/driver.c +++ b/src/driver.c @@ -33,6 +33,8 @@ #include "virstring.h" #include "virthread.h" #include "virutil.h" +#include "viridentity.h" +#include "datatypes.h" #include "configmake.h" =20 VIR_LOG_INIT("driver"); @@ -136,6 +138,7 @@ static virConnectPtr virGetConnectGeneric(virThreadLocal *threadPtr, const char *name) { virConnectPtr conn; + virErrorPtr saved; =20 if (virConnectCacheInitialize() < 0) return NULL; @@ -153,8 +156,32 @@ virGetConnectGeneric(virThreadLocal *threadPtr, const = char *name) =20 conn =3D virConnectOpen(uri); VIR_DEBUG("Opened new %s connection %p", name, conn); + if (!conn) + return NULL; + + if (conn->driver->connectSetIdentity !=3D NULL) { + g_autoptr(virIdentity) ident =3D NULL; + virTypedParameterPtr identparams =3D NULL; + int nidentparams =3D 0; + + VIR_DEBUG("Attempting to delegate current identity"); + if (!(ident =3D virIdentityGetCurrent())) + goto error; + + if (virIdentityGetParameters(ident, &identparams, &nidentparam= s) < 0) + goto error; + + if (virConnectSetIdentity(conn, identparams, nidentparams, 0) = < 0) + goto error; + } } return conn; + + error: + saved =3D virSaveLastError(); + virConnectClose(conn); + virSetError(saved); + return NULL; } =20 =20 --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826476; cv=none; d=zohomail.com; s=zohoarc; b=XP1ziwAzkShGh4LHNtxYus0oFNBAq0850feSc5DDiOZ/5DFrEPkjw10Ig4OhIKKS2qhSOKB+v2eRAbK809ot87OhJO3YtSv0Ms9OHfPQSy/kYvCa7CvAAisPuP7RlXufgD5fXVWM750EZDaFwqE0FgiHR+3p/s3RNXbImk8Hqtc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826476; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3srJDFYzeetdJ6lN4I4UMKlXuMJVw8eCgJSedeKZnTM=; b=I6ua01loC9hu22wSYjW/urUwRmAZYpcG0eyoLr5H6nh89wWrGxaFNVWa03U9deKaykfUT3ZiKMuSYGfw5CZDg+WjL2OhuWm5UuVDPQWjQEydAUZJYd9KkNhNfBtcCW/F6oDsuwWDrzNeFmE/N94zauyQ0lZ3VhC6Wbb9WNkoSuM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 162082647638160.54950843998222; Wed, 12 May 2021 06:34:36 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-206-QKWluWIMOkanDTH9vUttSA-1; Wed, 12 May 2021 09:34:32 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B868E180486D; Wed, 12 May 2021 13:34:26 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 964362CFB2; Wed, 12 May 2021 13:34:26 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 60362180102B; Wed, 12 May 2021 13:34:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDYD5U031381 for ; Wed, 12 May 2021 09:34:13 -0400 Received: by smtp.corp.redhat.com (Postfix) id 22B6E2C01F; Wed, 12 May 2021 13:34:13 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 462D160657; Wed, 12 May 2021 13:34:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826474; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=3srJDFYzeetdJ6lN4I4UMKlXuMJVw8eCgJSedeKZnTM=; b=ZMad+FJXXpfa12Jb6yQ4YDe/v3oicLb3eImHz1HbLr/Vt4qWApUbMuVFcqs3226w1GgHFl uC8MygDuwvlIlcehLXisWMxBTNfu9gletYdW0r4DrDh/Es6pooLZzyCjBNIV1dgEwOf10+ 47Dl9JHy+vgn59Lpz6RQ9+y9w1BFhiM= X-MC-Unique: QKWluWIMOkanDTH9vUttSA-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 09/10] src: elevate current identity privilege when fetching secret Date: Wed, 12 May 2021 14:33:55 +0100 Message-Id: <20210512133356.1162418-10-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) When fetching the value of a private secret, we need to use an elevated identity otherwise the secret driver will deny access. When using the modular daemons, the elevated identity needs to be active before the secret driver connection is opened, and it will apply to all APIs calls made on that conncetion. When using the monolithic daemon, the identity at time of opening the connection is ignored, and the elevated identity needs to be active precisely at the time the virSecretGetValue API call is made. After acquiring the secret value, the elevated identity should be cleared. This sounds complex, but is fairly straightfoward with the automatic cleanup callbacks. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/libxl/libxl_conf.c | 5 +++++ src/qemu/qemu_domain.c | 11 ++++++++++- src/qemu/qemu_tpm.c | 5 +++++ src/storage/storage_backend_iscsi.c | 5 +++++ src/storage/storage_backend_iscsi_direct.c | 5 +++++ src/storage/storage_backend_rbd.c | 5 +++++ src/storage/storage_util.c | 5 +++++ tests/qemuxml2argvmock.c | 9 +++++++++ tests/qemuxml2argvtest.c | 6 ++++++ 9 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index 4de2158bea..e33297a9ba 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -31,6 +31,7 @@ #include "datatypes.h" #include "virconf.h" #include "virfile.h" +#include "viridentity.h" #include "virstring.h" #include "viralloc.h" #include "viruuid.h" @@ -1001,6 +1002,10 @@ libxlMakeNetworkDiskSrc(virStorageSource *src, char = **srcstr) if (src->auth && src->protocol =3D=3D VIR_STORAGE_NET_PROTOCOL_RBD) { g_autofree uint8_t *secret =3D NULL; size_t secretlen =3D 0; + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D virIdentityElev= ateCurrent(); + + if (!oldident) + goto cleanup; =20 username =3D src->auth->username; if (!(conn =3D virConnectOpen("xen:///system"))) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index fe56d17486..10641846b3 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -41,6 +41,7 @@ #include "viralloc.h" #include "virlog.h" #include "virerror.h" +#include "viridentity.h" #include "cpu/cpu.h" #include "viruuid.h" #include "virfile.h" @@ -1116,9 +1117,13 @@ qemuDomainSecretPlainSetup(qemuDomainSecretInfo *sec= info, const char *username, virSecretLookupTypeDef *seclookupdef) { + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D virIdentityElevateC= urrent(); g_autoptr(virConnect) conn =3D virGetConnectSecret(); int ret =3D -1; =20 + if (!oldident) + return -1; + if (!conn) return -1; =20 @@ -1213,11 +1218,15 @@ qemuDomainSecretAESSetupFromSecret(qemuDomainObjPri= vate *priv, const char *username, virSecretLookupTypeDef *seclookupdef) { - g_autoptr(virConnect) conn =3D virGetConnectSecret(); qemuDomainSecretInfo *secinfo; g_autofree char *alias =3D qemuAliasForSecret(srcalias, secretuse); g_autofree uint8_t *secret =3D NULL; size_t secretlen =3D 0; + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D virIdentityElevateC= urrent(); + g_autoptr(virConnect) conn =3D virGetConnectSecret(); + + if (!oldident) + return NULL; =20 if (!conn) return NULL; diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 9ae7e5f94b..477a26dc69 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -33,6 +33,7 @@ #include "vircommand.h" #include "viralloc.h" #include "virkmod.h" +#include "viridentity.h" #include "virlog.h" #include "virutil.h" #include "viruuid.h" @@ -366,6 +367,10 @@ qemuTPMSetupEncryption(const unsigned char *secretuuid, virSecretLookupTypeDef seclookupdef =3D { .type =3D VIR_SECRET_LOOKUP_TYPE_UUID, }; + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D virIdentityElevateC= urrent(); + + if (!oldident) + return -1; =20 conn =3D virGetConnectSecret(); if (!conn) diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_back= end_iscsi.c index 67e59e856c..ed17ed11a6 100644 --- a/src/storage/storage_backend_iscsi.c +++ b/src/storage/storage_backend_iscsi.c @@ -34,6 +34,7 @@ #include "virerror.h" #include "virfile.h" #include "viriscsi.h" +#include "viridentity.h" #include "virlog.h" #include "virobject.h" #include "virstring.h" @@ -263,6 +264,7 @@ virStorageBackendISCSISetAuth(const char *portal, virStorageAuthDef *authdef =3D source->auth; int ret =3D -1; virConnectPtr conn =3D NULL; + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D NULL; =20 if (!authdef || authdef->authType =3D=3D VIR_STORAGE_AUTH_TYPE_NONE) return 0; @@ -275,6 +277,9 @@ virStorageBackendISCSISetAuth(const char *portal, return -1; } =20 + if (!(oldident =3D virIdentityElevateCurrent())) + return -1; + conn =3D virGetConnectSecret(); if (!conn) return -1; diff --git a/src/storage/storage_backend_iscsi_direct.c b/src/storage/stora= ge_backend_iscsi_direct.c index cb5b39baf4..0bff1882b9 100644 --- a/src/storage/storage_backend_iscsi_direct.c +++ b/src/storage/storage_backend_iscsi_direct.c @@ -29,6 +29,7 @@ #include "storage_util.h" #include "viralloc.h" #include "virerror.h" +#include "viridentity.h" #include "virlog.h" #include "virobject.h" #include "virstring.h" @@ -94,6 +95,7 @@ virStorageBackendISCSIDirectSetAuth(struct iscsi_context = *iscsi, virStorageAuthDef *authdef =3D source->auth; int ret =3D -1; virConnectPtr conn =3D NULL; + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D NULL; =20 if (!authdef || authdef->authType =3D=3D VIR_STORAGE_AUTH_TYPE_NONE) return 0; @@ -107,6 +109,9 @@ virStorageBackendISCSIDirectSetAuth(struct iscsi_contex= t *iscsi, return ret; } =20 + if (!(oldident =3D virIdentityElevateCurrent())) + return -1; + if (!(conn =3D virGetConnectSecret())) return ret; =20 diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backen= d_rbd.c index 9fbb2464d1..ce3ab11dd6 100644 --- a/src/storage/storage_backend_rbd.c +++ b/src/storage/storage_backend_rbd.c @@ -27,6 +27,7 @@ #include "storage_backend_rbd.h" #include "storage_conf.h" #include "viralloc.h" +#include "viridentity.h" #include "virlog.h" #include "viruuid.h" #include "virstring.h" @@ -196,6 +197,7 @@ virStorageBackendRBDOpenRADOSConn(virStorageBackendRBDS= tate *ptr, g_autofree char *mon_buff =3D NULL; =20 if (authdef) { + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D NULL; g_autofree char *rados_key =3D NULL; int rc; =20 @@ -206,6 +208,9 @@ virStorageBackendRBDOpenRADOSConn(virStorageBackendRBDS= tate *ptr, goto cleanup; } =20 + if (!(oldident =3D virIdentityElevateCurrent())) + goto cleanup; + conn =3D virGetConnectSecret(); if (!conn) return -1; diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index 7efadc2197..2b0d08c65d 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -68,6 +68,7 @@ #include "storage_source_conf.h" #include "virlog.h" #include "virfile.h" +#include "viridentity.h" #include "virjson.h" #include "virqemu.h" #include "virstring.h" @@ -1265,6 +1266,7 @@ storageBackendCreateQemuImgSecretPath(virStoragePoolO= bj *pool, size_t secretlen =3D 0; virConnectPtr conn =3D NULL; VIR_AUTOCLOSE fd =3D -1; + VIR_IDENTITY_AUTORESTORE virIdentity *oldident =3D NULL; =20 if (!enc) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -1279,6 +1281,9 @@ storageBackendCreateQemuImgSecretPath(virStoragePoolO= bj *pool, return NULL; } =20 + if (!(oldident =3D virIdentityElevateCurrent())) + return NULL; + conn =3D virGetConnectSecret(); if (!conn) return NULL; diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c index 77a0814c08..2265492f1e 100644 --- a/tests/qemuxml2argvmock.c +++ b/tests/qemuxml2argvmock.c @@ -18,10 +18,13 @@ =20 #include =20 +#define LIBVIRT_VIRIDENTITYPRIV_H_ALLOW + #include "internal.h" #include "viralloc.h" #include "vircommand.h" #include "vircrypto.h" +#include "viridentitypriv.h" #include "virmock.h" #include "virlog.h" #include "virnetdev.h" @@ -292,3 +295,9 @@ qemuInterfaceVDPAConnect(virDomainNetDef *net G_GNUC_UN= USED) abort(); return 1732; } + +char * +virIdentityEnsureSystemToken(void) +{ + return g_strdup("3de80bcbf22d4833897f1638e01be9b2"); +} diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index a9dafe226e..a93d21d61a 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -11,6 +11,7 @@ =20 # include "internal.h" # include "viralloc.h" +# include "viridentity.h" # include "qemu/qemu_alias.h" # include "qemu/qemu_capabilities.h" # include "qemu/qemu_command.h" @@ -650,6 +651,7 @@ testCompareXMLToArgv(const void *data) xmlNodePtr root; g_autofree char *archstr =3D NULL; virArch arch =3D VIR_ARCH_NONE; + g_autoptr(virIdentity) sysident =3D virIdentityGetSystem(); =20 if (info->arch !=3D VIR_ARCH_NONE && info->arch !=3D VIR_ARCH_X86_64) qemuTestSetHostArch(&driver, info->arch); @@ -670,6 +672,9 @@ testCompareXMLToArgv(const void *data) virSetConnectSecret(conn); virSetConnectStorage(conn); =20 + if (virIdentitySetCurrent(sysident) < 0) + goto cleanup; + if (testCheckExclusiveFlags(info->flags) < 0) goto cleanup; =20 @@ -809,6 +814,7 @@ testCompareXMLToArgv(const void *data) VIR_FREE(log); virDomainChrSourceDefClear(&monitor_chr); virObjectUnref(vm); + virIdentitySetCurrent(NULL); virSetConnectSecret(NULL); virSetConnectStorage(NULL); if (info->arch !=3D VIR_ARCH_NONE && info->arch !=3D VIR_ARCH_X86_64) --=20 2.31.1 From nobody Tue May 14 21:02:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826475; cv=none; d=zohomail.com; s=zohoarc; b=MBwYJzNU0LQtZK8WOXJY+eo4CmF96KxxXjnDhUiFR3F1PX2kkZ4qc3LgLRSsewIdf5ahkNLPuiD36fReFejgdtF1EsmomVbygovxNJcMNE8MMIIaT+b8DQeBVjvtPnQNA5bJv1vSwN85cuQ94aIOyeav1bmRba9LDBMYTf+WPcs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826475; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=DDQNcaRITqQB4Z/jAGPPQmu6nBKfpMz8314GK7gMz0U=; b=FhJx2kAlHnNL0x5WpjjEr1GmSDdrcEG4M1MFH0vV7ylf52aco+cEY/CmlrHxcfWXQPYB8uq8/nYmKp5sIax9VaSdWw1qN02xAJlNDRxvxQJMU/QyBjAiszgEjkipwT4/dWKd1rpHQ09H8WT75ybxg8uvCg3g8BdIK59IvEVf+po= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1620826475819554.868765012538; Wed, 12 May 2021 06:34:35 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-509-UgG9VhR1P46KcyKUeisINw-1; Wed, 12 May 2021 09:34:28 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F0D9018397A8; Wed, 12 May 2021 13:34:17 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D10045D6AC; Wed, 12 May 2021 13:34:17 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9CA341800BC1; Wed, 12 May 2021 13:34:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDYESB031396 for ; Wed, 12 May 2021 09:34:14 -0400 Received: by smtp.corp.redhat.com (Postfix) id 72CAE60657; Wed, 12 May 2021 13:34:14 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 785F963BA7; Wed, 12 May 2021 13:34:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826474; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=DDQNcaRITqQB4Z/jAGPPQmu6nBKfpMz8314GK7gMz0U=; b=VAClpxI15m/zNYxkhc/8oreOjMd0nR3jaAtQCwP0q+3TpoZpYQ8K0pZf9OAaMiZ62IDYPB eDv/6xqlkpN+xeoDZe4J5ASKpWlYAhOEkrytBNlRAWXM4Cnc1DOV4YQAjkALWKBaMy2Jrl TmW8g6d1pDwqQx8ag8JZk51N/ilcC7E= X-MC-Unique: UgG9VhR1P46KcyKUeisINw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 10/10] secret: rework handling of private secrets Date: Wed, 12 May 2021 14:33:56 +0100 Message-Id: <20210512133356.1162418-11-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) A secret can be marked with the "private" attribute. The intent was that it is not possible for any libvirt client to be able to read the secret value, it would only be accesible from within libvirtd. eg the QEMU driver can read the value to launch a guest. With the modular daemons, the QEMU, storage and secret drivers are all running in separate daemons. The QEMU and storage drivers thus appear to be normal libvirt client's from the POV of the secret driver, and thus they are not able to read a private secret. This is unhelpful. With the previous patches that introduced a "system token" to the identity object, we can now distinguish APIs invoked by libvirt daemons from those invoked by client applications. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/driver-secret.h | 9 +-------- src/libvirt-secret.c | 2 +- src/remote/remote_driver.c | 8 +------- src/secret/secret_driver.c | 34 +++++++++++++++++++++++++++------- src/util/virsecret.c | 3 +-- tests/qemuxml2argvtest.c | 3 +-- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/driver-secret.h b/src/driver-secret.h index eb6e82478c..1d21f62bb3 100644 --- a/src/driver-secret.h +++ b/src/driver-secret.h @@ -24,12 +24,6 @@ # error "Don't include this file directly, only use driver.h" #endif =20 -enum { - /* This getValue call is inside libvirt, override the "private" flag. - This flag cannot be set by outside callers. */ - VIR_SECRET_GET_VALUE_INTERNAL_CALL =3D 1 << 0, -}; - typedef virSecretPtr (*virDrvSecretLookupByUUID)(virConnectPtr conn, const unsigned char *uuid); @@ -57,8 +51,7 @@ typedef int typedef unsigned char * (*virDrvSecretGetValue)(virSecretPtr secret, size_t *value_size, - unsigned int flags, - unsigned int internalFlags); + unsigned int flags); =20 typedef int (*virDrvSecretUndefine)(virSecretPtr secret); diff --git a/src/libvirt-secret.c b/src/libvirt-secret.c index 75d40f53dc..a427805c7a 100644 --- a/src/libvirt-secret.c +++ b/src/libvirt-secret.c @@ -585,7 +585,7 @@ virSecretGetValue(virSecretPtr secret, size_t *value_si= ze, unsigned int flags) if (conn->secretDriver !=3D NULL && conn->secretDriver->secretGetValue= !=3D NULL) { unsigned char *ret; =20 - ret =3D conn->secretDriver->secretGetValue(secret, value_size, fla= gs, 0); + ret =3D conn->secretDriver->secretGetValue(secret, value_size, fla= gs); if (ret =3D=3D NULL) goto error; return ret; diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 0c72d69933..eed99af127 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5382,7 +5382,7 @@ remoteDomainBuildQemuMonitorEvent(virNetClientProgram= *prog G_GNUC_UNUSED, =20 static unsigned char * remoteSecretGetValue(virSecretPtr secret, size_t *value_size, - unsigned int flags, unsigned int internalFlags) + unsigned int flags) { unsigned char *rv =3D NULL; remote_secret_get_value_args args; @@ -5391,12 +5391,6 @@ remoteSecretGetValue(virSecretPtr secret, size_t *va= lue_size, =20 remoteDriverLock(priv); =20 - /* internalFlags intentionally do not go over the wire */ - if (internalFlags) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("no internalFlags s= upport")); - goto done; - } - make_nonnull_secret(&args.secret, secret); args.flags =3D flags; =20 diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c index 6ea8cc8ce9..d2175de8ed 100644 --- a/src/secret/secret_driver.c +++ b/src/secret/secret_driver.c @@ -36,6 +36,7 @@ #include "viruuid.h" #include "virerror.h" #include "virfile.h" +#include "viridentity.h" #include "virpidfile.h" #include "configmake.h" #include "virstring.h" @@ -352,8 +353,7 @@ secretSetValue(virSecretPtr secret, static unsigned char * secretGetValue(virSecretPtr secret, size_t *value_size, - unsigned int flags, - unsigned int internalFlags) + unsigned int flags) { unsigned char *ret =3D NULL; virSecretObj *obj; @@ -368,11 +368,31 @@ secretGetValue(virSecretPtr secret, if (virSecretGetValueEnsureACL(secret->conn, def) < 0) goto cleanup; =20 - if ((internalFlags & VIR_SECRET_GET_VALUE_INTERNAL_CALL) =3D=3D 0 && - def->isprivate) { - virReportError(VIR_ERR_INVALID_SECRET, "%s", - _("secret is private")); - goto cleanup; + /* + * For historical compat we want to deny access to + * private secrets, even if no ACL driver is + * present. + * + * We need to validate the identity requesting + * the secret value is running as the same user + * credentials as this driver. + * + * ie a non-root libvirt client should not be + * able to request the value from privileged + * libvirt driver. + * + * To apply restrictions to processes running under + * the same user account is out of scope. + */ + if (def->isprivate) { + int rv =3D virIdentityIsCurrentElevated(); + if (rv < 0) + goto cleanup; + if (rv =3D=3D 0) { + virReportError(VIR_ERR_INVALID_SECRET, "%s", + _("secret is private")); + goto cleanup; + } } =20 if (!(ret =3D virSecretObjGetValue(obj))) diff --git a/src/util/virsecret.c b/src/util/virsecret.c index 0695288229..604d900f77 100644 --- a/src/util/virsecret.c +++ b/src/util/virsecret.c @@ -174,8 +174,7 @@ virSecretGetSecretString(virConnectPtr conn, goto cleanup; } =20 - *secret =3D conn->secretDriver->secretGetValue(sec, secret_size, 0, - VIR_SECRET_GET_VALUE_INTE= RNAL_CALL); + *secret =3D conn->secretDriver->secretGetValue(sec, secret_size, 0); =20 if (!*secret) goto cleanup; diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index a93d21d61a..d5e59fe474 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -43,8 +43,7 @@ static virQEMUDriver driver; static unsigned char * fakeSecretGetValue(virSecretPtr obj G_GNUC_UNUSED, size_t *value_size, - unsigned int fakeflags G_GNUC_UNUSED, - unsigned int internalFlags G_GNUC_UNUSED) + unsigned int fakeflags G_GNUC_UNUSED) { char *secret; secret =3D g_strdup("AQCVn5hO6HzFAhAAq0NCv8jtJcIcE+HOBlMQ1A"); --=20 2.31.1