From patchwork Thu Nov 3 22:02:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Stipakov X-Patchwork-Id: 2836 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director13.mail.ord1d.rsapps.net ([172.27.255.54]) by backend30.mail.ord1d.rsapps.net with LMTP id 6HRtA5zVZGNdXgAAIUCqbw (envelope-from ) for ; Fri, 04 Nov 2022 05:04:28 -0400 Received: from proxy20.mail.iad3a.rsapps.net ([172.27.255.54]) by director13.mail.ord1d.rsapps.net with LMTP id 0H9XA5zVZGPSXgAA91zNiA (envelope-from ) for ; Fri, 04 Nov 2022 05:04:28 -0400 Received: from smtp38.gate.iad3a ([172.27.255.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy20.mail.iad3a.rsapps.net with LMTPS id KCX9N5vVZGMRIAAAtfLT2w (envelope-from ) for ; Fri, 04 Nov 2022 05:04:27 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp38.gate.iad3a.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Suspicious-Flag: YES X-Classification-ID: aefc36ea-5c1f-11ed-bfa9-525400000c92-1-1 Received: from [216.105.38.7] ([216.105.38.7:41842] helo=lists.sourceforge.net) by smtp38.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id AE/00-25769-B95D4636; Fri, 04 Nov 2022 05:04:27 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1oqsc9-0001Jr-2n; Fri, 04 Nov 2022 09:03:49 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1oqsc1-0001Jf-L0 for openvpn-devel@lists.sourceforge.net; Fri, 04 Nov 2022 09:03:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=7SMXDNbJKLdgAJZMYOny0iRAYsATAEBkq4IC01+IznI=; b=TTQahH7nieUI548CEj138LisXl RQyrbDHHoFjIrEXMPNBOpZuY1We5YJDBbPasMqSWaugYLc6q92iq3FwZ0s/xgz92PDh0HZsAkI2fx tA1ljLbmV+JYjdxTC4vLrWkcIoEUwwIDihvtjGlL7n0kwZfsi7rFo4Dc2bx775pI4dQ0=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=7SMXDNbJKLdgAJZMYOny0iRAYsATAEBkq4IC01+IznI=; b=b EkSp6GVMZn8Mq/zk1QnbfBgazHxbfz+V+yJ++/jwjWF6FIVnf0aQLzgDwXAdqlv9fcKbGvZVk37/E g4E16jjR9my2lGZnG2uWtPpmIzNaXmIGWXLSRzMxU1ohUCbzDthtSfYIMUYE67E1zfXFfyN7RCQ6S wCrHn7LbJMIrrSVU=; Received: from mail-lf1-f50.google.com ([209.85.167.50]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1oqsbx-0003WN-RU for openvpn-devel@lists.sourceforge.net; Fri, 04 Nov 2022 09:03:38 +0000 Received: by mail-lf1-f50.google.com with SMTP id j16so6530458lfe.12 for ; Fri, 04 Nov 2022 02:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7SMXDNbJKLdgAJZMYOny0iRAYsATAEBkq4IC01+IznI=; b=EcaXH4K48mT46zg7rYF/BO+R9nMjCZXXLzkXSAiapu5VI3DXZoEmnDDdxZoaNNzGgz oXlKWRwvwWPPX/N/xB/Qh9kwrTi/3gbf65KkWb+WdS1oKmaaZM3UmtFveDLxnsilEr+x X2I53vS/KoFfbxUF/aqq0uJglJ10OT2Wh1ebnVEnhB8IWJXGFcT+Y7EVHZdp4nFW14cO N0VivvMjKu5R9bspRW7yUJVMsxyjtrY7SvZOGQj6qosKYl1MDHbRJ9LPwo8ocZzSM/ud wCCEYtRYAV4r0GuYl1vd1zXQ4AA3Jcqn34/mSoljIFPm1j3XDDzu5Ah5iJGiruWDpGyC o2QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7SMXDNbJKLdgAJZMYOny0iRAYsATAEBkq4IC01+IznI=; b=mdFFGHYggeys92eZt/jHmgQK22h+kbN9N+PNfgqZQeQtriniO0i69GMEneQZuPBCwG giPgDdXUZMdqUyplNwR4ulqMn06NiL7vIevoR1mQh/88oMdh+C716kbAZwYVsHk++BMB HvSzyxWLxSvJmnbRwDTOADzabqrBQsc+W/AVEXYh8qXA2ayCWPJETRrxTIbXiv7Oy7zm ZOsuTOo1rfW9XQURyNWlYzOlKUA9r4YjEBaOATUxMJnsxnMumKBH+A9sHTuNGdDx6A6d lMpjROxAa56jFun81xeWpGxIohiKIRhIxl7GN6Y+iMSjlB00HzGz22W5mBaWEORrxmtW x1JQ== X-Gm-Message-State: ACrzQf0zX0SN+82lopQIL0BJubsq8U7ro6FyRTIRTGwygsDLaAEfQ664 TmwAE00/aUVQ5WFSkHtZv0xoTWQSu/MzhQ== X-Google-Smtp-Source: AMsMyM6XW+SLjA7QK7YUyr0xH8CvU+kNz707Mtbzho4COhJkE3F2tCPMZBH8il9Rkc3swChsXggoVQ== X-Received: by 2002:ac2:5183:0:b0:4a1:e99d:e7b6 with SMTP id u3-20020ac25183000000b004a1e99de7b6mr13955945lfi.676.1667552610906; Fri, 04 Nov 2022 02:03:30 -0700 (PDT) Received: from localhost.localdomain ([2a00:1d50:3:0:f990:4c5e:2d6:7eeb]) by smtp.gmail.com with ESMTPSA id b16-20020a2eb910000000b0027760138c53sm359993ljb.72.2022.11.04.02.03.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 02:03:30 -0700 (PDT) From: Lev Stipakov To: openvpn-devel@lists.sourceforge.net Date: Fri, 4 Nov 2022 11:02:47 +0200 Message-Id: <20221104090247.167-1-lstipakov@gmail.com> X-Mailer: git-send-email 2.38.1.windows.1 MIME-Version: 1.0 X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Lev Stipakov Properly detect process architecture and machine architecture, including arm64. Print process architecture and, if machine architecture is different (we are running in emulation), print that too. Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.167.50 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [lstipakov[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.167.50 listed in wl.mailspike.net] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1oqsbx-0003WN-RU Subject: [Openvpn-devel] [PATCH] win32: detect arm64 architecture and emulations X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Lev Stipakov Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Lev Stipakov Properly detect process architecture and machine architecture, including arm64. Print process architecture and, if machine architecture is different (we are running in emulation), print that too. Signed-off-by: Lev Stipakov Acked-by: Gert Doering --- src/openvpn/win32.c | 123 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 112 insertions(+), 11 deletions(-) diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index 82f0c73f..cfe4dbde 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -1345,18 +1345,106 @@ win32_version_info(void) return WIN_10; } -bool -win32_is_64bit(void) +typedef enum { + ARCH_X86, + ARCH_AMD64, + ARCH_ARM64, + ARCH_NATIVE, /* means no emulation, makes sense for host arch */ + ARCH_UNKNOWN +} arch_t; + +static void +win32_get_arch(arch_t *process_arch, arch_t *host_arch) { -#if defined(_WIN64) - return true; /* 64-bit programs run only on Win64 */ + *process_arch = ARCH_UNKNOWN; + *host_arch = ARCH_NATIVE; + + typedef BOOL (__stdcall *is_wow64_process2_t)(HANDLE, USHORT *, USHORT *); + is_wow64_process2_t is_wow64_process2 = (is_wow64_process2_t) + GetProcAddress(GetModuleHandle("Kernel32.dll"), "IsWow64Process2"); + + USHORT process_machine = 0; + USHORT native_machine = 0; + BOOL is_wow64 = FALSE; + +#ifdef _ARM64_ + *process_arch = ARCH_ARM64; +#elif defined(_WIN64) + *process_arch = ARCH_AMD64; + if (is_wow64_process2) + { + /* this could be amd64 on arm64 */ + BOOL is_wow64 = is_wow64_process2(GetCurrentProcess(), + &process_machine, &native_machine); + if (is_wow64 && native_machine == IMAGE_FILE_MACHINE_ARM64) + { + *host_arch = ARCH_ARM64; + } + } #elif defined(_WIN32) - /* 32-bit programs run on both 32-bit and 64-bit Windows */ - BOOL f64 = FALSE; - return IsWow64Process(GetCurrentProcess(), &f64) && f64; -#else /* if defined(_WIN64) */ - return false; /* Win64 does not support Win16 */ -#endif + *process_arch = ARCH_X86; + + if (is_wow64_process2) + { + /* check if we're running on arm64 or amd64 machine */ + is_wow64 = is_wow64_process2(GetCurrentProcess(), + &process_machine, &native_machine); + if (is_wow64) + { + switch (native_machine) + { + case IMAGE_FILE_MACHINE_ARM64: + *host_arch = ARCH_ARM64; + break; + + case IMAGE_FILE_MACHINE_AMD64: + *host_arch = ARCH_AMD64; + break; + + default: + *host_arch = ARCH_UNKNOWN; + break; + } + } + } + else + { + BOOL w64 = FALSE; + is_wow64 = IsWow64Process(GetCurrentProcess(), &w64) && w64; + if (is_wow64) + { + /* we are unable to differentiate between arm64 and amd64 + * machines here, so assume we are running on amd64 */ + *host_arch = ARCH_AMD64; + } + } +#endif /* _ARM64_ */ +} + +static void +win32_print_arch(arch_t arch, struct buffer *out) +{ + switch (arch) + { + case ARCH_X86: + buf_printf(out, "x86"); + break; + + case ARCH_AMD64: + buf_printf(out, "amd64"); + break; + + case ARCH_ARM64: + buf_printf(out, "arm64"); + break; + + case ARCH_UNKNOWN: + buf_printf(out, "(unknown)"); + break; + + default: + break; + } } const char * @@ -1397,7 +1485,20 @@ win32_version_string(struct gc_arena *gc, bool add_name) break; } - buf_printf(&out, win32_is_64bit() ? " 64bit" : " 32bit"); + buf_printf(&out, ", "); + + arch_t process_arch, host_arch; + win32_get_arch(&process_arch, &host_arch); + win32_print_arch(process_arch, &out); + + buf_printf(&out, " executable"); + + if (host_arch != ARCH_NATIVE) + { + buf_printf(&out, " running on "); + win32_print_arch(host_arch, &out); + buf_printf(&out, " host"); + } return (const char *)out.data; }