diff mbox series

[v3,2/2] proc: add Kconfigs to restrict /proc/pid/mem access

Message ID 20240409175750.206445-2-adrian.ratiu@collabora.com (mailing list archive)
State New, archived
Headers show
Series [v3,1/2] proc: restrict /proc/pid/mem access via param knobs | expand

Commit Message

Adrian Ratiu April 9, 2024, 5:57 p.m. UTC
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 <groeck@chromium.org>
Cc: Doug Anderson <dianders@chromium.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Jann Horn <jannh@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Christian Brauner <brauner@kernel.org>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 fs/proc/base.c   | 33 +++++++++++++++++++++++++++++++++
 security/Kconfig | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)

Comments

Kees Cook April 26, 2024, 11:16 p.m. UTC | #1
On Tue, Apr 09, 2024 at 08:57:50PM +0300, Adrian Ratiu wrote:
> 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 <groeck@chromium.org>
> Cc: Doug Anderson <dianders@chromium.org>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Jann Horn <jannh@google.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Randy Dunlap <rdunlap@infradead.org>
> Cc: Christian Brauner <brauner@kernel.org>
> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
> ---
>  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

No, please. :)

Just use use the _MAYBE/_maybe variants of the static branch DECLAREs and
branches, and make Kconfigs for:

CONFIG_PROC_MEM_RESTRICT_READ_DEFAULT
CONFIG_PROC_MEM_RESTRICT_WRITE_DEFAULT
CONFIG_PROC_MEM_RESTRICT_FOLL_FORCE_DEFAULT

Like:

DECLARE_STATIC_KEY_MAYBE(CONFIG_PROC_MEM_RESTRICT_READ_DEFAULT, proc_mem_restrict_read);

and then later:

	if (static_branch_maybe(CONFIG_PROC_MEM_RESTRICT_READ_DEFAULT,
				&proc_mem_restrict_read))
		...


Then all builds of the kernel will have it available, but system
builders who want it enabled by default will get a slightly more
optimized "if".

-Kees
diff mbox series

Patch

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