Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp2153757lqe; Tue, 9 Apr 2024 10:58:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW1rrOchUli+cq9VWhnULISrSZHKKe+JSwZ/7Yzq9RQpOKeS0MnlK8kHgxS/FFjYQoNqvznLze8Vh/Mop9M50lmgMjYBQ1vLyIUP/6KUA== X-Google-Smtp-Source: AGHT+IHcdKjZez2txSXyHySRL2Dv+ctQL5ogv+j22t8pe7+74Kq93oNfQOJ+/bOtrFczjqNMdlOp X-Received: by 2002:a05:6871:2b17:b0:232:f9e0:e4c7 with SMTP id dr23-20020a0568712b1700b00232f9e0e4c7mr102515oac.48.1712685533526; Tue, 09 Apr 2024 10:58:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712685533; cv=pass; d=google.com; s=arc-20160816; b=nJiDCLfwW4PLjNGNCz1/KBbUcVLZ//PrMx2vOI+LRW0AyApKj+OVnnAMCwYvbMVg3l YsQeoU4DpC2Am/f5sJFNup49sT8HY211TRrqYSmjIRrvb9YJu3PLuCYT+QFA1Pg6aEQK 1kCcIJr+agiu1PCX//IJIq44K7E1YsPA2q8JWrsyR+1yq1rcFNC8ZwrktpM7qJppToiv FULRDzu9Q13stWRiaTxwXxt/Mcb6N/gaMwHffk60CH+sLqa38+A9sAXEdUtm+HJKj96a HT4Y5gjCc//epAonwPYbEyfAW/llDUPtf9RBp7t1wEOSxChnB8hGkx9B4x+AmJhZO/oH zoUg== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dD2wONLqZmvMJ23jKiUQFZYTQLyrWSxt8EX2oU/c5Gs=; fh=zid6DMA7G+6ACHbQmV9wiM1WK9sONbyY/1aA90vlzFI=; b=TpAhuZvHRdwV1ez8tTkpTnr7nB1aQxaesp8BJLnnydZ+D24T4jh5RgFv9+7tJnQtTu M5VOpLQ5/oX6Jl1bpGnOslhuylnX7ANFZ7y+kq0SyadmWyC9jtX/Zy2qSsLIt83wVq32 XIAoWJEXAKZf7eh3wOCLgaSHFzJ9QQo3f+ClZ92IPzjFKqqI6EAE5bV+iS3egh49v6rY c6ax44w7ClhFcI+Avfqp5dT8h7UWdAx0o4fvKhF6zcjUtEH4D48liuGyLVAMCj/v7IAc R3jgKDlKCpa/pmfmpbgDHGb4hcg4G2Hh+rv3o9wOBwgjPw9P531uuvBrrrJNUER/Y/s6 6MRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=xlUx2b7a; 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-137432-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137432-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h16-20020a635310000000b005f0a54e50e2si8755411pgb.889.2024.04.09.10.58.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 10:58:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-137432-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=@collabora.com header.s=mail header.b=xlUx2b7a; 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-137432-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137432-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 28FE628524B for ; Tue, 9 Apr 2024 17:58:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 310A8156C61; Tue, 9 Apr 2024 17:58:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="xlUx2b7a" 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 877F4131732; Tue, 9 Apr 2024 17:58:25 +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=1712685507; cv=none; b=XHJKnnBlGkyquKIf+JomrOCLV2sZqRfmJuno0hM9bGcvvl/1v3Lvg7oXB85XF55MGeOwUz4RKXBTBQoS5hf++3a4RLfLgrgWWv8drsMK2cIblO05XocFrUh02NrVRON5dzgz2fmilQCYHtMiO1DduUuqfsfTWDuwXzOKVujGLbs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712685507; c=relaxed/simple; bh=AmtWxCZ4F4bQFddgYnhSmlD9kM0A3FWxzkbEkyZ+Mtw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qG1faasW26SyautHN5lhatP/jd0v5ni1tNd6Fj8zmw/JLaq+rw/jFLcZm14EXSGArQaBfUAeTl+/EURsrlX8vY9rzMcEBP1NT+laHuAAafUxSdWOIf14ihXgNqA6/atn6xeu+rKvVUzsZrcV9c8QotbrxVaDsd8CGnEzooWLHZU= 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=xlUx2b7a; 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=1712685503; bh=AmtWxCZ4F4bQFddgYnhSmlD9kM0A3FWxzkbEkyZ+Mtw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xlUx2b7afVKUYuFgYA+M7Kva1C+VYO4G61wjMXsU8B1dsaHffICXZA4Di8qELpGFT ifUi6409E73XP2CXtD/bn0DoXbKtuUFLlyieKyepIMIwZoNhR38gu5UGQ2ml7NSXWH m1Dg6MtAnK85Rr//swHhTt6STb6zZq9nvhvr8A+z/uaUA1o5+/bxWx6vBUNGSl5Wu7 Gty7IQP9iqOJLocCSscFAfdezDWscn+Es5xKOzDvQCXpz9gbOTHv/Er6kqW5geYXyM hOrY1My8uD+M4qSXW0EyOMC/W38bpKrZbKNYSM1LJ4fMys4dSapOb2HLbzO7BodNWD zS9BjQjJw4cww== 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 8C66C37820EF; Tue, 9 Apr 2024 17:58:22 +0000 (UTC) From: Adrian Ratiu To: linux-fsdevel@vger.kernel.org Cc: 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, Adrian Ratiu , Guenter Roeck , Doug Anderson , Kees Cook , Jann Horn , Andrew Morton , Randy Dunlap , Christian Brauner Subject: [PATCH v3 2/2] proc: add Kconfigs to restrict /proc/pid/mem access Date: Tue, 9 Apr 2024 20:57:50 +0300 Message-ID: <20240409175750.206445-2-adrian.ratiu@collabora.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240409175750.206445-1-adrian.ratiu@collabora.com> References: <20240409175750.206445-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-Transfer-Encoding: 8bit Some systems might have difficulty changing their bootloaders to enable the newly added restrict_proc_mem* params, for e.g. remote embedded doing OTA updates, so this provides a set of Kconfigs to set /proc/pid/mem restrictions at build-time. The boot params take precedence over the Kconfig values. This can be reversed, but doing it this way I think makes sense. Another idea is to have a global bool Kconfig which can enable or disable this mechanism in its entirety, however it does not seem necessary since all three knobs default to off, the branch logic overhead is rather minimal and I assume most of systems will want to restrict at least the use of FOLL_FORCE. Cc: Guenter Roeck Cc: Doug Anderson Cc: Kees Cook Cc: Jann Horn Cc: Andrew Morton Cc: Randy Dunlap Cc: Christian Brauner Signed-off-by: Adrian Ratiu --- fs/proc/base.c | 33 +++++++++++++++++++++++++++++++++ security/Kconfig | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index c733836c42a65..e8ee848fc4a98 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -889,6 +889,17 @@ static int __mem_open_check_access_restriction(struct file *file) !__mem_open_current_is_ptracer(file)) return -EACCES; +#ifdef CONFIG_SECURITY_PROC_MEM_WRITE_RESTRICT + /* Deny if writes are unconditionally disabled via Kconfig */ + if (!strncmp(CONFIG_SECURITY_PROC_MEM_WRITE_RESTRICT, "all", 3)) + return -EACCES; + + /* Deny if writes are allowed only for ptracers via Kconfig */ + if (!strncmp(CONFIG_SECURITY_PROC_MEM_WRITE_RESTRICT, "ptracer", 7) && + !__mem_open_current_is_ptracer(file)) + return -EACCES; +#endif + } else if (file->f_mode & FMODE_READ) { /* Deny if reads are unconditionally disabled via param */ if (static_branch_unlikely(&restrict_proc_mem[2])) @@ -898,6 +909,17 @@ static int __mem_open_check_access_restriction(struct file *file) if (static_branch_unlikely(&restrict_proc_mem[3]) && !__mem_open_current_is_ptracer(file)) return -EACCES; + +#ifdef CONFIG_SECURITY_PROC_MEM_READ_RESTRICT + /* Deny if reads are unconditionally disabled via Kconfig */ + if (!strncmp(CONFIG_SECURITY_PROC_MEM_READ_RESTRICT, "all", 3)) + return -EACCES; + + /* Deny if reads are allowed only for ptracers via Kconfig */ + if (!strncmp(CONFIG_SECURITY_PROC_MEM_READ_RESTRICT, "ptracer", 7) && + !__mem_open_current_is_ptracer(file)) + return -EACCES; +#endif } return 0; @@ -930,6 +952,17 @@ static unsigned int __mem_rw_get_foll_force_flag(struct file *file) !__mem_open_current_is_ptracer(file)) return 0; +#ifdef CONFIG_SECURITY_PROC_MEM_FOLL_FORCE_RESTRICT + /* Deny if FOLL_FORCE is disabled via Kconfig */ + if (!strncmp(CONFIG_SECURITY_PROC_MEM_FOLL_FORCE_RESTRICT, "all", 3)) + return 0; + + /* Deny if FOLL_FORCE is only allowed for ptracers via Kconfig */ + if (!strncmp(CONFIG_SECURITY_PROC_MEM_FOLL_FORCE_RESTRICT, "ptracer", 7) && + !__mem_open_current_is_ptracer(file)) + return 0; +#endif + return FOLL_FORCE; } diff --git a/security/Kconfig b/security/Kconfig index 412e76f1575d0..31a588cedec8d 100644 --- a/security/Kconfig +++ b/security/Kconfig @@ -19,6 +19,48 @@ config SECURITY_DMESG_RESTRICT If you are unsure how to answer this question, answer N. +config SECURITY_PROC_MEM_READ_RESTRICT + string "Restrict read access to /proc/*/mem files" + depends on PROC_FS + default "none" + help + This option allows specifying a restriction level for read access + to /proc/*/mem files. Can be one of: + - 'all' restricts all access unconditionally. + - 'ptracer' allows access only for ptracer processes. + + This can also be set at boot with the "restrict_proc_mem_read=" param. + + If unsure leave empty to continue using basic file permissions. + +config SECURITY_PROC_MEM_WRITE_RESTRICT + string "Restrict write access to /proc/*/mem files" + depends on PROC_FS + default "none" + help + This option allows specifying a restriction level for write access + to /proc/*/mem files. Can be one of: + - 'all' restricts all access unconditionally. + - 'ptracer' allows access only for ptracer processes. + + This can also be set at boot with the "restrict_proc_mem_write=" param. + + If unsure leave empty to continue using basic file permissions. + +config SECURITY_PROC_MEM_FOLL_FORCE_RESTRICT + string "Restrict use of FOLL_FORCE for /proc/*/mem access" + depends on PROC_FS + default "" + help + This option allows specifying a restriction level for FOLL_FORCE usage + for /proc/*/mem access. Can be one of: + - 'all' restricts all access unconditionally. + - 'ptracer' allows access only for ptracer processes. + + This can also be set at boot with the "restrict_proc_mem_foll_force=" param. + + If unsure leave empty to continue using FOLL_FORCE without restriction. + config SECURITY bool "Enable different security models" depends on SYSFS -- 2.30.2