From nobody Fri Nov 22 10:34:26 2024 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=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1716393737842600.1747318500016; Wed, 22 May 2024 09:02:17 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id ABDEB1D63; Wed, 22 May 2024 12:02:16 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 043ED1B95; Wed, 22 May 2024 12:00:30 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 658891A48; Wed, 22 May 2024 12:00:26 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 C284D1A1F for ; Wed, 22 May 2024 12:00:25 -0400 (EDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-446-98ob-t6jODKXC3KJHvWntA-1; Wed, 22 May 2024 12:00:23 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5DDA4801948 for ; Wed, 22 May 2024 16:00:23 +0000 (UTC) Received: from speedmetal.lan (unknown [10.45.242.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id C16B6C15BB1 for ; Wed, 22 May 2024 16:00:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716393625; h=from:from: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; bh=8lLOt9Wvy/D34ZHkEp8QKSbX/Dl1HzF1IJ2MxNvkI8c=; b=IhLG+VZ5japXG4sxY3+sDNbWk1bJnASaO0q1Kad1DJ5OLS7UVJdMMFkJ93hwdanLD+rgTH pwNaBpTrFO8X5Ea5JnH28L1YLq6YAO0l9+YykRS+jOtkxn+ZR6yi2a+bdj3aWNjtiAPant mS77zwCVB1JrbjDV1ZqcaztLF0XOwzc= X-MC-Unique: 98ob-t6jODKXC3KJHvWntA-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 2/4] virGetGroupList: Refactor and fix callers Date: Wed, 22 May 2024 18:00:17 +0200 Message-ID: <263b922fa2a52e7a77f45ad81f734c84d4909727.1716393556.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: QUN7WR6NQW62PPEFRSAVK7F4A7ZSB577 X-Message-ID-Hash: QUN7WR6NQW62PPEFRSAVK7F4A7ZSB577 X-MailFrom: pkrempa@redhat.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 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: 1716393739473100001 Content-Type: text/plain; charset="utf-8" Use contemporary style for declarations and automatic memory clearing for a helper string. Since the function can't fail any more, remove any mention of returning errno and remove error checks from all callers. Signed-off-by: Peter Krempa Reviewed-by: Jiri Denemark --- src/lxc/lxc_container.c | 4 ++-- src/security/security_dac.c | 7 +------ src/util/vircommand.c | 3 +-- src/util/virfile.c | 8 -------- src/util/virutil.c | 16 ++++++++-------- tests/commandtest.c | 5 ++--- tools/virt-login-shell-helper.c | 3 +-- 7 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 652697890f..7e460544fb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -2059,8 +2059,8 @@ static int lxcContainerChild(void *data) /* TODO is it safe to call it here or should this call be moved in * front of the clone() as otherwise there might be a risk for a * deadlock */ - if ((ngroups =3D virGetGroupList(virCommandGetUID(cmd), virCommandGetG= ID(cmd), - &groups)) < 0) + ngroups =3D virGetGroupList(virCommandGetUID(cmd), virCommandGetGID(cm= d), + &groups); goto cleanup; ret =3D 0; diff --git a/src/security/security_dac.c b/src/security/security_dac.c index 4e850e219e..669b90125c 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -524,14 +524,9 @@ static int virSecurityDACPreFork(virSecurityManager *mgr) { virSecurityDACData *priv =3D virSecurityManagerGetPrivateData(mgr); - int ngroups; g_clear_pointer(&priv->groups, g_free); - priv->ngroups =3D 0; - if ((ngroups =3D virGetGroupList(priv->user, priv->group, - &priv->groups)) < 0) - return -1; - priv->ngroups =3D ngroups; + priv->ngroups =3D virGetGroupList(priv->user, priv->group, &priv->grou= ps); return 0; } diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 81e74deee0..07bee9b12e 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -735,8 +735,7 @@ virExec(virCommand *cmd) childerr =3D null; } - if ((ngroups =3D virGetGroupList(cmd->uid, cmd->gid, &groups)) < 0) - goto cleanup; + ngroups =3D virGetGroupList(cmd->uid, cmd->gid, &groups); pid =3D virFork(); diff --git a/src/util/virfile.c b/src/util/virfile.c index f66ecd29a2..c4d22921ce 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -2285,8 +2285,6 @@ virFileAccessibleAs(const char *path, int mode, return access(path, mode); ngroups =3D virGetGroupList(uid, gid, &groups); - if (ngroups < 0) - return -1; pid =3D virFork(); @@ -2408,8 +2406,6 @@ virFileOpenForked(const char *path, * NFS servers. */ ngroups =3D virGetGroupList(uid, gid, &groups); - if (ngroups < 0) - return -errno; if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) { ret =3D -errno; @@ -2709,8 +2705,6 @@ virFileRemove(const char *path, gid =3D getegid(); ngroups =3D virGetGroupList(uid, gid, &groups); - if (ngroups < 0) - return -errno; pid =3D virFork(); @@ -2883,8 +2877,6 @@ virDirCreate(const char *path, gid =3D getegid(); ngroups =3D virGetGroupList(uid, gid, &groups); - if (ngroups < 0) - return -errno; pid =3D virFork(); diff --git a/src/util/virutil.c b/src/util/virutil.c index bd3bbe3f0d..dc5009f11d 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -880,14 +880,16 @@ VIR_WARNINGS_NO_POINTER_SIGN * storing a malloc'd result into @list. If uid is -1 or doesn't exist in = the * system database querying of the supplementary groups is skipped. * - * Returns the size of the list on success, or -1 on failure with error - * reported and errno set. May not be called between fork and exec. + * Returns the size of the list. Doesn't have an error path. + * May not be called between fork and exec. * */ int -virGetGroupList(uid_t uid, gid_t gid, gid_t **list) +virGetGroupList(uid_t uid, + gid_t gid, + gid_t **list) { int ret =3D 0; - char *user =3D NULL; + g_autofree char *user =3D NULL; gid_t primary; *list =3D NULL; @@ -925,14 +927,12 @@ virGetGroupList(uid_t uid, gid_t gid, gid_t **list) for (i =3D 0; i < ret; i++) { if ((*list)[i] =3D=3D gid) - goto cleanup; + return ret; } VIR_APPEND_ELEMENT(*list, i, gid); - ret =3D i; + return i; } - cleanup: - VIR_FREE(user); return ret; } diff --git a/tests/commandtest.c b/tests/commandtest.c index aa108ce583..08fbe1801a 100644 --- a/tests/commandtest.c +++ b/tests/commandtest.c @@ -908,9 +908,8 @@ static int test25(const void *unused G_GNUC_UNUSED) goto cleanup; } - if ((ngroups =3D virGetGroupList(virCommandGetUID(cmd), virCommandGetG= ID(cmd), - &groups)) < 0) - goto cleanup; + ngroups =3D virGetGroupList(virCommandGetUID(cmd), virCommandGetGID(cm= d), + &groups); /* Now, fork and try to exec a nonexistent binary. */ pid =3D virFork(); diff --git a/tools/virt-login-shell-helper.c b/tools/virt-login-shell-helpe= r.c index cb94c49720..1627ba85e5 100644 --- a/tools/virt-login-shell-helper.c +++ b/tools/virt-login-shell-helper.c @@ -260,8 +260,7 @@ main(int argc, char **argv) if (!(conf =3D virConfReadFile(login_shell_path, 0))) goto cleanup; - if ((ngroups =3D virGetGroupList(uid, gid, &groups)) < 0) - goto cleanup; + ngroups =3D virGetGroupList(uid, gid, &groups); if (virLoginShellAllowedUser(conf, name, groups, ngroups) < 0) goto cleanup; --=20 2.45.1