From nobody Mon Feb 9 01:16:20 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=1646420350; cv=none; d=zohomail.com; s=zohoarc; b=kSCyD5L5iNcRWdWqElRtTn9hyvi2czXOY+ySp1xHs9XnSDNBvP3lsqaZcvkvTYHPcz75M4xOPbC0PTUltgL5SLEau3ydBfNRSI9v6XDCzbUCRIcVSsZPlJ7W8AgdXO2Hksyps0G8f093wcY6xJC10/eRzcJLa/yzOg584G5aPwc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646420350; 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=gPvFWCKXMBPsfZIqyM+P/NLAeYAgxvVXGpPRUB48jL4=; b=PFb95JIXJreeZduK1iLpMKhDqRqFhHAlEFTOUf5Ps9yCkVRypuw3xU6c8NcgUonlZSWFbCWuYR82avC1iElFyndjTtDOSnYhhnZcNUgg18ebK2+1iW6gVFMw/AQs3bFvb0ZZO0C4eYgvuYosOn8+9wKvwgNIZdeSyS0VeCLXguI= 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 1646420350335645.9583709280118; Fri, 4 Mar 2022 10:59:10 -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-115-heo02UGzMn6c_o1FeOm84w-1; Fri, 04 Mar 2022 13:59:05 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8BB5D180FD71; Fri, 4 Mar 2022 18:58:59 +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 35F479231D; Fri, 4 Mar 2022 18:58:59 +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 06EBD1809C98; Fri, 4 Mar 2022 18:58:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 224IwEip031741 for ; Fri, 4 Mar 2022 13:58:14 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3592587B8F; Fri, 4 Mar 2022 18:58:14 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.39.194.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id A909687B87; Fri, 4 Mar 2022 18:57:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646420349; 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=gPvFWCKXMBPsfZIqyM+P/NLAeYAgxvVXGpPRUB48jL4=; b=QaHr/UkK/EJxZoX5qMMWfn9TNjzD4erH8wyYnba7rcFiDKrDfl9gNScoLvBEfV4uDCO3K3 0p+eunYZD+H9yQxnOpSeRnwtQlP6O9lhBS0LZ/ajwpsDWINWV0WtaBUZn7Xt5ksC5z7LVU A6GkMYEqHSV6CxQsPlXwrBuu/lAU9TY= X-MC-Unique: heo02UGzMn6c_o1FeOm84w-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH v2 2/8] os-posix: refactor code handling the -runas argument Date: Fri, 4 Mar 2022 18:56:14 +0000 Message-Id: <20220304185620.3272401-3-berrange@redhat.com> In-Reply-To: <20220304185620.3272401-1-berrange@redhat.com> References: <20220304185620.3272401-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Kevin Wolf , libvir-list@redhat.com, Stefan Weil , Hanna Reitz , Gerd Hoffmann , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , 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.16 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: 1646420352170100001 Change the change_process_uid() function so that it takes its input as parameters instead of relying on static global variables. Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrang=C3=A9 --- 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