From nobody Sat May 11 10:00:03 2024 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=1646395203; cv=none; d=zohomail.com; s=zohoarc; b=hYynWg5tlEbUoRIiQfS1LDi+A7lj7iXeEVUzSLqEG0IhGDGymZ+Nv5Y4o35yjSBqJ2sRqEzqdwgrLFcY3xcvdF1xH4gpgqr2sq/mUu5Io3zUxcWC8AzgcgWvVinB7qlwnqzeaYsMqZ/ya9yygRcmTcAe1V/wvcNDIkUV2k4lYrg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646395203; 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=u043MNg0X/ibMCczSn1TLthOQOkuTPJ0YFjxqylO6zg=; b=gs914FkjKuNg4NgQaSpYfoK2EBk/u0bC1D8/2EgVrLR7qK6oyCoGPGZtNS8g7FZ4LnGY43vULtGuyEZJKWRr84/nCX7OYQnJCvHISl/bEeiFZEA/kRdAMt58VbU3Xg/mB4Z5/53rVYmBJhc53p0c0P0IWAXYugiwf28PeCqwQ/E= 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 1646395203903886.1056307721569; Fri, 4 Mar 2022 04:00:03 -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-35-kQ7bG7qNOWSapnYAiVB-fg-1; Fri, 04 Mar 2022 07:00:01 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 30CD751F4; Fri, 4 Mar 2022 11:59:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DD48C10589AC; Fri, 4 Mar 2022 11:59:55 +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 AA41C4A701; Fri, 4 Mar 2022 11:59:55 +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 224BvUMg028846 for ; Fri, 4 Mar 2022 06:57:30 -0500 Received: by smtp.corp.redhat.com (Postfix) id D02102B4D2; Fri, 4 Mar 2022 11:57:30 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.39.194.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id AA4042B450; Fri, 4 Mar 2022 11:57:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646395202; 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=u043MNg0X/ibMCczSn1TLthOQOkuTPJ0YFjxqylO6zg=; b=N6seK6dNCYDov9GN516UhvH5SGkWf/mrs6SBIaFKodEqwckAZk5if8F95lV4eL5q4zHFxb c+Jf+YqExTkM+GtOfi8vJDJTRkEP3xj2uuku+Ooj4F4h7WnLXtddJxJd2uWFaHGlazkzjQ RSUlBjwvHAd/egEmTm7x0BI8W0QAbBk= X-MC-Unique: kQ7bG7qNOWSapnYAiVB-fg-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH 1/4] softmmu: remove deprecated --enable-fips option Date: Fri, 4 Mar 2022 11:56:54 +0000 Message-Id: <20220304115657.3177925-2-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.84 on 10.5.11.22 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: 1646395206495100003 Users requiring FIPS support must build QEMU with either the libgcrypt or gnutls libraries for as the crytography backend. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- docs/about/deprecated.rst | 12 ------------ docs/about/removed-features.rst | 11 +++++++++++ include/qemu/osdep.h | 3 --- os-posix.c | 8 -------- qemu-options.hx | 10 ---------- ui/vnc.c | 7 ------- util/osdep.c | 28 ---------------------------- 7 files changed, 11 insertions(+), 68 deletions(-) diff --git a/docs/about/deprecated.rst b/docs/about/deprecated.rst index 26d00812ba..a458dd453c 100644 --- a/docs/about/deprecated.rst +++ b/docs/about/deprecated.rst @@ -67,18 +67,6 @@ and will cause a warning. The replacement for the ``nodelay`` short-form boolean option is ``nodelay= =3Don`` rather than ``delay=3Doff``. =20 -``--enable-fips`` (since 6.0) -''''''''''''''''''''''''''''' - -This option restricts usage of certain cryptographic algorithms when -the host is operating in FIPS mode. - -If FIPS compliance is required, QEMU should be built with the ``libgcrypt`` -library enabled as a cryptography provider. - -Neither the ``nettle`` library, or the built-in cryptography provider are -supported on FIPS enabled hosts. - ``-writeconfig`` (since 6.0) ''''''''''''''''''''''''''''' =20 diff --git a/docs/about/removed-features.rst b/docs/about/removed-features.= rst index cb0575fd49..6ca66f658d 100644 --- a/docs/about/removed-features.rst +++ b/docs/about/removed-features.rst @@ -336,6 +336,17 @@ for the RISC-V ``virt`` machine and ``sifive_u`` machi= ne. The ``-no-quit`` was a synonym for ``-display ...,window-close=3Doff`` whi= ch should be used instead. =20 +``--enable-fips`` (removed in 7.0) +'''''''''''''''''''''''''''''''''' + +This option restricted usage of certain cryptographic algorithms when +the host is operating in FIPS mode. + +If FIPS compliance is required, QEMU should be built with the ``libgcrypt`` +or ``gnutls`` library enabled as a cryptography provider. + +Neither the ``nettle`` library, or the built-in cryptography provider are +supported on FIPS enabled hosts. =20 QEMU Machine Protocol (QMP) commands ------------------------------------ diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 7bcce3bceb..66e70e24ff 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -534,9 +534,6 @@ static inline void qemu_timersub(const struct timeval *= val1, =20 void qemu_set_cloexec(int fd); =20 -void fips_set_state(bool requested); -bool fips_get_state(void); - /* Return a dynamically allocated pathname denoting a file or directory th= at is * appropriate for storing local state. * diff --git a/os-posix.c b/os-posix.c index ae6c9f2a5e..7cd662098e 100644 --- a/os-posix.c +++ b/os-posix.c @@ -151,14 +151,6 @@ int os_parse_cmd_args(int index, const char *optarg) case QEMU_OPTION_daemonize: daemonize =3D 1; break; -#if defined(CONFIG_LINUX) - case QEMU_OPTION_enablefips: - warn_report("-enable-fips is deprecated, please build QEMU with " - "the `libgcrypt` library as the cryptography provider " - "to enable FIPS compliance"); - fips_set_state(true); - break; -#endif default: return -1; } diff --git a/qemu-options.hx b/qemu-options.hx index 094a6c1d7c..cb0c58904b 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4655,16 +4655,6 @@ HXCOMM Internal use DEF("qtest", HAS_ARG, QEMU_OPTION_qtest, "", QEMU_ARCH_ALL) DEF("qtest-log", HAS_ARG, QEMU_OPTION_qtest_log, "", QEMU_ARCH_ALL) =20 -#ifdef __linux__ -DEF("enable-fips", 0, QEMU_OPTION_enablefips, - "-enable-fips enable FIPS 140-2 compliance\n", - QEMU_ARCH_ALL) -#endif -SRST -``-enable-fips`` - Enable FIPS 140-2 compliance mode. -ERST - DEF("msg", HAS_ARG, QEMU_OPTION_msg, "-msg [timestamp[=3Don|off]][,guest-name=3D[on|off]]\n" " control error message format\n" diff --git a/ui/vnc.c b/ui/vnc.c index 3ccd33dedc..82b28aec95 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -4051,13 +4051,6 @@ void vnc_display_open(const char *id, Error **errp) password =3D qemu_opt_get_bool(opts, "password", false); } if (password) { - if (fips_get_state()) { - error_setg(errp, - "VNC password auth disabled due to FIPS mode, " - "consider using the VeNCrypt or SASL authentication= " - "methods as an alternative"); - goto fail; - } if (!qcrypto_cipher_supports( QCRYPTO_CIPHER_ALG_DES, QCRYPTO_CIPHER_MODE_ECB)) { error_setg(errp, diff --git a/util/osdep.c b/util/osdep.c index 723cdcb004..456df9e81a 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -43,8 +43,6 @@ extern int madvise(char *, size_t, int); #include "qemu/hw-version.h" #include "monitor/monitor.h" =20 -static bool fips_enabled =3D false; - static const char *hw_version =3D QEMU_HW_VERSION; =20 int socket_set_cork(int fd, int v) @@ -526,32 +524,6 @@ const char *qemu_hw_version(void) return hw_version; } =20 -void fips_set_state(bool requested) -{ -#ifdef __linux__ - if (requested) { - FILE *fds =3D fopen("/proc/sys/crypto/fips_enabled", "r"); - if (fds !=3D NULL) { - fips_enabled =3D (fgetc(fds) =3D=3D '1'); - fclose(fds); - } - } -#else - fips_enabled =3D false; -#endif /* __linux__ */ - -#ifdef _FIPS_DEBUG - fprintf(stderr, "FIPS mode %s (requested %s)\n", - (fips_enabled ? "enabled" : "disabled"), - (requested ? "enabled" : "disabled")); -#endif -} - -bool fips_get_state(void) -{ - return fips_enabled; -} - #ifdef _WIN32 static void socket_cleanup(void) { --=20 2.34.1 From nobody Sat May 11 10:00:03 2024 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 From nobody Sat May 11 10:00:03 2024 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=1646395197; cv=none; d=zohomail.com; s=zohoarc; b=cvxpiYMqXD0kRc8fVJEY0s+e9/E6d444XALzMg1lbdMI/FtEbaGoPVgtj4zXbty8QhQ1y7QgKzg9q1lQ33iykbi9hRs0vVa4cgEcR7EObajtsN53VZM2qbuxQCLs2PHe4UUfI2lcR74qcbGKQj12TEoDF+esJTZLrUggTccRsSI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646395197; 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=jYUk71hHJIIE+9k9q3hziT3DOL5otXzLZO7z1pTCA2Y=; b=a1LMpV8HY+tKS4IDuj9OKOR4bJc7i78Q+Lx4TE+njVuhQDL7WxBVcpHDJFk9QXgwIQ1ONJvWHNRWZAKd9vJe6HslX1pDZROhSWwiEmVkWw2RG4IRQ0xp84f7m7Ut6fBfUGy7tfMOKZZ/8vPLLoakSjzJegTdhhWcJfKmZm8cXRk= 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 1646395197341520.6526602571884; Fri, 4 Mar 2022 03:59: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-395-O2fVxvCsPcS_n8PRvTQ2Yw-1; Fri, 04 Mar 2022 06:59:55 -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 194CB51E0; Fri, 4 Mar 2022 11:59:50 +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 D72517D3E2; Fri, 4 Mar 2022 11:59:49 +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 9BD121809CB5; Fri, 4 Mar 2022 11:59:49 +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 224BvkxV028875 for ; Fri, 4 Mar 2022 06:57:46 -0500 Received: by smtp.corp.redhat.com (Postfix) id 75FF02B4AC; Fri, 4 Mar 2022 11:57:46 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.39.194.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id CC0EF2B450; Fri, 4 Mar 2022 11:57:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646395196; 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=jYUk71hHJIIE+9k9q3hziT3DOL5otXzLZO7z1pTCA2Y=; b=TfxPxHudSbgi1BkaQCgBC1rvKF5BEFccE02RMYz7Urdy0vYbcjUyVjgZQyZNYar9AgKSLT XkQbbaToaI2jJRbfhpNdSFCAFgvjxrkLiYxLrRvmNTRXVX+Nmi+eQnxjIltDMzAUqQrT+e LGApqSQnxb11j+gbWBl9lgnAdLrPW/4= X-MC-Unique: O2fVxvCsPcS_n8PRvTQ2Yw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH 3/4] os-posix: refactor code handling the -chroot argument Date: Fri, 4 Mar 2022 11:56:56 +0000 Message-Id: <20220304115657.3177925-4-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.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: 1646395198230100001 Change the change_root() 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: Philippe Mathieu-Daud=C3=A9 --- os-posix.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/os-posix.c b/os-posix.c index 5a127feee2..30da1a1491 100644 --- a/os-posix.c +++ b/os-posix.c @@ -188,19 +188,16 @@ static void change_process_uid(uid_t uid, gid_t gid, = const char *name) } } =20 -static void change_root(void) +static void change_root(const char *root) { - if (chroot_dir) { - if (chroot(chroot_dir) < 0) { - error_report("chroot failed"); - exit(1); - } - if (chdir("/")) { - error_report("not able to chdir to /: %s", strerror(errno)); - exit(1); - } + if (chroot(root) < 0) { + error_report("chroot failed"); + exit(1); + } + if (chdir("/")) { + error_report("not able to chdir to /: %s", strerror(errno)); + exit(1); } - } =20 void os_daemonize(void) @@ -267,7 +264,9 @@ void os_setup_post(void) } } =20 - change_root(); + if (chroot_dir) { + change_root(chroot_dir); + } if (user_uid !=3D -1 && user_gid !=3D -1) { change_process_uid(user_uid, user_gid, user_name); } --=20 2.34.1 From nobody Sat May 11 10:00:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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.133.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=1646395210; cv=none; d=zohomail.com; s=zohoarc; b=EBBLnZ57SpgGf0NmmpCVht5bBg9kSkbUhKtsyQMee+EbjLm0e11jVrQZoJ7YmYGIX97EU2AMyOlysDWA5IDiI4k8ow+S0pFw20z6m1EdFbEhSUIJ6IQ5+EdoFfppNbYoQ9zqspA2mcq1Sivv/21HIit7iu8sBufjV3T/2bgDL38= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646395210; 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=JQrAvBXB7rXvUaR8lWoeWYrV6XVdHj/elruQAyZxCYc=; b=j8i3qdkbKEo6DXZri4f0XaV/W4jYELvxWq1ifEGnLK4uwWL4vwbn1AyRs4vY2Kzs6xD3fF0poAp1BOEV3hcCU1qnE4G1SqweAdyBXA1on0//ZOkU/4tpw6USJgl+8nmAZBdmGPMAAavjcuGISyywSOLCM4QWWEAncwQWRIr/puM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.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.133.124]) by mx.zohomail.com with SMTPS id 1646395210370412.6941843715622; Fri, 4 Mar 2022 04:00: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-638-ZpsNcfmHNQiT0WyEhcvKuQ-1; Fri, 04 Mar 2022 07:00:04 -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 B75AB80EDA9; Fri, 4 Mar 2022 11:59: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 98CA983096; Fri, 4 Mar 2022 11:59: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 4354F1807D62; Fri, 4 Mar 2022 11:59:59 +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 224BvwKI028887 for ; Fri, 4 Mar 2022 06:57:58 -0500 Received: by smtp.corp.redhat.com (Postfix) id 710282B5B8; Fri, 4 Mar 2022 11:57:58 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.39.194.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id EE71F2B450; Fri, 4 Mar 2022 11:57:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646395209; 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=JQrAvBXB7rXvUaR8lWoeWYrV6XVdHj/elruQAyZxCYc=; b=FJzQAPiSxFg2qQkZJCK8wbhV3jNRsBx7+dEuxN8/LJ8LBXIdUkNYO8T/YPUkql/kb7hh7m THMBFaXKjm/WCgsVESgkpuuxxpFkMBc7oIzt+SMrITMzxRlOrLQ25++z04JZ5yLsGuQKWW r4HLHiJ1dPi+VvsPb3KFwhSzX8VagKQ= X-MC-Unique: ZpsNcfmHNQiT0WyEhcvKuQ-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH 4/4] softmmu: move parsing of -runas, -chroot and -daemonize code Date: Fri, 4 Mar 2022 11:56:57 +0000 Message-Id: <20220304115657.3177925-5-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: 1646395212454100001 With the future intent to try to move to a fully QAPI driven configuration system, we want to have any current command parsing well isolated from logic that applies the resulting configuration. We also don't want os-posix.c to contain code that is specific to the system emulators, as this file is linked to other binaries too. To satisfy these goals, we move parsing of the -runas, -chroot and -daemonize code out of the os-posix.c helper code, and pass the parsed data into APIs in os-posix.c. As a side benefit the 'os_daemonize()' function now lives upto to its name and will always daemonize, instead of using global state to decide to be a no-op sometimes. Signed-off-by: Daniel P. Berrang=C3=A9 --- include/sysemu/os-posix.h | 4 +- include/sysemu/os-win32.h | 1 - os-posix.c | 148 +++++++++++--------------------------- os-win32.c | 9 --- softmmu/vl.c | 76 ++++++++++++++++++-- 5 files changed, 113 insertions(+), 125 deletions(-) diff --git a/include/sysemu/os-posix.h b/include/sysemu/os-posix.h index 2edf33658a..390f3f8396 100644 --- a/include/sysemu/os-posix.h +++ b/include/sysemu/os-posix.h @@ -46,7 +46,9 @@ void os_set_line_buffering(void); void os_set_proc_name(const char *s); void os_setup_signal_handling(void); void os_daemonize(void); -void os_setup_post(void); +void os_setup_post(const char *chroot_dir, + uid_t uid, gid_t gid, + const char *username); int os_mlock(void); =20 #define closesocket(s) close(s) diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h index 43f569b5c2..4879f8731d 100644 --- a/include/sysemu/os-win32.h +++ b/include/sysemu/os-win32.h @@ -61,7 +61,6 @@ struct tm *localtime_r(const time_t *timep, struct tm *re= sult); =20 static inline void os_setup_signal_handling(void) {} static inline void os_daemonize(void) {} -static inline void os_setup_post(void) {} void os_set_line_buffering(void); static inline void os_set_proc_name(const char *dummy) {} =20 diff --git a/os-posix.c b/os-posix.c index 30da1a1491..f598a52a4f 100644 --- a/os-posix.c +++ b/os-posix.c @@ -42,11 +42,6 @@ #include #endif =20 -static char *user_name; -static uid_t user_uid =3D (uid_t)-1; -static gid_t user_gid =3D (gid_t)-1; - -static const char *chroot_dir; static int daemonize; static int daemon_pipe; =20 @@ -96,69 +91,6 @@ void os_set_proc_name(const char *s) } =20 =20 -static bool os_parse_runas_uid_gid(const char *optarg, - uid_t *runas_uid, gid_t *runas_gid) -{ - unsigned long lv; - const char *ep; - uid_t got_uid; - gid_t got_gid; - int rc; - - rc =3D qemu_strtoul(optarg, &ep, 0, &lv); - got_uid =3D lv; /* overflow here is ID in C99 */ - if (rc || *ep !=3D ':' || got_uid !=3D lv || got_uid =3D=3D (uid_t)-1)= { - return false; - } - - rc =3D qemu_strtoul(ep + 1, 0, 0, &lv); - got_gid =3D lv; /* overflow here is ID in C99 */ - if (rc || got_gid !=3D lv || got_gid =3D=3D (gid_t)-1) { - return false; - } - - *runas_uid =3D got_uid; - *runas_gid =3D got_gid; - return true; -} - -/* - * Parse OS specific command line options. - * return 0 if option handled, -1 otherwise - */ -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 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); - exit(1); - } - break; - case QEMU_OPTION_chroot: - chroot_dir =3D optarg; - break; - case QEMU_OPTION_daemonize: - daemonize =3D 1; - break; - default: - return -1; - } - - return 0; -} - static void change_process_uid(uid_t uid, gid_t gid, const char *name) { if (setgid(gid) < 0) { @@ -202,54 +134,56 @@ static void change_root(const char *root) =20 void os_daemonize(void) { - if (daemonize) { - pid_t pid; - int fds[2]; + pid_t pid; + int fds[2]; =20 - if (pipe(fds) =3D=3D -1) { - exit(1); - } + if (pipe(fds) =3D=3D -1) { + exit(1); + } =20 - pid =3D fork(); - if (pid > 0) { - uint8_t status; - ssize_t len; + pid =3D fork(); + if (pid > 0) { + uint8_t status; + ssize_t len; =20 - close(fds[1]); + close(fds[1]); =20 - do { - len =3D read(fds[0], &status, 1); - } while (len < 0 && errno =3D=3D EINTR); + do { + len =3D read(fds[0], &status, 1); + } while (len < 0 && errno =3D=3D EINTR); =20 - /* only exit successfully if our child actually wrote - * a one-byte zero to our pipe, upon successful init */ - exit(len =3D=3D 1 && status =3D=3D 0 ? 0 : 1); + /* only exit successfully if our child actually wrote + * a one-byte zero to our pipe, upon successful init */ + exit(len =3D=3D 1 && status =3D=3D 0 ? 0 : 1); =20 - } else if (pid < 0) { - exit(1); - } + } else if (pid < 0) { + exit(1); + } =20 - close(fds[0]); - daemon_pipe =3D fds[1]; - qemu_set_cloexec(daemon_pipe); + close(fds[0]); + daemon_pipe =3D fds[1]; + qemu_set_cloexec(daemon_pipe); =20 - setsid(); + setsid(); =20 - pid =3D fork(); - if (pid > 0) { + pid =3D fork(); + if (pid > 0) { exit(0); - } else if (pid < 0) { - exit(1); - } - umask(027); - - signal(SIGTSTP, SIG_IGN); - signal(SIGTTOU, SIG_IGN); - signal(SIGTTIN, SIG_IGN); + } else if (pid < 0) { + exit(1); } + umask(027); + + signal(SIGTSTP, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + + daemonize =3D true; } =20 -void os_setup_post(void) +void os_setup_post(const char *root_dir, + uid_t runas_uid, gid_t runas_gid, + const char *runas_name) { int fd =3D 0; =20 @@ -264,11 +198,11 @@ void os_setup_post(void) } } =20 - if (chroot_dir) { - change_root(chroot_dir); + if (root_dir !=3D NULL) { + change_root(root_dir); } - if (user_uid !=3D -1 && user_gid !=3D -1) { - change_process_uid(user_uid, user_gid, user_name); + if (runas_uid !=3D -1 && runas_gid !=3D -1) { + change_process_uid(runas_uid, runas_gid, runas_name); } =20 if (daemonize) { diff --git a/os-win32.c b/os-win32.c index e31c921983..6f21b57841 100644 --- a/os-win32.c +++ b/os-win32.c @@ -61,12 +61,3 @@ void os_set_line_buffering(void) setbuf(stdout, NULL); setbuf(stderr, NULL); } - -/* - * Parse OS specific command line options. - * return 0 if option handled, -1 otherwise - */ -int os_parse_cmd_args(int index, const char *optarg) -{ - return -1; -} diff --git a/softmmu/vl.c b/softmmu/vl.c index 1fe028800f..cdf27b6027 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -2602,11 +2602,13 @@ static void qemu_process_help_options(void) } } =20 -static void qemu_maybe_daemonize(const char *pid_file) +static void qemu_maybe_daemonize(bool daemonize, const char *pid_file) { Error *err =3D NULL; =20 - os_daemonize(); + if (daemonize) { + os_daemonize(); + } rcu_disable_atfork(); =20 if (pid_file && !qemu_write_pidfile(pid_file, &err)) { @@ -2768,6 +2770,35 @@ void qmp_x_exit_preconfig(Error **errp) } } =20 +#ifndef WIN32 +static bool os_parse_runas_uid_gid(const char *optarg, + uid_t *runas_uid, + gid_t *runas_gid) +{ + unsigned long lv; + const char *ep; + uid_t got_uid; + gid_t got_gid; + int rc; + + rc =3D qemu_strtoul(optarg, &ep, 0, &lv); + got_uid =3D lv; /* overflow here is ID in C99 */ + if (rc || *ep !=3D ':' || got_uid !=3D lv || got_uid =3D=3D (uid_t)-1)= { + return false; + } + + rc =3D qemu_strtoul(ep + 1, 0, 0, &lv); + got_gid =3D lv; /* overflow here is ID in C99 */ + if (rc || got_gid !=3D lv || got_gid =3D=3D (gid_t)-1) { + return false; + } + + *runas_gid =3D got_gid; + *runas_uid =3D got_uid; + return true; +} +#endif /* !WIN32 */ + void qemu_init(int argc, char **argv, char **envp) { QemuOpts *opts; @@ -2778,6 +2809,14 @@ void qemu_init(int argc, char **argv, char **envp) MachineClass *machine_class; bool userconfig =3D true; FILE *vmstate_dump_file =3D NULL; + bool daemonize =3D false; +#ifndef WIN32 + struct passwd *pwd; + uid_t runas_uid =3D -1; + gid_t runas_gid =3D -1; + g_autofree char *runas_name =3D NULL; + const char *chroot_dir =3D NULL; +#endif /* !WIN32 */ =20 qemu_add_opts(&qemu_drive_opts); qemu_add_drive_opts(&qemu_legacy_drive_opts); @@ -3659,11 +3698,32 @@ void qemu_init(int argc, char **argv, char **envp) case QEMU_OPTION_nouserconfig: /* Nothing to be parsed here. Especially, do not error out= below. */ break; - default: - if (os_parse_cmd_args(popt->index, optarg)) { - error_report("Option not supported in this build"); +#ifndef WIN32 + case QEMU_OPTION_runas: + pwd =3D getpwnam(optarg); + if (pwd) { + runas_uid =3D pwd->pw_uid; + runas_gid =3D pwd->pw_gid; + runas_name =3D g_strdup(pwd->pw_name); + } else if (!os_parse_runas_uid_gid(optarg, + &runas_uid, + &runas_gid)) { + error_report("User \"%s\" doesn't exist" + " (and is not :)", + optarg); exit(1); } + break; + case QEMU_OPTION_chroot: + chroot_dir =3D optarg; + break; + case QEMU_OPTION_daemonize: + daemonize =3D 1; + break; +#endif /* !WIN32 */ + default: + error_report("Option not supported in this build"); + exit(1); } } } @@ -3683,7 +3743,7 @@ void qemu_init(int argc, char **argv, char **envp) qemu_process_early_options(); =20 qemu_process_help_options(); - qemu_maybe_daemonize(pid_file); + qemu_maybe_daemonize(daemonize, pid_file); =20 /* * The trace backend must be initialized after daemonizing. @@ -3778,6 +3838,8 @@ void qemu_init(int argc, char **argv, char **envp) } qemu_init_displays(); accel_setup_post(current_machine); - os_setup_post(); +#ifndef WIN32 + os_setup_post(chroot_dir, runas_uid, runas_gid, runas_name); +#endif /* !WIN32 */ resume_mux_open(); } --=20 2.34.1