Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
naost3rn opened this issue Jun 22, 2023 · 1 comment
Labels

Comments

@naost3rn
Copy link

naost3rn commented Jun 22, 2023

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 ./libopenbsd.a
$ ./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:

if (ai->ai_flags & ~AI_MASK ||
(ai->ai_flags & AI_CANONNAME && ai->ai_flags & AI_FQDN)) {
ar->ar_gai_errno = EAI_BADFLAGS;
async_set_state(as, ASR_STATE_HALT);
break;
}

But, on systems such as Linux with musl that do not define AI_FQDN, it is defined as AI_CANONNAME at build time:

#ifndef AI_FQDN
#define AI_FQDN AI_CANONNAME
#endif

However, smtpd is not affected by this issue because the AI_CANONNAME flag is used by libc's getaddrinfo().

hints.ai_flags = AI_CANONNAME;
error = getaddrinfo(hostname, NULL, &hints, &res);

@omar-polo
Copy link
Contributor

Thanks for moving the issue here. I'll take a look in the following days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants