Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 36db7736 authored by Bernie Innocenti's avatar Bernie Innocenti
Browse files

Make resolv_gethostbyname() re-entrant

The new call signature is closer to gethostbyname2_r() and no longer
relies on thread-local storage for its buffers. res_get_static() is now
down to a single caller.

Test: m, flash, atest
Change-Id: Ic6e2c0292fece31c2586a81d8fb6a8df20c4f9f5
parent 852c08e5
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ namespace {
constexpr int MAX_QUERIES_PER_UID = 256;

// Max packet size for answer, sync with getaddrinfo.c
// TODO: switch to dynamically allocated buffers with std::vector
constexpr int MAXPACKET = 8 * 1024;

android::netdutils::OperationLimiter<uid_t> queryLimiter(MAX_QUERIES_PER_UID);
@@ -1033,7 +1034,8 @@ DnsProxyListener::GetHostByNameHandler::~GetHostByNameHandler() {
    free(mName);
}

void DnsProxyListener::GetHostByNameHandler::doDns64Synthesis(int32_t* rv, struct hostent** hpp,
void DnsProxyListener::GetHostByNameHandler::doDns64Synthesis(int32_t* rv, hostent* hbuf, char* buf,
                                                              size_t buflen, struct hostent** hpp,
                                                              NetworkDnsEventReported* event) {
    // Don't have to consider family AF_UNSPEC case because gethostbyname{, 2} only supports
    // family AF_INET or AF_INET6.
@@ -1051,7 +1053,7 @@ void DnsProxyListener::GetHostByNameHandler::doDns64Synthesis(int32_t* rv, struc
    // If caller wants IPv6 answers but no data, try to query IPv4 answers for synthesis
    const uid_t uid = mClient->getUid();
    if (queryLimiter.start(uid)) {
        *rv = android_gethostbynamefornetcontext(mName, AF_INET, &mNetContext, hpp, event);
        *rv = resolv_gethostbyname(mName, AF_INET, hbuf, buf, buflen, &mNetContext, hpp, event);
        queryLimiter.finish(uid);
        if (*rv) {
            *rv = EAI_NODATA;  // return original error code
@@ -1074,11 +1076,14 @@ void DnsProxyListener::GetHostByNameHandler::run() {
    maybeFixupNetContext(&mNetContext);
    const uid_t uid = mClient->getUid();
    hostent* hp = nullptr;
    hostent hbuf;
    char tmpbuf[MAXPACKET];
    int32_t rv = 0;
    NetworkDnsEventReported event;
    initDnsEvent(&event);
    if (queryLimiter.start(uid)) {
        rv = android_gethostbynamefornetcontext(mName, mAf, &mNetContext, &hp, &event);
        rv = resolv_gethostbyname(mName, mAf, &hbuf, tmpbuf, sizeof tmpbuf, &mNetContext, &hp,
                                  &event);
        queryLimiter.finish(uid);
    } else {
        rv = EAI_MEMORY;
@@ -1086,12 +1091,12 @@ void DnsProxyListener::GetHostByNameHandler::run() {
                   << ", max concurrent queries reached";
    }

    doDns64Synthesis(&rv, &hp, &event);
    doDns64Synthesis(&rv, &hbuf, tmpbuf, sizeof tmpbuf, &hp, &event);
    const int32_t latencyUs = saturate_cast<int32_t>(s.timeTakenUs());
    event.set_latency_micros(latencyUs);
    event.set_event_type(EVENT_GETHOSTBYNAME);

    LOG(DEBUG) << "GetHostByNameHandler::run: errno: " << (hp ? "success" : strerror(errno));
    LOG(DEBUG) << "GetHostByNameHandler::run: result: " << gai_strerror(rv);

    bool success = true;
    if (hp) {
+2 −1
Original line number Diff line number Diff line
@@ -84,7 +84,8 @@ class DnsProxyListener : public FrameworkListener {
        std::string threadName();

      private:
        void doDns64Synthesis(int32_t* rv, hostent** hpp, NetworkDnsEventReported* event);
        void doDns64Synthesis(int32_t* rv, hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
                              NetworkDnsEventReported* event);

        SocketClient* mClient;  // ref counted
        char* mName;            // owned. TODO: convert to std::string.
+11 −45
Original line number Diff line number Diff line
@@ -117,12 +117,6 @@ static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
                           NetworkDnsEventReported* event);
static int dns_gethtbyname(const char* name, int af, getnamaddr* info);

static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
                                  size_t hbuflen, const android_net_context* netcontext,
                                  NetworkDnsEventReported* event);
static int gethostbyname_internal_real(const char* name, int af, hostent* hp, char* buf,
                                       size_t buflen);

static int android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t, int,
                                                             struct hostent*, char*, size_t,
                                                             const struct android_net_context*,
@@ -401,13 +395,16 @@ nospc:
    return NULL;
}

static int gethostbyname_internal_real(const char* name, int af, hostent* hp, char* buf,
                                       size_t buflen) {
int resolv_gethostbyname(const char* name, int af, hostent* hp, char* buf, size_t buflen,
                         const android_net_context* netcontext, hostent** result,
                         NetworkDnsEventReported* event) {
    getnamaddr info;
    size_t size;

    _DIAGASSERT(name != NULL);
    res_state res = res_get_state();
    if (res == nullptr) return EAI_MEMORY;
    res_setnetcontext(res, netcontext, event);

    size_t size;
    switch (af) {
        case AF_INET:
            size = NS_INADDRSZ;
@@ -463,6 +460,7 @@ static int gethostbyname_internal_real(const char* name, int af, hostent* hp, ch
        int error = dns_gethtbyname(name, af, &info);
        if (error != 0) return error;
    }
    *result = hp;
    return 0;
nospc:
    return EAI_MEMORY;
@@ -481,17 +479,10 @@ fake:
    buf += size;
    buflen -= size;
    HENT_SCOPY(hp->h_name, name, buf, buflen);
    *result = hp;
    return 0;
}

// very similar in proxy-ness to android_getaddrinfo_proxy
static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
                                  size_t hbuflen, const android_net_context* netcontext,
                                  NetworkDnsEventReported* event) {
    res_setnetcontext(res, netcontext, event);
    return gethostbyname_internal_real(name, af, hp, hbuf, hbuflen);
}

static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af,
                                                   struct hostent* hp, char* buf, size_t buflen,
                                                   const struct android_net_context* netcontext,
@@ -554,7 +545,6 @@ static int android_gethostbyaddrfornetcontext_proxy_internal(
// the error code of the caller does not currently return to netd.
struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
                                    int* he) {
    const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
    char *name;
    char* cp;
    int af, len;
@@ -569,11 +559,8 @@ struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, s
    }
    char* p = NULL;

    /* Allocate a new space to read file lines like upstream does.
     * To keep reentrancy we cannot use res_get_static()->hostbuf here,
     * as the buffer may be used to store content for a previous hostent
     * returned by non-reentrant functions like gethostbyname().
     */
    // Allocate a new space to read file lines like upstream does.
    const size_t line_buf_size = MAXPACKET;
    if ((p = (char*) malloc(line_buf_size)) == NULL) {
        goto nospc;
    }
@@ -800,27 +787,6 @@ nospc:
    return EAI_MEMORY;
}

/*
 * Non-reentrant versions.
 */

int android_gethostbynamefornetcontext(const char* name, int af,
                                       const struct android_net_context* netcontext, hostent** hp,
                                       NetworkDnsEventReported* event) {
    assert(event != nullptr);

    res_state res = res_get_state();
    if (res == NULL) return EAI_MEMORY;
    res_static* rs = res_get_static();  // For thread-safety.
    int error;
    error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
                                   netcontext, event);
    if (error == 0) {
        *hp = &rs->host;
    }
    return error;
}

int android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
                                       const struct android_net_context* netcontext, hostent** hp,
                                       NetworkDnsEventReported* event) {
+10 −2
Original line number Diff line number Diff line
@@ -20,8 +20,16 @@
#include "netd_resolv/resolv.h"  // struct android_net_context
#include "stats.pb.h"

/*
 * Error code extending EAI_* codes defined in bionic/libc/include/netdb.h.
 * This error code, including EAI_*, returned from android_getaddrinfofornetcontext()
 * and resolv_gethostbyname() are used for DNS metrics.
 */
#define NETD_RESOLV_TIMEOUT 255  // consistent with RCODE_TIMEOUT

// This is the entry point for the gethostbyname() family of legacy calls.
int android_gethostbynamefornetcontext(const char*, int, const android_net_context*, hostent**,
int resolv_gethostbyname(const char* name, int af, hostent* hp, char* buf, size_t buflen,
                         const android_net_context* netcontext, hostent** result,
                         android::net::NetworkDnsEventReported*);

// This is the entry point for the gethostbyaddr() family of legacy calls.
+0 −7
Original line number Diff line number Diff line
@@ -43,13 +43,6 @@
 */
#define MARK_UNSET 0u

/*
 * Error code extending EAI_* codes defined in bionic/libc/include/netdb.h.
 * This error code, including EAI_*, returned from android_getaddrinfofornetcontext()
 * and android_gethostbynamefornetcontext() are used for DNS metrics.
 */
#define NETD_RESOLV_TIMEOUT 255  // consistent with RCODE_TIMEOUT

/*
 * A struct to capture context relevant to network operations.
 *
Loading