Skip to contentSkip to conversation

blktrace: put bounds on BLKTRACESETUP buf_size and buf_nr

PATCH In review Harshad Shirwadkar 4 participants 8 messages Jun 11, 2020 – Jun 15, 2020

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

PATCH v2 1/1 blktrace: put bounds on BLKTRACESETUP buf_size and buf_nr

Make sure that user requested memory via BLKTRACESETUP is within bounds. This can be easily exploited by setting really large values for buf_size and buf_nr in BLKTRACESETUP ioctl.

blktrace program has following hardcoded values for bufsize and bufnr:
BUF_SIZE=(512 * 1024)
BUF_NR=(4)

This is very easy to exploit. Setting buf_size / buf_nr in userspace program to big values make kernel go oom.

This patch adds a new new sysfs tunable called "blktrace_max_alloc" with the default value as:
blktrace_max_alloc=(1024 * 1024 * 16)

Verified that the fix makes BLKTRACESETUP return -E2BIG if the buf_size * buf_nr crosses the configured upper bound in the device's sysfs file. Verified that the bound checking is turned off when we write 0 to the sysfs file.

---
 Documentation/block/queue-sysfs.rst |  8 ++++++++
 block/blk-settings.c                |  1 +
 block/blk-sysfs.c                   | 27 +++++++++++++++++++++++++++
 include/linux/blkdev.h              |  5 +++++
 kernel/trace/blktrace.c             |  8 ++++++++
 5 files changed, 49 insertions(+)
Documentation/block/queue-sysfs.rst +8 -0
index 6a8513af9201..ef4eec68cd24 100644
@@ -251,4 +251,12 @@ devices are described in the ZBC (Zoned Block Commands) and ZAC
 do not support zone commands, they will be treated as regular block devices
 and zoned will report "none".
 
+blktrace_max_alloc (RW)
+----------
+BLKTRACESETUP ioctl takes the number of buffers and the size of each
+buffer as an argument and uses these values to allocate kernel memory
+for tracing purpose. This is the limit on the maximum kernel memory
+that can be allocated by BLKTRACESETUP ioctl. The default limit is
+16MB. Value of 0 indicates that this bound checking is disabled.
+
 Jens Axboe <[email protected]>, February 2009
block/blk-settings.c +1 -0
index 9a2c23cd9700..38535aa146f4 100644
@@ -60,6 +60,7 @@ void blk_set_default_limits(struct queue_limits *lim)
 	lim->io_opt = 0;
 	lim->misaligned = 0;
 	lim->zoned = BLK_ZONED_NONE;
+	lim->blktrace_max_alloc = BLKTRACE_MAX_ALLOC;
 }
 EXPORT_SYMBOL(blk_set_default_limits);
 
block/blk-sysfs.c +27 -0
index 02643e149d5e..e849e80930c4 100644
@@ -496,6 +496,26 @@ static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
 	return count;
 }
 
+static ssize_t queue_blktrace_max_alloc_show(struct request_queue *q, char *page)
+{
+	return queue_var_show(q->limits.blktrace_max_alloc, page);
+}
+
+static ssize_t queue_blktrace_max_alloc_store(struct request_queue *q,
+					      const char *page,
+					      size_t count)
+{
+	unsigned long blktrace_max_alloc;
+	int ret;
+
+	ret = queue_var_store(&blktrace_max_alloc, page, count);
+	if (ret < 0)
+		return ret;
+
+	q->limits.blktrace_max_alloc = blktrace_max_alloc;
+	return count;
+}
+
 static ssize_t queue_wc_show(struct request_queue *q, char *page)
 {
 	if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
@@ -731,6 +751,12 @@ static struct queue_sysfs_entry queue_wb_lat_entry = {
 	.store = queue_wb_lat_store,
 };
 
+static struct queue_sysfs_entry queue_blktrace_max_alloc_entry = {
+	.attr = {.name = "blktrace_max_alloc", .mode = 0644 },
+	.show = queue_blktrace_max_alloc_show,
+	.store = queue_blktrace_max_alloc_store,
+};
+
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
 static struct queue_sysfs_entry throtl_sample_time_entry = {
 	.attr = {.name = "throttle_sample_time", .mode = 0644 },
@@ -779,6 +805,7 @@ static struct attribute *queue_attrs[] = {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
 	&throtl_sample_time_entry.attr,
 #endif
+	&queue_blktrace_max_alloc_entry.attr,
 	NULL,
 };
 
include/linux/blkdev.h +5 -0
index 8fd900998b4e..3a72b0fd723e 100644
@@ -309,6 +309,10 @@ enum blk_queue_state {
 #define BLK_SCSI_MAX_CMDS	(256)
 #define BLK_SCSI_CMD_PER_LONG	(BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
 
+#define BLKTRACE_MAX_BUFSIZ	(1024 * 1024)
+#define BLKTRACE_MAX_BUFNR	16
+#define BLKTRACE_MAX_ALLOC	((BLKTRACE_MAX_BUFSIZ) * (BLKTRACE_MAX_BUFNR))
+
 /*
  * Zoned block device models (zoned limit).
  */
@@ -322,6 +326,7 @@ struct queue_limits {
 	unsigned long		bounce_pfn;
 	unsigned long		seg_boundary_mask;
 	unsigned long		virt_boundary_mask;
+	unsigned long		blktrace_max_alloc;
 
 	unsigned int		max_hw_sectors;
 	unsigned int		max_dev_sectors;
kernel/trace/blktrace.c +8 -0
index ea47f2084087..de028bdbb148 100644
@@ -477,11 +477,19 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
 {
 	struct blk_trace *bt = NULL;
 	struct dentry *dir = NULL;
+	u32 alloc_siz;
 	int ret;
 
 	if (!buts->buf_size || !buts->buf_nr)
 		return -EINVAL;
 
+	if (check_mul_overflow(buts->buf_size, buts->buf_nr, &alloc_siz))
+		return -E2BIG;
+
+	if (q->limits.blktrace_max_alloc &&
+	    alloc_siz > q->limits.blktrace_max_alloc)
+		return -E2BIG;
+
 	if (!blk_debugfs_root)
 		return -ENOENT;
 
Signature · 1 line
2.27.0.278.ge193c7cf3a9-goog
#1 lore raw
Quoted 34 lines · Harshad Shirwadkar

Make sure that user requested memory via BLKTRACESETUP is within bounds. This can be easily exploited by setting really large values for buf_size and buf_nr in BLKTRACESETUP ioctl.

blktrace program has following hardcoded values for bufsize and bufnr:
BUF_SIZE=(512 * 1024)
BUF_NR=(4)

This is very easy to exploit. Setting buf_size / buf_nr in userspace program to big values make kernel go oom.

This patch adds a new new sysfs tunable called "blktrace_max_alloc" with the default value as:
blktrace_max_alloc=(1024 * 1024 * 16)

Verified that the fix makes BLKTRACESETUP return -E2BIG if the buf_size * buf_nr crosses the configured upper bound in the device's sysfs file. Verified that the bound checking is turned off when we write 0 to the sysfs file.

---
  Documentation/block/queue-sysfs.rst |  8 ++++++++
  block/blk-settings.c                |  1 +
  block/blk-sysfs.c                   | 27 +++++++++++++++++++++++++++
  include/linux/blkdev.h              |  5 +++++
  kernel/trace/blktrace.c             |  8 ++++++++
  5 files changed, 49 insertions(+)
Documentation/block/queue-sysfs.rst +0 -0
index 6a8513af9201..ef4eec68cd24 100644

Documentation/block/queue-sysfs.rst devices are described in the ZBC (Zoned Block Commands) and ZAC · line 255

Quoted 5 lines
Documentation/block/queue-sysfs.rst +2 -0
@@ -251,4 +251,12 @@ devices are described in the ZBC (Zoned Block Commands) and ZAC
  do not support zone commands, they will be treated as regular block devices
  and zoned will report "none".
 
+blktrace_max_alloc (RW)
+----------

nit:-based on the pattern present in the same file how about following?

blk_trace_max_alloc (RW)
------------------------

Quoted 64 lines
Documentation/block/queue-sysfs.rst +6 -0
+BLKTRACESETUP ioctl takes the number of buffers and the size of each
+buffer as an argument and uses these values to allocate kernel memory
+for tracing purpose. This is the limit on the maximum kernel memory
+that can be allocated by BLKTRACESETUP ioctl. The default limit is
+16MB. Value of 0 indicates that this bound checking is disabled.
+
  Jens Axboe <[email protected]>, February 2009
block/blk-settings.c +1 -0
index 9a2c23cd9700..38535aa146f4 100644
@@ -60,6 +60,7 @@ void blk_set_default_limits(struct queue_limits *lim)
  	lim->io_opt = 0;
  	lim->misaligned = 0;
  	lim->zoned = BLK_ZONED_NONE;
+	lim->blktrace_max_alloc = BLKTRACE_MAX_ALLOC;
  }
  EXPORT_SYMBOL(blk_set_default_limits);
 
block/blk-sysfs.c +26 -0
index 02643e149d5e..e849e80930c4 100644
@@ -496,6 +496,26 @@ static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
  	return count;
  }
 
+static ssize_t queue_blktrace_max_alloc_show(struct request_queue *q, char *page)
+{
+	return queue_var_show(q->limits.blktrace_max_alloc, page);
+}
+
+static ssize_t queue_blktrace_max_alloc_store(struct request_queue *q,
+					      const char *page,
+					      size_t count)
+{
+	unsigned long blktrace_max_alloc;
+	int ret;
+
+	ret = queue_var_store(&blktrace_max_alloc, page, count);
+	if (ret < 0)
+		return ret;
+
+	q->limits.blktrace_max_alloc = blktrace_max_alloc;
+	return count;
+}
+
  static ssize_t queue_wc_show(struct request_queue *q, char *page)
  {
  	if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
@@ -731,6 +751,12 @@ static struct queue_sysfs_entry queue_wb_lat_entry = {
  	.store = queue_wb_lat_store,
  };
 
+static struct queue_sysfs_entry queue_blktrace_max_alloc_entry = {
+	.attr = {.name = "blktrace_max_alloc", .mode = 0644 },
+	.show = queue_blktrace_max_alloc_show,
+	.store = queue_blktrace_max_alloc_store,
+};
+
  #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  static struct queue_sysfs_entry throtl_sample_time_entry = {
  	.attr = {.name = "throttle_sample_time", .mode = 0644 },

block/blk-sysfs.c static struct attribute *queue_attrs[] = { · line 808

Quoted 4 lines
block/blk-sysfs.c +1 -0
@@ -779,6 +805,7 @@ static struct attribute *queue_attrs[] = {
  #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  	&throtl_sample_time_entry.attr,
  #endif
+	&queue_blktrace_max_alloc_entry.attr,

Is above attribute needs to be under CONFIG_BLK_DEV_IO_TRACE guard ?

include/linux/blkdev.h enum blk_queue_state { · line 312

Quoted 12 lines
block/blk-sysfs.c +0 -0
  	NULL,
  };
 
include/linux/blkdev.h +1 -0
index 8fd900998b4e..3a72b0fd723e 100644
@@ -309,6 +309,10 @@ enum blk_queue_state {
  #define BLK_SCSI_MAX_CMDS	(256)
  #define BLK_SCSI_CMD_PER_LONG	(BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
 
+#define BLKTRACE_MAX_BUFSIZ	(1024 * 1024)

nit:- macro name ending with SIZ is unusual for include/linux/blkdev.h. Consider renaming it to BLKTRACE_MAX_BUF_SIZE.

include/linux/blkdev.h enum blk_queue_state { · line 313

Quoted 1 line
include/linux/blkdev.h +1 -0
+#define BLKTRACE_MAX_BUFNR	16

nit:- just like BLKTRACE_MAX_BUFSIZ value of BLKTRACE_MAX_BUFNR needs to be guarded by () ?

include/linux/blkdev.h enum blk_queue_state { · line 314

Quoted 1 line
include/linux/blkdev.h +1 -0
+#define BLKTRACE_MAX_ALLOC	((BLKTRACE_MAX_BUFSIZ) * (BLKTRACE_MAX_BUFNR))

nit:- The macro BLKTRACE_MAX_BUFSIZ already has () we can get rid of the extra inner () for BLKTRACE_MAX_BUFSIZ in BLKTRACE_MAX_ALLOC.

Quoted 17 lines
include/linux/blkdev.h +2 -0
+
  /*
   * Zoned block device models (zoned limit).
   */
@@ -322,6 +326,7 @@ struct queue_limits {
  	unsigned long		bounce_pfn;
  	unsigned long		seg_boundary_mask;
  	unsigned long		virt_boundary_mask;
+	unsigned long		blktrace_max_alloc;
 
  	unsigned int		max_hw_sectors;
  	unsigned int		max_dev_sectors;
kernel/trace/blktrace.c +0 -0
index ea47f2084087..de028bdbb148 100644

kernel/trace/blktrace.c static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev, · line 480

Quoted 4 lines
kernel/trace/blktrace.c +1 -0
@@ -477,11 +477,19 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
  {
  	struct blk_trace *bt = NULL;
  	struct dentry *dir = NULL;
+	u32 alloc_siz;

nit:- please use full names 's/alloc_siz/alloc_size/'

Quoted 16 lines
kernel/trace/blktrace.c +7 -0
  	int ret;
 
  	if (!buts->buf_size || !buts->buf_nr)
  		return -EINVAL;
 
+	if (check_mul_overflow(buts->buf_size, buts->buf_nr, &alloc_siz))
+		return -E2BIG;
+
+	if (q->limits.blktrace_max_alloc &&
+	    alloc_siz > q->limits.blktrace_max_alloc)
+		return -E2BIG;
+
  	if (!blk_debugfs_root)
  		return -ENOENT;
 
#2 lore raw
In reply to Harshad Shirwadkar

block/blk-sysfs.c static struct queue_sysfs_entry queue_wb_lat_entry = { · line 758

Quoted 5 lines · Harshad Shirwadkar
block/blk-sysfs.c +5 -0
+static struct queue_sysfs_entry queue_blktrace_max_alloc_entry = {
+	.attr = {.name = "blktrace_max_alloc", .mode = 0644 },
+	.show = queue_blktrace_max_alloc_show,
+	.store = queue_blktrace_max_alloc_store,
+};

I may have sent you in the wrong direction with my comment about introducing sysfs attributes to control the blktrace_max_alloc limit. That comment was made before I fully understood the issue this patch addresses. There is a concern with introducing any limit for the amount of memory allocated for blktrace, namely that any such limit may be seen as a regression in the behavior of the BLKTRACESETUP ioctl. How about not limiting the memory allocated for blktrace buffers at all?

If triggering an out-of-memory complaint is a big concern, how about modifying relay_open() such that it becomes possible to pass a flag like __GFP_ACCOUNT to the memory allocations relay_open() performs? From memory-allocations.rst:

* Untrusted allocations triggered from userspace should be a subject of kmem accounting and must have ``__GFP_ACCOUNT`` bit set. There is the handy ``GFP_KERNEL_ACCOUNT`` shortcut for ``GFP_KERNEL`` allocations that should be accounted.

include/linux/blkdev.h struct queue_limits { · line 332

Quoted 8 lines
include/linux/blkdev.h +1 -0
@@ -322,6 +326,7 @@ struct queue_limits {
 	unsigned long		bounce_pfn;
 	unsigned long		seg_boundary_mask;
 	unsigned long		virt_boundary_mask;
+	unsigned long		blktrace_max_alloc;
 
 	unsigned int		max_hw_sectors;
 	unsigned int		max_dev_sectors;

Is this the wrong structure to add a limit like blktrace_max_alloc? As far as I can see struct queue_limits is only used for limits that are affected by stacking block devices. My understanding is that the blktrace_max_alloc limit is not affected by device stacking. See also blk_stack_limits().

Quoted 13 lines
kernel/trace/blktrace.c +1 -0
index ea47f2084087..de028bdbb148 100644
@@ -477,11 +477,19 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
 {
 	struct blk_trace *bt = NULL;
 	struct dentry *dir = NULL;
+	u32 alloc_siz;
 	int ret;
 
 	if (!buts->buf_size || !buts->buf_nr)
 		return -EINVAL;

kernel/trace/blktrace.c static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev, · line 494

Quoted 10 lines
kernel/trace/blktrace.c +7 -0
 
+	if (check_mul_overflow(buts->buf_size, buts->buf_nr, &alloc_siz))
+		return -E2BIG;
+
+	if (q->limits.blktrace_max_alloc &&
+	    alloc_siz > q->limits.blktrace_max_alloc)
+		return -E2BIG;
+
 	if (!blk_debugfs_root)
 		return -ENOENT;

Please add the check_mul_overflow() check in the code that performs the multiplication, namely relay_open(), such that all relay_open() callers are protected against multiplication overflows.

Thanks,

Bart.

#3 lore raw
Quoted 5 lines

Is this the wrong structure to add a limit like blktrace_max_alloc? As far as I can see struct queue_limits is only used for limits that are affected by stacking block devices. My understanding is that the blktrace_max_alloc limit is not affected by device stacking. See also blk_stack_limits().

I can also see that, how about adding a parameter in
struct request_queue under CONFIG_BLK_DEV_IO_TRACE if we are going that route ?

#4 lore raw

Thanks for the feedback Chaitanya and Bart.

In an offline sync-up with Shakeel and Bart, Shakeel mentioned that probably doing the GFP_ACCOUNT is the right approach as long as the trace buffer allocation lifetime can be tied with the process lifetime. Given that, we can drop this patch for now. Whenever I get time, I'll try to submit a new patch with that handled.

Quoted 11 lines · Chaitanya Kulkarni
Quoted 6 lines

Is this the wrong structure to add a limit like blktrace_max_alloc? As far as I can see struct queue_limits is only used for limits that are affected by stacking block devices. My understanding is that the blktrace_max_alloc limit is not affected by device stacking. See also blk_stack_limits().

I can also see that, how about adding a parameter in
struct request_queue under CONFIG_BLK_DEV_IO_TRACE if we are going that route ?

#5 lore raw
In reply to Harshad Shirwadkar
Quoted 11 lines · Harshad Shirwadkar

Make sure that user requested memory via BLKTRACESETUP is within bounds. This can be easily exploited by setting really large values for buf_size and buf_nr in BLKTRACESETUP ioctl.

blktrace program has following hardcoded values for bufsize and bufnr:
BUF_SIZE=(512 * 1024)
BUF_NR=(4)

This is very easy to exploit. Setting buf_size / buf_nr in userspace program to big values make kernel go oom.

Quoted 8 lines

This patch adds a new new sysfs tunable called "blktrace_max_alloc" with the default value as:
blktrace_max_alloc=(1024 * 1024 * 16)

Verified that the fix makes BLKTRACESETUP return -E2BIG if the buf_size * buf_nr crosses the configured upper bound in the device's sysfs file. Verified that the bound checking is turned off when we write 0 to the sysfs file.

I don't think the configurability makes any sense. We need to put a hard upper cap on, preferably one that doesn't break widely used userspace. Just pick a reasonable large value to get started.

#6 lore raw
Quoted 3 lines · Christoph Hellwig

I don't think the configurability makes any sense. We need to put a hard upper cap on, preferably one that doesn't break widely used userspace. Just pick a reasonable large value to get started.

Hi Christoph,

Something that has not been mentioned in the patch description is that this patch addresses an issue that was discovered by syzbot. Do you agree that the following changes are useful on top of a hard upper limit for the blktrace buffer and that these changes will address more potential syzbot issues?
- Use __GFP_ACCOUNT in the relay_open() call that allocates blktrace buffers such that the memory allocation is accounted to a process and such that the OOM killer becomes aware of blktrace buffer allocations.
- Making blkdev_close(), raw_release() and sg_release() shut down tracing in case the BLKTRACETEARDOWN ioctl has not been invoked. That will cause the blktrace buffers to be freed when e.g. the process that owns these buffers is killed.

Thanks,

Bart.

#7 lore raw
Quoted 7 lines · Bart Van Assche
Quoted 3 lines · Christoph Hellwig

I don't think the configurability makes any sense. We need to put a hard upper cap on, preferably one that doesn't break widely used userspace. Just pick a reasonable large value to get started.

Hi Christoph,

Quoted 8 lines

Something that has not been mentioned in the patch description is that this patch addresses an issue that was discovered by syzbot. Do you agree that the following changes are useful on top of a hard upper limit for the blktrace buffer and that these changes will address more potential syzbot issues?
- Use __GFP_ACCOUNT in the relay_open() call that allocates blktrace buffers such that the memory allocation is accounted to a process and such that the OOM killer becomes aware of blktrace buffer allocations.

Sounds ok.

Quoted 4 lines

- Making blkdev_close(), raw_release() and sg_release() shut down tracing in case the BLKTRACETEARDOWN ioctl has not been invoked. That will cause the blktrace buffers to be freed when e.g. the process that owns these buffers is killed.

While this sounds useful I think it might very well break existing use cases.