Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 13 additions & 8 deletions app/src/main/cpp/hook/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,26 @@ static size_t synth_proc_content(int kind, char *buf, size_t cap) {
if (k < 0 || (size_t)k >= cap - len) return 0;
len += (size_t)k;
}
(void)sysinfo(&si);
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now) != 0) memset(&now, 0, sizeof(now));
long long btime = (long long)now.tv_sec - (long long)ts.tv_sec;
// "processes" counts the forks since boot, which an app cannot know: 0, as intr and ctxt
k = snprintf(buf + len, cap - len,
"intr 0\nctxt 0\nbtime %lld\nprocesses %u\nprocs_running 1\nprocs_blocked 0\nsoftirq 0 0 0 0 0 0 0 0 0 0 0\n",
btime, (unsigned)si.procs);
"intr 0\nctxt 0\nbtime %lld\nprocesses 0\nprocs_running 1\nprocs_blocked 0\nsoftirq 0 0 0 0 0 0 0 0 0 0 0\n",
btime);
if (k < 0 || (size_t)k >= cap - len) return 0;
return len + (size_t)k;
}
case SYNTH_PROC_LOADAVG: {
(void)sysinfo(&si);
double scale = (double)(1 << SI_LOAD_SHIFT);
n = snprintf(buf, cap, "%.2f %.2f %.2f 1/%u %d\n", si.loads[0] / scale, si.loads[1] / scale,
si.loads[2] / scale, si.procs ? (unsigned)si.procs : 1u, (int)getpid());
// In hundredths, without the floating-point formats: a locale with a decimal comma would
// turn them into "1,50", which no reader of /proc expects
unsigned long l[3];
for (int i = 0; i < 3; i++) {
l[i] = (unsigned long)(((unsigned long long)si.loads[i] * 100ULL + (1ULL << (SI_LOAD_SHIFT - 1))) >> SI_LOAD_SHIFT);
}
n = snprintf(buf, cap, "%lu.%02lu %lu.%02lu %lu.%02lu 1/%u %d\n", l[0] / 100, l[0] % 100, l[1] / 100, l[1] % 100,
l[2] / 100, l[2] % 100, si.procs ? (unsigned)si.procs : 1u, (int)getpid());
break;
}
case SYNTH_PROC_VMSTAT: {
Expand Down Expand Up @@ -550,15 +555,15 @@ int openat2(int dirfd, const char *pathname, cortex_open_how_arg_t how, size_t u
if (orig_openat2) {
int ret = orig_openat2(dirfd, target, how, usize);
if (ret < 0 && how && (how->flags & O_ACCMODE) == O_RDONLY &&
is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) {
!in_root && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) {
return open_synthetic_proc_version(pathname, (int)(how->flags & 0xffffffffu));
}
if (ret >= 0 || errno != ENOSYS) return ret;
}
#ifdef __NR_openat2
long sret = call_orig_syscall(__NR_openat2, (unsigned long)dirfd, (unsigned long)target, (unsigned long)how, (unsigned long)usize, 0, 0);
if (sret < 0 && how && (how->flags & O_ACCMODE) == O_RDONLY &&
is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) {
!in_root && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) {
return open_synthetic_proc_version(pathname, (int)(how->flags & 0xffffffffu));
}
if (sret >= 0 || errno != ENOSYS) {
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/cpp/hook/ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,14 @@ struct if_nameindex *if_nameindex(void) {
if (cx_if_name_flags(sock, idx[i], name, &flags) != 0) continue;
res[k].if_index = (unsigned int)idx[i];
res[k].if_name = strdup(name);
if (!res[k].if_name) break;
if (!res[k].if_name) {
res[k].if_index = 0;
for (int j = 0; j < k; j++) free(res[j].if_name);
free(res);
close(sock);
errno = ENOBUFS;
return NULL;
}
k++;
}
close(sock);
Expand Down
68 changes: 43 additions & 25 deletions app/src/main/cpp/hook/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -1031,11 +1031,19 @@ int getprotobynumber_r(int proto, struct protoent *ret, char *buf, size_t buflen

// The port of a service name for getaddrinfo(): from Cortex's /etc/services, for the protocol of the
// socket type (tcp, then udp, for any). -1 when unknown.
static int rootfs_service_port(const char *service, int socktype) {
static int rootfs_service_port(const char *service, int socktype, int protocol) {
struct servent se;
char buf[CX_SERVENT_BUF];
const char *protos[2] = { socktype == SOCK_DGRAM ? "udp" : "tcp", socktype == 0 ? "udp" : NULL };
for (int i = 0; i < 2 && protos[i]; i++) {
// The transport the hints ask for: the socket type's, else the protocol's, else tcp then udp
const char *protos[2] = { "tcp", "udp" };
int count = 2;
if (socktype == SOCK_DGRAM || (socktype == 0 && protocol == IPPROTO_UDP)) {
protos[0] = "udp";
count = 1;
} else if (socktype != 0 || protocol == IPPROTO_TCP) {
count = 1;
}
for (int i = 0; i < count; i++) {
if (cx_fill_servent(service, 0, protos[i], &se, buf, sizeof(buf)) == 0) return ntohs((uint16_t)se.s_port);
}
return -1;
Expand Down Expand Up @@ -1104,31 +1112,41 @@ static int rootfs_hosts_addrinfo(int (*orig)(const char *, const char *, const s
h.ai_flags |= AI_NUMERICHOST;
struct addrinfo *head = NULL, *tail = NULL;
int last = EAI_NONAME;
for (int i = 0; i < n; i++) {
struct addrinfo *part = NULL;
last = orig(addrs[i], service, &h, &part);
if (last != 0 || !part) {
if (last == EAI_SERVICE || last == EAI_SOCKTYPE || last == EAI_BADFLAGS) break;
continue;
}
if (!head) {
head = part;
if ((hints->ai_flags & AI_CANONNAME) && part->ai_canonname && canon[0]) {
char *c = strdup(canon);
if (c) {
free(part->ai_canonname); // glibc's freeaddrinfo() frees it with free()
part->ai_canonname = c;
// AF_INET6 with AI_V4MAPPED but not AI_ALL: the IPv4 addresses (mapped) only when the name has
// no IPv6 one. So the IPv6 lines are taken first, then the IPv4 ones only if none fitted.
int mapped_if_none = h.ai_family == AF_INET6 && (h.ai_flags & AI_V4MAPPED) && !(h.ai_flags & AI_ALL);
int passes = mapped_if_none ? 2 : 1;
for (int pass = 0; pass < passes && !(pass == 1 && head); pass++) {
for (int i = 0; i < n; i++) {
if (mapped_if_none && (strchr(addrs[i], ':') != NULL) != (pass == 0)) continue;
struct addrinfo *part = NULL;
last = orig(addrs[i], service, &h, &part);
if (last != 0 || !part) {
if (last == EAI_SERVICE || last == EAI_SOCKTYPE || last == EAI_BADFLAGS) {
if (head) freeaddrinfo(head);
return last;
}
continue;
}
} else {
if (part->ai_canonname) {
free(part->ai_canonname);
part->ai_canonname = NULL;
if (!head) {
head = part;
if ((hints->ai_flags & AI_CANONNAME) && part->ai_canonname && canon[0]) {
char *c = strdup(canon);
if (c) {
free(part->ai_canonname); // glibc's freeaddrinfo() frees it with free()
part->ai_canonname = c;
}
}
} else {
if (part->ai_canonname) {
free(part->ai_canonname);
part->ai_canonname = NULL;
}
tail->ai_next = part;
}
tail->ai_next = part;
tail = part;
while (tail->ai_next) tail = tail->ai_next;
}
tail = part;
while (tail->ai_next) tail = tail->ai_next;
}
if (!head) return last == 0 ? EAI_NONAME : last;
*res = head;
Expand Down Expand Up @@ -1162,7 +1180,7 @@ int getaddrinfo(const char *node, const char *service,
(void)strtol(service, &end, 10);
if (end && *end) {
init_cortex_hook();
int port = rootfs_service_port(service, mod_hints.ai_socktype);
int port = rootfs_service_port(service, mod_hints.ai_socktype, mod_hints.ai_protocol);
if (port > 0) {
snprintf(port_text, sizeof(port_text), "%d", port);
service = port_text;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/hook/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static void init_cortex_hook(void) {
size_t rl = strlen(g_cortex_root), cl = strlen(curr_gconv);
set = strncmp(curr_gconv, g_cortex_root, rl) == 0 && strncmp(curr_gconv + rl, "/usr/lib/", 9) == 0 &&
cl > rl + 9 + 6 && strcmp(curr_gconv + cl - 6, "/gconv") == 0 &&
memchr(curr_gconv + rl + 9, '/', cl - rl - 9 - 6) == NULL;
memchr(curr_gconv + rl + 9, '/', cl - rl - 9 - 6) == NULL && strchr(curr_gconv, ':') == NULL;
}
if (set) cortex_libc_setenv("GCONV_PATH", gconv_buf, 1);
}
Expand Down
Loading