From 80c1c57262533f12a61a0370a0a49202a5158fd8 Mon Sep 17 00:00:00 2001 From: Brad House Date: Thu, 12 Dec 2024 12:22:24 -0500 Subject: [PATCH] don't use the AX_ namespace for custom m4 macros --- configure.ac | 4 +- m4/ares_check_user_namespace.m4 | 57 ++++++++++++++++++++++++ m4/ares_check_uts_namespace.m4 | 79 +++++++++++++++++++++++++++++++++ m4/ax_check_user_namespace.m4 | 1 - m4/ax_check_uts_namespace.m4 | 1 - 5 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 m4/ares_check_user_namespace.m4 create mode 100644 m4/ares_check_uts_namespace.m4 delete mode 100644 m4/ax_check_user_namespace.m4 delete mode 100644 m4/ax_check_uts_namespace.m4 diff --git a/configure.ac b/configure.ac index 97bf5467a6..1335adb570 100644 --- a/configure.ac +++ b/configure.ac @@ -826,8 +826,8 @@ if test "x$build_tests" != "xno" ; then else PKG_CHECK_MODULES([GMOCK112], [gmock >= 1.12.0], [ have_gmock_v112=yes ], [ have_gmock_v112=no ]) if test "x$have_gmock_v112" = "xyes" ; then - AX_CHECK_USER_NAMESPACE - AX_CHECK_UTS_NAMESPACE + ARES_CHECK_USER_NAMESPACE + ARES_CHECK_UTS_NAMESPACE fi fi fi diff --git a/m4/ares_check_user_namespace.m4 b/m4/ares_check_user_namespace.m4 new file mode 100644 index 0000000000..a26b384fda --- /dev/null +++ b/m4/ares_check_user_namespace.m4 @@ -0,0 +1,57 @@ +# -*- Autoconf -*- + +# SYNOPSIS +# +# ARES_CHECK_USER_NAMESPACE +# +# DESCRIPTION +# +# This macro checks whether the local system supports Linux user namespaces. +# If so, it calls AC_DEFINE(HAVE_USER_NAMESPACE). +# +# Copyright (C) The c-ares team +# SPDX-License-Identifier: MIT + +AC_DEFUN([ARES_CHECK_USER_NAMESPACE],[dnl + AC_CACHE_CHECK([whether user namespaces are supported], + ares_cv_user_namespace,[ + AC_LANG_PUSH([C]) + AC_RUN_IFELSE([AC_LANG_SOURCE([[ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +int userfn(void *d) { + usleep(100000); /* synchronize by sleep */ + return (getuid() != 0); +} +char userst[1024*1024]; +int main() { + char buffer[1024]; + int rc, status, fd; + pid_t child = clone(userfn, userst + 1024*1024, CLONE_NEWUSER|SIGCHLD, 0); + if (child < 0) return 1; + + snprintf(buffer, sizeof(buffer), "/proc/%d/uid_map", child); + fd = open(buffer, O_CREAT|O_WRONLY|O_TRUNC, 0755); + snprintf(buffer, sizeof(buffer), "0 %d 1\n", getuid()); + write(fd, buffer, strlen(buffer)); + close(fd); + + rc = waitpid(child, &status, 0); + if (rc <= 0) return 1; + if (!WIFEXITED(status)) return 1; + return WEXITSTATUS(status); +} + ]])],[ares_cv_user_namespace=yes],[ares_cv_user_namespace=no],[ares_cv_user_namespace=no]) + AC_LANG_POP([C]) + ]) + if test "$ares_cv_user_namespace" = yes; then + AC_DEFINE([HAVE_USER_NAMESPACE],[1],[Whether user namespaces are available]) + fi +]) # ARES_CHECK_USER_NAMESPACE diff --git a/m4/ares_check_uts_namespace.m4 b/m4/ares_check_uts_namespace.m4 new file mode 100644 index 0000000000..0aeefe4a9b --- /dev/null +++ b/m4/ares_check_uts_namespace.m4 @@ -0,0 +1,79 @@ +# -*- Autoconf -*- + +# SYNOPSIS +# +# ARES_CHECK_UTS_NAMESPACE +# +# DESCRIPTION +# +# This macro checks whether the local system supports Linux UTS namespaces. +# Also requires user namespaces to be available, so that non-root users +# can enter the namespace. +# If so, it calls AC_DEFINE(HAVE_UTS_NAMESPACE). +# +# Copyright (C) The c-ares team +# SPDX-License-Identifier: MIT + +AC_DEFUN([ARES_CHECK_UTS_NAMESPACE],[dnl + AC_CACHE_CHECK([whether UTS namespaces are supported], + ares_cv_uts_namespace,[ + AC_LANG_PUSH([C]) + AC_RUN_IFELSE([AC_LANG_SOURCE([[ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +int utsfn(void *d) { + char buffer[1024]; + const char *name = "autoconftest"; + int rc = sethostname(name, strlen(name)); + if (rc != 0) return 1; + gethostname(buffer, 1024); + return (strcmp(buffer, name) != 0); +} + +char st2[1024*1024]; +int fn(void *d) { + pid_t child; + int rc, status; + usleep(100000); /* synchronize by sleep */ + if (getuid() != 0) return 1; + child = clone(utsfn, st2 + 1024*1024, CLONE_NEWUTS|SIGCHLD, 0); + if (child < 0) return 1; + rc = waitpid(child, &status, 0); + if (rc <= 0) return 1; + if (!WIFEXITED(status)) return 1; + return WEXITSTATUS(status); +} +char st[1024*1024]; +int main() { + char buffer[1024]; + int rc, status, fd; + pid_t child = clone(fn, st + 1024*1024, CLONE_NEWUSER|SIGCHLD, 0); + if (child < 0) return 1; + + snprintf(buffer, sizeof(buffer), "/proc/%d/uid_map", child); + fd = open(buffer, O_CREAT|O_WRONLY|O_TRUNC, 0755); + snprintf(buffer, sizeof(buffer), "0 %d 1\n", getuid()); + write(fd, buffer, strlen(buffer)); + close(fd); + + rc = waitpid(child, &status, 0); + if (rc <= 0) return 1; + if (!WIFEXITED(status)) return 1; + return WEXITSTATUS(status); +} +]]) + ],[ares_cv_uts_namespace=yes],[ares_cv_uts_namespace=no],[ares_cv_uts_namespace=no]) + AC_LANG_POP([C]) + ]) + if test "$ares_cv_uts_namespace" = yes; then + AC_DEFINE([HAVE_UTS_NAMESPACE],[1],[Whether UTS namespaces are available]) + fi +]) # ARES_CHECK_UTS_NAMESPACE diff --git a/m4/ax_check_user_namespace.m4 b/m4/ax_check_user_namespace.m4 deleted file mode 100644 index 1becba2bb0..0000000000 --- a/m4/ax_check_user_namespace.m4 +++ /dev/null @@ -1 +0,0 @@ -404: Not Found \ No newline at end of file diff --git a/m4/ax_check_uts_namespace.m4 b/m4/ax_check_uts_namespace.m4 deleted file mode 100644 index 1becba2bb0..0000000000 --- a/m4/ax_check_uts_namespace.m4 +++ /dev/null @@ -1 +0,0 @@ -404: Not Found \ No newline at end of file