Commit 9f0c2c14 authored by Daniel Néri's avatar Daniel Néri Committed by Ariadne Conill

main/musl: fix incompatible behaviour of res_query(3)

The historical, but not clearly documented behaviour of res_query(3)
is to return -1 and set h_errno for failures like NXDOMAIN.
parent 69732795
From 9d0b8b92a508c328e7eac774847f001f80dfb5ff Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Mon, 24 Aug 2020 21:38:49 -0400
Subject: [PATCH] make h_errno thread-local
the framework to do this always existed but it was deemed unnecessary
because the only [ex-]standard functions using h_errno were not
thread-safe anyway. however, some of the nonstandard res_* functions
are also supposed to set h_errno to indicate the cause of error, and
were unable to do so because it was not thread-safe. this change is a
prerequisite for fixing them.
---
src/internal/pthread_impl.h | 1 +
src/network/h_errno.c | 6 ++----
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h
index 5742dfc5..5749a336 100644
--- a/src/internal/pthread_impl.h
+++ b/src/internal/pthread_impl.h
@@ -43,6 +43,7 @@ struct pthread {
long off;
volatile void *volatile pending;
} robust_list;
+ int h_errno_val;
volatile int timer_id;
locale_t locale;
volatile int killlock[1];
diff --git a/src/network/h_errno.c b/src/network/h_errno.c
index 4f700cea..8677a92b 100644
--- a/src/network/h_errno.c
+++ b/src/network/h_errno.c
@@ -1,9 +1,7 @@
#include <netdb.h>
-
-#undef h_errno
-int h_errno;
+#include "pthread_impl.h"
int *__h_errno_location(void)
{
- return &h_errno;
+ return &__pthread_self()->h_errno_val;
}
--
2.28.0
From 19f8642494b7d27b2ceed5c14d4a0b27cb749afe Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Mon, 24 Aug 2020 21:56:48 -0400
Subject: [PATCH] report res_query failures, including nxdomain/nodata, via
h_errno
while it's not clearly documented anywhere, this is the historical
behavior which some applications expect. applications which need to
see the response packet in these cases, for example to distinguish
between nonexistence in a secure vs insecure zone, must already use
res_mkquery with res_send in order to be portable, since most if not
all other implementations of res_query don't provide it.
---
src/network/res_query.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/src/network/res_query.c b/src/network/res_query.c
index 2f4da2e2..506dc231 100644
--- a/src/network/res_query.c
+++ b/src/network/res_query.c
@@ -1,3 +1,4 @@
+#define _BSD_SOURCE
#include <resolv.h>
#include <netdb.h>
@@ -6,7 +7,20 @@ int res_query(const char *name, int class, int type, unsigned char *dest, int le
unsigned char q[280];
int ql = __res_mkquery(0, name, class, type, 0, 0, 0, q, sizeof q);
if (ql < 0) return ql;
- return __res_send(q, ql, dest, len);
+ int r = __res_send(q, ql, dest, len);
+ if (r<12) {
+ h_errno = TRY_AGAIN;
+ return -1;
+ }
+ if ((dest[3] & 15) == 3) {
+ h_errno = HOST_NOT_FOUND;
+ return -1;
+ }
+ if ((dest[3] & 15) == 0 && !dest[6] && !dest[7]) {
+ h_errno = NO_DATA;
+ return -1;
+ }
+ return r;
}
weak_alias(res_query, res_search);
--
2.28.0
From cf27184d00c1588eccac71a3bc89c9798e60bbd9 Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Sun, 30 Aug 2020 21:30:37 -0400
Subject: [PATCH] restore h_errno ABI compatibility with ancient binaries
prior to commit e68c51ac46a9f273927aef8dcebc89912ab19ece, h_errno was
actually an external data object not a macro. bring back the symbol,
and use it as the storage for the main thread's h_errno.
technically this still doesn't provide full compatibility if the
application was multithreaded, but at the time there were no res_*
functions (and they did not set h_errno anyway), so any use of h_errno
would have been via thread-unsafe functions. thus a solution that just
fixes single-threaded applications seems acceptable.
---
src/network/h_errno.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/network/h_errno.c b/src/network/h_errno.c
index 8677a92b..638f7718 100644
--- a/src/network/h_errno.c
+++ b/src/network/h_errno.c
@@ -1,7 +1,11 @@
#include <netdb.h>
#include "pthread_impl.h"
+#undef h_errno
+int h_errno;
+
int *__h_errno_location(void)
{
+ if (!__pthread_self()->stack) return &h_errno;
return &__pthread_self()->h_errno_val;
}
--
2.28.0
......@@ -2,7 +2,7 @@
# Maintainer: Timo Teräs <timo.teras@iki.fi>
pkgname=musl
pkgver=1.2.1
pkgrel=0
pkgrel=1
pkgdesc="the musl c library (libc) implementation"
url="https://musl.libc.org/"
arch="all"
......@@ -19,6 +19,9 @@ nolibc) ;;
esac
source="https://musl.libc.org/releases/musl-$pkgver.tar.gz
handle-aux-at_base.patch
0001-make-h_errno-thread-local.patch
0001-report-res_query-failures-including-nxdomain-nodata-.patch
0001-restore-h_errno-ABI-compatibility-with-ancient-binar.patch
ldconfig
__stack_chk_fail_local.c
......@@ -162,6 +165,9 @@ compat() {
sha512sums="455464ef47108a78457291bda2b1ea574987a1787f6001e9376956f20521593a4816bc215dab41c1a80292ae7ebd315accb4d4fa6a1210ff77d9a4d68239e960 musl-1.2.1.tar.gz
a76f79b801497ad994746cf82bb6eaf86f9e1ae646e6819fbae8532a7f4eee53a96ac1d4e789ec8f66aea2a68027b0597f7a579b3369e01258da8accfce41370 handle-aux-at_base.patch
7dedeec490aeb50ea4cdd949e931a3b5fcc640a10dcd4624a0f1502ae56ece821f5038506b4288cea0684a6d05371e93f9c6607310efc7fab7fe0181ec8d1265 0001-make-h_errno-thread-local.patch
2294022a1128f221b3433f01b471382646abb6fb5cea69c3b6672ecd1b1b68c6cac73eaf1fc310af419ad7f3904e36828257a35974298c26201ff8fbd29be7e9 0001-report-res_query-failures-including-nxdomain-nodata-.patch
228304741328dad4d5331b0dd41c3a8e120a367b247013c71569e5acaab773bcb5d85b30d44dab0938f3bce4217a5b0164d7981c8bb1d5ab8c647657ad8d3b41 0001-restore-h_errno-ABI-compatibility-with-ancient-binar.patch
8d3a2d5315fc56fee7da9abb8b89bb38c6046c33d154c10d168fb35bfde6b0cf9f13042a3bceee34daf091bc409d699223735dcf19f382eeee1f6be34154f26f ldconfig
062bb49fa54839010acd4af113e20f7263dde1c8a2ca359b5fb2661ef9ed9d84a0f7c3bc10c25dcfa10bb3c5a4874588dff636ac43d5dbb3d748d75400756d0b __stack_chk_fail_local.c
0d80f37b34a35e3d14b012257c50862dfeb9d2c81139ea2dfa101d981d093b009b9fa450ba27a708ac59377a48626971dfc58e20a3799084a65777a0c32cbc7d getconf.c
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment