From nobody Tue Feb 10 09:24:21 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=quarantine dis=quarantine) header.from=huawei.com ARC-Seal: i=1; a=rsa-sha256; t=1672922002; cv=none; d=zohomail.com; s=zohoarc; b=He7HzEDhZToWy0YVfkiTuiQygNgrDMJOhEGGQ8kagAngcsJ5GTji3jBVOAFnNFqbQ3DQI19fptHl/y4hqUmmfs6Avts1DOwIrOW1R50tXReaWinZFAQwxdJuE9m07O2TBgSoGx6n5QLl7oce6oZUNmeuPOjhgEGfRHxvC0TeRUc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672922002; 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=HfBa3TUwB1B6QpftIxgtVfTZ/edCKexvSkhEHApg3q8=; b=Bwpf0+k5inV0eezdo6us8170ghElmTaYSf5MX6FWQarPuWD156HImhifyn66VVF17JiECU9XRlsDkuw2HvappUBRots9bBF+obCCydqwg0JPt2E0eH/OIW4U5wCSAAnMQThfD8MTn+YjiIh8Nc5Y51IEbq5Uj8vQV0sqIuO2f+I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=quarantine dis=quarantine) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1672922001975963.8224696776416; Thu, 5 Jan 2023 04:33:21 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-635-i1DYKBLLPa2kzuJALXWA9g-1; Thu, 05 Jan 2023 07:33:16 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9C5E3801779; Thu, 5 Jan 2023 12:33:13 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 86F692026D4B; Thu, 5 Jan 2023 12:33:13 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 707321946589; Thu, 5 Jan 2023 12:33:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id ED94F1946589 for ; Thu, 5 Jan 2023 12:33:12 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id CF5AD40ED784; Thu, 5 Jan 2023 12:33:12 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C5D364015309 for ; Thu, 5 Jan 2023 12:33:12 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AA787181E3FE for ; Thu, 5 Jan 2023 12:33:12 +0000 (UTC) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-90-OmzZUlqJMNGVEe2nxls7Ow-1; Thu, 05 Jan 2023 07:33:10 -0500 Received: from dggpeml500022.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Nnm6z4HvjznTWv for ; Thu, 5 Jan 2023 20:31:39 +0800 (CST) Received: from localhost.localdomain (10.175.124.27) by dggpeml500022.china.huawei.com (7.185.36.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Thu, 5 Jan 2023 20:33:07 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672922000; 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=HfBa3TUwB1B6QpftIxgtVfTZ/edCKexvSkhEHApg3q8=; b=e2nwCAd33qy/dMuD6OligGtegxnSGfa2HrUdtxG5Z//O2pyrvShqC/EOJA4UIKYLYg7oN+ evL48eeXipIaLOPDWTN5mXNNATUQ7snn/oZJWvA0cLPSPc2SqNlp82hxHKifHV9gEAODRO N9MYQDNrs/HWK+6YusXlfAl8n6H7v4o= X-MC-Unique: i1DYKBLLPa2kzuJALXWA9g-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: OmzZUlqJMNGVEe2nxls7Ow-1 From: Jiang Jiacheng To: Subject: [PATCH 6/9] security: use g_autofree and remove unnecessary label Date: Thu, 5 Jan 2023 20:26:52 +0800 Message-ID: <20230105122655.3413625-7-jiangjiacheng@huawei.com> In-Reply-To: <20230105122655.3413625-1-jiangjiacheng@huawei.com> References: <20230105122655.3413625-1-jiangjiacheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500022.china.huawei.com (7.185.36.66) X-CFilter-Loop: Reflected X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yubihong@huawei.com, zhengchuan@huawei.com, linyilu@huawei.com, jiangjiacheng@huawei.com, xiexiangyou@huawei.com Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1672922002434100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Jiang Jiacheng --- src/security/security_apparmor.c | 91 +++++++++++--------------------- 1 file changed, 31 insertions(+), 60 deletions(-) diff --git a/src/security/security_apparmor.c b/src/security/security_appar= mor.c index 36e8ce42b5..64b57f6a22 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -70,9 +70,9 @@ struct SDPDOP { static int profile_status(const char *str, const int check_enforcing) { - char *content =3D NULL; - char *tmp =3D NULL; - char *etmp =3D NULL; + g_autofree char *content =3D NULL; + g_autofree char *tmp =3D NULL; + g_autofree char *etmp =3D NULL; int rc =3D -2; =20 /* create string that is ' \0' for accurate matching */ @@ -87,7 +87,7 @@ profile_status(const char *str, const int check_enforcing) virReportSystemError(errno, _("Failed to read AppArmor profiles list " "\'%s\'"), APPARMOR_PROFILES_PATH); - goto cleanup; + return -2; } =20 if (strstr(content, tmp) !=3D NULL) @@ -99,11 +99,6 @@ profile_status(const char *str, const int check_enforcin= g) rc =3D 1; /* return '1' if loaded and enforcin= g */ } =20 - VIR_FREE(content); - cleanup: - VIR_FREE(tmp); - VIR_FREE(etmp); - return rc; } =20 @@ -320,12 +315,11 @@ AppArmorSetSecurityHostLabel(virSCSIVHostDevice *dev = G_GNUC_UNUSED, static int AppArmorSecurityManagerProbe(const char *virtDriver G_GNUC_UNUSED) { - char *template_qemu =3D NULL; - char *template_lxc =3D NULL; - int rc =3D SECURITY_DRIVER_DISABLE; + g_autofree char *template_qemu =3D NULL; + g_autofree char *template_lxc =3D NULL; =20 if (use_apparmor() < 0) - return rc; + return SECURITY_DRIVER_DISABLE; =20 /* see if template file exists */ template_qemu =3D g_strdup_printf("%s/TEMPLATE.qemu", APPARMOR_DIR "/l= ibvirt"); @@ -334,20 +328,15 @@ AppArmorSecurityManagerProbe(const char *virtDriver G= _GNUC_UNUSED) if (!virFileExists(template_qemu)) { virReportError(VIR_ERR_INTERNAL_ERROR, _("template \'%s\' does not exist"), template_qemu); - goto cleanup; + return SECURITY_DRIVER_DISABLE; } if (!virFileExists(template_lxc)) { virReportError(VIR_ERR_INTERNAL_ERROR, _("template \'%s\' does not exist"), template_lxc); - goto cleanup; + return SECURITY_DRIVER_DISABLE; } - rc =3D SECURITY_DRIVER_ENABLE; =20 - cleanup: - VIR_FREE(template_qemu); - VIR_FREE(template_lxc); - - return rc; + return SECURITY_DRIVER_ENABLE; } =20 /* Security driver initialization. DOI is for 'Domain of Interpretation' a= nd is @@ -387,8 +376,7 @@ static int AppArmorGenSecurityLabel(virSecurityManager *mgr G_GNUC_UNUSED, virDomainDef *def) { - int rc =3D -1; - char *profile_name =3D NULL; + g_autofree char *profile_name =3D NULL; virSecurityLabelDef *secdef =3D virDomainDefGetSecurityLabelDef(def, SECURITY_APPARMOR_NAME); =20 @@ -402,18 +390,18 @@ AppArmorGenSecurityLabel(virSecurityManager *mgr G_GN= UC_UNUSED, if (secdef->baselabel) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Cannot set a base label with AppArmour")); - return rc; + return -1; } =20 if (secdef->label || secdef->imagelabel) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("security label already defined for VM")); - return rc; + return -1; } =20 if ((profile_name =3D get_profile_name(def)) =3D=3D NULL) - return rc; + return -1; =20 secdef->label =3D g_strdup(profile_name); =20 @@ -431,18 +419,13 @@ AppArmorGenSecurityLabel(virSecurityManager *mgr G_GN= UC_UNUSED, goto err; } =20 - rc =3D 0; - goto cleanup; + return 0; =20 err: VIR_FREE(secdef->label); VIR_FREE(secdef->imagelabel); VIR_FREE(secdef->model); - - cleanup: - VIR_FREE(profile_name); - - return rc; + return -1; } =20 static int @@ -476,33 +459,29 @@ AppArmorGetSecurityProcessLabel(virSecurityManager *m= gr G_GNUC_UNUSED, { int rc =3D -1; int status; - char *profile_name =3D NULL; + g_autofree char *profile_name =3D NULL; =20 if ((profile_name =3D get_profile_name(def)) =3D=3D NULL) - return rc; + return -1; =20 status =3D profile_status(profile_name, 1); if (status < -1) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("error getting profile status")); - goto cleanup; + return -1; } else if (status =3D=3D -1) { sec->label[0] =3D '\0'; } else { if (virStrcpy(sec->label, profile_name, VIR_SECURITY_LABEL_BUFLEN)= < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("error copying profile name")); - goto cleanup; + return -1; } } =20 sec->enforcing =3D status =3D=3D 1; - rc =3D 0; - - cleanup: - VIR_FREE(profile_name); =20 - return rc; + return 0; } =20 /* Called on VM shutdown and destroy. See AppArmorGenSecurityLabel (above)= for @@ -555,7 +534,7 @@ AppArmorSetSecurityProcessLabel(virSecurityManager *mgr= G_GNUC_UNUSED, virDomainDef *def) { int rc =3D -1; - char *profile_name =3D NULL; + g_autofree char *profile_name =3D NULL; virSecurityLabelDef *secdef =3D virDomainDefGetSecurityLabelDef(def, SECURITY_APPARMOR_NAME); =20 @@ -563,7 +542,7 @@ AppArmorSetSecurityProcessLabel(virSecurityManager *mgr= G_GNUC_UNUSED, return 0; =20 if ((profile_name =3D get_profile_name(def)) =3D=3D NULL) - return rc; + return -1; =20 if (STRNEQ(SECURITY_APPARMOR_NAME, secdef->model)) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -572,21 +551,17 @@ AppArmorSetSecurityProcessLabel(virSecurityManager *m= gr G_GNUC_UNUSED, "hypervisor driver is \'%s\'."), secdef->model, SECURITY_APPARMOR_NAME); if (use_apparmor() > 0) - goto cleanup; + return -1; } =20 VIR_DEBUG("Changing AppArmor profile to %s", profile_name); if (aa_change_profile(profile_name) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("error calling aa_change_profile()")); - goto cleanup; + return -1; } - rc =3D 0; - - cleanup: - VIR_FREE(profile_name); =20 - return rc; + return 0; } =20 /* Called directly by API user prior to virCommandRun(). @@ -600,8 +575,8 @@ AppArmorSetSecurityChildProcessLabel(virSecurityManager= *mgr G_GNUC_UNUSED, virCommand *cmd) { int rc =3D -1; - char *profile_name =3D NULL; - char *cmd_str =3D NULL; + g_autofree char *profile_name =3D NULL; + g_autofree char *cmd_str =3D NULL; virSecurityLabelDef *secdef =3D virDomainDefGetSecurityLabelDef(def, SECURITY_APPARMOR_NAME); =20 @@ -615,21 +590,17 @@ AppArmorSetSecurityChildProcessLabel(virSecurityManag= er *mgr G_GNUC_UNUSED, "hypervisor driver is \'%s\'."), secdef->model, SECURITY_APPARMOR_NAME); if (use_apparmor() > 0) - goto cleanup; + return -1; } =20 if ((profile_name =3D get_profile_name(def)) =3D=3D NULL) - goto cleanup; + return -1; =20 cmd_str =3D virCommandToString(cmd, false); VIR_DEBUG("Changing AppArmor profile to %s on %s", profile_name, cmd_s= tr); virCommandSetAppArmorProfile(cmd, profile_name); - rc =3D 0; =20 - cleanup: - VIR_FREE(profile_name); - VIR_FREE(cmd_str); - return rc; + return 0; } =20 static int --=20 2.33.0