linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gerhard Heift <gerhard@heift.name>
To: David Sterba <dsterba@suse.cz>,
	Gerhard Heift <gerhard@heift.name>,
	linux-btrfs@vger.kernel.org
Subject: Re: [PATCH RFCv2 4/6] btrfs: new ioctl TREE_SEARCH_V2
Date: Tue, 28 Jan 2014 01:33:58 +0100	[thread overview]
Message-ID: <CACeaM898oNxquu8p4vaMV13Erbro_pPzt1gLRkGTP7JhpknVxw@mail.gmail.com> (raw)
In-Reply-To: <20140127174113.GC6498@twin.jikos.cz>

2014-01-27 David Sterba <dsterba@suse.cz>:
> On Mon, Jan 27, 2014 at 02:28:30PM +0100, Gerhard Heift wrote:
>> --- a/fs/btrfs/ioctl.c
>> +++ b/fs/btrfs/ioctl.c
>> @@ -2032,6 +2032,52 @@ static noinline int btrfs_ioctl_tree_search(struct file *file,
>>       return ret;
>>  }
>>
>> +static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
>> +                                        void __user *argp)
>> +{
>> +     struct btrfs_ioctl_search_args_v2 *args;
>> +     struct inode *inode;
>> +     int ret;
>> +     char *buf;
>> +     size_t buf_size;
>> +
>> +     if (!capable(CAP_SYS_ADMIN))
>> +             return -EPERM;
>> +
>> +     /* copy search header and buffer size */
>> +     args = memdup_user(argp, sizeof(*args));
>> +     if (IS_ERR(args))
>> +             return PTR_ERR(args);
>> +
>> +     buf_size = args->buf_size;
>> +
>> +     if (buf_size < sizeof(struct btrfs_ioctl_search_header)) {
>> +             kfree(args);
>> +             return -ENOMEM;
>
> ENOMEM does not seem correct here, it's not a memory allocation failure
> but rather an underflow of the buffer size, possibly EINVAL or EOVERFLOW
> as the other checks return.

Saw this to during the rewrite, fixed it for the new version.

>> +     }
>> +
>> +     /* limit memory */
>> +     if (buf_size > PAGE_SIZE * 32)
>> +             buf_size = PAGE_SIZE * 32;
>> +
>> +     buf = memdup_user(argp->buf, buf_size);
>
> Memory allocations are not that easy, getting a contiguous 32 * 4k = 128k
> buffer may often fail. And the point of the V2 ioctl was to avoid
> allocating the buffer, and do copy_to_user directly.
>
> Also, you remove this code in the next patch, I don't think this level
> of patch granularity is necessary.

In the new version I do it the other way around, first direct copy,
then the new ioctl, so this is not necessary any more.

>> +     if (IS_ERR(buf)) {
>> +             kfree(args);
>> +             return PTR_ERR(buf);
>> +     }
>> +
>> +     inode = file_inode(file);
>> +     ret = search_ioctl(inode, &args->key, buf_size, buf);
>> +     if (ret == 0 && (
>> +             copy_to_user(argp, args, sizeof(*args)) ||
>> +             copy_to_user(argp->buf, buf, buf_size)
>> +             ))
>> +             ret = -EFAULT;
>> +     kfree(buf);
>> +     kfree(args);
>> +     return ret;
>> +}

  reply	other threads:[~2014-01-28  0:33 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-27 13:28 [PATCH RFCv2] new ioctl TREE_SEARCH_V2 Gerhard Heift
2014-01-27 13:28 ` [PATCH RFCv2 1/6] btrfs: search_ioctl accepts varying buffer Gerhard Heift
2014-01-27 17:19   ` David Sterba
2014-01-27 13:28 ` [PATCH RFCv2 2/6] btrfs: search_ioctl rejects unused setted values Gerhard Heift
2014-01-27 17:28   ` David Sterba
2014-01-28  0:32     ` Gerhard Heift
2014-01-29 17:12       ` David Sterba
2014-01-27 19:06   ` Martin Steigerwald
2014-01-27 13:28 ` [PATCH RFCv2 3/6] btrfs: copy_to_sk returns EOVERFLOW for too small buffer Gerhard Heift
2014-01-27 13:28 ` [PATCH RFCv2 4/6] btrfs: new ioctl TREE_SEARCH_V2 Gerhard Heift
2014-01-27 17:41   ` David Sterba
2014-01-28  0:33     ` Gerhard Heift [this message]
2014-01-27 13:28 ` [PATCH RFCv2 5/6] btrfs: search_ioctl: direct copy to userspace Gerhard Heift
2014-01-27 18:11   ` David Sterba
2014-01-28  0:35     ` Gerhard Heift
2014-01-27 13:28 ` [PATCH RFCv2 6/6] btrfs: in tree_search extent buffer lifetime Gerhard Heift
2014-01-27 17:15 ` [PATCH RFCv2] new ioctl TREE_SEARCH_V2 David Sterba
2014-01-27 19:10 ` Goffredo Baroncelli
2014-01-27 19:31   ` Hugo Mills
2014-01-27 21:33     ` Goffredo Baroncelli
2014-01-28  9:29 ` Anand Jain
2014-01-28 12:51   ` Gerhard Heift

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=CACeaM898oNxquu8p4vaMV13Erbro_pPzt1gLRkGTP7JhpknVxw@mail.gmail.com \
    --to=gerhard@heift.name \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).