Skip to contentSkip to conversation

acpi: fix a potential inconsistency caused by double-fetch

PATCH Superseded Kangjie Lu 3 participants 6 messages Dec 25, 2018 – Jan 15, 2019 1 message not in the archive ACPI

lore
Share & tools
lore

Kangjie Lu

author
#0 lore raw

PATCH 1/1 acpi: fix a potential inconsistency caused by double-fetch

"user_buf->length" is in user space, and copied in twice. The second copy is after it passes the security check. If a user program races to change user_buf->length in user space, the data fetched in the second copy may invalidate the security check. The fix avoids the double-fetch issue by using the value passing the security check.

---
 drivers/acpi/custom_method.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/acpi/custom_method.c +5 -4
index 4451877f83b6..f75f664301b3 100644
@@ -26,17 +26,16 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
 	static u32 max_size;
 	static u32 uncopied_bytes;
 
-	struct acpi_table_header table;
 	acpi_status status;
 
 	if (!(*ppos)) {
 		/* parse the table header to get the table length */
 		if (count <= sizeof(struct acpi_table_header))
 			return -EINVAL;
-		if (copy_from_user(&table, user_buf,
-				   sizeof(struct acpi_table_header)))
+		if (get_user(max_size,
+				&(struct acpi_table_header *)user_buf->length))
 			return -EFAULT;
-		uncopied_bytes = max_size = table.length;
+		uncopied_bytes = max_size;
 		buf = kzalloc(max_size, GFP_KERNEL);
 		if (!buf)
 			return -ENOMEM;
@@ -56,6 +55,8 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
 		buf = NULL;
 		return -EFAULT;
 	}
+	/* Ensure table length is not changed in the second copy */
+	(struct acpi_table_header *)(buf + (*ppos))->length = max_size;
 
 	uncopied_bytes -= count;
 	*ppos += count;
Signature · 1 line
2.17.2 (Apple Git-113)
#1 lore raw
Thank you for the patch! Yet something to improve:

Hi Kangjie,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on pm/linux-next]
[also build test ERROR on v4.20 next-20181224]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Kangjie-Lu/acpi-fix-a-potential-inconsistency-caused-by-double-fetch/20181226-054721
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: x86_64-randconfig-x019-201851 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All error/warnings (new ones prefixed by >>):

In file included from include/linux/uaccess.h:14:0,
from drivers//acpi/custom_method.c:8:
drivers//acpi/custom_method.c: In function 'cm_write':

>> drivers//acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   arch/x86/include/asm/uaccess.h:138:41: note: in definition of macro '__inttype'
    __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
                                            ^
>> drivers//acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   arch/x86/include/asm/uaccess.h:138:12: error: first argument to '__builtin_choose_expr' not a constant
    __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
               ^
   arch/x86/include/asm/uaccess.h:174:11: note: in expansion of macro '__inttype'
     register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);  \
              ^~~~~~~~~
>> drivers//acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
>> drivers//acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   arch/x86/include/asm/uaccess.h:180:15: note: in definition of macro 'get_user'
           : "0" (ptr), "i" (sizeof(*(ptr))));  \
                  ^~~
>> drivers//acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   arch/x86/include/asm/uaccess.h:180:35: note: in definition of macro 'get_user'
           : "0" (ptr), "i" (sizeof(*(ptr))));  \
                                      ^~~
>> drivers//acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   arch/x86/include/asm/uaccess.h:181:30: note: in definition of macro 'get_user'
     (x) = (__force __typeof__(*(ptr))) __val_gu;   \
                                 ^~~
   drivers//acpi/custom_method.c:59:45: error: request for member 'length' in something not a structure or union
     (struct acpi_table_header *)(buf + (*ppos))->length = max_size;
                                                ^~

vim +/get_user +35 drivers//acpi/custom_method.c

   > 8	#include <linux/uaccess.h>
     9	#include <linux/debugfs.h>
    10	#include <linux/acpi.h>
    11	
    12	#include "internal.h"
    13	
    14	#define _COMPONENT		ACPI_SYSTEM_COMPONENT
    15	ACPI_MODULE_NAME("custom_method");
    16	MODULE_LICENSE("GPL");
    17	
    18	static struct dentry *cm_dentry;
    19	
    20	/* /sys/kernel/debug/acpi/custom_method */
    21	
    22	static ssize_t cm_write(struct file *file, const char __user * user_buf,
    23				size_t count, loff_t *ppos)
    24	{
    25		static char *buf;
    26		static u32 max_size;
    27		static u32 uncopied_bytes;
    28	
    29		acpi_status status;
    30	
    31		if (!(*ppos)) {
    32			/* parse the table header to get the table length */
    33			if (count <= sizeof(struct acpi_table_header))
    34				return -EINVAL;
  > 35			if (get_user(max_size,
  > 36					&(struct acpi_table_header *)user_buf->length))
    37				return -EFAULT;
    38			uncopied_bytes = max_size;
    39			buf = kzalloc(max_size, GFP_KERNEL);
    40			if (!buf)
    41				return -ENOMEM;
    42		}
    43	
    44		if (buf == NULL)
    45			return -EINVAL;
    46	
    47		if ((*ppos > max_size) ||
    48		    (*ppos + count > max_size) ||
    49		    (*ppos + count < count) ||
    50		    (count > uncopied_bytes))
    51			return -EINVAL;
    52	
    53		if (copy_from_user(buf + (*ppos), user_buf, count)) {
    54			kfree(buf);
    55			buf = NULL;
    56			return -EFAULT;
    57		}
    58		/* Ensure table length is not changed in the second copy */
    59		(struct acpi_table_header *)(buf + (*ppos))->length = max_size;
    60	
    61		uncopied_bytes -= count;
    62		*ppos += count;
    63	
    64		if (!uncopied_bytes) {
    65			status = acpi_install_method(buf);
    66			kfree(buf);
    67			buf = NULL;
    68			if (ACPI_FAILURE(status))
    69				return -EINVAL;
    70			add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
    71		}
    72	
    73		return count;
    74	}
    75	

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Kangjie Lu
Thank you for the patch! Perhaps something to improve:

Hi Kangjie,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on pm/linux-next]
[also build test WARNING on v4.20 next-20181224]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Kangjie-Lu/acpi-fix-a-potential-inconsistency-caused-by-double-fetch/20181226-054721
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: i386-randconfig-c0-12260412 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All warnings (new ones prefixed by >>):

In file included from include/linux/init.h:5:0,
from drivers/acpi/custom_method.c:5:
drivers/acpi/custom_method.c: In function 'cm_write':
drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union

        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   arch/x86/include/asm/uaccess.h:174:11: note: in expansion of macro '__inttype'
     register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);  \
              ^~~~~~~~~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   arch/x86/include/asm/uaccess.h:138:12: error: first argument to '__builtin_choose_expr' not a constant
    __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
               ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   arch/x86/include/asm/uaccess.h:174:11: note: in expansion of macro '__inttype'
     register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);  \
              ^~~~~~~~~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   arch/x86/include/asm/uaccess.h:174:11: note: in expansion of macro '__inttype'
     register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);  \
              ^~~~~~~~~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   arch/x86/include/asm/uaccess.h:138:12: error: first argument to '__builtin_choose_expr' not a constant
    __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
               ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   arch/x86/include/asm/uaccess.h:174:11: note: in expansion of macro '__inttype'
     register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);  \
              ^~~~~~~~~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   arch/x86/include/asm/uaccess.h:174:11: note: in expansion of macro '__inttype'
     register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);  \
              ^~~~~~~~~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   arch/x86/include/asm/uaccess.h:138:12: error: first argument to '__builtin_choose_expr' not a constant
    __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
               ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   arch/x86/include/asm/uaccess.h:174:11: note: in expansion of macro '__inttype'
     register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);  \
              ^~~~~~~~~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:36:42: error: request for member 'length' in something not a structure or union
        &(struct acpi_table_header *)user_buf->length))
                                             ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
>> drivers/acpi/custom_method.c:35:3: note: in expansion of macro 'if'
      if (get_user(max_size,
      ^~
   drivers/acpi/custom_method.c:35:7: note: in expansion of macro 'get_user'
      if (get_user(max_size,
          ^~~~~~~~
   drivers/acpi/custom_method.c:59:45: error: request for member 'length' in something not a structure or union
     (struct acpi_table_header *)(buf + (*ppos))->length = max_size;
                                                ^~

vim +/if +35 drivers/acpi/custom_method.c

   > 5	#include <linux/init.h>
     6	#include <linux/module.h>
     7	#include <linux/kernel.h>
     8	#include <linux/uaccess.h>
     9	#include <linux/debugfs.h>
    10	#include <linux/acpi.h>
    11	
    12	#include "internal.h"
    13	
    14	#define _COMPONENT		ACPI_SYSTEM_COMPONENT
    15	ACPI_MODULE_NAME("custom_method");
    16	MODULE_LICENSE("GPL");
    17	
    18	static struct dentry *cm_dentry;
    19	
    20	/* /sys/kernel/debug/acpi/custom_method */
    21	
    22	static ssize_t cm_write(struct file *file, const char __user * user_buf,
    23				size_t count, loff_t *ppos)
    24	{
    25		static char *buf;
    26		static u32 max_size;
    27		static u32 uncopied_bytes;
    28	
    29		acpi_status status;
    30	
    31		if (!(*ppos)) {
    32			/* parse the table header to get the table length */
    33			if (count <= sizeof(struct acpi_table_header))
    34				return -EINVAL;
  > 35			if (get_user(max_size,
    36					&(struct acpi_table_header *)user_buf->length))
    37				return -EFAULT;
    38			uncopied_bytes = max_size;
    39			buf = kzalloc(max_size, GFP_KERNEL);
    40			if (!buf)
    41				return -ENOMEM;
    42		}
    43	
    44		if (buf == NULL)
    45			return -EINVAL;
    46	
    47		if ((*ppos > max_size) ||
    48		    (*ppos + count > max_size) ||
    49		    (*ppos + count < count) ||
    50		    (count > uncopied_bytes))
    51			return -EINVAL;
    52	
    53		if (copy_from_user(buf + (*ppos), user_buf, count)) {
    54			kfree(buf);
    55			buf = NULL;
    56			return -EFAULT;
    57		}
    58		/* Ensure table length is not changed in the second copy */
    59		(struct acpi_table_header *)(buf + (*ppos))->length = max_size;
    60	
    61		uncopied_bytes -= count;
    62		*ppos += count;
    63	
    64		if (!uncopied_bytes) {
    65			status = acpi_install_method(buf);
    66			kfree(buf);
    67			buf = NULL;
    68			if (ACPI_FAILURE(status))
    69				return -EINVAL;
    70			add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
    71		}
    72	
    73		return count;
    74	}
    75	

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

author
#3 lore raw

PATCH v2 1/1 acpi: fix a potential inconsistency caused by double-fetch

In reply to Kangjie Lu

"user_buf->length" is in user space, and copied in twice. The second copy is after it passes the security check. If a user program races to change user_buf->length in user space, the data fetched in the second copy may invalidate the security check. The fix avoids the double-fetch issue by using the value passing the security check.

---
 drivers/acpi/custom_method.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/acpi/custom_method.c +6 -4
index 4451877f83b6..f10ee0519033 100644
@@ -26,17 +26,16 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
 	static u32 max_size;
 	static u32 uncopied_bytes;
 
-	struct acpi_table_header table;
 	acpi_status status;
 
 	if (!(*ppos)) {
 		/* parse the table header to get the table length */
 		if (count <= sizeof(struct acpi_table_header))
 			return -EINVAL;
-		if (copy_from_user(&table, user_buf,
-				   sizeof(struct acpi_table_header)))
+		if (get_user(max_size,
+					&((struct acpi_table_header *)user_buf)->length))
 			return -EFAULT;
-		uncopied_bytes = max_size = table.length;
+		uncopied_bytes = max_size;
 		buf = kzalloc(max_size, GFP_KERNEL);
 		if (!buf)
 			return -ENOMEM;
@@ -57,6 +56,9 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
 		return -EFAULT;
 	}
 
+	/* Ensure table length is not changed in the second copy */
+	((struct acpi_table_header *)(buf + (*ppos)))->length = max_size;
+
 	uncopied_bytes -= count;
 	*ppos += count;
 
Signature · 1 line
2.17.1
#4 lore raw
Quoted 6 lines · Kangjie Lu

"user_buf->length" is in user space, and copied in twice. The second copy is after it passes the security check. If a user program races to change user_buf->length in user space, the data fetched in the second copy may invalidate the security check. The fix avoids the double-fetch issue by using the value passing the security check.

AFAICS the patch really does two things: it avoids the issue described above and avoids using the (redundant) 'table' local variable on the stack. Arguably, you could fix the issue without getting rid of the redundant variable.

Quoted 32 lines
---
 drivers/acpi/custom_method.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/acpi/custom_method.c +3 -4
index 4451877f83b6..f10ee0519033 100644
@@ -26,17 +26,16 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
        static u32 max_size;
        static u32 uncopied_bytes;
 
-       struct acpi_table_header table;
        acpi_status status;
 
        if (!(*ppos)) {
                /* parse the table header to get the table length */
                if (count <= sizeof(struct acpi_table_header))
                        return -EINVAL;
-               if (copy_from_user(&table, user_buf,
-                                  sizeof(struct acpi_table_header)))
+               if (get_user(max_size,
+                                       &((struct acpi_table_header *)user_buf)->length))
                        return -EFAULT;
-               uncopied_bytes = max_size = table.length;
+               uncopied_bytes = max_size;
                buf = kzalloc(max_size, GFP_KERNEL);
                if (!buf)
                        return -ENOMEM;

drivers/acpi/custom_method.c static ssize_t cm_write(struct file *file, const char __user * user_buf, · line 60

Quoted 5 lines
drivers/acpi/custom_method.c +2 -0
@@ -57,6 +56,9 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
                return -EFAULT;
        }
 
+       /* Ensure table length is not changed in the second copy */
+       ((struct acpi_table_header *)(buf + (*ppos)))->length = max_size;

Why don't you return -EFAULT if max_size is different from ->length? Surely, the table should not be used at all in that case.

Moreover, wouldn't it be even better to compare the entire header with the one read previously and return -EFAULT if they don't match?

Quoted 5 lines
drivers/acpi/custom_method.c +1 -1
+
        uncopied_bytes -= count;
        *ppos += count;
 
--

A message that is not in the archive (sent off-list or not yet received): CAK8KejpGipqLpR93=ZnRze9FU7q5ZhJrGQV33czZwuxqSs0AyQ@mail.gmail.com

#6 lore raw
Quoted 53 lines · Kangjie Lu
Quoted 49 lines · Rafael J. Wysocki
Quoted 6 lines · Kangjie Lu

"user_buf->length" is in user space, and copied in twice. The second copy is after it passes the security check. If a user program races to change user_buf->length in user space, the data fetched in the second copy may invalidate the security check. The fix avoids the double-fetch issue by using the value passing the security check.

AFAICS the patch really does two things: it avoids the issue described above and avoids using the (redundant) 'table' local variable on the stack. Arguably, you could fix the issue without getting rid of the redundant variable.

Quoted 35 lines
---
 drivers/acpi/custom_method.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/acpi/custom_method.c +3 -4
index 4451877f83b6..f10ee0519033 100644
@@ -26,17 +26,16 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
        static u32 max_size;
        static u32 uncopied_bytes;
 
-       struct acpi_table_header table;
        acpi_status status;
 
        if (!(*ppos)) {
                /* parse the table header to get the table length */
                if (count <= sizeof(struct acpi_table_header))
                        return -EINVAL;
-               if (copy_from_user(&table, user_buf,
-                                  sizeof(struct acpi_table_header)))
+               if (get_user(max_size,
+                                       &((struct acpi_table_header *)user_buf)->length))
                        return -EFAULT;
-               uncopied_bytes = max_size = table.length;
+               uncopied_bytes = max_size;
                buf = kzalloc(max_size, GFP_KERNEL);
                if (!buf)
                        return -ENOMEM;
@@ -57,6 +56,9 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
                return -EFAULT;
        }
 
Quoted 8 lines
Quoted 5 lines

drivers/acpi/custom_method.c static ssize_t cm_write(struct file *file, const char __user * user_buf, · line 60

Quoted 2 lines
drivers/acpi/custom_method.c +2 -0
+       /* Ensure table length is not changed in the second copy */
+       ((struct acpi_table_header *)(buf + (*ppos)))->length = max_size;

Why don't you return -EFAULT if max_size is different from ->length? Surely, the table should not be used at all in that case.

We could do either, but I didn't see one is clearly better than the other.

As I said, why would you use any inconsistent data instead of returning an error?

Quoted 7 lines
Quoted 3 lines

Moreover, wouldn't it be even better to compare the entire header with the one read previously and return -EFAULT if they don't match?

If other fields are not critical and thus not checked, we don't have to compare the entire header for better performance reasons.

If you really care about consistency, performance doesn't matter that much.

Thanks,
Rafael