Linux-Fsdevel Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] sysctl: Make sure proc handlers can't expose heap memory
@ 2020-05-04 19:08 Kees Cook
  2020-05-04 19:59 ` Luis Chamberlain
  2020-05-05  5:58 ` Christoph Hellwig
  0 siblings, 2 replies; 9+ messages in thread
From: Kees Cook @ 2020-05-04 19:08 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Luis Chamberlain, Iurii Zaikin, Alexey Dobriyan, linux-mm,
	linux-fsdevel, linux-kernel

Just as a precaution, make sure that proc handlers don't accidentally
grow "count" beyond the allocated kbuf size.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
This applies to hch's sysctl cleanup tree...
---
 fs/proc/proc_sysctl.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 15030784566c..535ab26473af 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -546,6 +546,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
 	struct inode *inode = file_inode(filp);
 	struct ctl_table_header *head = grab_header(inode);
 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
+	size_t count_max = count;
 	void *kbuf;
 	ssize_t error;
 
@@ -590,6 +591,8 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
 
 	if (!write) {
 		error = -EFAULT;
+		if (WARN_ON(count > count_max))
+			count = count_max;
 		if (copy_to_user(ubuf, kbuf, count))
 			goto out_free_buf;
 	}
-- 
2.20.1


-- 
Kees Cook

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-04 19:08 [PATCH] sysctl: Make sure proc handlers can't expose heap memory Kees Cook
@ 2020-05-04 19:59 ` Luis Chamberlain
  2020-05-04 20:32   ` Kees Cook
  2020-05-05  5:58 ` Christoph Hellwig
  1 sibling, 1 reply; 9+ messages in thread
From: Luis Chamberlain @ 2020-05-04 19:59 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, Iurii Zaikin, Alexey Dobriyan, linux-mm,
	linux-fsdevel, linux-kernel

On Mon, May 04, 2020 at 12:08:55PM -0700, Kees Cook wrote:
> Just as a precaution, make sure that proc handlers don't accidentally
> grow "count" beyond the allocated kbuf size.
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> This applies to hch's sysctl cleanup tree...
> ---
>  fs/proc/proc_sysctl.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> index 15030784566c..535ab26473af 100644
> --- a/fs/proc/proc_sysctl.c
> +++ b/fs/proc/proc_sysctl.c
> @@ -546,6 +546,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
>  	struct inode *inode = file_inode(filp);
>  	struct ctl_table_header *head = grab_header(inode);
>  	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
> +	size_t count_max = count;
>  	void *kbuf;
>  	ssize_t error;
>  
> @@ -590,6 +591,8 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
>  
>  	if (!write) {
>  		error = -EFAULT;
> +		if (WARN_ON(count > count_max))
> +			count = count_max;

That crash a system with panic-on-warn. I don't think we want that?

 Luis

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-04 19:59 ` Luis Chamberlain
@ 2020-05-04 20:32   ` Kees Cook
  2020-05-04 21:59     ` Luis Chamberlain
  0 siblings, 1 reply; 9+ messages in thread
From: Kees Cook @ 2020-05-04 20:32 UTC (permalink / raw)
  To: Luis Chamberlain
  Cc: Christoph Hellwig, Iurii Zaikin, Alexey Dobriyan, linux-mm,
	linux-fsdevel, linux-kernel

On Mon, May 04, 2020 at 07:59:37PM +0000, Luis Chamberlain wrote:
> On Mon, May 04, 2020 at 12:08:55PM -0700, Kees Cook wrote:
> > Just as a precaution, make sure that proc handlers don't accidentally
> > grow "count" beyond the allocated kbuf size.
> > 
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> > This applies to hch's sysctl cleanup tree...
> > ---
> >  fs/proc/proc_sysctl.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> > index 15030784566c..535ab26473af 100644
> > --- a/fs/proc/proc_sysctl.c
> > +++ b/fs/proc/proc_sysctl.c
> > @@ -546,6 +546,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> >  	struct inode *inode = file_inode(filp);
> >  	struct ctl_table_header *head = grab_header(inode);
> >  	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
> > +	size_t count_max = count;
> >  	void *kbuf;
> >  	ssize_t error;
> >  
> > @@ -590,6 +591,8 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> >  
> >  	if (!write) {
> >  		error = -EFAULT;
> > +		if (WARN_ON(count > count_max))
> > +			count = count_max;
> 
> That crash a system with panic-on-warn. I don't think we want that?

Eh? None of the handlers should be making this mistake currently and
it's not a mistake that can be controlled from userspace. WARN() is
absolutely what's wanted here: report an impossible situation (and
handle it gracefully for the bulk of users that don't have
panic_on_warn set).

-- 
Kees Cook

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-04 20:32   ` Kees Cook
@ 2020-05-04 21:59     ` Luis Chamberlain
  2020-05-05  6:34       ` Greg KH
  0 siblings, 1 reply; 9+ messages in thread
From: Luis Chamberlain @ 2020-05-04 21:59 UTC (permalink / raw)
  To: Kees Cook, Greg KH
  Cc: Christoph Hellwig, Iurii Zaikin, Alexey Dobriyan, linux-mm,
	linux-fsdevel, linux-kernel

On Mon, May 04, 2020 at 01:32:07PM -0700, Kees Cook wrote:
> On Mon, May 04, 2020 at 07:59:37PM +0000, Luis Chamberlain wrote:
> > On Mon, May 04, 2020 at 12:08:55PM -0700, Kees Cook wrote:
> > > Just as a precaution, make sure that proc handlers don't accidentally
> > > grow "count" beyond the allocated kbuf size.
> > > 
> > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > ---
> > > This applies to hch's sysctl cleanup tree...
> > > ---
> > >  fs/proc/proc_sysctl.c | 3 +++
> > >  1 file changed, 3 insertions(+)
> > > 
> > > diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> > > index 15030784566c..535ab26473af 100644
> > > --- a/fs/proc/proc_sysctl.c
> > > +++ b/fs/proc/proc_sysctl.c
> > > @@ -546,6 +546,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > >  	struct inode *inode = file_inode(filp);
> > >  	struct ctl_table_header *head = grab_header(inode);
> > >  	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
> > > +	size_t count_max = count;
> > >  	void *kbuf;
> > >  	ssize_t error;
> > >  
> > > @@ -590,6 +591,8 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > >  
> > >  	if (!write) {
> > >  		error = -EFAULT;
> > > +		if (WARN_ON(count > count_max))
> > > +			count = count_max;
> > 
> > That would crash a system with panic-on-warn. I don't think we want that?
> 
> Eh? None of the handlers should be making this mistake currently and
> it's not a mistake that can be controlled from userspace. WARN() is
> absolutely what's wanted here: report an impossible situation (and
> handle it gracefully for the bulk of users that don't have
> panic_on_warn set).

Alrighty, Greg are you OK with this type of WARN_ON()? You recently
expressed concerns over its use due to panic-on-warn on another patch.

  LUis

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-04 19:08 [PATCH] sysctl: Make sure proc handlers can't expose heap memory Kees Cook
  2020-05-04 19:59 ` Luis Chamberlain
@ 2020-05-05  5:58 ` Christoph Hellwig
  1 sibling, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2020-05-05  5:58 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, Luis Chamberlain, Iurii Zaikin,
	Alexey Dobriyan, linux-mm, linux-fsdevel, linux-kernel

On Mon, May 04, 2020 at 12:08:55PM -0700, Kees Cook wrote:
> Just as a precaution, make sure that proc handlers don't accidentally
> grow "count" beyond the allocated kbuf size.
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> This applies to hch's sysctl cleanup tree...

This looks ok o me.  You should probably add Al to the Cc list as
he has picked up my series into a branch of vfs.git.

Acked-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-04 21:59     ` Luis Chamberlain
@ 2020-05-05  6:34       ` Greg KH
  2020-05-05 20:41         ` Kees Cook
  0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2020-05-05  6:34 UTC (permalink / raw)
  To: Luis Chamberlain
  Cc: Kees Cook, Christoph Hellwig, Iurii Zaikin, Alexey Dobriyan,
	linux-mm, linux-fsdevel, linux-kernel

On Mon, May 04, 2020 at 09:59:03PM +0000, Luis Chamberlain wrote:
> On Mon, May 04, 2020 at 01:32:07PM -0700, Kees Cook wrote:
> > On Mon, May 04, 2020 at 07:59:37PM +0000, Luis Chamberlain wrote:
> > > On Mon, May 04, 2020 at 12:08:55PM -0700, Kees Cook wrote:
> > > > Just as a precaution, make sure that proc handlers don't accidentally
> > > > grow "count" beyond the allocated kbuf size.
> > > > 
> > > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > > ---
> > > > This applies to hch's sysctl cleanup tree...
> > > > ---
> > > >  fs/proc/proc_sysctl.c | 3 +++
> > > >  1 file changed, 3 insertions(+)
> > > > 
> > > > diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> > > > index 15030784566c..535ab26473af 100644
> > > > --- a/fs/proc/proc_sysctl.c
> > > > +++ b/fs/proc/proc_sysctl.c
> > > > @@ -546,6 +546,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > > >  	struct inode *inode = file_inode(filp);
> > > >  	struct ctl_table_header *head = grab_header(inode);
> > > >  	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
> > > > +	size_t count_max = count;
> > > >  	void *kbuf;
> > > >  	ssize_t error;
> > > >  
> > > > @@ -590,6 +591,8 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > > >  
> > > >  	if (!write) {
> > > >  		error = -EFAULT;
> > > > +		if (WARN_ON(count > count_max))
> > > > +			count = count_max;
> > > 
> > > That would crash a system with panic-on-warn. I don't think we want that?
> > 
> > Eh? None of the handlers should be making this mistake currently and
> > it's not a mistake that can be controlled from userspace. WARN() is
> > absolutely what's wanted here: report an impossible situation (and
> > handle it gracefully for the bulk of users that don't have
> > panic_on_warn set).
> 
> Alrighty, Greg are you OK with this type of WARN_ON()? You recently
> expressed concerns over its use due to panic-on-warn on another patch.

We should never call WARN() on any path that a user can trigger.

If it is just a "the developer called this api in a foolish way" then we
could use a WARN_ON() to have them realize their mistake, but in my
personal experience, foolish developers don't even notice that kind of
mistake :(

thanks,

greg k-h

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-05  6:34       ` Greg KH
@ 2020-05-05 20:41         ` Kees Cook
  2020-05-05 22:03           ` Luis Chamberlain
  0 siblings, 1 reply; 9+ messages in thread
From: Kees Cook @ 2020-05-05 20:41 UTC (permalink / raw)
  To: Greg KH
  Cc: Luis Chamberlain, Christoph Hellwig, Iurii Zaikin,
	Alexey Dobriyan, linux-mm, linux-fsdevel, linux-kernel

On Tue, May 05, 2020 at 08:34:41AM +0200, Greg KH wrote:
> On Mon, May 04, 2020 at 09:59:03PM +0000, Luis Chamberlain wrote:
> > On Mon, May 04, 2020 at 01:32:07PM -0700, Kees Cook wrote:
> > > On Mon, May 04, 2020 at 07:59:37PM +0000, Luis Chamberlain wrote:
> > > > On Mon, May 04, 2020 at 12:08:55PM -0700, Kees Cook wrote:
> > > > > Just as a precaution, make sure that proc handlers don't accidentally
> > > > > grow "count" beyond the allocated kbuf size.
> > > > > 
> > > > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > > > ---
> > > > > This applies to hch's sysctl cleanup tree...
> > > > > ---
> > > > >  fs/proc/proc_sysctl.c | 3 +++
> > > > >  1 file changed, 3 insertions(+)
> > > > > 
> > > > > diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> > > > > index 15030784566c..535ab26473af 100644
> > > > > --- a/fs/proc/proc_sysctl.c
> > > > > +++ b/fs/proc/proc_sysctl.c
> > > > > @@ -546,6 +546,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > > > >  	struct inode *inode = file_inode(filp);
> > > > >  	struct ctl_table_header *head = grab_header(inode);
> > > > >  	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
> > > > > +	size_t count_max = count;
> > > > >  	void *kbuf;
> > > > >  	ssize_t error;
> > > > >  
> > > > > @@ -590,6 +591,8 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > > > >  
> > > > >  	if (!write) {
> > > > >  		error = -EFAULT;
> > > > > +		if (WARN_ON(count > count_max))
> > > > > +			count = count_max;
> > > > 
> > > > That would crash a system with panic-on-warn. I don't think we want that?
> > > 
> > > Eh? None of the handlers should be making this mistake currently and
> > > it's not a mistake that can be controlled from userspace. WARN() is
> > > absolutely what's wanted here: report an impossible situation (and
> > > handle it gracefully for the bulk of users that don't have
> > > panic_on_warn set).
> > 
> > Alrighty, Greg are you OK with this type of WARN_ON()? You recently
> > expressed concerns over its use due to panic-on-warn on another patch.
> 
> We should never call WARN() on any path that a user can trigger.
> 
> If it is just a "the developer called this api in a foolish way" then we
> could use a WARN_ON() to have them realize their mistake, but in my
> personal experience, foolish developers don't even notice that kind of
> mistake :(

Right -- while it'd be nice if the developer noticed it, it is _usually_
an unsuspecting end user (or fuzzer), in which case we absolutely want a
WARN (and not a BUG![1]) and have the situations handled gracefully, so
it can be reported and fixed.

-Kees

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#bug-and-bug-on

-- 
Kees Cook

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-05 20:41         ` Kees Cook
@ 2020-05-05 22:03           ` Luis Chamberlain
  2020-05-05 23:23             ` Kees Cook
  0 siblings, 1 reply; 9+ messages in thread
From: Luis Chamberlain @ 2020-05-05 22:03 UTC (permalink / raw)
  To: Kees Cook
  Cc: Greg KH, Christoph Hellwig, Iurii Zaikin, Alexey Dobriyan,
	linux-mm, linux-fsdevel, linux-kernel

On Tue, May 05, 2020 at 01:41:44PM -0700, Kees Cook wrote:
> On Tue, May 05, 2020 at 08:34:41AM +0200, Greg KH wrote:
> > On Mon, May 04, 2020 at 09:59:03PM +0000, Luis Chamberlain wrote:
> > > On Mon, May 04, 2020 at 01:32:07PM -0700, Kees Cook wrote:
> > > > On Mon, May 04, 2020 at 07:59:37PM +0000, Luis Chamberlain wrote:
> > > > > On Mon, May 04, 2020 at 12:08:55PM -0700, Kees Cook wrote:
> > > > > > Just as a precaution, make sure that proc handlers don't accidentally
> > > > > > grow "count" beyond the allocated kbuf size.
> > > > > > 
> > > > > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > > > > ---
> > > > > > This applies to hch's sysctl cleanup tree...
> > > > > > ---
> > > > > >  fs/proc/proc_sysctl.c | 3 +++
> > > > > >  1 file changed, 3 insertions(+)
> > > > > > 
> > > > > > diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> > > > > > index 15030784566c..535ab26473af 100644
> > > > > > --- a/fs/proc/proc_sysctl.c
> > > > > > +++ b/fs/proc/proc_sysctl.c
> > > > > > @@ -546,6 +546,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > > > > >  	struct inode *inode = file_inode(filp);
> > > > > >  	struct ctl_table_header *head = grab_header(inode);
> > > > > >  	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
> > > > > > +	size_t count_max = count;
> > > > > >  	void *kbuf;
> > > > > >  	ssize_t error;
> > > > > >  
> > > > > > @@ -590,6 +591,8 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
> > > > > >  
> > > > > >  	if (!write) {
> > > > > >  		error = -EFAULT;
> > > > > > +		if (WARN_ON(count > count_max))
> > > > > > +			count = count_max;
> > > > > 
> > > > > That would crash a system with panic-on-warn. I don't think we want that?
> > > > 
> > > > Eh? None of the handlers should be making this mistake currently and
> > > > it's not a mistake that can be controlled from userspace. WARN() is
> > > > absolutely what's wanted here: report an impossible situation (and
> > > > handle it gracefully for the bulk of users that don't have
> > > > panic_on_warn set).
> > > 
> > > Alrighty, Greg are you OK with this type of WARN_ON()? You recently
> > > expressed concerns over its use due to panic-on-warn on another patch.
> > 
> > We should never call WARN() on any path that a user can trigger.
> > 
> > If it is just a "the developer called this api in a foolish way" then we
> > could use a WARN_ON() to have them realize their mistake, but in my
> > personal experience, foolish developers don't even notice that kind of
> > mistake :(
> 
> Right -- while it'd be nice if the developer noticed it, it is _usually_
> an unsuspecting end user (or fuzzer), in which case we absolutely want a
> WARN (and not a BUG![1]) and have the situations handled gracefully, so
> it can be reported and fixed.

I've been using WARN*() for this exact purpose before, so I am as
surprised as you are bout these concerns. However if we have folks
shipping with panic-on-warn this would be rather detrimental to our
goals.

Greg, are you aware of folks shipping with panic-on-warn on some products?

  Luis

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

* Re: [PATCH] sysctl: Make sure proc handlers can't expose heap memory
  2020-05-05 22:03           ` Luis Chamberlain
@ 2020-05-05 23:23             ` Kees Cook
  0 siblings, 0 replies; 9+ messages in thread
From: Kees Cook @ 2020-05-05 23:23 UTC (permalink / raw)
  To: Luis Chamberlain
  Cc: Greg KH, Christoph Hellwig, Iurii Zaikin, Alexey Dobriyan,
	linux-mm, linux-fsdevel, linux-kernel

On Tue, May 05, 2020 at 10:03:27PM +0000, Luis Chamberlain wrote:
> On Tue, May 05, 2020 at 01:41:44PM -0700, Kees Cook wrote:
> > Right -- while it'd be nice if the developer noticed it, it is _usually_
> > an unsuspecting end user (or fuzzer), in which case we absolutely want a
> > WARN (and not a BUG![1]) and have the situations handled gracefully, so
> > it can be reported and fixed.
> 
> I've been using WARN*() for this exact purpose before, so I am as
> surprised as you are bout these concerns. However if we have folks

I don't see any mismatch here: it's not user-reachable, which is what
Greg said. WARN is for non-user-reachable "impossible situations". We
want to know if those can be hit (via bad API usage, races, etc). If
it's reachable from userspace, then it can't be a WARN() any more and
needs to be pr_warn().

> shipping with panic-on-warn this would be rather detrimental to our
> goals.
> 
> Greg, are you aware of folks shipping with panic-on-warn on some products?

People shipping with panic_on_warn are expecting to panic for WARNs like
this. :P

-- 
Kees Cook

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

end of thread, other threads:[~2020-05-05 23:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-04 19:08 [PATCH] sysctl: Make sure proc handlers can't expose heap memory Kees Cook
2020-05-04 19:59 ` Luis Chamberlain
2020-05-04 20:32   ` Kees Cook
2020-05-04 21:59     ` Luis Chamberlain
2020-05-05  6:34       ` Greg KH
2020-05-05 20:41         ` Kees Cook
2020-05-05 22:03           ` Luis Chamberlain
2020-05-05 23:23             ` Kees Cook
2020-05-05  5:58 ` Christoph Hellwig

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).