From nobody Sun Feb 8 21:28:49 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1646395137; cv=none; d=zohomail.com; s=zohoarc; b=NCPOu/AFvZtaryRCK2tMZSOknXVKgngJRCjIN9rnxRq+VJMeGJgi8qwP00ikFN6Itbba4w4lOnbBIHWD1xCP+yd0ZpRV21t1joUmSQBvLl3pstwN/nm9z3NvpR/3Tbj1e7B5RIB96WfZKpEpV1mMLjgre0Y5VcedMf6tUj4mTwI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646395137; 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=1nYHzC/UfWPANjgBseo3XpxZKrIiVBEM621wJS1z1is=; b=GiLRcDVUFWwYn++pyxUzSRt6OmSp3ZqBrE1JFfH/OHYIdZkiytWpYH7ViAePqLrCzwZ6aIrSWQu+xbCFxj6RESRUsE053W5Q9JbR+hxaObeT5CJbnXtZmq56F/vrufqfz5JyZEf6P2JVMJN6xoz4hKz3ZRFxNr9ZdhoAPtULspo= 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=pass header.from= (p=none dis=none) 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 1646395137968365.09094967452484; Fri, 4 Mar 2022 03:58:57 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-623-fUlmdPGSOlOMsTPnv-iwQw-1; Fri, 04 Mar 2022 06:58:53 -0500 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 E185751DF; Fri, 4 Mar 2022 11:58:45 +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 216EC83094; Fri, 4 Mar 2022 11:58:44 +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 C042E18095C9; Fri, 4 Mar 2022 11:58:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 224BvXj0028856 for ; Fri, 4 Mar 2022 06:57:33 -0500 Received: by smtp.corp.redhat.com (Postfix) id 75C372B4AC; Fri, 4 Mar 2022 11:57:33 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.39.194.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id 331712B450; Fri, 4 Mar 2022 11:57:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646395136; 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=1nYHzC/UfWPANjgBseo3XpxZKrIiVBEM621wJS1z1is=; b=GUjPmdFBA8QvqEbCKRL8anE5fgc1rUQJ4dz7/pcJYBDb3TKRUFskfjUGfRMn+8IfqUm9/D CB/3DrC0hMC6MOGnR17OKvNv0sKJoJbCGs5qBTEe6rLFu5tfs/TzknRPzXpRyEoTnn8Bua dRe2RNmEdl7A+zZbSWO+LLauVBpZ3Fg= X-MC-Unique: fUlmdPGSOlOMsTPnv-iwQw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH 2/4] os-posix: refactor code handling the -runas argument Date: Fri, 4 Mar 2022 11:56:55 +0000 Message-Id: <20220304115657.3177925-3-berrange@redhat.com> In-Reply-To: <20220304115657.3177925-1-berrange@redhat.com> References: <20220304115657.3177925-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Cc: Kevin Wolf , libvir-list@redhat.com, Stefan Weil , Hanna Reitz , Gerd Hoffmann , Paolo Bonzini , Eric Blake 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) X-ZM-MESSAGEID: 1646395139781100001 Change the change_process_uid() function so that it takes its input as parameters instead of relying on static global variables. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Eric Blake --- os-posix.c | 83 +++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/os-posix.c b/os-posix.c index 7cd662098e..5a127feee2 100644 --- a/os-posix.c +++ b/os-posix.c @@ -42,13 +42,9 @@ #include #endif =20 -/* - * Must set all three of these at once. - * Legal combinations are unset by name by uid - */ -static struct passwd *user_pwd; /* NULL non-NULL NULL */ -static uid_t user_uid =3D (uid_t)-1; /* -1 -1 >=3D0 */ -static gid_t user_gid =3D (gid_t)-1; /* -1 -1 >=3D0 */ +static char *user_name; +static uid_t user_uid =3D (uid_t)-1; +static gid_t user_gid =3D (gid_t)-1; =20 static const char *chroot_dir; static int daemonize; @@ -100,7 +96,8 @@ void os_set_proc_name(const char *s) } =20 =20 -static bool os_parse_runas_uid_gid(const char *optarg) +static bool os_parse_runas_uid_gid(const char *optarg, + uid_t *runas_uid, gid_t *runas_gid) { unsigned long lv; const char *ep; @@ -120,9 +117,8 @@ static bool os_parse_runas_uid_gid(const char *optarg) return false; } =20 - user_pwd =3D NULL; - user_uid =3D got_uid; - user_gid =3D got_gid; + *runas_uid =3D got_uid; + *runas_gid =3D got_gid; return true; } =20 @@ -132,13 +128,18 @@ static bool os_parse_runas_uid_gid(const char *optarg) */ int os_parse_cmd_args(int index, const char *optarg) { + struct passwd *user_pwd; + switch (index) { case QEMU_OPTION_runas: user_pwd =3D getpwnam(optarg); if (user_pwd) { - user_uid =3D -1; - user_gid =3D -1; - } else if (!os_parse_runas_uid_gid(optarg)) { + user_uid =3D user_pwd->pw_uid; + user_gid =3D user_pwd->pw_gid; + user_name =3D g_strdup(user_pwd->pw_name); + } else if (!os_parse_runas_uid_gid(optarg, + &user_uid, + &user_gid)) { error_report("User \"%s\" doesn't exist" " (and is not :)", optarg); @@ -158,41 +159,33 @@ int os_parse_cmd_args(int index, const char *optarg) return 0; } =20 -static void change_process_uid(void) +static void change_process_uid(uid_t uid, gid_t gid, const char *name) { - assert((user_uid =3D=3D (uid_t)-1) || user_pwd =3D=3D NULL); - assert((user_uid =3D=3D (uid_t)-1) =3D=3D - (user_gid =3D=3D (gid_t)-1)); - - if (user_pwd || user_uid !=3D (uid_t)-1) { - gid_t intended_gid =3D user_pwd ? user_pwd->pw_gid : user_gid; - uid_t intended_uid =3D user_pwd ? user_pwd->pw_uid : user_uid; - if (setgid(intended_gid) < 0) { - error_report("Failed to setgid(%d)", intended_gid); - exit(1); - } - if (user_pwd) { - if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) { - error_report("Failed to initgroups(\"%s\", %d)", - user_pwd->pw_name, user_pwd->pw_gid); - exit(1); - } - } else { - if (setgroups(1, &user_gid) < 0) { - error_report("Failed to setgroups(1, [%d])", - user_gid); - exit(1); - } - } - if (setuid(intended_uid) < 0) { - error_report("Failed to setuid(%d)", intended_uid); + if (setgid(gid) < 0) { + error_report("Failed to setgid(%d)", gid); + exit(1); + } + if (name) { + if (initgroups(name, gid) < 0) { + error_report("Failed to initgroups(\"%s\", %d)", + name, gid); exit(1); } - if (setuid(0) !=3D -1) { - error_report("Dropping privileges failed"); + } else { + if (setgroups(1, &gid) < 0) { + error_report("Failed to setgroups(1, [%d])", + gid); exit(1); } } + if (setuid(uid) < 0) { + error_report("Failed to setuid(%d)", uid); + exit(1); + } + if (setuid(0) !=3D -1) { + error_report("Dropping privileges failed"); + exit(1); + } } =20 static void change_root(void) @@ -275,7 +268,9 @@ void os_setup_post(void) } =20 change_root(); - change_process_uid(); + if (user_uid !=3D -1 && user_gid !=3D -1) { + change_process_uid(user_uid, user_gid, user_name); + } =20 if (daemonize) { uint8_t status =3D 0; --=20 2.34.1