Skip to contentSkip to conversation

ACPI / debugger: copy_to_user doesn't return errors

PATCH In review Dan Carpenter 3 participants 4 messages Dec 24, 2015 – Dec 25, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 ACPI / debugger: copy_to_user doesn't return errors

The copy_to/from_user() functions don't return error codes, they return the number of bytes remaining. We had intended to return -EFUALT here. We actually have already checked access_ok() in an earlier function so I don't think these functions will fail but let's fix it anyway.

drivers/acpi/acpi_dbg.c +6 -4
index f2c92ab..2a1777b 100644
@@ -592,9 +592,10 @@ static int acpi_aml_read_user(char __user *buf, int len)
 	smp_rmb();
 	p = &crc->buf[crc->tail];
 	n = min(len, circ_count_to_end(crc));
-	ret = copy_to_user(buf, p, n);
-	if (IS_ERR_VALUE(ret))
+	if (copy_to_user(buf, p, n)) {
+		ret = -EFAULT;
 		goto out;
+	}
 	/* sync tail after removing logs */
 	smp_mb();
 	crc->tail = (crc->tail + n) & (ACPI_AML_BUF_SIZE - 1);
@@ -661,9 +662,10 @@ static int acpi_aml_write_user(const char __user *buf, int len)
 	smp_mb();
 	p = &crc->buf[crc->head];
 	n = min(len, circ_space_to_end(crc));
-	ret = copy_from_user(p, buf, n);
-	if (IS_ERR_VALUE(ret))
+	if (copy_from_user(p, buf, n)) {
+		ret = -EFAULT;
 		goto out;
+	}
 	/* sync head after inserting cmds */
 	smp_wmb();
 	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
#1 lore raw
Quoted 12 lines · Dan Carpenter

The copy_to/from_user() functions don't return error codes, they return the number of bytes remaining. We had intended to return -EFUALT here. We actually have already checked access_ok() in an earlier function so I don't think these functions will fail but let's fix it anyway.

drivers/acpi/acpi_dbg.c +0 -0
index f2c92ab..2a1777b 100644

drivers/acpi/acpi_dbg.c static int acpi_aml_read_user(char __user *buf, int len) · line 596

Quoted 5 lines
drivers/acpi/acpi_dbg.c +0 -2
@@ -592,9 +592,10 @@ static int acpi_aml_read_user(char __user *buf, int len)
 	smp_rmb();
 	p = &crc->buf[crc->tail];
 	n = min(len, circ_count_to_end(crc));
-	ret = copy_to_user(buf, p, n);
-	if (IS_ERR_VALUE(ret))

I'm not familiar with IS_ERR_VALUE. Is it to allow functions that return unsigned values to also return negative error codes?

thanks,
julia

Quoted 25 lines
drivers/acpi/acpi_dbg.c +6 -3
+	if (copy_to_user(buf, p, n)) {
+		ret = -EFAULT;
 		goto out;
+	}
 	/* sync tail after removing logs */
 	smp_mb();
 	crc->tail = (crc->tail + n) & (ACPI_AML_BUF_SIZE - 1);
@@ -661,9 +662,10 @@ static int acpi_aml_write_user(const char __user *buf, int len)
 	smp_mb();
 	p = &crc->buf[crc->head];
 	n = min(len, circ_space_to_end(crc));
-	ret = copy_from_user(p, buf, n);
-	if (IS_ERR_VALUE(ret))
+	if (copy_from_user(p, buf, n)) {
+		ret = -EFAULT;
 		goto out;
+	}
 	/* sync head after inserting cmds */
 	smp_wmb();
 	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
--

To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 9 lines · Julia Lawall
Quoted 7 lines · Dan Carpenter

The copy_to/from_user() functions don't return error codes, they return the number of bytes remaining. We had intended to return -EFUALT here. We actually have already checked access_ok() in an earlier function so I don't think these functions will fail but let's fix it anyway.

Quoted 13 lines

drivers/acpi/acpi_dbg.c static int acpi_aml_read_user(char __user *buf, int len) · line 596

Quoted 10 lines
drivers/acpi/acpi_dbg.c +0 -2
index f2c92ab..2a1777b 100644
@@ -592,9 +592,10 @@ static int acpi_aml_read_user(char __user *buf, int len)
 	smp_rmb();
 	p = &crc->buf[crc->tail];
 	n = min(len, circ_count_to_end(crc));
-	ret = copy_to_user(buf, p, n);
-	if (IS_ERR_VALUE(ret))

I'm not familiar with IS_ERR_VALUE. Is it to allow functions that return unsigned values to also return negative error codes?

Yes, but here it is used as a substitue for checking if (ret < 0) or if (ret >= 0). I would have prefered to open code it. Also I prefer if (ret) for when the value is either zero or negative error codes but here we have standardized on IS_ERR_VALUE(ret) for everything.

regards,
dan carpenter

#3 lore raw

Hi,

Quoted 29 lines

From: Dan Carpenter [mailto:[email protected]]
Sent: Friday, December 25, 2015 3:16 AM
Subject: Re: [patch 2/2] ACPI / debugger: copy_to_user doesn't return errors

Quoted 7 lines · Julia Lawall
Quoted 5 lines · Dan Carpenter

The copy_to/from_user() functions don't return error codes, they return the number of bytes remaining. We had intended to return -EFUALT here. We actually have already checked access_ok() in an earlier function so I don't think these functions will fail but let's fix it anyway.

Quoted 7 lines

drivers/acpi/acpi_dbg.c static int acpi_aml_read_user(char __user *buf,

Quoted 7 lines
drivers/acpi/acpi_dbg.c +0 -0
index f2c92ab..2a1777b 100644
@@ -592,9 +592,10 @@ static int acpi_aml_read_user(char __user *buf,

int len)

Quoted 8 lines

drivers/acpi/acpi_dbg.c static int acpi_aml_read_user(char __user *buf, int len) · line 596

Quoted 5 lines
drivers/acpi/acpi_dbg.c +0 -2
 	smp_rmb();
 	p = &crc->buf[crc->tail];
 	n = min(len, circ_count_to_end(crc));
-	ret = copy_to_user(buf, p, n);
-	if (IS_ERR_VALUE(ret))

I'm not familiar with IS_ERR_VALUE. Is it to allow functions that return unsigned values to also return negative error codes?

Quoted 4 lines

Yes, but here it is used as a substitue for checking if (ret < 0) or if (ret >= 0). I would have prefered to open code it. Also I prefer if (ret) for when the value is either zero or negative error codes but here we have standardized on IS_ERR_VALUE(ret) for everything.

[Lv Zheng]
This patch looks correct.
It's good for me to know the rule for copy_to/from_user(). Thanks for pointing out.

Thanks and best regards
-Lv

Quoted 3 lines

regards,
dan carpenter