From nobody Wed Feb 11 02:54:52 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=1672997114; cv=none; d=zohomail.com; s=zohoarc; b=HTdcLR8HC69iR1E+2+bKhg624UmSkeEUquFlVqvSNfIHMKzwqymGr8FaSTcpS1t4qBUqkbYJcBqU5PYHhx3Gz53QbWZ9+IL/2CH5hZ11xT08/wh56k9sTE9iyDybEdQQeomcB9rgV2cxd3aZQjkTHU0cS35stKeZDU5Y5xWK1EY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672997114; 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=BrovuYkmvD17FbWQQ0S493gV/wtU9T6dPHo8vDsm8DQ=; b=G42xIQrB1JjFyRyKVt7lC8BjLneayn2RIcXJO0xOHZTnUQrivulJ8WsTRJlmOsevdpArDD70bjFRjL3I6maieXx1aQF0//klj2aZdAG8m5wcAsT0ET2aO/nz8X/FbYC3CaEUyq3izPSAV2KdCEQpdMEM17Cwoi+g3qTNaE6my0I= 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 1672997114288631.1494496548479; Fri, 6 Jan 2023 01:25:14 -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-577-OBEesKV-MjasImT927KwzQ-1; Fri, 06 Jan 2023 04:25:07 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4845E858902; Fri, 6 Jan 2023 09:25:02 +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 248581121331; Fri, 6 Jan 2023 09:25:02 +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 113591946A6B; Fri, 6 Jan 2023 09:25:02 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 47E2D19465A4 for ; Fri, 6 Jan 2023 09:25:01 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 3B741492B00; Fri, 6 Jan 2023 09:25:01 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3036A492B01 for ; Fri, 6 Jan 2023 09:25:01 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (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 14FA485C082 for ; Fri, 6 Jan 2023 09:25:01 +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-197-U47ECSRbP4uFENcUIRz_rQ-1; Fri, 06 Jan 2023 04:24:58 -0500 Received: from dggpeml500022.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NpHvL2wmGznV7n for ; Fri, 6 Jan 2023 17:23:26 +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; Fri, 6 Jan 2023 17:24:54 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672997113; 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=BrovuYkmvD17FbWQQ0S493gV/wtU9T6dPHo8vDsm8DQ=; b=hvNnwh8eiYfiVhKNeo9br8AFO4VMHO4S8BjutFzNhW/ha+XOfHX27ijP2suWArCJ7RaWuO HK71Aw0AzxIuqOOEbBbvKczf5n08kmbAc8DPG0NL/hPrNg9dmSlyeMbxr9lPUHhs3W2rhO khTmnWwbFXjgP2gzU7QmU6ER9L8qyCk= X-MC-Unique: OBEesKV-MjasImT927KwzQ-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: U47ECSRbP4uFENcUIRz_rQ-1 From: Jiang Jiacheng To: Subject: [PATCH V2 6/9] security: use g_autofree and remove unnecessary label Date: Fri, 6 Jan 2023 17:18:36 +0800 Message-ID: <20230106091839.1263372-7-jiangjiacheng@huawei.com> In-Reply-To: <20230106091839.1263372-1-jiangjiacheng@huawei.com> References: <20230106091839.1263372-1-jiangjiacheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) 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.10 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.3 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: 1672997114778100013 Content-Type: text/plain; charset="utf-8" Signed-off-by: Jiang Jiacheng --- src/security/security_apparmor.c | 94 +++++++++++--------------------- 1 file changed, 31 insertions(+), 63 deletions(-) diff --git a/src/security/security_apparmor.c b/src/security/security_appar= mor.c index 36e8ce42b5..b63b248975 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 @@ -474,35 +457,30 @@ AppArmorGetSecurityProcessLabel(virSecurityManager *m= gr G_GNUC_UNUSED, pid_t pid G_GNUC_UNUSED, virSecurityLabelPtr sec) { - 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 @@ -554,8 +532,7 @@ static int 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 +540,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 +549,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(). @@ -599,9 +572,8 @@ AppArmorSetSecurityChildProcessLabel(virSecurityManager= *mgr G_GNUC_UNUSED, virDomainDef *def, 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 +587,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