Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1509894rbb; Mon, 26 Feb 2024 11:31:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVyViJBCSggerwC+7eD72+hJfpg/sQ5Gou09F3qCwhCkPRZQm6nNxdmo4ipiYvUfwPBfvFA0dH0T7SGvO2UBFUksGz99cvYk9X5xTpyww== X-Google-Smtp-Source: AGHT+IEFzfp0D6Bqk/7WsK639yb8q3wJPkIcI6lnA4uIRhAmsVDrddGsqJ0ttYOe2RxnNqmxOz1y X-Received: by 2002:a05:6a20:e617:b0:19e:840f:34d9 with SMTP id my23-20020a056a20e61700b0019e840f34d9mr74388pzb.29.1708975877108; Mon, 26 Feb 2024 11:31:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708975877; cv=pass; d=google.com; s=arc-20160816; b=dqO0dspcdHUYnTCpTx5h1eg6ZgHiokcgcrFw4KmngFO2E4+qVmx3W76+9VqDwvfGXM Qqss1rx30TnP3FVmnd1oZhWi2lVygU/9mzP6gNl7n43y78vnVv7koSvJEClz6pEBs+Bs RndO8OuKS49zhqqQbhZIaGaJzwNvqKeSLMUA1cPx6D2zUPTeta0Ec0SlBNzlCQM0zaGX tgqR2//26P/KKMdEcEGm9UccVNOP6OPAIIaHUn0qECEELAA6aI/+FouKCNF+vdiifXt0 rpPJfCDXKcSDVW0uSxhn/s70XiD9AYzoUvhVKrSdK6IHyZ7ynZK11uxnO7n8RIxvsuGi H9MQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=82wd+SSaRsguFcZt+U0NWaZAuP5LxkM0aZkdE6Hnh5c=; fh=ALFD1hjdGZlyj1XDo4co6cBDrvn9+JIjHetBi1o8qQo=; b=AwEkAmPCh38p0jhRr3NWg/Vu1khqR1pvRUmu0v/h0Sz9lfHU6qz0qlgs4VRQ3X15n7 +Q93apsUly3LiQaaXyTfWjWE14zoQnU8xMAUd4oPUwgzldc7X7Tb5nwEwlWz62V1iZoq ofOOUdItvlkbWLl/0RX5aA8iBWxCNuosAEdEoto6sgnTQBxgyBGmilm/CUVhNG3HfrmM GhwaNIpRLn8hn9DtNZm0lzphvgNDSoCUyZ5I9fwEeuJd5eKI60CfRDhrVRoUERbb5Z8K PKEFHRhYXdO7xwYm5vlEldW95KyTCNNsqT8OVnbFVvTOr/TYxMpmasCL/uYEFnv8LtGL 1Vkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SuUEgLZv; 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-82233-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-82233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e9-20020a63d949000000b005e438e99128si3806016pgj.721.2024.02.26.11.31.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 11:31:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-82233-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SuUEgLZv; 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-82233-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-82233-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 06A972980D1 for ; Mon, 26 Feb 2024 19:23:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 02FA212F5A4; Mon, 26 Feb 2024 19:22:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="SuUEgLZv" Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 5C539130AEE for ; Mon, 26 Feb 2024 19:22:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708975358; cv=none; b=etwwHM2k8DH3yGp34+UKqZf6GheikdcL35yQVo5oa6uAj2c/y59mWEpl+jp76xjTmFWR/mk899gdlik9ws5k45qJvqkNru2emLOWf70cyGdbOCG7YGVNYpJ54nyVpb+YiofTE/Z3uxn5g/G7e4bezxdHaoFBWYeghi1zmDorK5Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708975358; c=relaxed/simple; bh=MCDjtaRWEF67bT1IdABaJO8kKhnAH/vfBzzPU+hrTKk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=GvkB0ocBHJQjTsFvCV2HUW7uNq3KbEM8gRZ3TNtw9/jdUVBwmEEO4fIMdO1trwoNAXYW/+X3JVUPVozyUS/Lz61u613IeHMjo3gxmTs2Ynhw4006U1Tfgw+FaZSiQZCPr7hCyYY3lBUotyCVz/SSOxHk5ZzjUm92adUPSdWVkTc= 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=SuUEgLZv; arc=none smtp.client-ip=209.85.214.172 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-pl1-f172.google.com with SMTP id d9443c01a7336-1dc3b4b9b62so29734865ad.1 for ; Mon, 26 Feb 2024 11:22:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1708975355; x=1709580155; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=82wd+SSaRsguFcZt+U0NWaZAuP5LxkM0aZkdE6Hnh5c=; b=SuUEgLZvpDNR1cbi6DQiXEsCpA3fNrg6t9rUUEdWz86OkXxeuRvzI5j8UE0mf1x2uH VEpTGnz2tml+LdYLDJduDpfCPM57iKHc7fOe8x2zrEin+x1dn5XqNRvMjvk9opwZSaGP +hjZkxLOrFNMSFwDMashvLPNPlSkSyQeN7kYo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708975355; x=1709580155; h=in-reply-to:content-transfer-encoding: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=82wd+SSaRsguFcZt+U0NWaZAuP5LxkM0aZkdE6Hnh5c=; b=ceJ8TLEm138llwOhPOMFVNfTb9oiP4NnncdC1aiglo2GYyHjCoWBSiYEAfmx1GBGcN j19ZZkaty4Glox4/GHunjVZh1IRgz2nNDtyUF30ELmMwQ7hse9B3PD/U+UAbJSNFUbbh N9TOitXMv8b+WOY5BWxnE/YtBsG7nXElsDxRxDeviOhDi1yIAG6WE9E8tKoXBIy6itR6 hIbBpYuiRocxdPZoTzrzdmZ/Sa8+bRLNDnu+mqy/wkXC+dMdZtm0LwmOlwksUwQ3Rwhz TEwwgiPaWZeCFOFVirfffNd4RU4ZpXgi4Ls1eNGYz6g6hjRtc+tvrZfnVihQLHjK6tsL tx5w== X-Forwarded-Encrypted: i=1; AJvYcCW1JIGqCBnDMTia541Ru66TJWW/WcvpTvaCbzXfXyUnjS2yzlFgNxi/Too21aG1AA1EC0YNXma+jM1HHLcOcX2+RiBetkF1f7A19XuF X-Gm-Message-State: AOJu0Yzn9FQWdgVv9XRhMAVJ5jDfFu/GBwDwylZUFrajFLGlTlDyNbS7 G/b6oc8Mt0bcbxbSPX5sNIubxKSZDbalAWb3Jl36qnSHLystqhOVE1ZhcJtyEg== X-Received: by 2002:a17:903:4303:b0:1db:9ff1:b59b with SMTP id jz3-20020a170903430300b001db9ff1b59bmr7578787plb.23.1708975355588; Mon, 26 Feb 2024 11:22:35 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id h2-20020a170902748200b001db8f7720e2sm38549pll.288.2024.02.26.11.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 11:22:35 -0800 (PST) Date: Mon, 26 Feb 2024 11:22:34 -0800 From: Kees Cook To: Adrian Ratiu , jannhorn@google.com Cc: Doug Anderson , linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@collabora.com, Guenter Roeck , Mike Frysinger , linux-hardening@vger.kernel.org Subject: Re: [PATCH] proc: allow restricting /proc/pid/mem writes Message-ID: <202402261110.B8129C002@keescook> References: <20240221210626.155534-1-adrian.ratiu@collabora.com> 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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Mon, Feb 26, 2024 at 09:10:54AM -0800, Doug Anderson wrote: > Hi, > > On Wed, Feb 21, 2024 at 1:06 PM 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 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 This should have a "Co-developed-by: Mike..." tag, since you're making changes and not just passing it along directly. > > 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(-) > > Thanks for posting this! This looks reasonable to me, but I'm nowhere > near an expert on this so I won't add a Reviewed-by tag. > > This feels like the kind of thing that Kees might be interested in > reviewing, so adding him to the "To" list. I'd love to make /proc/$pid/mem more strict. A few comments: > [...] > + if (ptracer_capable(current, mm->user_ns) && It really looks like you're trying to do a form of ptrace_may_access(), but _without_ the introspection exception? Also, using "current" in the write path can lead to problems[1], so this should somehow use file->f_cred, or limit write access during the open() instead? > [...] > +config SECURITY_PROC_MEM_RESTRICT_WRITES Instead of a build-time CONFIG, I'd prefer a boot-time config (or a sysctl, but that's be harder given the perms). That this is selectable by distro users, etc, and they don't need to rebuild their kernel to benefit from it. Jann Horn has tried to restrict access to this file in the past as well, so he may have some additional advice about it. -Kees [1] https://docs.kernel.org/security/credentials.html#open-file-credentials -- Kees Cook