OpenSMTPD / libasr

Portable asr. Discontinued, it's only maintained as part of OpenSMPTD now.
Other
25 stars 9 forks source link

Using AI_CANONNAME on systems that do not defined AI_FQDN causes BADFLAGS error #26

Closed naost3rn closed 1 year ago

naost3rn commented 1 year ago

Executing the following test code in Alpine Linux will result in a BADFLAGS error.

#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <asr.h>

int main() {
    struct asr_query *query;
    struct asr_result result;
    struct addrinfo hints;
    const char *hostname = "www.yahoo.co.jp";

    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_CANONNAME;
    query = getaddrinfo_async(hostname, NULL, &hints, NULL);
    asr_run_sync(query, &result);
    if (errno != 0) {
        printf("asr run error: %s\n", strerror(errno));
        return 1;
    } else if (result.ar_gai_errno) {
        printf("gataddrinfo error: %s\n", gai_strerror(result.ar_gai_errno));
        return 1;
    }
    return 0;
}
$ cc test.c -lasr
$ ./a.out 
gataddrinfo error: Invalid flags

Only of the AI_CANONNAME and AI_FQDN bits can be set, Only the AI_CANONNAME and AI_FQDN bits can be set, and are verified in getaddrinfo_async_run() as follows: https://github.com/OpenSMTPD/libasr/blob/15fdb71b8b6664543189ca186810fabb579a9cf0/src/getaddrinfo_async.c#L168-L173

However, on systems such as Linux with musl that do not define AI_FQDN, it is defined as AI_CANONNAME at build time: https://github.com/OpenSMTPD/libasr/blob/15fdb71b8b6664543189ca186810fabb579a9cf0/src/asr_compat.h#L42-L44

naost3rn commented 1 year ago

On hand, the following patch was applied and it worked fine.

diff --git a/src/getaddrinfo_async.c b/src/getaddrinfo_async.c
index 4a56981..54317bb 100644
--- a/src/getaddrinfo_async.c
+++ b/src/getaddrinfo_async.c
@@ -166,7 +166,7 @@ getaddrinfo_async_run(struct asr_query *as, struct asr_result *ar)
 #endif

        if (ai->ai_flags & ~AI_MASK ||
-           (ai->ai_flags & AI_CANONNAME && ai->ai_flags & AI_FQDN)) {
+           (ai->ai_flags & AI_CANONNAME && ai->ai_flags & AI_FQDN && AI_CANONNAME != AI_FQDN)) {
            ar->ar_gai_errno = EAI_BADFLAGS;
            async_set_state(as, ASR_STATE_HALT);
            break;
omar-polo commented 1 year ago

Hello,

Sorry for the delay in replying.

We decided to discontinue libasr-portable and only maintain the bundled version in OpenSMTPD so this repository will be soon archived.

Does this still happens on the (updated) libasr bundled in OpenSMTPD? Does it affect OpenSMTPD too? Even if it didn't, I'd like to have this fixed in the OpenSMTPD repository as other portable projects may use that as starting point, or smtpd fall in this issue.

Can you please open a PR on the https://github.com/OpenSMTPD/OpenSMTPD repository to discuss this? Thanks!

naost3rn commented 1 year ago

This issue has been moved to the https://github.com/OpenSMTPD/OpenSMTPD/issues/1215.

omar-polo commented 1 year ago

closing since it has been moved.