2010-08-02 08:31:23

by Kulikov Vasiliy

[permalink] [raw]
Subject: [PATCH 1/9 v2] staging: dt3155: check put_user() return value

put_user() return code was not checked for errors. To simplify the code
fill local struct dt3155_read with data and copy it with single
copy_to_user() call.

Signed-off-by: Kulikov Vasiliy <[email protected]>
---
drivers/staging/dt3155/dt3155_drv.c | 17 ++++++++---------
1 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/drivers/staging/dt3155/dt3155_drv.c b/drivers/staging/dt3155/dt3155_drv.c
index 7316996..4013f09 100644
--- a/drivers/staging/dt3155/dt3155_drv.c
+++ b/drivers/staging/dt3155/dt3155_drv.c
@@ -745,10 +745,11 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,
struct dt3155_status *dts = &dt3155_status[minor];
struct dt3155_fbuffer *fb = &dts->fbuffer;
struct frame_info *frame_info;
+ struct dt3155_read dt_read;

/* TODO: this should check the error flag and */
/* return an error on hardware failures */
- if (count != sizeof(struct dt3155_read))
+ if (count != sizeof(dt_read))
{
printk(KERN_INFO "DT3155 ERROR (NJC): count is not right\n");
return -EINVAL;
@@ -797,17 +798,15 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,

/* make this an offset */
offset = frame_info->addr - dts->mem_addr;
+ dt_read.offset = offset;
+ dt_read.frame_seq = fb->frame_count;
+ dt_read.state = dts->state;
+ memcpy(&dt_read.frame_info, frame_info, sizeof(frame_info));

- put_user(offset, (unsigned int __user *)buf);
- buf += sizeof(u32);
- put_user(fb->frame_count, (unsigned int __user *)buf);
- buf += sizeof(u32);
- put_user(dts->state, (unsigned int __user *)buf);
- buf += sizeof(u32);
- if (copy_to_user(buf, frame_info, sizeof(*frame_info)))
+ if (copy_to_user(buf, &dt_read, sizeof(dt_read)))
return -EFAULT;

- return sizeof(struct dt3155_read);
+ return sizeof(dt_read);
}

static unsigned int dt3155_poll (struct file * filp, poll_table *wait)
--
1.7.0.4


2010-08-02 12:41:31

by Simon Horman

[permalink] [raw]
Subject: Re: [PATCH 1/9 v2] staging: dt3155: check put_user() return value

On Mon, Aug 02, 2010 at 12:31:12PM +0400, Kulikov Vasiliy wrote:
> put_user() return code was not checked for errors. To simplify the code
> fill local struct dt3155_read with data and copy it with single
> copy_to_user() call.
>
> Signed-off-by: Kulikov Vasiliy <[email protected]>
> ---
> drivers/staging/dt3155/dt3155_drv.c | 17 ++++++++---------
> 1 files changed, 8 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/staging/dt3155/dt3155_drv.c b/drivers/staging/dt3155/dt3155_drv.c
> index 7316996..4013f09 100644
> --- a/drivers/staging/dt3155/dt3155_drv.c
> +++ b/drivers/staging/dt3155/dt3155_drv.c
> @@ -745,10 +745,11 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,
> struct dt3155_status *dts = &dt3155_status[minor];
> struct dt3155_fbuffer *fb = &dts->fbuffer;
> struct frame_info *frame_info;
> + struct dt3155_read dt_read;
>
> /* TODO: this should check the error flag and */
> /* return an error on hardware failures */
> - if (count != sizeof(struct dt3155_read))
> + if (count != sizeof(dt_read))

I believe that sizeof(struct dt3155_read) is preferred,
so this change isn't needed and sizeof(struct dt3155_read)
should be used below (three times).

> {
> printk(KERN_INFO "DT3155 ERROR (NJC): count is not right\n");
> return -EINVAL;
> @@ -797,17 +798,15 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,
>
> /* make this an offset */
> offset = frame_info->addr - dts->mem_addr;
> + dt_read.offset = offset;
> + dt_read.frame_seq = fb->frame_count;
> + dt_read.state = dts->state;
> + memcpy(&dt_read.frame_info, frame_info, sizeof(frame_info));
>
> - put_user(offset, (unsigned int __user *)buf);
> - buf += sizeof(u32);
> - put_user(fb->frame_count, (unsigned int __user *)buf);
> - buf += sizeof(u32);
> - put_user(dts->state, (unsigned int __user *)buf);
> - buf += sizeof(u32);
> - if (copy_to_user(buf, frame_info, sizeof(*frame_info)))
> + if (copy_to_user(buf, &dt_read, sizeof(dt_read)))
> return -EFAULT;
>
> - return sizeof(struct dt3155_read);
> + return sizeof(dt_read);
> }
>
> static unsigned int dt3155_poll (struct file * filp, poll_table *wait)
> --
> 1.7.0.4

2010-08-02 12:56:45

by Kulikov Vasiliy

[permalink] [raw]
Subject: [PATCH 1/9 v3] staging: dt3155: check put_user() return value

put_user() returns code was not checked for errors. To simplify the code
fill local struct dt3155_read with data and copy it with single
copy_to_user() call.

Signed-off-by: Kulikov Vasiliy <[email protected]>
---
drivers/staging/dt3155/dt3155_drv.c | 13 ++++++-------
1 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/dt3155/dt3155_drv.c b/drivers/staging/dt3155/dt3155_drv.c
index 7316996..6031837 100644
--- a/drivers/staging/dt3155/dt3155_drv.c
+++ b/drivers/staging/dt3155/dt3155_drv.c
@@ -745,6 +745,7 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,
struct dt3155_status *dts = &dt3155_status[minor];
struct dt3155_fbuffer *fb = &dts->fbuffer;
struct frame_info *frame_info;
+ struct dt3155_read dt_read;

/* TODO: this should check the error flag and */
/* return an error on hardware failures */
@@ -797,14 +798,12 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,

/* make this an offset */
offset = frame_info->addr - dts->mem_addr;
+ dt_read.offset = offset;
+ dt_read.frame_seq = fb->frame_count;
+ dt_read.state = dts->state;
+ memcpy(&dt_read.frame_info, frame_info, sizeof(struct frame_info));

- put_user(offset, (unsigned int __user *)buf);
- buf += sizeof(u32);
- put_user(fb->frame_count, (unsigned int __user *)buf);
- buf += sizeof(u32);
- put_user(dts->state, (unsigned int __user *)buf);
- buf += sizeof(u32);
- if (copy_to_user(buf, frame_info, sizeof(*frame_info)))
+ if (copy_to_user(buf, &dt_read, sizeof(struct dt3155_read)))
return -EFAULT;

return sizeof(struct dt3155_read);
--
1.7.0.4

2010-08-02 13:51:42

by Simon Horman

[permalink] [raw]
Subject: Re: [PATCH 1/9 v3] staging: dt3155: check put_user() return value

On Mon, Aug 02, 2010 at 04:56:38PM +0400, Vasiliy Kulikov wrote:
> put_user() returns code was not checked for errors. To simplify the code
> fill local struct dt3155_read with data and copy it with single
> copy_to_user() call.
>
> Signed-off-by: Kulikov Vasiliy <[email protected]>

Looks good to me.

Reviewed-by: Simon Horman <[email protected]>

> ---
> drivers/staging/dt3155/dt3155_drv.c | 13 ++++++-------
> 1 files changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/staging/dt3155/dt3155_drv.c b/drivers/staging/dt3155/dt3155_drv.c
> index 7316996..6031837 100644
> --- a/drivers/staging/dt3155/dt3155_drv.c
> +++ b/drivers/staging/dt3155/dt3155_drv.c
> @@ -745,6 +745,7 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,
> struct dt3155_status *dts = &dt3155_status[minor];
> struct dt3155_fbuffer *fb = &dts->fbuffer;
> struct frame_info *frame_info;
> + struct dt3155_read dt_read;
>
> /* TODO: this should check the error flag and */
> /* return an error on hardware failures */
> @@ -797,14 +798,12 @@ static ssize_t dt3155_read(struct file *filep, char __user *buf,
>
> /* make this an offset */
> offset = frame_info->addr - dts->mem_addr;
> + dt_read.offset = offset;
> + dt_read.frame_seq = fb->frame_count;
> + dt_read.state = dts->state;
> + memcpy(&dt_read.frame_info, frame_info, sizeof(struct frame_info));
>
> - put_user(offset, (unsigned int __user *)buf);
> - buf += sizeof(u32);
> - put_user(fb->frame_count, (unsigned int __user *)buf);
> - buf += sizeof(u32);
> - put_user(dts->state, (unsigned int __user *)buf);
> - buf += sizeof(u32);
> - if (copy_to_user(buf, frame_info, sizeof(*frame_info)))
> + if (copy_to_user(buf, &dt_read, sizeof(struct dt3155_read)))
> return -EFAULT;
>
> return sizeof(struct dt3155_read);
> --
> 1.7.0.4