Re: [PATCH V2] Fix unsafe memory access by memcmp
From: Dan Carpenter
Date: Mon Apr 04 2022 - 10:38:11 EST
On Mon, Apr 04, 2022 at 04:29:48PM +0200, Fabio M. De Francesco wrote:
> On luned? 4 aprile 2022 14:03:32 CEST Dan Carpenter wrote:
> > On Mon, Apr 04, 2022 at 01:25:37PM +0200, Fabio M. De Francesco wrote:
> > > On luned? 4 aprile 2022 12:50:41 CEST Dan Carpenter wrote:
> > > > On Sun, Apr 03, 2022 at 10:52:07PM -0400, Charlie Sands wrote:
> > > > > This patch fixes sparse warnings about the memcmp function unsafely
> > > > > accessing userspace memory without first copying it to kernel space.
> > > > >
> > > > > Signed-off-by: Charlie Sands <sandsch@xxxxxxxxxxxxxxxxxxxxx>
> > > > > ---
> > > > >
> > > > > V2: Fixed checkpatch.pl warning and changed variable name as suggested
> > > > > by Greg K. H. and improved error checking on the "copy_from_user" function as
> > > > > suggested by Pavel Skripkin.
> > > > >
> > > > > drivers/staging/r8188eu/os_dep/ioctl_linux.c | 21 ++++++++++++--------
> > > > > 1 file changed, 13 insertions(+), 8 deletions(-)
> > > > >
> > > > > diff --git a/drivers/staging/r8188eu/os_dep/ioctl_linux.c b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> > > > > index 7df213856d66..4b4eec2bde96 100644
> > > > > --- a/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> > > > > +++ b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> > > > > @@ -3233,23 +3233,28 @@ static int rtw_p2p_get(struct net_device *dev,
> > > > > struct iw_request_info *info,
> > > > > union iwreq_data *wrqu, char *extra)
> > > > > {
> > > > > - if (!memcmp(wrqu->data.pointer, "status", 6)) {
> > > > > + char wrqu_data[9];
> > > > > +
> > > > > + if (copy_from_user(wrqu_data, wrqu->data.pointer, 9) != 0)
> > > > > + return 0;
> > > >
> > > > return -EFAULT; We can't assume that that user wants to copy 9 bytes
> > > > especially when they're passing a 4 character + NUL string.
> > > >
> > > > This is a custom ioctl. Called from ioctl_private_iw_point().
> > > >
> > > > I think if you try to dereference a user pointer like this then it will
> > > > cause a crash, right? So that means no one has ever tested or used this
> > > > code and we hopefully we can just delete it?
> > >
> > > After a quick look, I'm pretty confident that we can also delete
> > > rtw_p2p_get2() and rtw_p2p_set() unless I'm overlooking something.
> >
> > What are the problems with rtw_p2p_get2() and rtw_p2p_set()?
> >
> > regards,
> > dan carpenter
> >
> Is it safe to access user space pointers without using proper helpers?
No.
> In those cases I mean: is it safe without using copy_from_user()?
Correct. You need to use copy_from_user().
>
> As I said, perhaps I'm overlooking something. However my conclusions
> follow by your own argument.
>
> If I understand what you wrote, you asked to delete rtw_p2p_get()
> because it looks like nobody "has ever tested or used this code".
>
> rtw_p2p_get2() and rtw_p2p_set() use the same pattern of rtw_p2p_get()
> when they access user space without using the proper helpers.
Those functions use "extra" which is a kernel pointer. Which user
pointer do they use? Sparse doesn't detect it.
regards,
dan carpenter