mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
	linux-kernel@vger.kernel.org, Christoph Hellwig <hch@lst.de>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Kees Cook <keescook@chromium.org>
Cc: linux-abi@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Alexey Dobriyan <adobriyan@gmail.com>
Subject: Re: [PATCH] sysctl: Limit the size of I/Os to PAGE_SIZE
Date: Thu, 13 May 2021 12:12:54 -0400	[thread overview]
Message-ID: <47a34aa5-ad1a-6259-d9cb-f85f314f9ffb@toxicpanda.com> (raw)
In-Reply-To: <20210513160649.2280429-1-willy@infradead.org>

On 5/13/21 12:06 PM, Matthew Wilcox (Oracle) wrote:
> We currently allow a read or a write that is up to KMALLOC_MAX_SIZE.
> This has caused problems when cat decides to do a 64kB read and
> so we allocate a 64kB buffer for the sysctl handler to store into.
> The immediate problem was fixed by switching to kvmalloc(), but it's
> ridiculous to allocate so much memory to read what is likely to be a
> few bytes.
> 
> sysfs limits reads and writes to PAGE_SIZE, and I feel we should do the
> same for sysctl.  The largest sysctl anyone's been able to come up with
> is 433 bytes for /proc/sys/dev/cdrom/info
> 
> This will allow simplifying the BPF sysctl code later, but I'll leave
> that for someone who understands it better.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>   fs/proc/proc_sysctl.c | 15 +++++++++------
>   1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> index dea0f5ee540c..a97a8a4ff270 100644
> --- a/fs/proc/proc_sysctl.c
> +++ b/fs/proc/proc_sysctl.c
> @@ -562,11 +562,14 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
>   	if (!table->proc_handler)
>   		goto out;
>   
> -	/* don't even try if the size is too large */
> +	/* reads may return short values; large writes must fail now */
> +	if (count >= PAGE_SIZE) {
> +		if (write)
> +			goto out;
> +		count = PAGE_SIZE;
> +	}
>   	error = -ENOMEM;
> -	if (count >= KMALLOC_MAX_SIZE)
> -		goto out;
> -	kbuf = kvzalloc(count + 1, GFP_KERNEL);
> +	kbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
>   	if (!kbuf)
>   		goto out;
>   

Below here we have

kbuf[count] = '\0';

with will overflow with this patch.  So maybe instead we do

if (count >= PAGE_SIZE)
	count = PAGE_SIZE - 1;
kbuf = kmalloc(count);

and that solves your problem and keeps us from overflowing.  Thanks,

Josef

  reply	other threads:[~2021-05-13 16:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-13 16:06 Matthew Wilcox (Oracle)
2021-05-13 16:12 ` Josef Bacik [this message]
2021-05-13 16:15   ` Josef Bacik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47a34aa5-ad1a-6259-d9cb-f85f314f9ffb@toxicpanda.com \
    --to=josef@toxicpanda.com \
    --cc=adobriyan@gmail.com \
    --cc=hch@lst.de \
    --cc=keescook@chromium.org \
    --cc=linux-abi@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®