[Openvpn-devel,master+release/2.5] error.c: use correct API to get error description on Windows
| Message ID | 20220218124149.149-1-lstipakov@gmail.com |
|---|---|
| State | Superseded |
| Headers |
Return-Path: <openvpn-devel-bounces@lists.sourceforge.net> Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.27.255.52]) by backend41.mail.ord1d.rsapps.net with LMTP id GKCjA3SUD2IzRgAAqwncew (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) for <patchwork@openvpn.net>; Fri, 18 Feb 2022 07:43:32 -0500 Received: from proxy18.mail.iad3a.rsapps.net ([172.27.255.52]) by director9.mail.ord1d.rsapps.net with LMTP id qP/fDnSUD2JeDgAAalYnBA (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) for <patchwork@openvpn.net>; Fri, 18 Feb 2022 07:43:32 -0500 Received: from smtp6.gate.iad3a ([172.27.255.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy18.mail.iad3a.rsapps.net with LMTPS id uMEFCHSUD2LUfwAAon3hFg (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) for <patchwork@openvpn.net>; Fri, 18 Feb 2022 07:43:32 -0500 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: smtp6.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: 60803fda-90b8-11ec-a297-5254002f0085-1-1 Received: from [216.105.38.7] ([216.105.38.7:59116] helo=lists.sourceforge.net) by smtp6.gate.iad3a.rsapps.net (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 8E/32-07529-3749F026; Fri, 18 Feb 2022 07:43:31 -0500 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.94.2) (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) id 1nL2au-0002HT-Dw; Fri, 18 Feb 2022 12:42:39 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from <lstipakov@gmail.com>) id 1nL2at-0002HG-Ck for openvpn-devel@lists.sourceforge.net; Fri, 18 Feb 2022 12:42:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: MIME-Version:Content-Type:Content-Transfer-Encoding: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=hnskYEUtL6WJGm0TVLZDe9LwMnlOUb3K9/Icp3465L8=; b=Ty6akGK8vNaC6OzIoYfAClqsSL U/Wi7Y5RwtWQbuBLo1IKoNZovDdVdPwW1anaBaybw5X76SYDkUCeGmGMGMW9Z+7aBocKtPmxx6gPX gPyq2gOKGauEL6ISwBkvRoCfblBFDFICNjrQYIkSZpKJscLbLWCEcKd72yNkyvLQmGmw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version: Content-Type:Content-Transfer-Encoding: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=hnskYEUtL6WJGm0TVLZDe9LwMnlOUb3K9/Icp3465L8=; b=IBFVOPOxSVtS4WzeOPrUNIiC+o LKnQzIdklQUnLm/9Ea/SS9UKyqk0VRC4HkI3/RnzFVyR9xDCuCiXGkrPHqhDXsbJxjRwYhC5rJ1li uMSPxGO3zlzfi91D4v7vmHWL0tJto/AtZsJxdIc5o+BT+NysLtyvsO2DGBAH7Gak5g0o=; Received: from mail-wm1-f51.google.com ([209.85.128.51]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.94.2) id 1nL2ao-0064b7-7O for openvpn-devel@lists.sourceforge.net; Fri, 18 Feb 2022 12:42:37 +0000 Received: by mail-wm1-f51.google.com with SMTP id w13so4675203wmi.2 for <openvpn-devel@lists.sourceforge.net>; Fri, 18 Feb 2022 04:42:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id; bh=hnskYEUtL6WJGm0TVLZDe9LwMnlOUb3K9/Icp3465L8=; b=eUFqygmOZArEIgrFEX4muvPC5JTmgb9+qIgbrCqvB0Mw0vRJfZGwrwknJ5mOZ8/7m8 RP7iK1vehF4pqZKhE+SZ+z5ORZWsJvfsvqp+H+fDbuiMh3OVHyvRztwoj1026Uqhmohy 7y9oIeo9VIBauVCY/hm6dg6OWdnRRBNE594sR9DXsOEibqw/8QjGP07OknSHKxSnGysF /BNEoUJtmX/gW4GfGbjbXW+AV9rBPVU44eZEnQQwPpXvQ9MsoFgkG2YYuLjdAkFsSpfZ 7sZxyH8LTW2jMoLcwcrxRC68BBI+fEbRwhTSRp2RD6qd66Xl1gICyqUW3YlVWr6T46E2 iYdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=hnskYEUtL6WJGm0TVLZDe9LwMnlOUb3K9/Icp3465L8=; b=JmAv/RRVpZRPorjBpQ46maYUW2tIO8a/9hmNIUj+AIUQ6q7MKnTbfY0orZUSKCNEkf XVkM/97WVdZ3EGIPJE7MH2+GtFesLm0NYN6loEeSsczg9tlIQvORV2nXoEL7aRMhjQjq uCZhrQbRaE3niSgNQ1DAZRpMWmwXPlNb0N7H7hpP7AT7vIPnXxQt1JhguerSkKls2zOU C5lFoEe1h9w4xHp4N8xcGwWa/Q0j60LGWqURNYhsoUGrReHSFNFf41WoKSwPl1lBzmwK 0jz2gBP4uHoOAFtysZa43jfXV/1SfhdbLj9roit15g25ktdOLlYeufrtUpk1dcUsyNcZ aYAQ== X-Gm-Message-State: AOAM532JizwNbAjzLOTvSzDZnYAB+kugmJfM/5OHYSuxC9FSYFrLc2AB a6ZhenERh0DuLVka0mu8zA8xpYT+9qOvWw== X-Google-Smtp-Source: ABdhPJyOGP/XiEqnuOHnNiywj1lb0GmliWblsZlFZR6Fhq3p3P0RCFU3/k3rJ3UtCKgGqFtur5H2XA== X-Received: by 2002:a05:600c:2285:b0:37c:3074:43d3 with SMTP id 5-20020a05600c228500b0037c307443d3mr7215672wmf.195.1645188147418; Fri, 18 Feb 2022 04:42:27 -0800 (PST) Received: from LAPTOP-4L3N7KFS.localdomain (nat4.panoulu.net. [185.38.2.4]) by smtp.gmail.com with ESMTPSA id r13sm29409081wro.89.2022.02.18.04.42.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 04:42:27 -0800 (PST) From: Lev Stipakov <lstipakov@gmail.com> To: openvpn-devel@lists.sourceforge.net Date: Fri, 18 Feb 2022 14:41:49 +0200 Message-Id: <20220218124149.149-1-lstipakov@gmail.com> X-Mailer: git-send-email 2.17.1 X-Spam-Report: Spam detection software, running on the system "util-spamd-2.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 On Windows we use GetLastError() to get error code. To get error description, we must use FormatMessage() and not strerror(). Replace strerror() with openvpn_strerror() macro, which is resolved to strerror_win32() (which calls FormatMessage) on Windows and to strerror() on other platforms. Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [lstipakov[at]gmail.com] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -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_VALID Message has at least one valid DKIM or DK signature -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.128.51 listed in wl.mailspike.net] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.128.51 listed in list.dnswl.org] -0.0 T_SCC_BODY_TEXT_LINE No description available. X-Headers-End: 1nL2ao-0064b7-7O Subject: [Openvpn-devel] [PATCH master+release/2.5] error.c: use correct API to get error description on Windows X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: <openvpn-devel.lists.sourceforge.net> List-Unsubscribe: <https://lists.sourceforge.net/lists/options/openvpn-devel>, <mailto:openvpn-devel-request@lists.sourceforge.net?subject=unsubscribe> List-Archive: <http://sourceforge.net/mailarchive/forum.php?forum_name=openvpn-devel> List-Post: <mailto:openvpn-devel@lists.sourceforge.net> List-Help: <mailto:openvpn-devel-request@lists.sourceforge.net?subject=help> List-Subscribe: <https://lists.sourceforge.net/lists/listinfo/openvpn-devel>, <mailto:openvpn-devel-request@lists.sourceforge.net?subject=subscribe> Cc: Lev Stipakov <lev@openvpn.net> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox |
| Series |
[Openvpn-devel,master+release/2.5] error.c: use correct API to get error description on Windows
|
|
Commit Message
Lev Stipakov
Feb. 18, 2022, 1:41 a.m. UTC
From: Lev Stipakov <lev@openvpn.net> On Windows we use GetLastError() to get error code. To get error description, we must use FormatMessage() and not strerror(). Replace strerror() with openvpn_strerror() macro, which is resolved to strerror_win32() (which calls FormatMessage) on Windows and to strerror() on other platforms. Signed-off-by: Lev Stipakov <lev@openvpn.net> --- src/openvpn/error.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Comments
We had a long discussion with ordex about this patch and came to the
conclusion that error printing is currently broken on Windows and
needs a proper fixing.
Why is it broken?
- the bug that my patch fixes - we use Windows's GetLastError to get
Windows last error code and with that strerror to get C runtime error
description
- we have code which uses M_ERRNO to get description of C runtime
errors, for example
int fd = platform_open(filename, O_CREAT | O_TRUNC | O_WRONLY,
S_IRUSR | S_IWUSR);
if (fd == -1)
{
msg(M_ERRNO, "Cannot open file '%s' for write", filename);
return false;
}
but this doesn't really work, because msg(M_ERRNO) uses
GetLastError on Windows to get error code, and C runtime (in above
case it is _wopen) doesn't set the WinAPI's last error code.
- we have code which uses M_ERRNO to print description of socket errors
const ssize_t size = send(sd, buf, strlen(buf), MSG_NOSIGNAL);
if (size != (ssize_t) strlen(buf))
{
msg(D_LINK_ERRORS | M_ERRNO, "send_line: TCP port write failed
on send()");
return false;
}
but in this case we should use WSAGetLastError on Windows and errno on
other platforms.
What we propose:
- M_ERRNO prints only C runtime errors on all platforms and should be
only used with C runtime functions
- We add M_WINERR which uses GetLastError and FormatMessage to print
Windows errors
- We add M_SOCKERR, which is resolved into M_ERRNO on all platforms
except Windows and on Windows it is M_WSAERR. We use WSAGetLastError
and FormatMessage to print WSA errors. Socket functions use M_SOCKERR.
-Lev
Hi On Mon, Feb 21, 2022 at 4:24 AM Lev Stipakov <lstipakov@gmail.com> wrote: > We had a long discussion with ordex about this patch and came to the > conclusion that error printing is currently broken on Windows and > needs a proper fixing. > > +1 > What we propose: > > - M_ERRNO prints only C runtime errors on all platforms and should be > only used with C runtime functions > - We add M_WINERR which uses GetLastError and FormatMessage to print > Windows errors > While this would be a cleaner fix, it also requires extensive changes and it is not always easy to decide where to use M_ERRNO and where to use M_WINERR. E.g., without looking into the internals of platform.c one doesn't know whether platform_open() uses _wopen() or CreateFile(). A possible option is to continue the use of M_ERRNO on WIndows as is (except for socket errors), continue to use GetLastError() in x_msg(), but if/when the latter if it returns zero, try errno and strerrror(). Not ideal but less changes and easier and transparent to the user of msg(). That said, I haven't checked whether GetLastError() returns zero or a valid and correct error code for C runtime errors -- without which this approach wont work. - We add M_SOCKERR, which is resolved into M_ERRNO on all platforms > except Windows and on Windows it is M_WSAERR. We use WSAGetLastError > and FormatMessage to print WSA errors. Socket functions use M_SOCKERR. > Sounds good. BTW, there are a couple of uses of strerror() in check_status() (error.c) missed by the original patch. Selva <div dir="ltr"><div>Hi</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 21, 2022 at 4:24 AM Lev Stipakov <<a href="mailto:lstipakov@gmail.com">lstipakov@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">We had a long discussion with ordex about this patch and came to the<br> conclusion that error printing is currently broken on Windows and<br> needs a proper fixing.<br><br></blockquote><div><br></div><div>+1 </div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> What we propose:<br> <br> - M_ERRNO prints only C runtime errors on all platforms and should be<br> only used with C runtime functions<br> - We add M_WINERR which uses GetLastError and FormatMessage to print<br> Windows errors<br></blockquote><div><br></div><div>While this would be a cleaner fix, it also requires extensive changes and it is not always easy to decide where to use M_ERRNO and where to use M_WINERR. E.g., without looking into the internals of platform.c one doesn't know whether platform_open() uses _wopen() or CreateFile().</div><div><br></div><div>A possible option is to continue the use of M_ERRNO on WIndows as is (except for socket errors), continue to use GetLastError() in x_msg(), but if/when the latter if it returns zero, try errno and strerrror(). Not ideal but less changes and easier and transparent to the user of msg(). That said, I haven't checked whether GetLastError() returns zero or a valid and correct error code for C runtime errors -- without which this approach wont work.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> - We add M_SOCKERR, which is resolved into M_ERRNO on all platforms<br> except Windows and on Windows it is M_WSAERR. We use WSAGetLastError<br> and FormatMessage to print WSA errors. Socket functions use M_SOCKERR.<br></blockquote><div><br></div><div>Sounds good.</div><div><br></div><div>BTW, there are a couple of uses of strerror() in check_status() (error.c) missed by the original patch.</div><div><br></div><div>Selva</div></div></div>
Hi, > While this would be a cleaner fix, it also requires extensive changes and > it is not always easy to decide where to use M_ERRNO and where to use > M_WINERR. E.g., without looking into the internals of platform.c one > doesn't know whether platform_open() uses _wopen() or CreateFile(). > True. > A possible option is to continue the use of M_ERRNO on WIndows as is > (except for socket errors), continue to use GetLastError() in x_msg(), but > if/when the latter if it returns zero, try errno and strerrror(). Not ideal > but less changes and easier and transparent to the user of msg(). That > said, I haven't checked whether GetLastError() returns zero or a valid and > correct error code for C runtime errors -- without which this approach wont > work. > I did some tests, ::GetLastError appears to return zero or correct error code: sqrt(-1); int err = errno; // -33 EDOM DWORD lasterr = GetLastError(); // 0 _open("c:\\Temp\\notfound", _O_RDONLY); err = errno; // 2 ENOENT lasterr = GetLastError(); // 2 ERROR_FILE_NOT_FOUND _open("c:\\Temp\\directory", _O_RDONLY); err = errno; // 13 EACCES lasterr = GetLastError(); // 5 ERROR_ACCESS_DENIED malloc(10000000000); err = errno; // 12 ENOMEM lasterr = GetLastError(); // 8 ERROR_NOT_ENOUGH_MEMORY So let's go with this approach. > BTW, there are a couple of uses of strerror() in check_status() (error.c) > missed by the original patch. > Thanks, I will address it in this "proper" fix.
diff --git a/src/openvpn/error.c b/src/openvpn/error.c index 54796d03..cb2a0db1 100644 --- a/src/openvpn/error.c +++ b/src/openvpn/error.c @@ -268,7 +268,7 @@ x_msg_va(const unsigned int flags, const char *format, va_list arglist) if ((flags & M_ERRNO) && e) { openvpn_snprintf(m2, ERR_BUF_SIZE, "%s: %s (errno=%d)", - m1, strerror(e), e); + m1, openvpn_strerror(e, &gc), e); SWAP; }