From nobody Tue Jan 21 07:36:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=canonical.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1736263694452820.8162367013801; Tue, 7 Jan 2025 07:28:14 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 83EB7E5C; Tue, 7 Jan 2025 10:28:13 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 8C66A1185; Tue, 7 Jan 2025 10:24:29 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 8CE84C7C; Tue, 7 Jan 2025 10:24:26 -0500 (EST) Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 06FEFC6B for ; Tue, 7 Jan 2025 10:24:23 -0500 (EST) Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 183E13F2BD for ; Tue, 7 Jan 2025 15:24:22 +0000 (UTC) Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-21632eacb31so167452065ad.0 for ; Tue, 07 Jan 2025 07:24:22 -0800 (PST) Received: from georgia.. ([2001:1284:f502:1965:7bc6:bb4:7636:7bb1]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-842b85f00f9sm31048120a12.43.2025.01.07.07.24.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 07:24:19 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1736263462; bh=17lQZKl+ciFe61HPR9L6kunP/iM/1XszbrSsGsku9vA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SyKg2KTJGX0S0Z3vZF7e81M6SOtyi6YyqoYUT28jt+Y9k4eewkTOzggKrZXELOybm PbtzzE9EMzSxfJul4Lx3ccG7OTu6dT6902FMYkuIFwHXq+R0oj4MS+aHfZuU866SWJ EtgAnFfI9eSdDzTgzBWPFCZblE0ia9MLGvmcE2jXlr/K1Jb6RiZkHjFn42duycUTlw 9EHe+88houJ+pWZybsCCItAdOSSrcRR9Jle3wjUvHPu2zfcvWAEdktYywNkfrbfFqD Z69mk5/Ga5/ET9Yn/h8+buYWPASRMERYYiB4E3mQCJRdyYSwN4Pc/P0i4Plstuj0KP mBkLlor/zZA9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736263460; x=1736868260; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=17lQZKl+ciFe61HPR9L6kunP/iM/1XszbrSsGsku9vA=; b=MgRWmo0SKwsJrnZbUrLkKDVuqJEIDu/3JK+3mTB/yBdnllGtz2hGt1MYZmma2vZyas 8V3EksnjXkQvMEI9LKwJrdK7W2xVjj3WXKr0f5GTb9ER7znpOoqrWGw2UU8XCfWqYQd1 S0cZ4PNNWYBhxjMvPGn6qXBlTkDf5uXW9L4qOL+ShEo4BVCTNF+DEoADKsxlYZOghm6F JgRt2u5cz4tb/WVL+De/Tfysmuh4BAfyIp811XyepQxwFUtpWG9R+frtOeYuq/BEqIg6 Ir+wgVgusjOMmtOj5XB7RkpTYErfTWrka51DELsnNt8WjOVWBiDp3UK9tFZxzfgS305g RceQ== X-Gm-Message-State: AOJu0Yx1kx+n7wHlK1lTDnT1X1xlStV/CaooCRPCo0MIXGH0XM7MV63i NoccGqkMQ7JS9QXMCt4Sym/y+348LihqXcIdQLVEuax2HFA0b2DjO+U9v3+xy0lX0kEahjpilkD ZfDiEKmTO2dSSleR+mnRK9Myppuw4iXENH7MQdlkHTWAUypD4CTn2mQNtHBqlejTYXnWP0irr3B n89d4= X-Gm-Gg: ASbGnctlIiof/DUIZQwzqy/YH7nFUJkS2qdYGX2jsm1yaj6Ua42SiBRu2UbwkhifjPQ FOd8twls5PaJrVvNFd3Vs8YUOyYfUZNF+i+uniw2pW8fKTSaCSXtg57Cb1o9J63bqIsN3OjoDHs TwiaLi86i1ChcbZSU+YlHgbMXAKlMnDW2hR/athDYJZWTgmGAt2FxPFCPGzST2WEObaeOtoRw9i OPZLvpjhy/GEZ4hINko7iuhXVvg9Kk5Gk4W+1cyY1Et6CrRHW0TkzezmNWssAeKlk7W X-Received: by 2002:a17:903:2310:b0:215:a190:ba10 with SMTP id d9443c01a7336-219e6ea23d7mr825999825ad.15.1736263460147; Tue, 07 Jan 2025 07:24:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IFblI3HCBcXFPqOIaWUMPZd7dDwOz9aJH/vvR2ZDr1i66NhzWQJoAFp6iZD1VZbic+aGAh1ww== X-Received: by 2002:a17:903:2310:b0:215:a190:ba10 with SMTP id d9443c01a7336-219e6ea23d7mr825999485ad.15.1736263459726; Tue, 07 Jan 2025 07:24:19 -0800 (PST) From: Georgia Garcia To: devel@lists.libvirt.org Subject: [PATCH v3 2/4] security: replace uses of label and VIR_FREE by g_autofree Date: Tue, 7 Jan 2025 12:23:37 -0300 Message-ID: <20250107152357.1026544-3-georgia.garcia@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250107152357.1026544-1-georgia.garcia@canonical.com> References: <20250107152357.1026544-1-georgia.garcia@canonical.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: KUD3EVE72ZNJKB7QWAS3ETUGI5LGQOHB X-Message-ID-Hash: KUD3EVE72ZNJKB7QWAS3ETUGI5LGQOHB X-MailFrom: georgia.garcia@canonical.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Georgia Garcia X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1736263696379116600 Content-Type: text/plain; charset="utf-8" Moving towards full adoption of GLib APIs in the AppArmor code. Signed-off-by: Georgia Garcia Reviewed-by: Jim Fehlig --- src/security/security_apparmor.c | 42 +++++-------- src/security/virt-aa-helper.c | 100 ++++++++++--------------------- 2 files changed, 46 insertions(+), 96 deletions(-) diff --git a/src/security/security_apparmor.c b/src/security/security_appar= mor.c index ae2175d334..91c51f6395 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -115,37 +115,28 @@ profile_loaded(const char *str) static int profile_status_file(const char *str) { - char *profile =3D NULL; - char *content =3D NULL; - char *tmp =3D NULL; - int rc =3D -1; + g_autofree char *profile =3D NULL; + g_autofree char *content =3D NULL; + g_autofree char *tmp =3D NULL; int len; =20 profile =3D g_strdup_printf("%s/%s", APPARMOR_DIR "/libvirt", str); =20 if (!virFileExists(profile)) - goto failed; + return -1; =20 if ((len =3D virFileReadAll(profile, MAX_FILE_LEN, &content)) < 0) { virReportSystemError(errno, _("Failed to read \'%1$s\'"), profile); - goto failed; + return -1; } =20 /* create string that is ' flags=3D(complain)\0' */ tmp =3D g_strdup_printf(" %s flags=3D(complain)", str); =20 if (strstr(content, tmp) !=3D NULL) - rc =3D 0; - else - rc =3D 1; - - failed: - VIR_FREE(tmp); - VIR_FREE(profile); - VIR_FREE(content); - - return rc; + return 0; + return 1; } =20 /* @@ -218,7 +209,7 @@ static int use_apparmor(void) { int rc =3D -1; - char *libvirt_daemon =3D NULL; + g_autofree char *libvirt_daemon =3D NULL; =20 if (virFileResolveLink("/proc/self/exe", &libvirt_daemon) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -232,7 +223,7 @@ use_apparmor(void) return 1; =20 if (access(APPARMOR_PROFILES_PATH, R_OK) !=3D 0) - goto cleanup; + return rc; =20 /* First check profile status using full binary path. If that fails * check using profile name. @@ -247,8 +238,6 @@ use_apparmor(void) rc =3D -1; } =20 - cleanup: - VIR_FREE(libvirt_daemon); return rc; } =20 @@ -950,7 +939,8 @@ AppArmorSetChardevLabel(virSecurityManager *mgr, virDomainChrSourceDef *dev_source, bool chardevStdioLogd G_GNUC_UNUSED) { - char *in =3D NULL, *out =3D NULL; + g_autofree char *in =3D NULL; + g_autofree char *out =3D NULL; int ret =3D -1; virSecurityLabelDef *secdef; =20 @@ -971,11 +961,11 @@ AppArmorSetChardevLabel(virSecurityManager *mgr, out =3D g_strdup_printf("%s.out", dev_source->data.file.path); if (virFileExists(in)) { if (reload_profile(mgr, def, in, true) < 0) - goto done; + return ret; } if (virFileExists(out)) { if (reload_profile(mgr, def, out, true) < 0) - goto done; + return ret; } ret =3D reload_profile(mgr, def, dev_source->data.file.path, true); break; @@ -995,9 +985,6 @@ AppArmorSetChardevLabel(virSecurityManager *mgr, break; } =20 - done: - VIR_FREE(in); - VIR_FREE(out); return ret; } =20 @@ -1083,12 +1070,11 @@ AppArmorSetPathLabel(virSecurityManager *mgr, bool allowSubtree) { int rc =3D -1; - char *full_path =3D NULL; + g_autofree char *full_path =3D NULL; =20 if (allowSubtree) { full_path =3D g_strdup_printf("%s/{,**}", path); rc =3D reload_profile(mgr, def, full_path, true); - VIR_FREE(full_path); } else { rc =3D reload_profile(mgr, def, path, true); } diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index 94a28bf331..1626d5a89c 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -146,9 +146,8 @@ vah_info(const char *str) static int parserCommand(const char *profile_name, const char cmd) { - int result =3D -1; char flag[3]; - char *profile; + g_autofree char *profile =3D NULL; int status; int ret; =20 @@ -163,7 +162,7 @@ parserCommand(const char *profile_name, const char cmd) =20 if (!virFileExists(profile)) { vah_error(NULL, 0, _("profile does not exist")); - goto cleanup; + return -1; } else { const char * const argv[] =3D { "/sbin/apparmor_parser", flag, profile, NULL @@ -175,23 +174,18 @@ parserCommand(const char *profile_name, const char cm= d) (WIFEXITED(status) && WEXITSTATUS(status) !=3D 0)) { if (ret !=3D 0) { vah_error(NULL, 0, _("failed to run apparmor_parser")); - goto cleanup; + return -1; } else if (cmd =3D=3D 'R' && WIFEXITED(status) && WEXITSTATUS(status) =3D=3D 234) { vah_warning(_("unable to unload already unloaded profile")= ); } else { vah_error(NULL, 0, _("apparmor_parser exited with error")); - goto cleanup; + return -1; } } } =20 - result =3D 0; - - cleanup: - VIR_FREE(profile); - - return result; + return 0; } =20 /* @@ -201,18 +195,17 @@ static int update_include_file(const char *include_file, const char *included_files, bool append) { - int rc =3D -1; int plen, flen =3D 0; int fd; - char *pcontent =3D NULL; - char *existing =3D NULL; + g_autofree char *pcontent =3D NULL; + g_autofree char *existing =3D NULL; const char *warning =3D "# DO NOT EDIT THIS FILE DIRECTLY. IT IS MANAGED BY LIBVIRT.\n"; =20 if (virFileExists(include_file)) { flen =3D virFileReadAll(include_file, MAX_FILE_LEN, &existing); if (flen < 0) - return rc; + return -1; } =20 if (append && virFileExists(include_file)) @@ -223,38 +216,31 @@ update_include_file(const char *include_file, const c= har *included_files, plen =3D strlen(pcontent); if (plen > MAX_FILE_LEN) { vah_error(NULL, 0, _("invalid length for new profile")); - goto cleanup; + return -1; } =20 /* only update the disk profile if it is different */ if (flen > 0 && flen =3D=3D plen && STREQLEN(existing, pcontent, plen)= ) { - rc =3D 0; - goto cleanup; + return 0; } =20 /* write the file */ if ((fd =3D open(include_file, O_CREAT | O_TRUNC | O_WRONLY, 0644)) = =3D=3D -1) { vah_error(NULL, 0, _("failed to create include file")); - goto cleanup; + return -1; } =20 if (safewrite(fd, pcontent, plen) < 0) { /* don't write the '\0' */ VIR_FORCE_CLOSE(fd); vah_error(NULL, 0, _("failed to write to profile")); - goto cleanup; + return -1; } =20 if (VIR_CLOSE(fd) !=3D 0) { vah_error(NULL, 0, _("failed to close or write to profile")); - goto cleanup; + return -1; } - rc =3D 0; - - cleanup: - VIR_FREE(pcontent); - VIR_FREE(existing); - - return rc; + return 0; } =20 /* @@ -574,7 +560,7 @@ caps_mockup(vahControl * ctl, const char *xmlStr) { g_autoptr(xmlDoc) xml =3D NULL; g_autoptr(xmlXPathContext) ctxt =3D NULL; - char *arch; + g_autofree char *arch =3D NULL; =20 if (!(xml =3D virXMLParse(NULL, xmlStr, _("(domain_definition)"), "domain", &ctxt, NULL, false))) { @@ -600,7 +586,6 @@ caps_mockup(vahControl * ctl, const char *xmlStr) ctl->arch =3D virArchFromHost(); } else { ctl->arch =3D virArchFromString(arch); - VIR_FREE(arch); } =20 return 0; @@ -685,15 +670,15 @@ get_definition(vahControl * ctl, const char *xmlStr) static int vah_add_path(virBuffer *buf, const char *path, const char *perms, bool rec= ursive) { - char *tmp =3D NULL; int rc =3D -1; bool readonly =3D true; bool explicit_deny_rule =3D true; char *sub =3D NULL; - char *perms_new =3D NULL; - char *pathdir =3D NULL; - char *pathtmp =3D NULL; - char *pathreal =3D NULL; + g_autofree char *tmp =3D NULL; + g_autofree char *perms_new =3D NULL; + g_autofree char *pathdir =3D NULL; + g_autofree char *pathtmp =3D NULL; + g_autofree char *pathreal =3D NULL; =20 if (path =3D=3D NULL) return rc; @@ -730,7 +715,7 @@ vah_add_path(virBuffer *buf, const char *path, const ch= ar *perms, bool recursive if ((pathreal =3D realpath(pathdir, NULL)) =3D=3D NULL) { vah_error(NULL, 0, pathdir); vah_error(NULL, 0, _("could not find realpath")); - goto cleanup; + return rc; } tmp =3D g_strdup_printf("%s%s", pathreal, pathtmp); } @@ -754,7 +739,7 @@ vah_add_path(virBuffer *buf, const char *path, const ch= ar *perms, bool recursive vah_error(NULL, 0, path); vah_error(NULL, 0, _("skipped restricted file")); } - goto cleanup; + return rc; } =20 if (tmp[strlen(tmp) - 1] =3D=3D '/') @@ -771,13 +756,6 @@ vah_add_path(virBuffer *buf, const char *path, const c= har *perms, bool recursive virBufferAsprintf(buf, " \"%s/\" r,\n", tmp); } =20 - cleanup: - VIR_FREE(pathdir); - VIR_FREE(pathtmp); - VIR_FREE(pathreal); - VIR_FREE(perms_new); - VIR_FREE(tmp); - return rc; } =20 @@ -793,36 +771,28 @@ vah_add_file_chardev(virBuffer *buf, const char *perms, const int type) { - char *pipe_in; - char *pipe_out; - int rc =3D -1; + g_autofree char *pipe_in =3D NULL; + g_autofree char *pipe_out =3D NULL; =20 if (type =3D=3D VIR_DOMAIN_CHR_TYPE_PIPE) { /* add the pipe input */ pipe_in =3D g_strdup_printf("%s.in", path); =20 if (vah_add_file(buf, pipe_in, perms) !=3D 0) - goto clean_pipe_in; + return -1; =20 /* add the pipe output */ pipe_out =3D g_strdup_printf("%s.out", path); =20 if (vah_add_file(buf, pipe_out, perms) !=3D 0) - goto clean_pipe_out; - - rc =3D 0; - clean_pipe_out: - VIR_FREE(pipe_out); - clean_pipe_in: - VIR_FREE(pipe_in); + return -1; } else { /* add the file */ if (vah_add_file(buf, path, perms) !=3D 0) return -1; - rc =3D 0; } =20 - return rc; + return 0; } =20 static int @@ -1473,8 +1443,8 @@ main(int argc, char **argv) vahControl _ctl =3D { 0 }; vahControl *ctl =3D &_ctl; int rc =3D -1; - char *profile =3D NULL; - char *include_file =3D NULL; + g_autofree char *profile =3D NULL; + g_autofree char *include_file =3D NULL; off_t size; bool purged =3D 0; =20 @@ -1517,7 +1487,7 @@ main(int argc, char **argv) if (ctl->cmd =3D=3D 'D') unlink(include_file); } else if (ctl->cmd =3D=3D 'c' || ctl->cmd =3D=3D 'r') { - char *included_files =3D NULL; + g_autofree char *included_files =3D NULL; g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; =20 if (ctl->cmd =3D=3D 'c' && virFileExists(profile)) @@ -1579,7 +1549,7 @@ main(int argc, char **argv) =20 /* create the profile from TEMPLATE */ if (ctl->cmd =3D=3D 'c' || purged) { - char *tmp =3D NULL; + g_autofree char *tmp =3D NULL; #if defined(WITH_APPARMOR_3) const char *ifexists =3D "if exists "; #else @@ -1597,7 +1567,6 @@ main(int argc, char **argv) vah_error(ctl, 0, _("could not create profile")); unlink(include_file); } - VIR_FREE(tmp); } =20 if (rc =3D=3D 0 && !ctl->dryrun) { @@ -1613,14 +1582,9 @@ main(int argc, char **argv) unlink(profile); } } - cleanup: - VIR_FREE(included_files); } - + cleanup: vahDeinit(ctl); =20 - VIR_FREE(profile); - VIR_FREE(include_file); - exit(rc =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE); } --=20 2.43.0