[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 07/51] os-posix.c: create and export os_set_runas()
From: |
Paolo Bonzini |
Subject: |
[PULL 07/51] os-posix.c: create and export os_set_runas() |
Date: |
Thu, 7 Sep 2023 14:59:16 +0200 |
From: Michael Tokarev <mjt@tls.msk.ru>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20230901101302.3618955-3-mjt@tls.msk.ru>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/sysemu/os-posix.h | 1 +
os-posix.c | 23 ++++++++++++++++-------
2 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/include/sysemu/os-posix.h b/include/sysemu/os-posix.h
index 65b9c94e919..d32630f9e76 100644
--- a/include/sysemu/os-posix.h
+++ b/include/sysemu/os-posix.h
@@ -50,6 +50,7 @@ void os_setup_signal_handling(void);
int os_set_daemonize(bool d);
bool is_daemonized(void);
void os_daemonize(void);
+bool os_set_runas(const char *optarg);
void os_setup_post(void);
int os_mlock(void);
diff --git a/os-posix.c b/os-posix.c
index cfcb96533c1..f0ee5c8b002 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -102,8 +102,14 @@ void os_set_proc_name(const char *s)
#endif
}
-
-static bool os_parse_runas_uid_gid(const char *optarg)
+/*
+ * Prepare to change user ID. optarg can be one of 3 forms:
+ * - a username, in which case user ID will be changed to its uid,
+ * with primary and supplementary groups set up too;
+ * - a numeric uid, in which case only the uid will be set;
+ * - a pair of numeric uid:gid.
+ */
+bool os_set_runas(const char *optarg)
{
unsigned long lv;
const char *ep;
@@ -111,6 +117,13 @@ static bool os_parse_runas_uid_gid(const char *optarg)
gid_t got_gid;
int rc;
+ user_pwd = getpwnam(optarg);
+ if (user_pwd) {
+ user_uid = -1;
+ user_gid = -1;
+ return true;
+ }
+
rc = qemu_strtoul(optarg, &ep, 0, &lv);
got_uid = lv; /* overflow here is ID in C99 */
if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
@@ -137,11 +150,7 @@ int os_parse_cmd_args(int index, const char *optarg)
{
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)) {
+ if (!os_set_runas(optarg)) {
error_report("User \"%s\" doesn't exist"
" (and is not <uid>:<gid>)",
optarg);
--
2.41.0
- [PULL 01/51] linux-user, bsd-user: disable on unsupported host architectures, (continued)
- [PULL 01/51] linux-user, bsd-user: disable on unsupported host architectures, Paolo Bonzini, 2023/09/07
- [PULL 03/51] target/i386: generalize operand size "ph" for use in CVTPS2PD, Paolo Bonzini, 2023/09/07
- [PULL 02/51] target/i386: raise FERR interrupt with iothread locked, Paolo Bonzini, 2023/09/07
- [PULL 05/51] target/i386: Add support for AMX-COMPLEX in CPUID enumeration, Paolo Bonzini, 2023/09/07
- [PULL 04/51] target/i386: fix memory operand size for CVTPS2PD, Paolo Bonzini, 2023/09/07
- [PULL 06/51] include/sysemu/os-posix.h: move *daemonize* declarations together, Paolo Bonzini, 2023/09/07
- [PULL 08/51] os-posix.c: create and export os_set_chroot(), Paolo Bonzini, 2023/09/07
- [PULL 10/51] os-posix.c: move code around, Paolo Bonzini, 2023/09/07
- [PULL 09/51] os-posix.c, softmmu/vl.c: move os_parse_cmd_args() into qemu_init(), Paolo Bonzini, 2023/09/07
- [PULL 14/51] contrib/plugins: remove -soname argument, Paolo Bonzini, 2023/09/07
- [PULL 07/51] os-posix.c: create and export os_set_runas(),
Paolo Bonzini <=
- [PULL 12/51] softmmu/vl.c: inline include/qemu/qemu-options.h into vl.c, Paolo Bonzini, 2023/09/07
- [PULL 15/51] contrib/plugins/cache: Fix string format, Paolo Bonzini, 2023/09/07
- [PULL 17/51] contrib/plugins/howvec: Fix string format, Paolo Bonzini, 2023/09/07
- [PULL 16/51] contrib/plugins/drcov: Fix string format, Paolo Bonzini, 2023/09/07
- [PULL 21/51] meson: update unsupported host/CPU messages, Paolo Bonzini, 2023/09/07
- [PULL 25/51] configure: remove boolean variables for targets, Paolo Bonzini, 2023/09/07
- [PULL 11/51] os-posix.c: remove unneeded #includes, Paolo Bonzini, 2023/09/07
- [PULL 20/51] meson: do not unnecessarily use cmake for dependencies, Paolo Bonzini, 2023/09/07
- [PULL 24/51] meson: compile bundled device trees, Paolo Bonzini, 2023/09/07