Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp639579ybz; Wed, 22 Apr 2020 05:17:44 -0700 (PDT) X-Google-Smtp-Source: APiQypKIUjHR5zsIYENd6G8jA/Vp81OIsExELtGozft45GDG82K3P0+wug9dz3X4zgNUqqgETv/a X-Received: by 2002:a50:a68a:: with SMTP id e10mr22799714edc.113.1587557864485; Wed, 22 Apr 2020 05:17:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587557864; cv=none; d=google.com; s=arc-20160816; b=Z0fVNBe5M+JoXpSwBX+B53ir7YKP95gH/pqREab3sqpWWwBb9PZ6Un86RQ2d5GSFgt tpxEC3FKX3ReHT3B4fVP5h5c5re8FsvFxBaQQLL5BoIirhkX6VfBqAJoGa1m7KMk2jPT yWteOLJVPPavNpbHcVTXYTHvs04LpdVWzAzN3BjYfs3igGA2V0/k8MBCvLY23o9/K3zD uqbGSesQ1fmMTjRCzeaF9wGGFChivJiY8IZbQwlJi5zYdIeH1RJPGAjZPCFRyNKPlkMX YaW5H1owSYLlxL6fpsUNFW37DBID3TiWqPuJEwK5f25LOrq2LpfSMWbmRu08fbCa6NCn P+yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pKMNS2Ehf20oXWDXfxQ69pH/Md2qpfHYI8u7rX3RkWs=; b=CaxyndfOvg42BeGZDDkEZXUJ36w2hScaYp4bVf3buYaxIUd/gJPNnuCDN0wvYil3IQ hPy2wggpuS7dQMEY8JH8eUnvULV8cK/iwu4718p1KDU53N6qTbcsEekx0+vQebqp8ED9 CHnApOcgImKcNbVOwBQOAHzcGmhmfhOUPvdCn1iEh4n27rD8lwKFQoLNYliY9elziUtc xITS7tOtroYU+POcwV7Tjk+4USyoIlraKw8gVYtxzfv6eb2jkxXdwfU9oaMw43szAfbl e2ukEvwKFxCz0NRgmI819YQL8iyX8Byk4gzRJX+6KjX3JfRXKjymKlXODLVa6UmBkfRP cg/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=aqGnl+1Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h24si3237733edv.469.2020.04.22.05.17.21; Wed, 22 Apr 2020 05:17:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=aqGnl+1Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726472AbgDVKAO (ORCPT + 99 others); Wed, 22 Apr 2020 06:00:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:47044 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726591AbgDVKAD (ORCPT ); Wed, 22 Apr 2020 06:00:03 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A0B262076C; Wed, 22 Apr 2020 10:00:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587549602; bh=+U3prn+/Xwk6KvRtW9pZv7787tBJrTh6JRlUQJZZSfQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aqGnl+1ZJWRKRvsJqSSS0OhFOPc36S6zQaYLraZyJhGrNTF5KVh01ia9NbbYh2zFX SAzaSNoMHHF6JVYUYr9nGF0inUQhUsuwk5Vdqt0oOZ3v+xiuH9vgn/dh/33QX6GJ/F zuaNyfMa5Xv376UoFtLwiB+isdEZGzkQnLR3MoSU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Eric W. Biederman" Subject: [PATCH 4.4 031/100] signal: Extend exec_id to 64bits Date: Wed, 22 Apr 2020 11:56:01 +0200 Message-Id: <20200422095028.419279027@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200422095022.476101261@linuxfoundation.org> References: <20200422095022.476101261@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric W. Biederman commit d1e7fd6462ca9fc76650fbe6ca800e35b24267da upstream. Replace the 32bit exec_id with a 64bit exec_id to make it impossible to wrap the exec_id counter. With care an attacker can cause exec_id wrap and send arbitrary signals to a newly exec'd parent. This bypasses the signal sending checks if the parent changes their credentials during exec. The severity of this problem can been seen that in my limited testing of a 32bit exec_id it can take as little as 19s to exec 65536 times. Which means that it can take as little as 14 days to wrap a 32bit exec_id. Adam Zabrocki has succeeded wrapping the self_exe_id in 7 days. Even my slower timing is in the uptime of a typical server. Which means self_exec_id is simply a speed bump today, and if exec gets noticably faster self_exec_id won't even be a speed bump. Extending self_exec_id to 64bits introduces a problem on 32bit architectures where reading self_exec_id is no longer atomic and can take two read instructions. Which means that is is possible to hit a window where the read value of exec_id does not match the written value. So with very lucky timing after this change this still remains expoiltable. I have updated the update of exec_id on exec to use WRITE_ONCE and the read of exec_id in do_notify_parent to use READ_ONCE to make it clear that there is no locking between these two locations. Link: https://lore.kernel.org/kernel-hardening/20200324215049.GA3710@pi3.com.pl Fixes: 2.3.23pre2 Cc: stable@vger.kernel.org Signed-off-by: "Eric W. Biederman" Signed-off-by: Greg Kroah-Hartman --- fs/exec.c | 2 +- include/linux/sched.h | 4 ++-- kernel/signal.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) --- a/fs/exec.c +++ b/fs/exec.c @@ -1207,7 +1207,7 @@ void setup_new_exec(struct linux_binprm /* An exec changes our domain. We are no longer part of the thread group */ - current->self_exec_id++; + WRITE_ONCE(current->self_exec_id, current->self_exec_id + 1); flush_signal_handlers(current, 0); } EXPORT_SYMBOL(setup_new_exec); --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1612,8 +1612,8 @@ struct task_struct { struct seccomp seccomp; /* Thread group tracking */ - u32 parent_exec_id; - u32 self_exec_id; + u64 parent_exec_id; + u64 self_exec_id; /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, * mempolicy */ spinlock_t alloc_lock; --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1660,7 +1660,7 @@ bool do_notify_parent(struct task_struct * This is only possible if parent == real_parent. * Check if it has changed security domain. */ - if (tsk->parent_exec_id != tsk->parent->self_exec_id) + if (tsk->parent_exec_id != READ_ONCE(tsk->parent->self_exec_id)) sig = SIGCHLD; }