linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] pid: Replace struct pid 1-element array with flex-array
@ 2023-06-30 18:04 Kees Cook
  2023-06-30 19:00 ` Gustavo A. R. Silva
  2023-07-01  6:24 ` Christian Brauner
  0 siblings, 2 replies; 3+ messages in thread
From: Kees Cook @ 2023-06-30 18:04 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Kees Cook, Jan Kara, Andreas Gruenbacher, Daniel Verkamp,
	Paul E. McKenney, Jeff Xu, Andrew Morton, Boqun Feng,
	Luis Chamberlain, Frederic Weisbecker,
	syzbot+ac3b41786a2d0565b6d5, linux-kernel, linux-hardening

For pid namespaces, struct pid uses a dynamically sized array member,
"numbers". This was implemented using the ancient 1-element fake flexible
array, which has been deprecated for decades. Replace it with a C99
flexible array, refactor the array size calculations to use struct_size(),
and address elements via indexes. Note that the static initializer (which
defines a single element) works as-is, and requires no special handling.

Without this, CONFIG_UBSAN_BOUNDS (and potentially CONFIG_FORTIFY_SOURCE)
will trigger bounds checks when entering a pid namespace:
https://lore.kernel.org/lkml/20230517-bushaltestelle-super-e223978c1ba6@brauner

For example: unshare --fork --pid --mount-proc readlink /proc/self

Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Andreas Gruenbacher <agruenba@redhat.com>
Cc: Daniel Verkamp <dverkamp@chromium.org>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Jeff Xu <jeffxu@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Frederic Weisbecker <frederic@kernel.org>
Reported-by: syzbot+ac3b41786a2d0565b6d5@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/000000000000c6de2a05fbdecbbb@google.com/
Acked-by: Jeff Xu <jeffxu@google.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
v2:
 - drop pointer math to array index conversions (torvalds)
 - use struct_size_t now that it exists (torvalds)
 - updated commit log with reproducer example
v1: https://lore.kernel.org/lkml/20230517225838.never.965-kees@kernel.org/
---
 include/linux/pid.h    | 2 +-
 kernel/pid.c           | 7 +++++--
 kernel/pid_namespace.c | 2 +-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/include/linux/pid.h b/include/linux/pid.h
index b75de288a8c2..653a527574c4 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -67,7 +67,7 @@ struct pid
 	/* wait queue for pidfd notifications */
 	wait_queue_head_t wait_pidfd;
 	struct rcu_head rcu;
-	struct upid numbers[1];
+	struct upid numbers[];
 };
 
 extern struct pid init_struct_pid;
diff --git a/kernel/pid.c b/kernel/pid.c
index f93954a0384d..6a1d23a11026 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -656,8 +656,11 @@ void __init pid_idr_init(void)
 
 	idr_init(&init_pid_ns.idr);
 
-	init_pid_ns.pid_cachep = KMEM_CACHE(pid,
-			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
+	init_pid_ns.pid_cachep = kmem_cache_create("pid",
+			struct_size_t(struct pid, numbers, 1),
+			__alignof__(struct pid),
+			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT,
+			NULL);
 }
 
 static struct file *__pidfd_fget(struct task_struct *task, int fd)
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
index b43eee07b00c..0bf44afe04dd 100644
--- a/kernel/pid_namespace.c
+++ b/kernel/pid_namespace.c
@@ -48,7 +48,7 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 		return kc;
 
 	snprintf(name, sizeof(name), "pid_%u", level + 1);
-	len = sizeof(struct pid) + level * sizeof(struct upid);
+	len = struct_size_t(struct pid, numbers, level + 1);
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] pid: Replace struct pid 1-element array with flex-array
  2023-06-30 18:04 [PATCH v2] pid: Replace struct pid 1-element array with flex-array Kees Cook
@ 2023-06-30 19:00 ` Gustavo A. R. Silva
  2023-07-01  6:24 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2023-06-30 19:00 UTC (permalink / raw)
  To: Kees Cook, Christian Brauner
  Cc: Jan Kara, Andreas Gruenbacher, Daniel Verkamp, Paul E. McKenney,
	Jeff Xu, Andrew Morton, Boqun Feng, Luis Chamberlain,
	Frederic Weisbecker, syzbot+ac3b41786a2d0565b6d5, linux-kernel,
	linux-hardening



On 6/30/23 12:04, Kees Cook wrote:
> For pid namespaces, struct pid uses a dynamically sized array member,
> "numbers". This was implemented using the ancient 1-element fake flexible
> array, which has been deprecated for decades. Replace it with a C99
> flexible array, refactor the array size calculations to use struct_size(),
> and address elements via indexes. Note that the static initializer (which
> defines a single element) works as-is, and requires no special handling.
> 
> Without this, CONFIG_UBSAN_BOUNDS (and potentially CONFIG_FORTIFY_SOURCE)
> will trigger bounds checks when entering a pid namespace:
> https://lore.kernel.org/lkml/20230517-bushaltestelle-super-e223978c1ba6@brauner
> 
> For example: unshare --fork --pid --mount-proc readlink /proc/self
> 
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Jan Kara <jack@suse.cz>
> Cc: Andreas Gruenbacher <agruenba@redhat.com>
> Cc: Daniel Verkamp <dverkamp@chromium.org>
> Cc: "Paul E. McKenney" <paulmck@kernel.org>
> Cc: Jeff Xu <jeffxu@google.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Boqun Feng <boqun.feng@gmail.com>
> Cc: Luis Chamberlain <mcgrof@kernel.org>
> Cc: Frederic Weisbecker <frederic@kernel.org>
> Reported-by: syzbot+ac3b41786a2d0565b6d5@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/lkml/000000000000c6de2a05fbdecbbb@google.com/
> Acked-by: Jeff Xu <jeffxu@google.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>

I would vote in favor of the array over the pointer notation, but anyway... we need
to land this change, and that's what matters.

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks!
--
Gustavo

> ---
> v2:
>   - drop pointer math to array index conversions (torvalds)
>   - use struct_size_t now that it exists (torvalds)
>   - updated commit log with reproducer example
> v1: https://lore.kernel.org/lkml/20230517225838.never.965-kees@kernel.org/
> ---
>   include/linux/pid.h    | 2 +-
>   kernel/pid.c           | 7 +++++--
>   kernel/pid_namespace.c | 2 +-
>   3 files changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/include/linux/pid.h b/include/linux/pid.h
> index b75de288a8c2..653a527574c4 100644
> --- a/include/linux/pid.h
> +++ b/include/linux/pid.h
> @@ -67,7 +67,7 @@ struct pid
>   	/* wait queue for pidfd notifications */
>   	wait_queue_head_t wait_pidfd;
>   	struct rcu_head rcu;
> -	struct upid numbers[1];
> +	struct upid numbers[];
>   };
>   
>   extern struct pid init_struct_pid;
> diff --git a/kernel/pid.c b/kernel/pid.c
> index f93954a0384d..6a1d23a11026 100644
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -656,8 +656,11 @@ void __init pid_idr_init(void)
>   
>   	idr_init(&init_pid_ns.idr);
>   
> -	init_pid_ns.pid_cachep = KMEM_CACHE(pid,
> -			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
> +	init_pid_ns.pid_cachep = kmem_cache_create("pid",
> +			struct_size_t(struct pid, numbers, 1),
> +			__alignof__(struct pid),
> +			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT,
> +			NULL);
>   }
>   
>   static struct file *__pidfd_fget(struct task_struct *task, int fd)
> diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
> index b43eee07b00c..0bf44afe04dd 100644
> --- a/kernel/pid_namespace.c
> +++ b/kernel/pid_namespace.c
> @@ -48,7 +48,7 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
>   		return kc;
>   
>   	snprintf(name, sizeof(name), "pid_%u", level + 1);
> -	len = sizeof(struct pid) + level * sizeof(struct upid);
> +	len = struct_size_t(struct pid, numbers, level + 1);
>   	mutex_lock(&pid_caches_mutex);
>   	/* Name collision forces to do allocation under mutex. */
>   	if (!*pkc)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] pid: Replace struct pid 1-element array with flex-array
  2023-06-30 18:04 [PATCH v2] pid: Replace struct pid 1-element array with flex-array Kees Cook
  2023-06-30 19:00 ` Gustavo A. R. Silva
@ 2023-07-01  6:24 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2023-07-01  6:24 UTC (permalink / raw)
  To: Kees Cook
  Cc: Jan Kara, Andreas Gruenbacher, Daniel Verkamp, Paul E. McKenney,
	Jeff Xu, Andrew Morton, Boqun Feng, Luis Chamberlain,
	Frederic Weisbecker, syzbot+ac3b41786a2d0565b6d5, linux-kernel,
	linux-hardening

On Fri, Jun 30, 2023 at 11:04:22AM -0700, Kees Cook wrote:
> For pid namespaces, struct pid uses a dynamically sized array member,
> "numbers". This was implemented using the ancient 1-element fake flexible
> array, which has been deprecated for decades. Replace it with a C99
> flexible array, refactor the array size calculations to use struct_size(),
> and address elements via indexes. Note that the static initializer (which
> defines a single element) works as-is, and requires no special handling.
> 
> Without this, CONFIG_UBSAN_BOUNDS (and potentially CONFIG_FORTIFY_SOURCE)
> will trigger bounds checks when entering a pid namespace:
> https://lore.kernel.org/lkml/20230517-bushaltestelle-super-e223978c1ba6@brauner
> 
> For example: unshare --fork --pid --mount-proc readlink /proc/self
> 
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Jan Kara <jack@suse.cz>
> Cc: Andreas Gruenbacher <agruenba@redhat.com>
> Cc: Daniel Verkamp <dverkamp@chromium.org>
> Cc: "Paul E. McKenney" <paulmck@kernel.org>
> Cc: Jeff Xu <jeffxu@google.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Boqun Feng <boqun.feng@gmail.com>
> Cc: Luis Chamberlain <mcgrof@kernel.org>
> Cc: Frederic Weisbecker <frederic@kernel.org>
> Reported-by: syzbot+ac3b41786a2d0565b6d5@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/lkml/000000000000c6de2a05fbdecbbb@google.com/
> Acked-by: Jeff Xu <jeffxu@google.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> v2:
>  - drop pointer math to array index conversions (torvalds)
>  - use struct_size_t now that it exists (torvalds)
>  - updated commit log with reproducer example

Thanks for that. Linus already merged the other version I gave him with
his requested changes but without your struct_size_t() update. It didn't
exist when you originally sent that patch afaiu and I just realized that
it existed right now. In any case, I'm just going to send a trivial
follow-up patch switching the two places to struct_size_t().

Thanks, Kees!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-01  6:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-30 18:04 [PATCH v2] pid: Replace struct pid 1-element array with flex-array Kees Cook
2023-06-30 19:00 ` Gustavo A. R. Silva
2023-07-01  6:24 ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).