Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2365026lqo; Mon, 13 May 2024 16:51:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWN4zTCXEDQMxVhqymAZq+V+S5w/ygEyg1wgViWHvcCWgXXlMIJmPibU5c/s3W5shhWxDwn/wYgXRl13HUaJBo/sFZchSq8pITWrrr+lA== X-Google-Smtp-Source: AGHT+IHD4xqcbZceAFbl+K53Ov4Bqgh6klLsWnmbeDhrvWVFFK2inwf6eTaRYXl5NwwzmdKT1nk+ X-Received: by 2002:a17:902:e94d:b0:1e4:a667:550e with SMTP id d9443c01a7336-1ef43e284d5mr127139715ad.20.1715644267664; Mon, 13 May 2024 16:51:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715644267; cv=pass; d=google.com; s=arc-20160816; b=ZWTMAncz/5fl3M2oqLazMb5Qpt/KVY06flb++8VjxrDU4O4SmEqFG98X1w0zsYcjWD JIGjwsOdINhaJXyYSVI4CTmngm0ojjckSzrwO163lTCk3LB4hEbIg8+FEt8jt3h1wH9b h+VhWo9NbrQahv09eT697URugMZDxyekPoNqjTo2D3qWYShPFlXYeqlqIOEdZEM3aOto uCA5UY3KbRfL44f2oF9shGqSKs2aOelAB1Bkz9GIYXE1/ScVij/pq4yll6ZyJzA8pV7E zVXRxRyOon63BA91eHz3S3v4rLiB/9VN9Th4vKM2CoEbE98anK3QLQpl41Y8HqBqHmxR ut4g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=8HkwG0xAsY0LmcCu7366TLsdMJMye5dnEFWq92mCE/g=; fh=7XHmKOtuWv3mkveGptatapYeXtzpSEkl7jQGp8DuFXU=; b=EgGh5+yOnPkFnCsqIk0PrsE5jKNuIuOz3iogkFY58iAvP8MiE9tg9GKdzUqYxtqWaJ n82pLjniGbEr/GBgxYLvzwcozeY6dVSTBJqkPCNeQtAfkXZSFNjA4SJLEgZw8LENVOhQ 2RFdrowrxJpXshuCtLXMl51DXTwaz2jjL9K345QNJQ4xS+Y4j09eZZbVPvC3SCOs0xC6 h2Enqu5phr4yDioB30XF79NfdcYY1UJN9ssN37xDRfW1c5XCN7xmqtTTmDF3fOrZ61bq ZsplWhJtC3wiTbWUP3GA39WW+CicNMxPa844kfEDzKj3uqnj4x30Cc+xPwwa+RfdResO MODA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="EP/a1jCa"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-178188-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c037370si97474075ad.351.2024.05.13.16.51.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 16:51:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-178188-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="EP/a1jCa"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-178188-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 162F3B227D4 for ; Mon, 13 May 2024 23:50:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 75D5586248; Mon, 13 May 2024 23:50:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="EP/a1jCa" Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4308984DE3 for ; Mon, 13 May 2024 23:50:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715644228; cv=none; b=bKpG1UIngtx6A2OPv4njyFEQsHG/MI76x6Z0wAx4Had1y+RpBAAxyYtjuhwEkMFdnEgnrSBs0pIgncEcQBYk/73ajD+yJaj93i8y7jlKqC+Zw2yCUy78ugEP6uSIGG2z6jaNtwSvMylLNSqGvAuf4yFa9h/0mnApIv7ZyHTdYFE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715644228; c=relaxed/simple; bh=jabLpUywj9mXHcjULb4WKpzqpVQcaeWIMySbgxjnxi0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=AEWGaBIQubWRaf8gbOLbFclakfp6+4xVcTj5njsNFpIkUQhsDbqfCScgqyerOWlG7LF4ohhNwpkihaPewc1sJiSdoEARpPaMOlHxjj6aQqKXU38szfxBe3znefVMac354Iy48KJbdvrpxYCIwQtVBzLABT92mU9RYQ8LgLtsVvI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=EP/a1jCa; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2b27c660174so3355551a91.1 for ; Mon, 13 May 2024 16:50:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715644225; x=1716249025; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=8HkwG0xAsY0LmcCu7366TLsdMJMye5dnEFWq92mCE/g=; b=EP/a1jCavQP7/vtImxx1GMoFn6hqxJ0lE0ObXSsb+ShWRslI4lafy3cBW49oRol60Z j2S+j4dl5YVWv+EFDDWjdUcVNHrCkLjwLaJttd9v1ydMIkiCWBVuEeEMUnrqVxrQnz9g txPjS/QCxnl61hSlEXYe27BrYlE7xO8njlDjk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715644225; x=1716249025; 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 :message-id:reply-to; bh=8HkwG0xAsY0LmcCu7366TLsdMJMye5dnEFWq92mCE/g=; b=H6cjuhMP5LnNruZzY/n6xCRDPWsElsCP5qbnGeJVPNvMIgtvNl2v/5Y3o2vQujidnb mdmEtQ8wdEhUPzJifbuHcKbkrGG/4hPVxapEgv5cjmTiLtGBAJs5oZEDzQ/k5CdodeRv iMC/uNY2netHmUHByftRNwFKHObDrcGRenIx7aGt7oah/e+WaosRu2P4oj8SKjCooE5N vmgSCc7vJpxN6Tz7VKzqW4+VCXMj/V2WG7hh6uF0pvj0OR8s13lgYJvBnsSK+sCnfSOV HXqKFsW3s13+N51VNncg8nMOhLcsQvwNfgZ7P+PhVv8JJlZodWFokHrofziRB1ldsGg/ MmyA== X-Forwarded-Encrypted: i=1; AJvYcCVrCc6PyvaAWSH+QbZ4MuZ4VQEMp6wR4wkLUMkVplC+fs27hhOK4v5UiA/1v+SCENx5AUzf+OL1DTnKgUzKYHWoRwHKKl5Ye7+yyD9q X-Gm-Message-State: AOJu0YzBuHcn1avufuOlRzOPKgYtDufJGxIu22YYCbogoHr091PZDe+w Tl4Yn7cT1kDjAgwgv4S4TlMYbMcsZJZyVaZu+DyCwakOvMNnvhnCq81i9ioBrQ== X-Received: by 2002:a17:90a:b796:b0:2b1:dd0e:e20 with SMTP id 98e67ed59e1d1-2b6ccb7e5f8mr9202653a91.38.1715644224543; Mon, 13 May 2024 16:50:24 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2b671056613sm9392889a91.4.2024.05.13.16.50.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 16:50:23 -0700 (PDT) Date: Mon, 13 May 2024 16:50:23 -0700 From: Kees Cook To: Christian Brauner Cc: Adrian Ratiu , linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-doc@vger.kernel.org, kernel@collabora.com, gbiv@google.com, ryanbeltran@google.com, inglorion@google.com, ajordanr@google.com, jorgelo@chromium.org, Guenter Roeck , Doug Anderson , Jann Horn , Andrew Morton , Randy Dunlap , Mike Frysinger Subject: Re: [PATCH v3 1/2] proc: restrict /proc/pid/mem access via param knobs Message-ID: <202405131641.219CD40A62@keescook> References: <20240409175750.206445-1-adrian.ratiu@collabora.com> <202404261544.1EAD63D@keescook> <20240503-nulltarif-karten-82213463dedc@brauner> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240503-nulltarif-karten-82213463dedc@brauner> On Fri, May 03, 2024 at 11:57:56AM +0200, Christian Brauner wrote: > On Fri, Apr 26, 2024 at 04:10:49PM -0700, Kees Cook wrote: > > On Tue, Apr 09, 2024 at 08:57:49PM +0300, Adrian Ratiu wrote: > > > Prior to v2.6.39 write access to /proc//mem was restricted, > > > after which it got allowed in commit 198214a7ee50 ("proc: enable > > > writing to /proc/pid/mem"). Famous last words from that patch: > > > "no longer a security hazard". :) > > > > > > Afterwards exploits started causing drama like [1]. The exploits > > > using /proc/*/mem can be rather sophisticated like [2] which > > > installed an arbitrary payload from noexec storage into a running > > > process then exec'd it, which itself could include an ELF loader > > > to run arbitrary code off noexec storage. > > > > > > One of the well-known problems with /proc/*/mem writes is they > > > ignore page permissions via FOLL_FORCE, as opposed to writes via > > > process_vm_writev which respect page permissions. These writes can > > > also be used to bypass mode bits. > > > > > > To harden against these types of attacks, distrbutions might want > > > to restrict /proc/pid/mem accesses, either entirely or partially, > > > for eg. to restrict FOLL_FORCE usage. > > > > > > Known valid use-cases which still need these accesses are: > > > > > > * Debuggers which also have ptrace permissions, so they can access > > > memory anyway via PTRACE_POKEDATA & co. Some debuggers like GDB > > > are designed to write /proc/pid/mem for basic functionality. > > > > > > * Container supervisors using the seccomp notifier to intercept > > > syscalls and rewrite memory of calling processes by passing > > > around /proc/pid/mem file descriptors. > > > > > > There might be more, that's why these params default to disabled. > > > > > > Regarding other mechanisms which can block these accesses: > > > > > > * seccomp filters can be used to block mmap/mprotect calls with W|X > > > perms, but they often can't block open calls as daemons want to > > > read/write their runtime state and seccomp filters cannot check > > > file paths, so plain write calls can't be easily blocked. > > > > > > * Since the mem file is part of the dynamic /proc// space, we > > > can't run chmod once at boot to restrict it (and trying to react > > > to every process and run chmod doesn't scale, and the kernel no > > > longer allows chmod on any of these paths). > > > > > > * SELinux could be used with a rule to cover all /proc/*/mem files, > > > but even then having multiple ways to deny an attack is useful in > > > case one layer fails. > > > > > > Thus we introduce three kernel parameters to restrict /proc/*/mem > > > access: read, write and foll_force. All three can be independently > > > set to the following values: > > > > > > all => restrict all access unconditionally. > > > ptracer => restrict all access except for ptracer processes. > > > > > > If left unset, the existing behaviour is preserved, i.e. access > > > is governed by basic file permissions. > > > > > > Examples which can be passed by bootloaders: > > > > > > restrict_proc_mem_foll_force=all > > > restrict_proc_mem_write=ptracer > > > restrict_proc_mem_read=ptracer > > > > > > Each distribution needs to decide what restrictions to apply, > > > depending on its use-cases. Embedded systems might want to do > > > more, while general-purpouse distros might want a more relaxed > > > policy, because for e.g. foll_force=all and write=all both break > > > break GDB, so it might be a bit excessive. > > > > > > Based on an initial patch by Mike Frysinger . > > > > Thanks for this new version! > > > > > > > > Link: https://lwn.net/Articles/476947/ [1] > > > Link: https://issues.chromium.org/issues/40089045 [2] > > > Cc: Guenter Roeck > > > Cc: Doug Anderson > > > Cc: Kees Cook > > > Cc: Jann Horn > > > Cc: Andrew Morton > > > Cc: Randy Dunlap > > > Cc: Christian Brauner > > > Co-developed-by: Mike Frysinger > > > Signed-off-by: Mike Frysinger > > > Signed-off-by: Adrian Ratiu > > > --- > > > .../admin-guide/kernel-parameters.txt | 27 +++++ > > > fs/proc/base.c | 103 +++++++++++++++++- > > > include/linux/jump_label.h | 5 + > > > 3 files changed, 133 insertions(+), 2 deletions(-) > > > > > > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt > > > index 6e62b8cb19c8d..d7f7db41369c7 100644 > > > --- a/Documentation/admin-guide/kernel-parameters.txt > > > +++ b/Documentation/admin-guide/kernel-parameters.txt > > > @@ -5665,6 +5665,33 @@ > > > reset_devices [KNL] Force drivers to reset the underlying device > > > during initialization. > > > > > > + restrict_proc_mem_read= [KNL] > > > + Format: {all | ptracer} > > > + Allows restricting read access to /proc/*/mem files. > > > + Depending on restriction level, open for reads return -EACCESS. > > > + Can be one of: > > > + - 'all' restricts all access unconditionally. > > > + - 'ptracer' allows access only for ptracer processes. > > > + If not specified, then basic file permissions continue to apply. > > > + > > > + restrict_proc_mem_write= [KNL] > > > + Format: {all | ptracer} > > > + Allows restricting write access to /proc/*/mem files. > > > + Depending on restriction level, open for writes return -EACCESS. > > > + Can be one of: > > > + - 'all' restricts all access unconditionally. > > > + - 'ptracer' allows access only for ptracer processes. > > > + If not specified, then basic file permissions continue to apply. > > > + > > > + restrict_proc_mem_foll_force= [KNL] > > > + Format: {all | ptracer} > > > + Restricts the use of the FOLL_FORCE flag for /proc/*/mem access. > > > + If restricted, the FOLL_FORCE flag will not be added to vm accesses. > > > + Can be one of: > > > + - 'all' restricts all access unconditionally. > > > + - 'ptracer' allows access only for ptracer processes. > > > + If not specified, FOLL_FORCE is always used. > > > > bike shedding: I wonder if this should be a fake namespace (adding a dot > > just to break it up for reading more easily), and have words reordered > > to the kernel's more common subject-verb-object: proc_mem.restrict_read=... > > > > > + > > > resume= [SWSUSP] > > > Specify the partition device for software suspend > > > Format: > > > diff --git a/fs/proc/base.c b/fs/proc/base.c > > > index 18550c071d71c..c733836c42a65 100644 > > > --- a/fs/proc/base.c > > > +++ b/fs/proc/base.c > > > @@ -152,6 +152,41 @@ struct pid_entry { > > > NULL, &proc_pid_attr_operations, \ > > > { .lsmid = LSMID }) > > > > > > +/* > > > + * each restrict_proc_mem_* param controls the following static branches: > > > + * key[0] = restrict all writes > > > + * key[1] = restrict writes except for ptracers > > > + * key[2] = restrict all reads > > > + * key[3] = restrict reads except for ptracers > > > + * key[4] = restrict all FOLL_FORCE usage > > > + * key[5] = restrict FOLL_FORCE usage except for ptracers > > > + */ > > > +DEFINE_STATIC_KEY_ARRAY_FALSE_RO(restrict_proc_mem, 6); > > > > So, I don't like having open-coded numbers. And I'm not sure there's a > > benefit to stuffing these all into an array? So: > > > > DEFINE_STATIC_KEY_FALSE_RO(proc_mem_restrict_read); > > DEFINE_STATIC_KEY_FALSE_RO(proc_mem_restrict_write); > > DEFINE_STATIC_KEY_FALSE_RO(proc_mem_restrict_foll_force); > > > > > + > > > +static int __init early_restrict_proc_mem(char *buf, int offset) > > > +{ > > > + if (!buf) > > > + return -EINVAL; > > > + > > > + if (strncmp(buf, "all", 3) == 0) > > > > I'd use strcmp() to get exact matches. That way "allalksdjflas" doesn't > > match. :) > > > > > + static_branch_enable(&restrict_proc_mem[offset]); > > > + else if (strncmp(buf, "ptracer", 7) == 0) > > > + static_branch_enable(&restrict_proc_mem[offset + 1]); > > > + > > > + return 0; > > > +} > > > > Then don't bother with a common helper since you've got a macro, and > > it'll all get tossed after __init anyway. > > > > > + > > > +#define DEFINE_EARLY_RESTRICT_PROC_MEM(name, offset) \ > > > +static int __init early_restrict_proc_mem_##name(char *buf) \ > > > +{ \ > > > + return early_restrict_proc_mem(buf, offset); \ > > > +} \ > > > +early_param("restrict_proc_mem_" #name, early_restrict_proc_mem_##name) > > > + > > > +DEFINE_EARLY_RESTRICT_PROC_MEM(write, 0); > > > +DEFINE_EARLY_RESTRICT_PROC_MEM(read, 2); > > > +DEFINE_EARLY_RESTRICT_PROC_MEM(foll_force, 4); > > > > #define DEFINE_EARLY_PROC_MEM_RESTRICT(name) \ > > static int __init early_proc_mem_restrict_##name(char *buf) \ > > { \ > > if (!buf) \ > > return -EINVAL; \ > > \ > > if (strcmp(buf, "all") == 0) \ > > static_branch_enable(&proc_mem_restrict_##name); \ > > else if (strcmp(buf, "ptracer") == 0) \ > > static_branch_enable(&proc_mem_restrict_##name); \ > > \ > > return 0; \ > > } \ > > early_param("proc_mem_restrict_" #name, early_proc_mem_restrict_##name) > > > > > > > + > > > /* > > > * Count the number of hardlinks for the pid_entry table, excluding the . > > > * and .. links. > > > @@ -825,9 +860,58 @@ static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) > > > return 0; > > > } > > > > > > +static bool __mem_open_current_is_ptracer(struct file *file) > > > +{ > > > + struct inode *inode = file_inode(file); > > > + struct task_struct *task = get_proc_task(inode); > > > + int ret = false; > > > + > > > + if (task) { > > > + rcu_read_lock(); > > > + if (current == ptrace_parent(task)) > > > + ret = true; > > > + rcu_read_unlock(); > > > + put_task_struct(task); > > > + } > > > > This creates a ToCToU race between this check (which releases the task) > > and the later memopen which make get a different task (and mm). > > > > To deal with this, I think you need to add a new mode flag for > > proc_mem_open(), and add the checking there. > > > > > + > > > + return ret; > > > +} > > > + > > > +static int __mem_open_check_access_restriction(struct file *file) > > > +{ > > > + if (file->f_mode & FMODE_WRITE) { > > > + /* Deny if writes are unconditionally disabled via param */ > > > + if (static_branch_unlikely(&restrict_proc_mem[0])) > > > + return -EACCES; > > > + > > > + /* Deny if writes are allowed only for ptracers via param */ > > > + if (static_branch_unlikely(&restrict_proc_mem[1]) && > > > + !__mem_open_current_is_ptracer(file)) > > > + return -EACCES; > > > + > > > + } else if (file->f_mode & FMODE_READ) { > > > > I think this "else" means that O_RDWR opens will only check the write > > flag, so drop the "else". > > > > > + /* Deny if reads are unconditionally disabled via param */ > > > + if (static_branch_unlikely(&restrict_proc_mem[2])) > > > + return -EACCES; > > > + > > > + /* Deny if reads are allowed only for ptracers via param */ > > > + if (static_branch_unlikely(&restrict_proc_mem[3]) && > > > + !__mem_open_current_is_ptracer(file)) > > > + return -EACCES; > > > + } > > > + > > > + return 0; > > > +} > > > + > > > static int mem_open(struct inode *inode, struct file *file) > > > { > > > - int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); > > > + int ret; > > > + > > > + ret = __mem_open_check_access_restriction(file); > > > + if (ret) > > > + return ret; > > > + > > > + ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); > > > > > > /* OK to pass negative loff_t, we can catch out-of-range */ > > > file->f_mode |= FMODE_UNSIGNED_OFFSET; > > > @@ -835,6 +919,20 @@ static int mem_open(struct inode *inode, struct file *file) > > > return ret; > > > } > > > > > > +static unsigned int __mem_rw_get_foll_force_flag(struct file *file) > > > +{ > > > + /* Deny if FOLL_FORCE is disabled via param */ > > > + if (static_branch_unlikely(&restrict_proc_mem[4])) > > > + return 0; > > > + > > > + /* Deny if FOLL_FORCE is allowed only for ptracers via param */ > > > + if (static_branch_unlikely(&restrict_proc_mem[5]) && > > > + !__mem_open_current_is_ptracer(file)) > > > > This is like the ToCToU: the task may have changed out from under us > > between the open the read/write. > > But why would you care? As long as the task is the ptracer it doesn't > really matter afaict. Because the mm you're writing to may no longer be associated with the task. proc_mem_operations.open() will take a reference to the current task's mm, via proc_mem_open() through __mem_open(): struct task_struct *task = get_proc_task(inode); ... mm = mm_access(task, mode | PTRACE_MODE_FSCREDS); ... file->private_data = mm; And in the proposed check added to mem_rw(), if get_proc_task(inode) returns a different task (i.e. the pid got recycled and the original mm is still associated with a forked task), then it could write to the forked task using the ptrace check against the new task. Looking at it again now, I think it should be possible to just revalidate the mm in __mem_open_current_is_ptracer(), though. i.e. it would be allowed if ptrace check passes and file->private_data == mm_access(...), for the mem_rw case... -- Kees Cook