Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp2605971rdb; Wed, 21 Feb 2024 13:07:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWr3vqOot+fR1h2jShpW50ZldgJ17FHIcoZxbl0zaQoTRGuTuvHxxf/8NqRmgqHt5KOap06qbt7xnW8MqUmvvxgXD67R1s9vtdfW+V0lg== X-Google-Smtp-Source: AGHT+IFaMWa2albAmHU5zU4aq7PA2hRVp3snaDWjfeqnJbJ9cD8ugk+utjI0SLHPoxEgjcvkKWth X-Received: by 2002:a05:620a:1225:b0:783:e30b:3759 with SMTP id v5-20020a05620a122500b00783e30b3759mr19865894qkj.35.1708549624088; Wed, 21 Feb 2024 13:07:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708549624; cv=pass; d=google.com; s=arc-20160816; b=JvDAH/P3bUPwZxBHUB961PSxHydTKQ8iR2stAH5ze7myxTg1N2iBgXUti/z1hQwI7c p8Q3HMhEK948j71slZc29fNzHBniNf0XJ/NDYUQk4S7rd2B9M2hmDk0FPaj/qF3v052F 6fIDrPdYqLMAlo88UXW+C4teB//aoDrXRzVamSiwR7jih6sAB/ZKF2rT2uxWkX8O2oXB TzIpbKSHGyBqqUtoW1cKV7STEDtt50uumwLHoR4A3qx7hF+HTqZ8osUzX4OZ1fkqPrQj fwceZtQF3XRJAZV/pGuEt2aH3tHxcouJvFmc2eo6olWKAvu7+OV+Ggbuh9nm539lj6td xppg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=/zzex0Md3xlvrCgcQ1O6HrtJFLIRYcBC92lrcH3OCpY=; fh=RRitfpq+InK+G4SnrnIsHasq3GrzSKig5AkEbBMFW/g=; b=SryKcXUgkxGQFSRjSOv5iFRAFB55sJzhbLTzoomI487LBVMiwoUq+EuBmSj6qetRpR 0SibsRVN0/Wxbjm5Ix7aPZLTOXdZP2j80CkbQa7FIQFUHvZVwm4XFn1rnWJ0wNvzGaDE VN+5/JkP6pLNLx54cLwSf6ctVZG+YLHplhuF8ukIgpkE522dR6IHhoQYxE8Wvs8qtKKF LL9v1qnTqmpRRndCAHcT4APQBfT/r9JEQ7AS1d63lK80Zc09zapOLIR7lSGNsR/4lrnR ynHD8zvc/ecnuicp6Oh4ntHDV5/b8nW69159rgQjx5Xuld1DeBQUgBb5MwcAkf6gBT/Y KyXA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Rfw6Bzj3; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-75494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75494-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id dw25-20020a05620a601900b0078733080d05si12468307qkb.157.2024.02.21.13.07.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 13:07:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Rfw6Bzj3; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-75494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75494-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C1C721C224C6 for ; Wed, 21 Feb 2024 21:07:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DDE7C127B4E; Wed, 21 Feb 2024 21:06:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="Rfw6Bzj3" Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38895126F3A; Wed, 21 Feb 2024 21:06:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708549596; cv=none; b=BtmEEpaz339wLeSQ0QohQc3TRhQTC2yTgjW/TtE5755mDAwyo55PU/ZnROObHju8ZlswN6FUQMrV41ExVI6r9CtuCEhTDQ67djkmqbNDeppIAc1821Q17xpfnt5lI9cO52cn2jedYFGt32q3qbJu5aydHZdqPWiQjCYPvWu2nW0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708549596; c=relaxed/simple; bh=8VXcAAJto8svyUnZ97pB7BNBTXBuRwzYNmSFpJXmiLs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=j6148Rj+X7jhOBgmDLSPuy2PM1NBHbX0Bku6gh/XMS667Bt359Xe+VezS7Cq8NiLGeTeQwmb+eUlIVPey1TMEreMZVjvvP+GVy1RLOiR1haigNQDl5JG1sg5t9N1sXsAHJ/0pJDaY9ZDTVLgJzWsU4LT2Sy3z62fAKmq61KyJC4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=Rfw6Bzj3; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1708549593; bh=8VXcAAJto8svyUnZ97pB7BNBTXBuRwzYNmSFpJXmiLs=; h=From:To:Cc:Subject:Date:From; b=Rfw6Bzj3tefn3WJ80NhDISRYJyhzmubBd3g7vnQA01bWTgOvBAuF4u3KfJgDFJ6bo PHAcLX+0Td8RDueLE4WCAlRg9HTPaW2Y8jfyIwFUyYRz8TBoR0aWp7m0qXafnDdeYT joTS2suyoGcf9sKky0E7nFeMtDHOi94M+GFwdiHtjWKfHkGXziB0WhdMXaHHtefe8P 7Dvm/fT6c/i6/MRtdw6krH/lsKGeJYgKg75YTelZ0H1hVr9LorZbY3cZ7tIf0aCUjO DtacEfTSJWuWccTgP4xSjcZaCMZWGDHSFfA8/mw+qf67bJBe+dJGNV4kdjsxgmAAwd 7D7Eic7Z3uqqg== Received: from localhost.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: aratiu) by madrid.collaboradmins.com (Postfix) with ESMTPSA id ED19D37820D3; Wed, 21 Feb 2024 21:06:32 +0000 (UTC) From: Adrian Ratiu To: linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel@collabora.com, Adrian Ratiu , Guenter Roeck , Doug Anderson , Mike Frysinger Subject: [PATCH] proc: allow restricting /proc/pid/mem writes Date: Wed, 21 Feb 2024 23:06:26 +0200 Message-ID: <20240221210626.155534-1-adrian.ratiu@collabora.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 appeared started causing drama like [1]. The /proc/*/mem exploits 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. As part of hardening against these types of attacks, distrbutions can restrict /proc/*/mem to only allow writes when they makes sense, like in case of debuggers which have ptrace permissions, as they are able to access memory anyway via PTRACE_POKEDATA and friends. Dropping the mode bits disables write access for non-root users. Trying to `chmod` the paths back fails as the kernel rejects it. For users with CAP_DAC_OVERRIDE (usually just root) we have to disable the mem_write callback to avoid bypassing the mode bits. Writes can be used to bypass permissions on memory maps, even if a memory region is mapped r-x (as is a program's executable pages), the process can open its own /proc/self/mem file and write to the pages directly. Even if seccomp filters block mmap/mprotect calls with W|X perms, they often cannot block open calls as daemons want to read/write their own runtime state and seccomp filters cannot check file paths. Write calls also can't be blocked in general via seccomp. 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 on layer fails. [1] https://lwn.net/Articles/476947/ [2] https://issues.chromium.org/issues/40089045 Based on an initial patch by Mike Frysinger . Cc: Guenter Roeck Cc: Doug Anderson Signed-off-by: Mike Frysinger Signed-off-by: Adrian Ratiu --- Tested on next-20240220. I would really like to avoid depending on CONFIG_MEMCG which is required for the struct mm_stryct "owner" pointer. Any suggestions how check the ptrace owner without MEMCG? --- fs/proc/base.c | 26 ++++++++++++++++++++++++-- security/Kconfig | 13 +++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index 98a031ac2648..e4d6829c5d1a 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -152,6 +152,12 @@ struct pid_entry { NULL, &proc_pid_attr_operations, \ { .lsmid = LSMID }) +#ifdef CONFIG_SECURITY_PROC_MEM_RESTRICT_WRITES +# define PROC_PID_MEM_MODE S_IRUSR +#else +# define PROC_PID_MEM_MODE (S_IRUSR|S_IWUSR) +#endif + /* * Count the number of hardlinks for the pid_entry table, excluding the . * and .. links. @@ -899,7 +905,24 @@ static ssize_t mem_read(struct file *file, char __user *buf, static ssize_t mem_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { +#ifdef CONFIG_SECURITY_PROC_MEM_RESTRICT_WRITES + /* Allow processes already ptracing the target process */ +#ifdef CONFIG_MEMCG + struct mm_struct *mm = file->private_data; + + rcu_read_lock(); + if (ptracer_capable(current, mm->user_ns) && + current == ptrace_parent(mm->owner)) { + rcu_read_unlock(); + return mem_rw(file, (char __user *)buf, count, ppos, 1); + } + rcu_read_unlock(); +#endif + + return -EACCES; +#else return mem_rw(file, (char __user*)buf, count, ppos, 1); +#endif } loff_t mem_lseek(struct file *file, loff_t offset, int orig) @@ -3281,7 +3303,7 @@ static const struct pid_entry tgid_base_stuff[] = { #ifdef CONFIG_NUMA REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), #endif - REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), + REG("mem", PROC_PID_MEM_MODE, proc_mem_operations), LNK("cwd", proc_cwd_link), LNK("root", proc_root_link), LNK("exe", proc_exe_link), @@ -3631,7 +3653,7 @@ static const struct pid_entry tid_base_stuff[] = { #ifdef CONFIG_NUMA REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), #endif - REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), + REG("mem", PROC_PID_MEM_MODE, proc_mem_operations), LNK("cwd", proc_cwd_link), LNK("root", proc_root_link), LNK("exe", proc_exe_link), diff --git a/security/Kconfig b/security/Kconfig index 412e76f1575d..4082a07a33e5 100644 --- a/security/Kconfig +++ b/security/Kconfig @@ -19,6 +19,19 @@ config SECURITY_DMESG_RESTRICT If you are unsure how to answer this question, answer N. +config SECURITY_PROC_MEM_RESTRICT_WRITES + bool "Restrict /proc//mem write access" + default n + help + This restricts writes to /proc//mem, except when the current + process ptraces the /proc//mem task, because a ptracer already + has write access to the tracee memory. + + Write access to this file allows bypassing memory map permissions, + such as modifying read-only code. + + If you are unsure how to answer this question, answer N. + config SECURITY bool "Enable different security models" depends on SYSFS -- 2.30.2