Merge "clat: enable PACKET_AUXDATA cmsg processing"
diff --git a/clatd.h b/clatd.h
index e810a3b..cfcb253 100644
--- a/clatd.h
+++ b/clatd.h
@@ -43,7 +43,11 @@
// ie. if we ever read >= MAXMTU bytes we should discard.
#define MAXMTU (0xFFFF + 28 + 1)
#define PACKETLEN (sizeof(struct tun_pi) + MAXMTU)
-#define CLATD_VERSION "1.5"
+
+// logcat_hexdump() maximum binary data length
+#define MAXDUMPLEN PACKETLEN
+
+#define CLATD_VERSION "1.6"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
diff --git a/dump.c b/dump.c
index 289f161..dff3d5e 100644
--- a/dump.c
+++ b/dump.c
@@ -226,11 +226,11 @@
/* generic hex dump */
void logcat_hexdump(const char *info, const uint8_t *data, size_t len) {
- char output[PACKETLEN * 3 + 2];
+ char output[MAXDUMPLEN * 3 + 2];
size_t i;
output[0] = '\0';
- for (i = 0; i < len && i < PACKETLEN; i++) {
+ for (i = 0; i < len && i < MAXDUMPLEN; i++) {
snprintf(output + i * 3, 4, " %02x", data[i]);
}
output[len * 3 + 3] = '\0';
diff --git a/main.c b/main.c
index 2ab865e..11da65b 100644
--- a/main.c
+++ b/main.c
@@ -19,9 +19,12 @@
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/personality.h>
+#include <sys/utsname.h>
#include <unistd.h>
#include "clatd.h"
@@ -151,6 +154,34 @@
uplink_interface, plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)",
v6_addr ? v6_addr : "(none)");
+ {
+ // Compile time detection of 32 vs 64-bit build. (note: C does not have 'constexpr')
+ // Avoid use of preprocessor macros to get compile time syntax checking even on 64-bit.
+ const int user_bits = sizeof(void*) * 8;
+ const bool user32 = (user_bits == 32);
+
+ // Note that on 64-bit all this personality related code simply compile optimizes out.
+ // 32-bit: fetch current personality (see 'man personality': 0xFFFFFFFF means retrieve only)
+ // On Linux fetching personality cannot fail.
+ const int prev_personality = user32 ? personality(0xFFFFFFFFuL) : PER_LINUX;
+ // 32-bit: attempt to get rid of kernel spoofing of 'uts.machine' architecture,
+ // In theory this cannot fail, as PER_LINUX should always be supported.
+ if (user32) (void)personality((prev_personality & ~PER_MASK) | PER_LINUX);
+ // 64-bit: this will compile time evaluate to false.
+ const bool was_linux32 = (prev_personality & PER_MASK) == PER_LINUX32;
+
+ struct utsname uts = {};
+ if (uname(&uts)) exit(1); // only possible error is EFAULT, but 'uts' is on stack
+
+ // sysname is likely 'Linux', release is 'kver', machine is kernel's *true* architecture
+ logmsg(ANDROID_LOG_INFO, "%d-bit userspace on %s kernel %s for %s%s.", user_bits,
+ uts.sysname, uts.release, uts.machine, was_linux32 ? " (was spoofed)" : "");
+
+ // 32-bit: try to return to the 'default' personality
+ // In theory this cannot fail, because it was already previously in use.
+ if (user32) (void)personality(prev_personality);
+ }
+
// Loop until someone sends us a signal or brings down the tun interface.
if (signal(SIGTERM, stop_loop) == SIG_ERR) {
logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));