linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] acpi: fix a potential inconsistency caused by double-fetch
@ 2018-12-25 21:46 Kangjie Lu
  2018-12-25 22:20 ` kbuild test robot
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Kangjie Lu @ 2018-12-25 21:46 UTC (permalink / raw)
  To: kjlu; +Cc: pakki001, Rafael J. Wysocki, Len Brown, linux-acpi, linux-kernel

"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.

Signed-off-by: Kangjie Lu <kjlu@umn.edu>
---
 drivers/acpi/custom_method.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/acpi/custom_method.c b/drivers/acpi/custom_method.c
index 4451877f83b6..f75f664301b3 100644
--- a/drivers/acpi/custom_method.c
+++ b/drivers/acpi/custom_method.c
@@ -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;
-- 
2.17.2 (Apple Git-113)


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] acpi: fix a potential inconsistency caused by double-fetch
  2018-12-25 21:46 [PATCH] acpi: fix a potential inconsistency caused by double-fetch Kangjie Lu
@ 2018-12-25 22:20 ` kbuild test robot
  2018-12-26  0:11 ` kbuild test robot
  2019-01-09  8:14 ` [PATCH v2] " Kangjie Lu
  2 siblings, 0 replies; 6+ messages in thread
From: kbuild test robot @ 2018-12-25 22:20 UTC (permalink / raw)
  To: Kangjie Lu
  Cc: kbuild-all, kjlu, pakki001, Rafael J. Wysocki, Len Brown,
	linux-acpi, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 5488 bytes --]

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

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 28829 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] acpi: fix a potential inconsistency caused by double-fetch
  2018-12-25 21:46 [PATCH] acpi: fix a potential inconsistency caused by double-fetch Kangjie Lu
  2018-12-25 22:20 ` kbuild test robot
@ 2018-12-26  0:11 ` kbuild test robot
  2019-01-09  8:14 ` [PATCH v2] " Kangjie Lu
  2 siblings, 0 replies; 6+ messages in thread
From: kbuild test robot @ 2018-12-26  0:11 UTC (permalink / raw)
  To: Kangjie Lu
  Cc: kbuild-all, kjlu, pakki001, Rafael J. Wysocki, Len Brown,
	linux-acpi, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 13513 bytes --]

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

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 28333 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] acpi: fix a potential inconsistency caused by double-fetch
  2018-12-25 21:46 [PATCH] acpi: fix a potential inconsistency caused by double-fetch Kangjie Lu
  2018-12-25 22:20 ` kbuild test robot
  2018-12-26  0:11 ` kbuild test robot
@ 2019-01-09  8:14 ` Kangjie Lu
  2019-01-14 11:15   ` Rafael J. Wysocki
  2 siblings, 1 reply; 6+ messages in thread
From: Kangjie Lu @ 2019-01-09  8:14 UTC (permalink / raw)
  To: kjlu; +Cc: pakki001, Rafael J. Wysocki, Len Brown, linux-acpi, linux-kernel

"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.

Signed-off-by: Kangjie Lu <kjlu@umn.edu>
---
 drivers/acpi/custom_method.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/acpi/custom_method.c b/drivers/acpi/custom_method.c
index 4451877f83b6..f10ee0519033 100644
--- a/drivers/acpi/custom_method.c
+++ b/drivers/acpi/custom_method.c
@@ -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;
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] acpi: fix a potential inconsistency caused by double-fetch
  2019-01-09  8:14 ` [PATCH v2] " Kangjie Lu
@ 2019-01-14 11:15   ` Rafael J. Wysocki
       [not found]     ` <CAK8KejpGipqLpR93=ZnRze9FU7q5ZhJrGQV33czZwuxqSs0AyQ@mail.gmail.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Rafael J. Wysocki @ 2019-01-14 11:15 UTC (permalink / raw)
  To: Kangjie Lu
  Cc: pakki001, Rafael J. Wysocki, Len Brown, ACPI Devel Maling List,
	Linux Kernel Mailing List

On Wed, Jan 9, 2019 at 9:14 AM Kangjie Lu <kjlu@umn.edu> wrote:
>
> "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.

>
> Signed-off-by: Kangjie Lu <kjlu@umn.edu>
> ---
>  drivers/acpi/custom_method.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/acpi/custom_method.c b/drivers/acpi/custom_method.c
> index 4451877f83b6..f10ee0519033 100644
> --- a/drivers/acpi/custom_method.c
> +++ b/drivers/acpi/custom_method.c
> @@ -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;

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?

> +
>         uncopied_bytes -= count;
>         *ppos += count;
>
> --

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] acpi: fix a potential inconsistency caused by double-fetch
       [not found]     ` <CAK8KejpGipqLpR93=ZnRze9FU7q5ZhJrGQV33czZwuxqSs0AyQ@mail.gmail.com>
@ 2019-01-15 19:05       ` Rafael J. Wysocki
  0 siblings, 0 replies; 6+ messages in thread
From: Rafael J. Wysocki @ 2019-01-15 19:05 UTC (permalink / raw)
  To: Kangjie Lu
  Cc: Rafael J. Wysocki, Aditya Pakki, Rafael J. Wysocki, Len Brown,
	ACPI Devel Maling List, Linux Kernel Mailing List

On Tue, Jan 15, 2019 at 7:05 AM Kangjie Lu <kjlu@umn.edu> wrote:
>
>
>
> On Mon, Jan 14, 2019 at 5:15 AM Rafael J. Wysocki <rafael@kernel.org> wrote:
>>
>> On Wed, Jan 9, 2019 at 9:14 AM Kangjie Lu <kjlu@umn.edu> wrote:
>> >
>> > "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.
>>
>> >
>> > Signed-off-by: Kangjie Lu <kjlu@umn.edu>
>> > ---
>> >  drivers/acpi/custom_method.c | 10 ++++++----
>> >  1 file changed, 6 insertions(+), 4 deletions(-)
>> >
>> > diff --git a/drivers/acpi/custom_method.c b/drivers/acpi/custom_method.c
>> > index 4451877f83b6..f10ee0519033 100644
>> > --- a/drivers/acpi/custom_method.c
>> > +++ b/drivers/acpi/custom_method.c
>> > @@ -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;
>>
>> 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?

>>
>> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-01-15 19:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-25 21:46 [PATCH] acpi: fix a potential inconsistency caused by double-fetch Kangjie Lu
2018-12-25 22:20 ` kbuild test robot
2018-12-26  0:11 ` kbuild test robot
2019-01-09  8:14 ` [PATCH v2] " Kangjie Lu
2019-01-14 11:15   ` Rafael J. Wysocki
     [not found]     ` <CAK8KejpGipqLpR93=ZnRze9FU7q5ZhJrGQV33czZwuxqSs0AyQ@mail.gmail.com>
2019-01-15 19:05       ` Rafael J. Wysocki

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).