Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions crypto/fipsmodule/cpucap/cpu_getauxval_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// After including this header (on Linux), OPENSSL_HAS_GETAUXVAL is defined
// and getauxval(type) is callable -- regardless of whether <sys/auxv.h> was
// available -- so call sites do not need to special-case the libc.
// AT_NULL, AT_HWCAP, AT_HWCAP2, and AT_EXECFN are also defined when the
// system header omits them (for example under `-D_XOPEN_SOURCE=700`).

#if defined(OPENSSL_LINUX)

Expand Down Expand Up @@ -41,26 +43,15 @@

#if defined(OPENSSL_HAS_GETAUXVAL)
#include <sys/auxv.h>
#else

// When <sys/auxv.h> is not available (e.g. older uclibc without getauxval),
// fall back to reading /proc/self/auxv. The file contains sequential pairs of
// unsigned long values: [type_0, value_0, type_1, value_1, ..., AT_NULL, 0].

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

// O_CLOEXEC was added in Linux 2.6.23 / glibc 2.7. Older toolchains (e.g.
// manylinux1 / CentOS 5 with glibc 2.5) do not define it. Fall back to 0;
// the fd is opened, read, and closed synchronously within this function
// (no intervening fork), so close-on-exec is not security-relevant here.
#if !defined(O_CLOEXEC)
#define O_CLOEXEC 0
#endif

// Auxiliary vector type constants from the Linux kernel ABI
// (include/uapi/linux/auxvec.h). The specific values used here are stable.
//
// <sys/auxv.h> may exist but still hide AT_HWCAP2 when building with
// `-D_XOPEN_SOURCE=700` and without `_GNU_SOURCE` (aws-lc-sys Linux
// builds on manylinux 2.17). glibc also only advertised AT_HWCAP2 starting
// in 2.18 (see #1682).
#if !defined(AT_NULL)
#define AT_NULL 0
#endif
Expand All @@ -74,6 +65,24 @@
#define AT_EXECFN 31
#endif

#if !defined(OPENSSL_HAS_GETAUXVAL)

// When <sys/auxv.h> is not available (e.g. older uclibc without getauxval),
// fall back to reading /proc/self/auxv. The file contains sequential pairs of
// unsigned long values: [type_0, value_0, type_1, value_1, ..., AT_NULL, 0].

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

// O_CLOEXEC was added in Linux 2.6.23 / glibc 2.7. Older toolchains (e.g.
// manylinux1 / CentOS 5 with glibc 2.5) do not define it. Fall back to 0;
// the fd is opened, read, and closed synchronously within this function
// (no intervening fork), so close-on-exec is not security-relevant here.
#if !defined(O_CLOEXEC)
#define O_CLOEXEC 0
#endif

// getauxval returns the value of the auxiliary-vector entry of the given
// |type|, or 0 if no such entry exists or /proc/self/auxv is unavailable.
//
Expand Down Expand Up @@ -138,7 +147,7 @@ static OPENSSL_UNUSED unsigned long getauxval(unsigned long type) {
// the fallback above.
#define OPENSSL_HAS_GETAUXVAL

#endif // OPENSSL_HAS_GETAUXVAL
#endif // !defined(OPENSSL_HAS_GETAUXVAL)

#endif // OPENSSL_LINUX

Expand Down
Loading