// SPDX-License-Identifier: GPL-3.0-or-later // Unprivileged Linux containment. A preview has no device, network, writable // filesystem or process-execution authority. Fail closed if the kernel lacks it. #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include inline void confineQml(const QString &root) { if (syscall(SYS_landlock_create_ruleset, nullptr, 0, LANDLOCK_CREATE_RULESET_VERSION) < 3) throw std::runtime_error("QML preview needs Landlock ABI 3 or later"); const __u64 read = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR; landlock_ruleset_attr attr{}; attr.handled_access_fs = (1ULL << 15) - 1; // ABI 3, including REFER/TRUNCATE int rules = syscall(SYS_landlock_create_ruleset, &attr, sizeof(attr), 0); if (rules < 0) throw std::runtime_error("Cannot create QML filesystem boundary"); const QStringList paths{root, "/usr/share/fonts", "/etc/fonts", "/etc/localtime", "/dev/urandom"}; for (const auto &path : paths) { int fd = open(path.toLocal8Bit().constData(), O_PATH | O_CLOEXEC); if (fd < 0) { if(path == root) {close(rules); throw std::runtime_error("QML runtime missing");} continue; } struct stat st{}; fstat(fd, &st); landlock_path_beneath_attr rule{}; rule.parent_fd=fd; rule.allowed_access = S_ISDIR(st.st_mode) ? read : LANDLOCK_ACCESS_FS_READ_FILE; int result=syscall(SYS_landlock_add_rule,rules,LANDLOCK_RULE_PATH_BENEATH,&rule,0); close(fd); if(result) {close(rules);throw std::runtime_error("Cannot admit QML runtime path");} } if(prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0) || syscall(SYS_landlock_restrict_self,rules,0)) { close(rules);throw std::runtime_error("Cannot restrict QML filesystem"); } close(rules); std::vector filter{ BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(seccomp_data,arch)), BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,AUDIT_ARCH_X86_64,1,0), BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_KILL_PROCESS), BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(seccomp_data,nr)), BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K,0x40000000,0,1), BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_KILL_PROCESS)}; for(int number : {SYS_socket,SYS_connect,SYS_bind,SYS_listen,SYS_accept,SYS_accept4, SYS_execve,SYS_execveat,SYS_fork,SYS_vfork,SYS_ptrace, SYS_process_vm_readv,SYS_process_vm_writev,SYS_mount,SYS_umount2, SYS_bpf,SYS_ioctl,SYS_io_uring_setup,SYS_unshare,SYS_setns}) { filter.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,number,0,1)); filter.push_back(BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ERRNO|EPERM)); } // glibc falls back from clone3 to clone; only thread creation is admitted. filter.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,SYS_clone3,0,1)); filter.push_back(BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ERRNO|ENOSYS)); filter.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K,SYS_clone,0,3)); filter.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS,offsetof(seccomp_data,args[0]))); filter.push_back(BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K,CLONE_THREAD,1,0)); filter.push_back(BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ERRNO|EPERM)); filter.push_back(BPF_STMT(BPF_RET|BPF_K,SECCOMP_RET_ALLOW)); sock_fprog program{static_cast(filter.size()),filter.data()}; if(prctl(PR_SET_SECCOMP,SECCOMP_MODE_FILTER,&program)) throw std::runtime_error("Cannot restrict QML system calls"); }