From nobody Mon Nov 23 04:44:04 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1792512341; cv=none; d=zohomail.com; s=zohoarc; b=mKmSGpQhbnlIg/4YYR/CFE7h1P8vClvZWU7Un+3dFzzQBdh7fNPZNFJEBGo7ItA7RHcrKCeQhC4etDgpXb1ios237Qn6W9sYQQjuzjBQ98Bqj0lDDoUR5SdnyCpFLAo168zOOAFs0xswus9KiMkLmTpIrcIY8pNiZAwH4IpWPzM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792512341; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WBvSHQCpkwhScUzVRurv8qSM38o96lgMwhMXnuYj2Vc=; b=VgxVlQGSIfOafYAhJoPV77R1phW1LgmJcKs4y2BR2mcd1srMhzUe/MvpxTcizIAgHZBjssh43YB7f2wXaGDc7Na0Oi0orw0rChh5w7KA2L2bNgEJMGwq0Rn3WklgQZg07Vky/cs8HbTiB5tMtifU6hyVOaHzg5kslgVq83sIfrs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792512341224267.5786671476077; Tue, 20 Oct 2026 09:05:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xJCIr-0000jx-GE; Tue, 20 Oct 2026 12:03:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xJB4b-0001j6-HJ for qemu-devel@nongnu.org; Tue, 20 Oct 2026 10:44:17 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xJB4W-0002CN-8Z for qemu-devel@nongnu.org; Tue, 20 Oct 2026 10:44:16 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2e9d1114320so17935145ad.3 for ; Tue, 20 Oct 2026 07:44:10 -0700 (PDT) Received: from a1-free ([161.33.196.121]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ea151840c4sm12978845ad.67.2026.10.20.07.44.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Oct 2026 07:44:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792507449; x=1793112249; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=WBvSHQCpkwhScUzVRurv8qSM38o96lgMwhMXnuYj2Vc=; b=r2r4A2cKzWdz4zWNYBzfrMCwuItfEaL/Mxi1jytlhO/s9iW79jmOPDeIzhQ4C9eepj w/O22zUkGylrwUrMwZ0QDIkuLH/ALbhpGtreY38NHmAKLU6qJ7IIthK9muZ1W0bdZszD LU4n5ZlVZ/CkVcpSkq/AkITuFH3LTOBfmud2KJdqjSqmii4oIZoh4su1fAeb2rDIl1fg hSarjZ+jdeww2RvstX32NhRhAVBr/xOasnM5JrQp0ygUtYHeN5bTPTeeomGfpy3i2u+i TQKm/wdUca0VyTVc90KbwOfhmtLAFBjoYYn03lKrPwdLPH960JoS55rvsRUspRBNJnyH /uVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792507449; x=1793112249; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=WBvSHQCpkwhScUzVRurv8qSM38o96lgMwhMXnuYj2Vc=; b=gnWyo4OmwiU9goWK2OoNIjyO5jR2o6ENlozSpJAEwlvVmkiqmwlAVG2dg8/2CKu6t7 nPux4hFrfczsO/Fpfd7iev1Nryz9cVDiVGJeekOIJNwA6S7rRMOnZQGT96ot2pA4+gd7 BWTdIznkwn7b2W9hj6iKOy8xrWUgI/2t6RN6e0Uj9iNLZeMweodTRCmm+8cq76LdMnv4 CCcMWYmCk1Cr50BpO3F3ucgg9Ry8uwr1z5IUNVXSYOdc5KEDe+p927FriomhdU+AOC9P JlnQDmhPks4pisSUwI+kx/gY0Rtl+05NL3fmaN6Anll/nnuDo75R6sgCq33O9jbPxr1K 5aLg== X-Gm-Message-State: AFq9FYIn8PjmLGmN78zT0qYvwQXRBw2GlK3L6zALFfY1DBE5ss35IXES g0OyC8ZwD6cIppjfPymSfV7hqhlWW59tBEhnFY4QDu5TZXRX5tP9ugI6YRSUig== X-Gm-Gg: AYBFou32lC31qbHocJ5951qFbBU7A0EnYMjDw6ss3pta/+bhCfT8XtZG6vxzDjQDHmS gP8dSlCv3xUeTRCL0zTZaOtefqG9p775GEojuUmzSMX90NbIlBc8n4FZXoD2jHJmIFWRozBDYqL GPmpzn/nt2CY3TwyNGnRnH1UIiB0pyz74dhZObv+n9DOHcN4iDNkerVDNnVnBZ3ikts/4xo/SpA kP+fIlWEElm36/j32HhxYoq7W57EHPzHLqf2j4JbHHGUtSHeiqBXvaCvrG50lw1Aj8Qg/FO6IoU c+Jrj9H+wYFSdI0nj/1KhsIJylzXXm0UjPqI663hEcFuGYo2Ent6YJLi2UPQIql4oklYPC0sPIC 7suVMfH6NZzXiownqgkhhT/8lnR2UL4HrfL7vJJPPiGajjX5aJoKmVuDoEem+b/v2PoxNhMWxZ5 QfksIVB4Fousvhu42ZGihMdE2ElNo+/ZNEQAXYCv15dgjtGwhysHsF9It8AA4AlA== X-Received: by 2002:a17:902:ef50:b0:2e2:c3a2:9b15 with SMTP id d9443c01a7336-2e9a6833d93mr86310975ad.6.1792507449148; Tue, 20 Oct 2026 07:44:09 -0700 (PDT) From: takano32 To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, deller@gmx.de, pierrick.bouvier@oss.qualcomm.com, takano32 Subject: [PATCH] linux-user/i386: reject bad CS/SS selectors on rt_sigreturn Date: Tue, 20 Oct 2026 14:44:06 +0000 Message-ID: <20261020144406.2542302-1-takano32@gmail.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=takano32@gmail.com; helo=mail-pl1-x636.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 20 Oct 2026 12:01:57 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1792512343971158500 Content-Type: text/plain; charset="utf-8" restore_sigcontext() handed cs/ss straight to cpu_x86_load_seg(). In protected mode that ends in helper_load_seg(), which raises #GP for an invalid selector and calls cpu_loop_exit_restore() -- a longjmp out of the syscall handler. Landing in cpu_exec() that way trips g_assert(cpu =3D=3D current_cpu) in cpu_exec_longjmp_cleanup() and aborts the whole emulator. A guest that builds its own signal frame and executes rt_sigreturn (SROP) leaves cs/ss zeroed, so this is easy to hit: $ qemu-x86_64 ./hello_srop < payload # frame with cs=3D0, ss=3D0 ERROR:accel/tcg/cpu-exec.c:515:cpu_exec_longjmp_cleanup: assertion failed: (cpu =3D=3D current_cpu) Aborted x86 Linux does not validate the selectors either: restore_sigcontext() only does regs->cs =3D sc.cs | 0x03; regs->ss =3D sc.ss | 0x03; and a bogus value matters only on the way back to user mode, where it costs the task and not the kernel. QEMU cannot be that casual, because the selector goes through cpu_x86_load_seg() while the syscall handler is still running. So take the existing badframe path instead: accept the selectors the CPU is already running with and return false for anything else, which makes do_rt_sigreturn() raise SIGSEGV in the guest. Tested with an x86_64 guest on an aarch64 host, against qemu master plus this patch: a frame with cs=3D0x33, ss=3D0x2b restores and exits with the requested code; a frame with cs=3D0, ss=3D0 now raises SIGSEGV in the guest instead of aborting qemu (unpatched qemu 10.2.1 aborts with the assertion above). Signed-off-by: takano32 --- linux-user/i386/signal.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/linux-user/i386/signal.c b/linux-user/i386/signal.c index 9650fac940..01d54be310 100644 --- a/linux-user/i386/signal.c +++ b/linux-user/i386/signal.c @@ -775,8 +775,32 @@ static bool restore_sigcontext(CPUX86State *env, struc= t target_sigcontext *sc) env->eip =3D tswapl(sc->rip); #endif =20 - cpu_x86_load_seg(env, R_CS, lduw_le_p(&sc->cs) | 3); - cpu_x86_load_seg(env, R_SS, lduw_le_p(&sc->ss) | 3); + /* + * Validate the selectors before loading them. + * + * In protected mode cpu_x86_load_seg() ends up in helper_load_seg(), + * which raises #GP for a bad selector and calls cpu_loop_exit_restore= (). + * That longjmps out of the syscall handler while cpu_exec_longjmp_cle= anup() + * still expects the CPU it started with, so + * + * g_assert(cpu =3D=3D current_cpu) + * + * fires and the whole emulator aborts. A guest that builds its own + * signal frame and executes rt_sigreturn (SROP) leaves cs/ss zeroed, + * so this is trivial to hit. Linux treats such a frame as a bad frame + * and delivers SIGSEGV, so do the same and take the badframe path. + */ + { + uint16_t cs =3D lduw_le_p(&sc->cs) | 3; + uint16_t ss =3D lduw_le_p(&sc->ss) | 3; + + if (cs !=3D (env->segs[R_CS].selector | 3) || + ss !=3D (env->segs[R_SS].selector | 3)) { + return false; + } + cpu_x86_load_seg(env, R_CS, cs); + cpu_x86_load_seg(env, R_SS, ss); + } =20 tmpflags =3D tswapl(sc->eflags); env->eflags =3D (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5); --=20 2.53.0