fix(flatpak): file dialogs and forwarded descriptors work in Flatpak apps (#3190)

* fix(flatpak): file dialogs and forwarded descriptors work in Flatpak apps

Flatpak apps opened from the desktop could not open files: the file
chooser's result went through a document store that cannot mount on
Android, and image loaders started with flatpak-spawn --forward-fd lost
their D-Bus descriptor because the app relaunches sandboxes and only
stdio survived.

- A session document store hands picked files and folders back in place
  instead of failing without a mount.
- The bwrap shim offers its inherited descriptors once over a private
  socket, and the native hook puts them back on their original numbers
  before the program starts.
- The native hook resolves /proc/<pid>/root paths to the other
  process's sandbox, so xdg-desktop-portal reads .flatpak-info and
  identifies the calling app.

* fix(flatpak): check documents against the app's view and harden fd forwarding

The document store hands a picked file back in place only when the
calling app's own sandbox can open it with the requested access, so
paths the app cannot see are refused instead of handed out broken.
/proc/<pid>/root lookups follow links inside that process's view,
duplicated root descriptors keep their mapping, and close_range drops
stale records. Forwarded descriptors survive targets at the fd limit,
truncated offers are ignored, and the shim refuses more descriptors
than one message can carry.

* fix(flatpak): match picked documents by file identity and keep O_NOFOLLOW on process roots

The document store accepts an in-place path only when the app's view
holds the same file (device and inode) as the picked descriptor, and
reads each process root once. A bare /proc/<pid>/root opened with
O_NOFOLLOW is no longer mapped, so it fails like the kernel's link.

* Keep a trailing-slash process root translated and track only directory roots
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-25 03:03:03 -05:00
1 parent 19d8819137
commit 953549704a
14 files changed
+1336 -7

No files matched your search

+286
View File
@@ -136,7 +136,95 @@ static void init_ddnatv_hook(void) {
// time it was read.
static void compact_loader_args(int argc, char **argv);
#define DDNATV_MAX_FORWARDED_FDS 253
static int forwarded_targets(char *text, int *targets, int max) {
int count = 0;
for (char *p = text; *p && count < max;) {
char *end;
long fd = strtol(p, &end, 10);
if (end == p) break;
targets[count++] = fd >= 0 && fd <= INT_MAX ? (int)fd : -1;
p = *end == ',' ? end + 1 : end;
}
return count;
}
static int is_forwarded_target(int fd, const int *targets, int wanted) {
for (int i = 0; i < wanted; i++) {
if (targets[i] == fd) return 1;
}
return 0;
}
static void adopt_forwarded_fds(void) {
const char *name = getenv("DROIDDECK_FDS");
if (!name) return;
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
size_t len = strlen(name);
int usable = len > 0 && len + 1 <= sizeof(addr.sun_path);
if (usable) memcpy(addr.sun_path + 1, name, len);
unsetenv("DROIDDECK_FDS");
if (!usable) return;
int sock = (int)syscall(SYS_socket, AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sock < 0) return;
static char text[4096];
static int got[DDNATV_MAX_FORWARDED_FDS];
int count = 0;
int truncated = 0;
ssize_t n = -1;
socklen_t addr_len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + 1 + len);
if (syscall(SYS_connect, sock, &addr, addr_len) == 0) {
static union {
struct cmsghdr header;
char buf[CMSG_SPACE(sizeof(got))];
} control;
struct iovec iov = {text, sizeof(text) - 1};
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control.buf;
msg.msg_controllen = sizeof(control.buf);
do {
n = syscall(SYS_recvmsg, sock, &msg, MSG_CMSG_CLOEXEC);
} while (n < 0 && errno == EINTR);
truncated = n >= 0 && (msg.msg_flags & (MSG_CTRUNC | MSG_TRUNC)) != 0;
for (struct cmsghdr *c = n >= 0 ? CMSG_FIRSTHDR(&msg) : NULL; c; c = CMSG_NXTHDR(&msg, c)) {
if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_RIGHTS) continue;
size_t k = (c->cmsg_len - CMSG_LEN(0)) / sizeof(int);
for (size_t i = 0; i < k && count < DDNATV_MAX_FORWARDED_FDS; i++) {
memcpy(&got[count++], CMSG_DATA(c) + i * sizeof(int), sizeof(int));
}
}
}
syscall(SYS_close, sock);
text[n > 0 ? n : 0] = '\0';
static int targets[DDNATV_MAX_FORWARDED_FDS];
int wanted = truncated ? 0 : forwarded_targets(text, targets, DDNATV_MAX_FORWARDED_FDS);
if (wanted != count) wanted = 0;
static int parked[DDNATV_MAX_FORWARDED_FDS];
int n_parked = 0;
for (int i = 0; i < count; i++) {
int fd = got[i];
while (fd >= 0 && is_forwarded_target(fd, targets, wanted) && n_parked < DDNATV_MAX_FORWARDED_FDS) {
parked[n_parked++] = fd;
fd = (int)syscall(SYS_fcntl, fd, F_DUPFD_CLOEXEC, 3);
}
got[i] = is_forwarded_target(fd, targets, wanted) ? -1 : fd;
}
for (int i = 0; i < n_parked; i++) syscall(SYS_close, parked[i]);
for (int i = 0; i < count; i++) {
if (got[i] < 0) continue;
if (i < wanted && targets[i] > 2) syscall(SYS_dup3, got[i], targets[i], 0);
syscall(SYS_close, got[i]);
}
}
__attribute__((constructor(103))) static void init_ddnatv_hook_early(int argc, char **argv) {
adopt_forwarded_fds();
init_ddnatv_hook();
compact_loader_args(argc, argv);
// The kernel names the process after what it executed, the rootfs ld.so: process monitors, and
@@ -280,6 +368,200 @@ static int proc_env_value(pid_t pid, const char *key, char *out, size_t outsz) {
return found;
}
static pid_t proc_root_pid(const char *path, const char **rest) {
if (!path || strncmp(path, "/proc/", 6) != 0 || path[6] < '1' || path[6] > '9') return 0;
const char *p = path + 6;
long pid = 0;
while (*p >= '0' && *p <= '9') {
pid = pid * 10 + (*p++ - '0');
if (pid > 0x3fffffff) return 0;
}
if (strncmp(p, "/root", 5) != 0 || (p[5] != '\0' && p[5] != '/')) return 0;
*rest = p + 5;
return (pid_t)pid;
}
static int proc_view_of(pid_t pid, char **root, char **binds) {
char path[64];
snprintf(path, sizeof(path), "/proc/%d/environ", (int)pid);
size_t used;
int whole;
*root = NULL;
*binds = NULL;
char *env = read_proc_file(path, &used, &whole);
if (!env) return 0;
for (char *e = env; e < env + used; e += strlen(e) + 1) {
if (!*root && strncmp(e, "DDNATV_ROOT=", 12) == 0 && e[12] == '/') *root = strdup(e + 12);
else if (!*binds && strncmp(e, "DDNATV_BINDS=", 13) == 0) *binds = strdup(e + 13);
}
free(env);
if (*root) return 1;
free(*binds);
*binds = NULL;
return 0;
}
static const char *proc_view_path(const char *root, char *binds, const char *guest, char *buf, size_t size) {
const char *best_src = NULL, *best_dst = NULL;
size_t best_len = 0;
char *save = NULL;
for (char *entry = binds ? strtok_r(binds, "|", &save) : NULL; entry; entry = strtok_r(NULL, "|", &save)) {
char *colon = strchr(entry, ':');
const char *dst = colon ? colon + 1 : entry;
if (colon) *colon = '\0';
size_t len = strlen(dst);
while (len > 1 && dst[len - 1] == '/') len--;
if (entry[0] != '/' || dst[0] != '/' || len < 2 || len <= best_len || !is_path_prefix(guest, dst, len)) continue;
best_src = entry;
best_dst = dst;
best_len = len;
}
if (best_src && strncmp(best_src, best_dst, best_len) == 0 && best_src[best_len] == '\0') {
ddnatv_copy_cstr(buf, size, guest);
} else if (best_src) {
size_t n = strncmp(best_src, "/proc/", 6) == 0 && best_src[6] >= '0' && best_src[6] <= '9'
? ddnatv_copy_cstr(buf, size, best_src)
: ddnatv_kernel_path(best_src, strlen(best_src), buf, size);
ddnatv_append(buf, size, n, guest + best_len, strlen(guest + best_len));
} else if (is_android_host_path(guest)) {
ddnatv_copy_cstr(buf, size, guest);
} else {
size_t n = ddnatv_copy_cstr(buf, size, root);
if (guest[1]) ddnatv_append(buf, size, n, guest, strlen(guest));
}
return buf;
}
static const char *proc_view_host(const char *root, const char *binds, const char *guest, char *buf, size_t size) {
char *copy = binds ? strdup(binds) : NULL;
proc_view_path(root, copy, guest, buf, size);
free(copy);
return buf;
}
static const char *proc_root_host(const char *path, char *buf, size_t size, int bare, int follow) {
const char *rest = NULL;
pid_t pid = proc_root_pid(path, &rest);
if (pid <= 0 || (!bare && rest[0] == '\0')) return NULL;
char *root, *binds;
if (!proc_view_of(pid, &root, &binds)) return NULL;
size_t root_len = strlen(root);
while (root_len > 1 && root[root_len - 1] == '/') root_len--;
char root_k[PATH_MAX], guest[PATH_MAX];
ddnatv_kernel_path(root, root_len, root_k, sizeof(root_k));
ddnatv_copy_cstr(guest, sizeof(guest), rest[0] ? rest : "/");
normalize_path(guest);
if (strcmp(root_k, g_ddnatv_root) == 0 && strcmp(binds ? binds : "", g_binds_raw ? g_binds_raw : "") == 0) {
const char *own = (follow ? rewrite_path_follow : rewrite_path_in_root)(guest, buf, size);
if (own != buf) ddnatv_copy_cstr(buf, size, own);
} else {
proc_view_host(root_k, binds, guest, buf, size);
#if defined(__aarch64__)
for (int depth = 0; follow && depth < 8 && strncmp(buf, "/proc/", 6) != 0; depth++) {
char target[PATH_MAX];
long n = raw_readlinkat(AT_FDCWD, buf, target, sizeof(target) - 1);
if (n <= 0) break;
target[n] = '\0';
char joined[PATH_MAX];
if (target[0] == '/') {
ddnatv_copy_cstr(joined, sizeof(joined), target);
} else {
const char *slash = strrchr(guest, '/');
size_t used = ddnatv_append(joined, sizeof(joined), 0, guest, slash ? (size_t)(slash - guest) : 0);
used = ddnatv_append(joined, sizeof(joined), used, "/", 1);
ddnatv_append(joined, sizeof(joined), used, target, strlen(target));
}
normalize_path(joined);
ddnatv_copy_cstr(guest, sizeof(guest), joined);
proc_view_host(root_k, binds, guest, buf, size);
}
#endif
}
free(root);
free(binds);
return buf;
}
struct ddnatv_proc_root_fd {
int fd;
pid_t pid;
unsigned long dev, ino;
};
static struct ddnatv_proc_root_fd g_proc_root_fds[32];
static int g_n_proc_root_fds = 0;
static pthread_mutex_t g_proc_root_mutex = PTHREAD_MUTEX_INITIALIZER;
static void forget_proc_root_range(unsigned int first, unsigned int last) {
if (__atomic_load_n(&g_n_proc_root_fds, __ATOMIC_ACQUIRE) == 0) return;
pthread_mutex_lock(&g_proc_root_mutex);
for (int i = 0; i < g_n_proc_root_fds;) {
unsigned int fd = (unsigned int)g_proc_root_fds[i].fd;
if (fd >= first && fd <= last) g_proc_root_fds[i] = g_proc_root_fds[--g_n_proc_root_fds];
else i++;
}
pthread_mutex_unlock(&g_proc_root_mutex);
}
static void forget_proc_root_fd(int fd) {
if (fd >= 0) forget_proc_root_range((unsigned int)fd, (unsigned int)fd);
}
static void store_proc_root_fd(struct ddnatv_proc_root_fd entry) {
pthread_mutex_lock(&g_proc_root_mutex);
int slot = 0;
while (slot < g_n_proc_root_fds && g_proc_root_fds[slot].fd != entry.fd) slot++;
if (slot == g_n_proc_root_fds && slot == (int)(sizeof(g_proc_root_fds) / sizeof(g_proc_root_fds[0]))) slot = 0;
g_proc_root_fds[slot] = entry;
if (slot == g_n_proc_root_fds) __atomic_store_n(&g_n_proc_root_fds, g_n_proc_root_fds + 1, __ATOMIC_RELEASE);
pthread_mutex_unlock(&g_proc_root_mutex);
}
static void copy_proc_root_fd(int oldfd, int newfd) {
if (newfd < 0 || oldfd == newfd) return;
forget_proc_root_fd(newfd);
if (oldfd < 0 || __atomic_load_n(&g_n_proc_root_fds, __ATOMIC_ACQUIRE) == 0) return;
struct ddnatv_proc_root_fd entry = {-1, 0, 0, 0};
pthread_mutex_lock(&g_proc_root_mutex);
for (int i = 0; i < g_n_proc_root_fds; i++) {
if (g_proc_root_fds[i].fd == oldfd) entry = g_proc_root_fds[i];
}
pthread_mutex_unlock(&g_proc_root_mutex);
if (entry.fd < 0) return;
entry.fd = newfd;
store_proc_root_fd(entry);
}
static void remember_proc_root_fd(int fd, const char *path) {
const char *rest = NULL;
pid_t pid = proc_root_pid(path, &rest);
struct stat st;
if (fd < 0) return;
if (pid <= 0 || rest[strspn(rest, "/")] != '\0' || syscall(SYS_fstat, fd, &st) != 0 || !S_ISDIR(st.st_mode)) {
forget_proc_root_fd(fd);
return;
}
store_proc_root_fd((struct ddnatv_proc_root_fd){fd, pid, (unsigned long)st.st_dev, (unsigned long)st.st_ino});
}
static const char *proc_root_at(int dirfd, const char *rel, char *buf, size_t size) {
if (dirfd < 0 || __atomic_load_n(&g_n_proc_root_fds, __ATOMIC_ACQUIRE) == 0) return NULL;
pid_t pid = 0;
unsigned long dev = 0, ino = 0;
pthread_mutex_lock(&g_proc_root_mutex);
for (int i = 0; i < g_n_proc_root_fds; i++) {
if (g_proc_root_fds[i].fd != dirfd) continue;
pid = g_proc_root_fds[i].pid;
dev = g_proc_root_fds[i].dev;
ino = g_proc_root_fds[i].ino;
break;
}
pthread_mutex_unlock(&g_proc_root_mutex);
struct stat st;
if (pid <= 0 || syscall(SYS_fstat, dirfd, &st) != 0 || (unsigned long)st.st_dev != dev || (unsigned long)st.st_ino != ino) return NULL;
if (snprintf(buf, size, "/proc/%d/root/%s", (int)pid, rel) >= (int)size) return NULL;
return buf;
}
static int is_loader_option(const char *arg) {
return strcmp(arg, "--preload") == 0 || strcmp(arg, "--library-path") == 0 || strcmp(arg, "--argv0") == 0;
}
@@ -475,6 +757,10 @@ static const char *rewrite_path_follow(const char *path, char *buffer, size_t bu
if (!path) return NULL;
if (path[0] != '/') return rewrite_path(path, buffer, bufsize);
init_ddnatv_hook();
if (path[1] == 'p' && strncmp(path, "/proc/", 6) == 0 && path[6] >= '1' && path[6] <= '9') {
const char *other = proc_root_host(path, buffer, bufsize, 0, 1);
if (other) return other;
}
int moved = 0;
for (int i = 0; i < g_nbinds; i++) {
const struct ddnatv_bind *b = &g_binds[i];