softmmu: move and refactor -runas, -chroot and -daemonize
cover softmmu: move and refactor -runas, -chroot and -daemonize
This small series was motivated by my thoughts on the proposals in
https://lists.gnu.org/archive/html/qemu-devel/2022-03/msg01135.html
It demostrates the approach I mention there, and has the further benefit of untangling and isolating the implementation of UID changing, chrooting and daemonized, from the parsing of the corresponding command line options.
Daniel P. Berrangé (4):
softmmu: remove deprecated --enable-fips option
os-posix: refactor code handling the -runas argument
os-posix: refactor code handling the -chroot argument
softmmu: move parsing of -runas, -chroot and -daemonize code
docs/about/deprecated.rst | 12 -- docs/about/removed-features.rst | 11 ++ include/qemu/osdep.h | 3 - include/sysemu/os-posix.h | 4 +- include/sysemu/os-win32.h | 1 - os-posix.c | 222 ++++++++++---------------------- os-win32.c | 9 -- qemu-options.hx | 10 -- softmmu/vl.c | 76 ++++++++++- ui/vnc.c | 7 - util/osdep.c | 28 ---- 11 files changed, 154 insertions(+), 229 deletions(-)
Signature · 1 line
2.34.1
PATCH 2/4 os-posix: refactor code handling the -runas argument
Change the change_process_uid() function so that it takes its input as parameters instead of relying on static global variables.
--- os-posix.c | 83 +++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 44 deletions(-)
os-posix.c +39 -44
@@ -42,13 +42,9 @@#include <sys/prctl.h> #endif -/* -* 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 = (uid_t)-1 ;/* -1 -1 >=0 */ -static gid_t user_gid = (gid_t)-1 ;/* -1 -1 >=0 */ +static char *user_name; +static uid_t user_uid = (uid_t)-1 ; +static gid_t user_gid = (gid_t)-1 ;static const char *chroot_dir;static int daemonize;
@@ -100,7 +96,8 @@ void os_set_proc_name(const char *s) } -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; } - user_pwd =NULL ; - user_uid = got_uid; - user_gid = got_gid; + *runas_uid = got_uid; + *runas_gid = got_gid;return true; }
@@ -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 =getpwnam (optarg);if (user_pwd) { - user_uid =-1 ; - user_gid =-1 ; - }else if (!os_parse_runas_uid_gid (optarg)) { + user_uid = user_pwd->pw_uid; + user_gid = user_pwd->pw_gid; + user_name =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 <uid>:<gid>)" , optarg);
@@ -158,41 +159,33 @@ int os_parse_cmd_args(int index, const char *optarg)return 0 ; } -static void change_process_uid (void ) +static void change_process_uid (uid_t uid,gid_t gid,const char *name) { -assert ((user_uid == (uid_t)-1 ) || user_pwd ==NULL ); -assert ((user_uid == (uid_t)-1 ) == - (user_gid == (gid_t)-1 )); - -if (user_pwd || user_uid != (uid_t)-1 ) { -gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid; -uid_t intended_uid = 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 ) !=-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 ) !=-1 ) { +error_report ("Dropping privileges failed" ); +exit (1 ); + } }static void change_root (void )
@@ -275,7 +268,9 @@ void os_setup_post(void) }change_root (); -change_process_uid (); +if (user_uid !=-1 && user_gid !=-1 ) { +change_process_uid (user_uid, user_gid, user_name); + }if (daemonize) {uint8_t status =0 ;
Signature · 1 line
2.34.1
PATCH 4/4 softmmu: move parsing of -runas, -chroot and -daemonize code
In reply to Daniel P. Berrangé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.
--- 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(-)
include/sysemu/os-posix.h +3 -1
@@ -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 );#define closesocket (s) close(s)
include/sysemu/os-win32.h +0 -1
@@ -61,7 +61,6 @@ struct tm *localtime_r(const time_t *timep, struct tm *result);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) {}
os-posix.c +41 -107
186 lines not on this page · raw message
os-win32.c +0 -9
@@ -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 ; -}
softmmu/vl.c +69 -7
116 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 1/4 softmmu: remove deprecated --enable-fips option
In reply to Daniel P. BerrangéUsers requiring FIPS support must build QEMU with either the libgcrypt or gnutls libraries for as the crytography backend.
--- 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(-)
docs/about/deprecated.rst +0 -12
@@ -67,18 +67,6 @@ and will cause a warning. The replacement for the ``nodelay`` short-form boolean option is ``nodelay=on`` rather than ``delay=off``. -``--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) '''''''''''''''''''''''''''''
docs/about/removed-features.rst +11 -0
@@ -336,6 +336,17 @@ for the RISC-V ``virt`` machine and ``sifive_u`` machine. The ``-no-quit`` was a synonym for ``-display ...,window-close=off`` which should be used instead. +``--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. QEMU Machine Protocol (QMP) commands ------------------------------------
include/qemu/osdep.h +0 -3
@@ -534,9 +534,6 @@ static inline void qemu_timersub(const struct timeval *val1,void qemu_set_cloexec (int fd); -void fips_set_state (bool requested); -bool fips_get_state (void ); -/* Return a dynamically allocated pathname denoting a file or directory that is * appropriate for storing local state. *
os-posix.c +0 -8
@@ -151,14 +151,6 @@ int os_parse_cmd_args(int index, const char *optarg)case QEMU_OPTION_daemonize: daemonize =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 ; }
qemu-options.hx +0 -10
@@ -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) -#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[=on|off]][,guest-name=[on|off]]\n" " control error message format\n"
ui/vnc.c +0 -7
@@ -4051,13 +4051,6 @@ void vnc_display_open(const char *id, Error **errp) password =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,
util/osdep.c +0 -28
@@ -43,8 +43,6 @@ extern int madvise(char *, size_t, int);#include "qemu/hw-version.h" #include "monitor/monitor.h" -static bool fips_enabled = false; -static const char *hw_version =QEMU_HW_VERSION ;int socket_set_cork (int fd,int v)
@@ -526,32 +524,6 @@ const char *qemu_hw_version(void)return hw_version; } -void fips_set_state (bool requested) -{ -#ifdef __linux__ -if (requested) { -FILE *fds =fopen ("/proc/sys/crypto/fips_enabled" ,"r" ); -if (fds !=NULL ) { - fips_enabled = (fgetc (fds) =='1' ); -fclose (fds); - } - } -#else - fips_enabled = 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 _WIN32static void socket_cleanup (void ) {
Signature · 1 line
2.34.1
PATCH 3/4 os-posix: refactor code handling the -chroot argument
In reply to Daniel P. BerrangéChange the change_root() function so that it takes its input as parameters instead of relying on static global variables.
--- os-posix.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
os-posix.c +11 -12
@@ -188,19 +188,16 @@ static void change_process_uid(uid_t uid, gid_t gid, const char *name) } } -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 ); } - }void os_daemonize (void )
@@ -267,7 +264,9 @@ void os_setup_post(void) } } -change_root (); +if (chroot_dir) { +change_root (chroot_dir); + }if (user_uid !=-1 && user_gid !=-1 ) {change_process_uid (user_uid, user_gid, user_name); }
Signature · 1 line
2.34.1
Re: [PATCH 1/4] softmmu: remove deprecated --enable-fips option
In reply to Daniel P. BerrangéQuoted 4 lines · Daniel P. Berrangé
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é <[email protected]>
Quoted 9 lines
--- 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(-)
Re: [PATCH 3/4] os-posix: refactor code handling the -chroot argument
In reply to Daniel P. BerrangéQuoted 7 lines · Daniel P. Berrangé
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é <[email protected]>--- os-posix.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
Re: [PATCH 4/4] softmmu: move parsing of -runas, -chroot and -daemonize code
In reply to Daniel P. BerrangéQuoted 50 lines · Daniel P. Berrangé
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.cto 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.chelper code, and pass the parsed data into APIs inos-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é <[email protected]>--- 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(-)softmmu/vl.c +4
-2@@ -2602,11 +2602,13 @@ static void qemu_process_help_options(void) } } -static void qemu_maybe_daemonize (const char *pid_file) +static void qemu_maybe_daemonize (bool daemonize,const char *pid_file) {Error *err =NULL ; -os_daemonize (); +if (daemonize) { +os_daemonize (); + }rcu_disable_atfork ();if (pid_file && !qemu_write_pidfile (pid_file, &err)) {
softmmu/vl.c void qemu_init(int argc, char **argv, char **envp) · line 3746
Quoted 5 lines
softmmu/vl.c +1
-1@@ -3683,7 +3743,7 @@ void qemu_init(int argc, char **argv, char **envp)qemu_process_early_options ();qemu_process_help_options (); -qemu_maybe_daemonize (pid_file); +qemu_maybe_daemonize (daemonize, pid_file);
This commit is a bit flawed, because we're until we call the os_daemonize() method, the is_daemonized() method won't return true. Unfortunately some callers rely is_daemonized() returning true merely for the request, even though we've not yet put it into action. ie the method would have been better called is_daemonize_requested()
The upshot is that we fail to properly close stderr.
I'll send a v2 that handles this by fully removing the is_daemonize() method.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 4/4] softmmu: move parsing of -runas, -chroot and -daemonize code
Quoted 10 lines · Daniel P. Berrangé
Quoted 9 lines · Daniel P. Berrangé
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.cto contain code that is specific to the system emulators, as this file is linked to other binaries too.
Quoted 5 lines
Quoted 5 lines
To satisfy these goals, we move parsing of the -runas, -chroot and -daemonize code out of the
os-posix.chelper code, and pass the parsed data into APIs inos-posix.c.As a side benefit the '
os_daemonize()' function now lives upto to
up to
Quoted 2 lines
Quoted 2 lines
its name and will always daemonize, instead of using global state to decide to be a no-op sometimes.
Yay.
Quoted 13 lines
softmmu/vl.c void qemu_init(int argc, char **argv, char **envp) · line 3746
Quoted 6 lines
softmmu/vl.c +1
-1@@ -3683,7 +3743,7 @@ void qemu_init(int argc, char **argv, char **envp)qemu_process_early_options ();qemu_process_help_options (); -qemu_maybe_daemonize (pid_file); +qemu_maybe_daemonize (daemonize, pid_file);This commit is a bit flawed, because we're until we call the
os_daemonize()method, theis_daemonized()method won't return true. Unfortunately some callers relyis_daemonized()returning true merely for the request, even though we've not yet put it into action. ie the method would have been better calledis_daemonize_requested()
Eww, indeed.
Quoted 5 lines
The upshot is that we fail to properly close stderr.
I'll send a v2 that handles this by fully removing the
is_daemonize()method.
Looking forward to it.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH 2/4] os-posix: refactor code handling the -runas argument
In reply to Daniel P. BerrangéQuoted 8 lines · Daniel P. Berrangé
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é <[email protected]>--- os-posix.c | 83 +++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 44 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH 1/4] softmmu: remove deprecated --enable-fips option
In reply to Daniel P. BerrangéQuoted 2 lines · Daniel P. Berrangé
Users requiring FIPS support must build QEMU with either the libgcrypt or gnutls libraries for as the crytography backend.
s/for //
Quoted 11 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- 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(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org