From nobody Tue Feb 10 13:16:22 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1694092015; cv=none; d=zohomail.com; s=zohoarc; b=Qa0h4CO+yvtYq0aghmWrcewpVnvEsa5ZZGXAReRs9UNwAaN0PPatL969J66S2B1fVm1wUqVhrhbFD27UKDfsAbYiO8eAdyDXjeyzP8mrNAfafdNmITGqrnsUOjVIh4qJBDoSlW/5iN04cuMS/wZiIyT5iIbSyhkwu7GyQrVe04c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694092015; 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=apj0wqzDG1HTN7fF4efJ4aV5GoQliv8/RoC2dU7fXXI=; b=L0xcWzhyoNSGqo12GO+gDDT8MTuIx8pDS4RFeFe7reGf20qaKRLPOPQaw9BKbJO/GpB4GcmdZCW+3jLGLaPGnB/lXomuEIOi0G68aVnT7ZT6M0YXxHdBpQhCJdZYgQ1VApnaTEoz8orRpd7opQixf0qOrny+kSci1pD/8N/GbmY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 169409201516180.30965668052863; Thu, 7 Sep 2023 06:06:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qeEdC-0002tP-Js; Thu, 07 Sep 2023 09:01:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qeEcp-0002mA-9m for qemu-devel@nongnu.org; Thu, 07 Sep 2023 09:00:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qeEcb-00079e-4m for qemu-devel@nongnu.org; Thu, 07 Sep 2023 09:00:47 -0400 Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-556-pNio4c1VPPWu2kAHRChmjw-1; Thu, 07 Sep 2023 09:00:27 -0400 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-31f3233861aso639845f8f.2 for ; Thu, 07 Sep 2023 06:00:24 -0700 (PDT) Received: from [192.168.1.174] ([151.48.237.81]) by smtp.gmail.com with ESMTPSA id f12-20020adffccc000000b003143c9beeaesm23194222wrs.44.2023.09.07.06.00.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Sep 2023 06:00:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694091632; h=from:from: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; bh=apj0wqzDG1HTN7fF4efJ4aV5GoQliv8/RoC2dU7fXXI=; b=A1dmLpnb6O8+1n3xcQLxLe838pF+h6g8zu/5QY8ZEfu+DnO7uvNM3yGWT/fQ/F2wj2YwO6 p0STjanHj6Lj6P0DMbQtBUZnzrcBsLZ085k3rQJYsmZi1H81FVmjigF+PvS6+z+fWMJCdG Wrc1tRmb7sVMiXjnnVjeQUaVH7p0Hqc= X-MC-Unique: pNio4c1VPPWu2kAHRChmjw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1694091623; x=1694696423; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=apj0wqzDG1HTN7fF4efJ4aV5GoQliv8/RoC2dU7fXXI=; b=Bgt2zoDztHEYZhVb1ThFfD/5ViNsrsexZxv8lMcSFgQf4d6+Zyu/fkMgGfcnprG2Ky D8SMYbQppyY3b4g0kRQAV5mcwtnd75E+7lkuIjbCGDYFe3qFUOJ+IxUkqZruY/bgBPkP LGb8vIBTEmChtjEs07Hyih6tnbHUNM8rYU/kvYxHcvB8b59TPme8mWSCWEvXeeYoTsoY 1BFxiA58yS6MqTKuHgm6lHe/xDRr39Rz9YbxJCVOxS2hVvcZZptZfhxaptJ460jzn4Xc FdblOslOgNsP87bNKds9xf9e4nuNIUhLreeU/CES0q11yYS5XV6Q3krrc0RL5enfrcan IOsg== X-Gm-Message-State: AOJu0YxVYIKerwkAvHKyyyY2ruazXl3ozDwEAcgwTIuVVKjjjUAvJjH6 u0D22J4OAOeQMC0YMsfqPdA6Aji1H387rY6S+2YXY1GzrwYj6v0fQGDig//gWbOBzzuqVH86Dsb ACPssC7KFqG6//mHJKqXq8BeXChZOeNGjD97WAEO6UnB0BowqvelYdLmHd4z4rc9UYArPZJed3w Y= X-Received: by 2002:a05:6000:901:b0:317:67bf:3376 with SMTP id cw1-20020a056000090100b0031767bf3376mr4901169wrb.57.1694091623584; Thu, 07 Sep 2023 06:00:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHsb3vkCbR5ju2oPyvM0m3aM74jGqGiBL0Yl9WyfZsawGMVIPWy+fDbibu03Jzmli/yWJ4KNg== X-Received: by 2002:a05:6000:901:b0:317:67bf:3376 with SMTP id cw1-20020a056000090100b0031767bf3376mr4901147wrb.57.1694091623281; Thu, 07 Sep 2023 06:00:23 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Michael Tokarev , Eric Blake , Richard Henderson Subject: [PULL 07/51] os-posix.c: create and export os_set_runas() Date: Thu, 7 Sep 2023 14:59:16 +0200 Message-ID: <20230907130004.500601-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230907130004.500601-1-pbonzini@redhat.com> References: <20230907130004.500601-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1694092016957100003 Content-Type: text/plain; charset="utf-8" From: Michael Tokarev Signed-off-by: Michael Tokarev Reviewed-by: Eric Blake Reviewed-by: Richard Henderson Message-ID: <20230901101302.3618955-3-mjt@tls.msk.ru> Signed-off-by: Paolo Bonzini --- 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); =20 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 } =20 - -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; =20 + user_pwd =3D getpwnam(optarg); + if (user_pwd) { + user_uid =3D -1; + user_gid =3D -1; + return true; + } + 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)= { @@ -137,11 +150,7 @@ int os_parse_cmd_args(int index, const char *optarg) { 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)) { + if (!os_set_runas(optarg)) { error_report("User \"%s\" doesn't exist" " (and is not :)", optarg); --=20 2.41.0