Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp949753rwb; Tue, 4 Oct 2022 13:03:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7IBgtA7FxFKhZ2EcHjKC15RNxiRf4MXyWydHOG7CViMMFUwu2cEskq4SFBn9PkppHP7sd+ X-Received: by 2002:a17:90b:2644:b0:1fa:e0be:4dcb with SMTP id pa4-20020a17090b264400b001fae0be4dcbmr1286208pjb.85.1664913837671; Tue, 04 Oct 2022 13:03:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664913837; cv=none; d=google.com; s=arc-20160816; b=uQGb0kZ9f7U7Xeh+3D8ROkGdz3elcMDGG/bHhdbOgC5KaeFSmaNMYvPqJTdYFDN4S/ ERlbp//IW0DF2xJpYZuTafDNxBlte1JTVKknFl/AeXrpb+ZfAF4mFgClb50NRuGnk7IM Zlq+mdPSU85h1in6AajXlXX35JWVIQGoiXyREJ0T3oYj98rZO3ty7/zsFxy5pxW/d0Bl 9CeM14TXxtJzP+vDo3FP2gUeYefx5wPGH4dfue2BNbk860Ss8FtWDzi0zU/E8xvOOfP5 MI/mWJbYZdmA1udXP8qaloZcoPRJBhNxmgpsA6hI3n6Fbl8jXzWQe49SMopfFVE0ck1A zimA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=xjSo2INdaTUjWFOLlUFz9Rasj3EDdp7SsPRO7zAKVVs=; b=Phn5EegjY2N6KPZ3MPwVQ/4NuBoLrYZPEYGJu/7fmE9wGqs8auyfzjt3d4nEDv6SD8 v1gPRKkJEM5C+Z4xVN8u0iP7PnYNyNXFWEpA4DK8P0Ih8n1MHa1zhjBzSx21/FIoV41b 59STaam0Ls9rgLjDByc6g8cHy7nhcL7NC73uG+OTarNyzXDXAnaGivNTh5Rk+eSA8cZb MERaST3oPmhH1yMWDdEVK3qP5g49laPRj7qzenv3OOSAGlGyFUemLLfRwVnlHVRb2wVX ZEITm4U3IyxZ2fVuBjFquS5CVagie0PBZbYS3/B4cBeeGuV/p3yCyqpTIXsujlu208zo aqRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AA++EJmO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j15-20020a170903024f00b00174b83659fasi13137630plh.502.2022.10.04.13.03.44; Tue, 04 Oct 2022 13:03:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AA++EJmO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229716AbiJDT2P (ORCPT + 99 others); Tue, 4 Oct 2022 15:28:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229715AbiJDT2N (ORCPT ); Tue, 4 Oct 2022 15:28:13 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0299696D2 for ; Tue, 4 Oct 2022 12:28:11 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id r18so398099pgr.12 for ; Tue, 04 Oct 2022 12:28:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=xjSo2INdaTUjWFOLlUFz9Rasj3EDdp7SsPRO7zAKVVs=; b=AA++EJmO9JQQ6GdYSvziU2tKo81E6app2ZDepTKSIa/VguooGRqwVI5G2F851r0WpO L8yGm6c/8lN9gfZmKfAIWb4cjc15A8YKbAeXGUiyBIN9TldTglijNwWIqlV/U+mALKe2 cPNWygdNgkXFxnhba14nlGPOoPwjndmJqGlAc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=xjSo2INdaTUjWFOLlUFz9Rasj3EDdp7SsPRO7zAKVVs=; b=kxUTr/mjLbB4yjTMFIkTuKWNdzJm5cX9BQMg7EoLnLY/n3m3/FEwxliBKIF0vpAjJK vMlQnxo/1IC22d3JJ0OPv7OKElFHP0GZA+SEMCGXp9yd9Q6uCc1+XlplNmAfQGx9If71 Kk89sIi6dCmHyrJ3VF6dLCoaVQ5C5NS0w1Uk2cJR2wocaD4Dh5zNGq4xbiND9o0+dPlv TVGR5GK9z08I/l+38jhZP9BPl61wWFzZ0meoRu3l3SnSrwWzikZPKK6FkowRqMO9r+Vk vAH5kIaRQhDgtBEoUotFwRne+/pGbJVeJik0ttFkihTD7NvlcTP3OlX+oBo2Zr7fUaAD brkA== X-Gm-Message-State: ACrzQf2inU4rPCZsy/bI6fpQRb0sHZmhnBACvxtseODhWa6v1ZQmHkgy uu14Zp8/nmR0ZigBSJ1riDpdvg== X-Received: by 2002:a63:b4d:0:b0:454:d8b4:285 with SMTP id a13-20020a630b4d000000b00454d8b40285mr4531547pgl.410.1664911691390; Tue, 04 Oct 2022 12:28:11 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n3-20020a622703000000b0053e85a4a2c9sm6115840pfn.5.2022.10.04.12.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 12:28:10 -0700 (PDT) Date: Tue, 4 Oct 2022 12:28:09 -0700 From: Kees Cook To: David Laight Cc: Jann Horn , Rick Edgecombe , "x86@kernel.org" , "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , "linux-kernel@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-mm@kvack.org" , "linux-arch@vger.kernel.org" , "linux-api@vger.kernel.org" , Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jonathan Corbet , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , "joao.moreira@intel.com" , John Allen , "kcc@google.com" , "eranian@google.com" , "rppt@kernel.org" , "jamorris@linux.microsoft.com" , "dethoma@microsoft.com" Subject: Re: [PATCH v2 00/39] Shadowstacks for userspace Message-ID: <202210041224.8B7071AAD@keescook> References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <202210030946.CB90B94C11@keescook> <202210032158.CE0941C4D@keescook> <752d296b68f04c2282bd898cf7ec3f0f@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <752d296b68f04c2282bd898cf7ec3f0f@AcuMS.aculab.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 04, 2022 at 09:57:48AM +0000, David Laight wrote: > From: Kees Cook > ... > > > > > > If you don't want /proc/$pid/mem to be able to do stuff like that, > > > then IMO the way to go is to change when /proc/$pid/mem uses > > > FOLL_FORCE, or to limit overall write access to /proc/$pid/mem. > > > > Yeah, all reasonable. I just wish we could ditch FOLL_FORCE; it continues > > to weird me out how powerful that fd's side-effects are. > > Could you remove FOLL_FORCE from /proc/$pid/mem and add a > /proc/$pid/mem_force that enable FOLL_FORCE but requires root > (or similar) access. > > Although I suspect gdb may like to have write access to > code? As Jann has reminded me out of band, while FOLL_FORCE is still worrisome, it's really /proc/$pid/mem access at all without an active ptrace attachment (and to self). Here's my totally untested idea to require access to /proc/$pid/mem having an established ptrace relationship: diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h index c952c5ba8fab..0393741eeabb 100644 --- a/include/linux/ptrace.h +++ b/include/linux/ptrace.h @@ -64,6 +64,7 @@ extern void exit_ptrace(struct task_struct *tracer, struct list_head *dead); #define PTRACE_MODE_NOAUDIT 0x04 #define PTRACE_MODE_FSCREDS 0x08 #define PTRACE_MODE_REALCREDS 0x10 +#define PTRACE_MODE_ATTACHED 0x20 /* shorthands for READ/ATTACH and FSCREDS/REALCREDS combinations */ #define PTRACE_MODE_READ_FSCREDS (PTRACE_MODE_READ | PTRACE_MODE_FSCREDS) diff --git a/fs/proc/base.c b/fs/proc/base.c index 93f7e3d971e4..fadec587d133 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -826,7 +826,7 @@ static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) static int mem_open(struct inode *inode, struct file *file) { - int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); + int ret = __mem_open(inode, file, PTRACE_MODE_ATTACHED); /* OK to pass negative loff_t, we can catch out-of-range */ file->f_mode |= FMODE_UNSIGNED_OFFSET; diff --git a/kernel/ptrace.c b/kernel/ptrace.c index 1893d909e45c..c97e6d734ae5 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -304,6 +304,12 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode) * or halting the specified task is impossible. */ + /* + * If an existing ptrace relationship is required, not even + * introspection is allowed. + */ + if ((mode & PTRACE_MODE_ATTACHED) && ptrace_parent(task) != current) + return -EPERM; /* Don't let security modules deny introspection */ if (same_thread_group(task, current)) return 0; -- Kees Cook